@@ -16,6 +16,8 @@ import (
16
16
"k8s.io/apimachinery/pkg/api/errors"
17
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
18
"k8s.io/apimachinery/pkg/runtime/schema"
19
+ corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
20
+ rbacv1ac "k8s.io/client-go/applyconfigurations/rbac/v1"
19
21
20
22
"github.com/operator-framework/api/pkg/operators/v1alpha1"
21
23
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
@@ -153,7 +155,6 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
153
155
{
154
156
name : "adds certs to deployment spec" ,
155
157
mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
156
- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
157
158
service := corev1.Service {
158
159
ObjectMeta : metav1.ObjectMeta {
159
160
Name : "test-service" ,
@@ -167,7 +168,24 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
167
168
Selector : selector (t , "test=label" ).MatchLabels ,
168
169
},
169
170
}
170
- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
171
+
172
+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
173
+ for _ , p := range args .ports {
174
+ ac := corev1ac .ServicePort ().
175
+ WithName (p .Name ).
176
+ WithPort (p .Port ).
177
+ WithTargetPort (p .TargetPort )
178
+ portsApplyConfig = append (portsApplyConfig , ac )
179
+ }
180
+
181
+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
182
+ WithSpec (corev1ac .ServiceSpec ().
183
+ WithPorts (portsApplyConfig ... ).
184
+ WithSelector (selector (t , "test=label" ).MatchLabels )).
185
+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (& v1alpha1.ClusterServiceVersion {})).
186
+ WithLabels (map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue })
187
+
188
+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
171
189
172
190
hosts := []string {
173
191
fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -255,7 +273,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
255
273
},
256
274
}
257
275
258
- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
276
+ crbLabels := map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue }
277
+ for key , val := range ownerutil .OwnerLabel (ownerutil .Owner (& v1alpha1.ClusterServiceVersion {}), owner .GetObjectKind ().GroupVersionKind ().Kind ) {
278
+ crbLabels [key ] = val
279
+ }
280
+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (AuthDelegatorClusterRoleBindingName (service .GetName ())).
281
+ WithSubjects (rbacv1ac .Subject ().
282
+ WithKind ("ServiceAccount" ).
283
+ WithAPIGroup ("" ).
284
+ WithName (args .depSpec .Template .Spec .ServiceAccountName ).
285
+ WithNamespace ("" )). // Empty owner with no namespace
286
+ WithRoleRef (rbacv1ac .RoleRef ().
287
+ WithAPIGroup ("rbac.authorization.k8s.io" ).
288
+ WithKind ("ClusterRole" ).
289
+ WithName ("system:auth-delegator" )).
290
+ WithLabels (crbLabels )
291
+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
259
292
260
293
authReaderRoleBinding := & rbacv1.RoleBinding {
261
294
Subjects : []rbacv1.Subject {
@@ -381,7 +414,6 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
381
414
{
382
415
name : "doesn't add duplicate service ownerrefs" ,
383
416
mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
384
- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
385
417
service := corev1.Service {
386
418
ObjectMeta : metav1.ObjectMeta {
387
419
Name : "test-service" ,
@@ -396,7 +428,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
396
428
Selector : selector (t , "test=label" ).MatchLabels ,
397
429
},
398
430
}
399
- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
431
+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
432
+ for _ , p := range args .ports {
433
+ ac := corev1ac .ServicePort ().
434
+ WithName (p .Name ).
435
+ WithPort (p .Port ).
436
+ WithTargetPort (p .TargetPort )
437
+ portsApplyConfig = append (portsApplyConfig , ac )
438
+ }
439
+
440
+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
441
+ WithSpec (corev1ac .ServiceSpec ().
442
+ WithPorts (portsApplyConfig ... ).
443
+ WithSelector (selector (t , "test=label" ).MatchLabels )).
444
+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (owner )).
445
+ WithLabels (map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue })
446
+
447
+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
400
448
401
449
hosts := []string {
402
450
fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -484,7 +532,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
484
532
},
485
533
}
486
534
487
- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
535
+ crbLabels := map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue }
536
+ for key , val := range ownerutil .OwnerLabel (owner , owner .GetObjectKind ().GroupVersionKind ().Kind ) {
537
+ crbLabels [key ] = val
538
+ }
539
+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (service .GetName () + "-system:auth-delegator" ).
540
+ WithSubjects (rbacv1ac .Subject ().
541
+ WithKind ("ServiceAccount" ).
542
+ WithAPIGroup ("" ).
543
+ WithName ("test-sa" ).
544
+ WithNamespace (namespace )).
545
+ WithRoleRef (rbacv1ac .RoleRef ().
546
+ WithAPIGroup ("rbac.authorization.k8s.io" ).
547
+ WithKind ("ClusterRole" ).
548
+ WithName ("system:auth-delegator" )).
549
+ WithLabels (crbLabels )
550
+
551
+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
488
552
489
553
authReaderRoleBinding := & rbacv1.RoleBinding {
490
554
Subjects : []rbacv1.Subject {
@@ -602,9 +666,8 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
602
666
},
603
667
},
604
668
{
605
- name : "labels an unlabelled secret if present" ,
669
+ name : "labels an unlabelled secret if present; creates Service and ClusterRoleBinding if not existing " ,
606
670
mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
607
- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
608
671
service := corev1.Service {
609
672
ObjectMeta : metav1.ObjectMeta {
610
673
Name : "test-service" ,
@@ -618,7 +681,24 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
618
681
Selector : selector (t , "test=label" ).MatchLabels ,
619
682
},
620
683
}
621
- mockOpClient .EXPECT ().CreateService (& service ).Return (& service , nil )
684
+
685
+ portsApplyConfig := []* corev1ac.ServicePortApplyConfiguration {}
686
+ for _ , p := range args .ports {
687
+ ac := corev1ac .ServicePort ().
688
+ WithName (p .Name ).
689
+ WithPort (p .Port ).
690
+ WithTargetPort (p .TargetPort )
691
+ portsApplyConfig = append (portsApplyConfig , ac )
692
+ }
693
+
694
+ svcApplyConfig := corev1ac .Service (service .Name , service .Namespace ).
695
+ WithSpec (corev1ac .ServiceSpec ().
696
+ WithPorts (portsApplyConfig ... ).
697
+ WithSelector (selector (t , "test=label" ).MatchLabels )).
698
+ WithOwnerReferences (ownerutil .NonBlockingOwnerApplyConfiguration (& v1alpha1.ClusterServiceVersion {})).
699
+ WithLabels (map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue })
700
+
701
+ mockOpClient .EXPECT ().ApplyService (svcApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (& service , nil )
622
702
623
703
hosts := []string {
624
704
fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -715,8 +795,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
715
795
Name : "system:auth-delegator" ,
716
796
},
717
797
}
718
-
719
- mockOpClient .EXPECT ().UpdateClusterRoleBinding (authDelegatorClusterRoleBinding ).Return (authDelegatorClusterRoleBinding , nil )
798
+ crbLabels := map [string ]string {OLMManagedLabelKey : OLMManagedLabelValue }
799
+ for key , val := range ownerutil .OwnerLabel (ownerutil .Owner (& v1alpha1.ClusterServiceVersion {}), owner .GetObjectKind ().GroupVersionKind ().Kind ) {
800
+ crbLabels [key ] = val
801
+ }
802
+ crbApplyConfig := rbacv1ac .ClusterRoleBinding (AuthDelegatorClusterRoleBindingName (service .GetName ())).
803
+ WithSubjects (rbacv1ac .Subject ().WithKind ("ServiceAccount" ).
804
+ WithAPIGroup ("" ).
805
+ WithName ("test-sa" ).
806
+ WithNamespace (namespace )).
807
+ WithRoleRef (rbacv1ac .RoleRef ().
808
+ WithAPIGroup ("rbac.authorization.k8s.io" ).
809
+ WithKind ("ClusterRole" ).
810
+ WithName ("system:auth-delegator" )).
811
+ WithLabels (crbLabels )
812
+
813
+ mockOpClient .EXPECT ().ApplyClusterRoleBinding (crbApplyConfig , metav1.ApplyOptions {Force : true , FieldManager : "olm.install" }).Return (authDelegatorClusterRoleBinding , nil )
720
814
721
815
authReaderRoleBinding := & rbacv1.RoleBinding {
722
816
Subjects : []rbacv1.Subject {
@@ -740,13 +834,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
740
834
mockOpClient .EXPECT ().UpdateRoleBinding (authReaderRoleBinding ).Return (authReaderRoleBinding , nil )
741
835
},
742
836
state : fakeState {
743
- existingService : & corev1.Service {
744
- ObjectMeta : metav1.ObjectMeta {
745
- OwnerReferences : []metav1.OwnerReference {
746
- ownerutil .NonBlockingOwner (& v1alpha1.ClusterServiceVersion {}),
747
- },
748
- },
749
- },
837
+ existingService : nil ,
750
838
// unlabelled secret won't be in cache
751
839
getSecretError : errors .NewNotFound (schema.GroupResource {
752
840
Group : "" ,
@@ -758,9 +846,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
758
846
existingRoleBinding : & rbacv1.RoleBinding {
759
847
ObjectMeta : metav1.ObjectMeta {},
760
848
},
761
- existingClusterRoleBinding : & rbacv1.ClusterRoleBinding {
762
- ObjectMeta : metav1.ObjectMeta {},
763
- },
849
+ existingClusterRoleBinding : nil ,
764
850
},
765
851
fields : fields {
766
852
owner : & v1alpha1.ClusterServiceVersion {},
0 commit comments