@@ -15,6 +15,8 @@ import (
15
15
"k8s.io/apimachinery/pkg/api/errors"
16
16
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
17
"k8s.io/apimachinery/pkg/runtime/schema"
18
+ corev1ac "k8s.io/client-go/applyconfigurations/core/v1"
19
+ rbacv1ac "k8s.io/client-go/applyconfigurations/rbac/v1"
18
20
19
21
"github.com/operator-framework/api/pkg/operators/v1alpha1"
20
22
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
@@ -152,7 +154,6 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
152
154
{
153
155
name : "adds certs to deployment spec" ,
154
156
mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
155
- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
156
157
service := corev1.Service {
157
158
ObjectMeta : metav1.ObjectMeta {
158
159
Name : "test-service" ,
@@ -165,7 +166,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
165
166
Selector : selector (t , "test=label" ).MatchLabels ,
166
167
},
167
168
}
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 )
169
186
170
187
hosts := []string {
171
188
fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -250,7 +267,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
250
267
},
251
268
}
252
269
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 )
254
286
255
287
authReaderRoleBinding := & rbacv1.RoleBinding {
256
288
Subjects : []rbacv1.Subject {
@@ -375,7 +407,6 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
375
407
{
376
408
name : "doesn't add duplicate service ownerrefs" ,
377
409
mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
378
- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
379
410
service := corev1.Service {
380
411
ObjectMeta : metav1.ObjectMeta {
381
412
Name : "test-service" ,
@@ -389,7 +420,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
389
420
Selector : selector (t , "test=label" ).MatchLabels ,
390
421
},
391
422
}
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 )
393
439
394
440
hosts := []string {
395
441
fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -474,7 +520,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
474
520
},
475
521
}
476
522
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 )
478
540
479
541
authReaderRoleBinding := & rbacv1.RoleBinding {
480
542
Subjects : []rbacv1.Subject {
@@ -591,9 +653,8 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
591
653
},
592
654
},
593
655
{
594
- name : "labels an unlabelled secret if present" ,
656
+ name : "labels an unlabelled secret if present; creates Service and ClusterRoleBinding if not existing " ,
595
657
mockExternal : func (mockOpClient * operatorclientmocks.MockClientInterface , fakeLister * operatorlisterfakes.FakeOperatorLister , namespace string , args args ) {
596
- mockOpClient .EXPECT ().DeleteService (namespace , "test-service" , & metav1.DeleteOptions {}).Return (nil )
597
658
service := corev1.Service {
598
659
ObjectMeta : metav1.ObjectMeta {
599
660
Name : "test-service" ,
@@ -606,7 +667,23 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
606
667
Selector : selector (t , "test=label" ).MatchLabels ,
607
668
},
608
669
}
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 )
610
687
611
688
hosts := []string {
612
689
fmt .Sprintf ("%s.%s" , service .GetName (), namespace ),
@@ -700,8 +777,22 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
700
777
Name : "system:auth-delegator" ,
701
778
},
702
779
}
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 )
705
796
706
797
authReaderRoleBinding := & rbacv1.RoleBinding {
707
798
Subjects : []rbacv1.Subject {
@@ -724,13 +815,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
724
815
mockOpClient .EXPECT ().UpdateRoleBinding (authReaderRoleBinding ).Return (authReaderRoleBinding , nil )
725
816
},
726
817
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 ,
734
819
// unlabelled secret won't be in cache
735
820
getSecretError : errors .NewNotFound (schema.GroupResource {
736
821
Group : "" ,
@@ -742,9 +827,7 @@ func TestInstallCertRequirementsForDeployment(t *testing.T) {
742
827
existingRoleBinding : & rbacv1.RoleBinding {
743
828
ObjectMeta : metav1.ObjectMeta {},
744
829
},
745
- existingClusterRoleBinding : & rbacv1.ClusterRoleBinding {
746
- ObjectMeta : metav1.ObjectMeta {},
747
- },
830
+ existingClusterRoleBinding : nil ,
748
831
},
749
832
fields : fields {
750
833
owner : & v1alpha1.ClusterServiceVersion {},
0 commit comments