@@ -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,23 @@ 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+
185+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
169186
170187 hosts := []string {
171188 fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -250,7 +267,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
250267 },
251268 }
252269
253- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
270+ crbLabels := map [string ]string {}
271+ for key , val := range ownerutil .OwnerLabel (ownerutil .Owner (& v1alpha1.ClusterServiceVersion {}), owner .GetObjectKind ().GroupVersionKind ().Kind ) {
272+ crbLabels [key ] = val
273+ }
274+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (service .GetName () + "-system:auth-delegator" ).
275+ WithSubjects (rbacv1ac .Subject ().
276+ WithKind ("ServiceAccount" ).
277+ WithAPIGroup ("" ).
278+ WithName (args .depSpec .Template .Spec .ServiceAccountName ).
279+ WithNamespace ("" )). // Empty owner with no namespace
280+ WithRoleRef (rbacv1ac .RoleRef ().
281+ WithAPIGroup ("rbac.authorization.k8s.io" ).
282+ WithKind ("ClusterRole" ).
283+ WithName ("system:auth-delegator" )).
284+ WithLabels (crbLabels )
285+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
254286
255287 authReaderRoleBinding := & rbacv1.RoleBinding {
256288 Subjects : []rbacv1.Subject {
@@ -375,7 +407,6 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
375407 {
376408 name : "doesn't add duplicate service ownerrefs" ,
377409 mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
378- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
379410 service := corev1.Service {
380411 ObjectMeta : metav1.ObjectMeta {
381412 Name : "test-service" ,
@@ -389,7 +420,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
389420 Selector : selector (t , "test=label" ).MatchLabels ,
390421 },
391422 }
392- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
423+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
424+ for _ , p := range args .ports {
425+ ac := corev1ac .ServicePort ().
426+ WithName (p .Name ).
427+ WithPort (p .Port ).
428+ WithTargetPort (p .TargetPort )
429+ portsApplyConfig = append (portsApplyConfig , ac )
430+ }
431+
432+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
433+ WithSpec (corev1ac .ServiceSpec ().
434+ WithPorts (portsApplyConfig ... ).
435+ WithSelector (selector (t , "test=label" ).MatchLabels )).
436+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (owner ))
437+
438+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
393439
394440 hosts := []string {
395441 fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -474,7 +520,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
474520 },
475521 }
476522
477- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
523+ crbLabels := map [string ]string {}
524+ for key , val := range ownerutil .OwnerLabel (owner , owner .GetObjectKind ().GroupVersionKind ().Kind ) {
525+ crbLabels [key ] = val
526+ }
527+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (service .GetName () + "-system:auth-delegator" ).
528+ WithSubjects (rbacv1ac .Subject ().
529+ WithKind ("ServiceAccount" ).
530+ WithAPIGroup ("" ).
531+ WithName ("test-sa" ).
532+ WithNamespace (namespace )).
533+ WithRoleRef (rbacv1ac .RoleRef ().
534+ WithAPIGroup ("rbac.authorization.k8s.io" ).
535+ WithKind ("ClusterRole" ).
536+ WithName ("system:auth-delegator" )).
537+ WithLabels (crbLabels )
538+
539+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
478540
479541 authReaderRoleBinding := & rbacv1.RoleBinding {
480542 Subjects : []rbacv1.Subject {
@@ -591,9 +653,8 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
591653 },
592654 },
593655 {
594- name : "labels an unlabelled secret if present" ,
656+ name : "labels an unlabelled secret if present; creates Service and ClusterRoleBinding if not existing " ,
595657 mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
596- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
597658 service := corev1.Service {
598659 ObjectMeta : metav1.ObjectMeta {
599660 Name : "test-service" ,
@@ -606,7 +667,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
606667 Selector : selector (t , "test=label" ).MatchLabels ,
607668 },
608669 }
609- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
670+
671+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
672+ for _ , p := range args .ports {
673+ ac := corev1ac .ServicePort ().
674+ WithName (p .Name ).
675+ WithPort (p .Port ).
676+ WithTargetPort (p .TargetPort )
677+ portsApplyConfig = append (portsApplyConfig , ac )
678+ }
679+
680+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
681+ WithSpec (corev1ac .ServiceSpec ().
682+ WithPorts (portsApplyConfig ... ).
683+ WithSelector (selector (t , "test=label" ).MatchLabels )).
684+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (& v1alpha1.ClusterServiceVersion {}))
685+
686+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
610687
611688 hosts := []string {
612689 fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -700,8 +777,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
700777 Name : "system:auth-delegator" ,
701778 },
702779 }
703-
704- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
780+ crbLabels := map [string ]string {}
781+ for key , val := range ownerutil .OwnerLabel (ownerutil .Owner (& v1alpha1.ClusterServiceVersion {}), owner .GetObjectKind ().GroupVersionKind ().Kind ) {
782+ crbLabels [key ] = val
783+ }
784+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (service .GetName () + "-system:auth-delegator" ).
785+ WithSubjects (rbacv1ac .Subject ().WithKind ("ServiceAccount" ).
786+ WithAPIGroup ("" ).
787+ WithName ("test-sa" ).
788+ WithNamespace (namespace )).
789+ WithRoleRef (rbacv1ac .RoleRef ().
790+ WithAPIGroup ("rbac.authorization.k8s.io" ).
791+ WithKind ("ClusterRole" ).
792+ WithName ("system:auth-delegator" )).
793+ WithLabels (crbLabels )
794+
795+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
705796
706797 authReaderRoleBinding := & rbacv1.RoleBinding {
707798 Subjects : []rbacv1.Subject {
@@ -724,13 +815,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
724815 mockOpClient .EXPECT ().UpdateRoleBinding (authReaderRoleBinding ).Return (authReaderRoleBinding , nil )
725816 },
726817 state : fakeState {
727- existingService : & corev1.Service {
728- ObjectMeta : metav1.ObjectMeta {
729- OwnerReferences : []metav1.OwnerReference {
730- ownerutil .NonBlockingOwner (& v1alpha1.ClusterServiceVersion {}),
731- },
732- },
733- },
818+ existingService : nil ,
734819 // unlabelled secret won't be in cache
735820 getSecretError : errors .NewNotFound (schema.GroupResource {
736821 Group : "" ,
@@ -742,9 +827,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
742827 existingRoleBinding : & rbacv1.RoleBinding {
743828 ObjectMeta : metav1.ObjectMeta {},
744829 },
745- existingClusterRoleBinding : & rbacv1.ClusterRoleBinding {
746- ObjectMeta : metav1.ObjectMeta {},
747- },
830+ existingClusterRoleBinding : nil ,
748831 },
749832 fields : fields {
750833 owner : & v1alpha1.ClusterServiceVersion {},
0 commit comments