@@ -15,6 +15,8 @@ import (
1515 "k8s.io/apimachinery/pkg/api/errors"
1616 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1717 "k8s.io/apimachinery/pkg/runtime/schema"
18+ corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
19+ rbacv1ac "k8s.io/client-go/applyconfigurations/rbac/v1"
1820
1921 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2022 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
@@ -152,7 +154,6 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
152154 {
153155 name : "adds certs to deployment spec" ,
154156 mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
155- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
156157 service := corev1.Service {
157158 ObjectMeta : metav1.ObjectMeta {
158159 Name : "test-service" ,
@@ -165,7 +166,24 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
165166 Selector : selector (t , "test=label" ).MatchLabels ,
166167 },
167168 }
168- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
169+
170+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
171+ for _ , p := range args .ports {
172+ ac := corev1ac .ServicePort ().
173+ WithName (p .Name ).
174+ WithPort (p .Port ).
175+ WithTargetPort (p .TargetPort )
176+ portsApplyConfig = append (portsApplyConfig , ac )
177+ }
178+
179+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
180+ WithSpec (corev1ac .ServiceSpec ().
181+ WithPorts (portsApplyConfig ... ).
182+ WithSelector (selector (t , "test=label" ).MatchLabels )).
183+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (& v1alpha1.ClusterServiceVersion {})).
184+ WithLabels (map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue })
185+
186+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
169187
170188 hosts := []string {
171189 fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -250,7 +268,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
250268 },
251269 }
252270
253- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
271+ crbLabels := map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue }
272+ for key , val := range ownerutil .OwnerLabel (ownerutil .Owner (& v1alpha1.ClusterServiceVersion {}), owner .GetObjectKind ().GroupVersionKind ().Kind ) {
273+ crbLabels [key ] = val
274+ }
275+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (service .GetName () + "-system:auth-delegator" ).
276+ WithSubjects (rbacv1ac .Subject ().
277+ WithKind ("ServiceAccount" ).
278+ WithAPIGroup ("" ).
279+ WithName (args .depSpec .Template .Spec .ServiceAccountName ).
280+ WithNamespace ("" )). // Empty owner with no namespace
281+ WithRoleRef (rbacv1ac .RoleRef ().
282+ WithAPIGroup ("rbac.authorization.k8s.io" ).
283+ WithKind ("ClusterRole" ).
284+ WithName ("system:auth-delegator" )).
285+ WithLabels (crbLabels )
286+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
254287
255288 authReaderRoleBinding := & rbacv1.RoleBinding {
256289 Subjects : []rbacv1.Subject {
@@ -375,7 +408,6 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
375408 {
376409 name : "doesn't add duplicate service ownerrefs" ,
377410 mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
378- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
379411 service := corev1.Service {
380412 ObjectMeta : metav1.ObjectMeta {
381413 Name : "test-service" ,
@@ -389,7 +421,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
389421 Selector : selector (t , "test=label" ).MatchLabels ,
390422 },
391423 }
392- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
424+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
425+ for _ , p := range args .ports {
426+ ac := corev1ac .ServicePort ().
427+ WithName (p .Name ).
428+ WithPort (p .Port ).
429+ WithTargetPort (p .TargetPort )
430+ portsApplyConfig = append (portsApplyConfig , ac )
431+ }
432+
433+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
434+ WithSpec (corev1ac .ServiceSpec ().
435+ WithPorts (portsApplyConfig ... ).
436+ WithSelector (selector (t , "test=label" ).MatchLabels )).
437+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (owner )).
438+ WithLabels (map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue })
439+
440+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
393441
394442 hosts := []string {
395443 fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -474,7 +522,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
474522 },
475523 }
476524
477- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
525+ crbLabels := map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue }
526+ for key , val := range ownerutil .OwnerLabel (owner , owner .GetObjectKind ().GroupVersionKind ().Kind ) {
527+ crbLabels [key ] = val
528+ }
529+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (service .GetName () + "-system:auth-delegator" ).
530+ WithSubjects (rbacv1ac .Subject ().
531+ WithKind ("ServiceAccount" ).
532+ WithAPIGroup ("" ).
533+ WithName ("test-sa" ).
534+ WithNamespace (namespace )).
535+ WithRoleRef (rbacv1ac .RoleRef ().
536+ WithAPIGroup ("rbac.authorization.k8s.io" ).
537+ WithKind ("ClusterRole" ).
538+ WithName ("system:auth-delegator" )).
539+ WithLabels (crbLabels )
540+
541+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
478542
479543 authReaderRoleBinding := & rbacv1.RoleBinding {
480544 Subjects : []rbacv1.Subject {
@@ -591,9 +655,8 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
591655 },
592656 },
593657 {
594- name : "labels an unlabelled secret if present" ,
658+ name : "labels an unlabelled secret if present; creates Service and ClusterRoleBinding if not existing " ,
595659 mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
596- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
597660 service := corev1.Service {
598661 ObjectMeta : metav1.ObjectMeta {
599662 Name : "test-service" ,
@@ -606,7 +669,24 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
606669 Selector : selector (t , "test=label" ).MatchLabels ,
607670 },
608671 }
609- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
672+
673+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
674+ for _ , p := range args .ports {
675+ ac := corev1ac .ServicePort ().
676+ WithName (p .Name ).
677+ WithPort (p .Port ).
678+ WithTargetPort (p .TargetPort )
679+ portsApplyConfig = append (portsApplyConfig , ac )
680+ }
681+
682+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
683+ WithSpec (corev1ac .ServiceSpec ().
684+ WithPorts (portsApplyConfig ... ).
685+ WithSelector (selector (t , "test=label" ).MatchLabels )).
686+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (& v1alpha1.ClusterServiceVersion {})).
687+ WithLabels (map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue })
688+
689+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
610690
611691 hosts := []string {
612692 fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -700,8 +780,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
700780 Name : "system:auth-delegator" ,
701781 },
702782 }
703-
704- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
783+ crbLabels := map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue }
784+ for key , val := range ownerutil .OwnerLabel (ownerutil .Owner (& v1alpha1.ClusterServiceVersion {}), owner .GetObjectKind ().GroupVersionKind ().Kind ) {
785+ crbLabels [key ] = val
786+ }
787+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (service .GetName () + "-system:auth-delegator" ).
788+ WithSubjects (rbacv1ac .Subject ().WithKind ("ServiceAccount" ).
789+ WithAPIGroup ("" ).
790+ WithName ("test-sa" ).
791+ WithNamespace (namespace )).
792+ WithRoleRef (rbacv1ac .RoleRef ().
793+ WithAPIGroup ("rbac.authorization.k8s.io" ).
794+ WithKind ("ClusterRole" ).
795+ WithName ("system:auth-delegator" )).
796+ WithLabels (crbLabels )
797+
798+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
705799
706800 authReaderRoleBinding := & rbacv1.RoleBinding {
707801 Subjects : []rbacv1.Subject {
@@ -724,13 +818,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
724818 mockOpClient .EXPECT ().UpdateRoleBinding (authReaderRoleBinding ).Return (authReaderRoleBinding , nil )
725819 },
726820 state : fakeState {
727- existingService : & corev1.Service {
728- ObjectMeta : metav1.ObjectMeta {
729- OwnerReferences : []metav1.OwnerReference {
730- ownerutil .NonBlockingOwner (& v1alpha1.ClusterServiceVersion {}),
731- },
732- },
733- },
821+ existingService : nil ,
734822 // unlabelled secret won't be in cache
735823 getSecretError : errors .NewNotFound (schema.GroupResource {
736824 Group : "" ,
@@ -742,9 +830,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
742830 existingRoleBinding : & rbacv1.RoleBinding {
743831 ObjectMeta : metav1.ObjectMeta {},
744832 },
745- existingClusterRoleBinding : & rbacv1.ClusterRoleBinding {
746- ObjectMeta : metav1.ObjectMeta {},
747- },
833+ existingClusterRoleBinding : nil ,
748834 },
749835 fields : fields {
750836 owner : & v1alpha1.ClusterServiceVersion {},
0 commit comments