@@ -3,6 +3,7 @@ package serviceaccounts
33import (
44 "context"
55 "fmt"
6+ "strings"
67 "time"
78
89 operatorv1 "github.com/openshift/api/operator/v1"
@@ -11,13 +12,15 @@ import (
1112 operatorinformerv1 "github.com/openshift/client-go/operator/informers/externalversions/operator/v1"
1213 operatorlistersv1 "github.com/openshift/client-go/operator/listers/operator/v1"
1314
15+ "github.com/openshift/console-operator/bindata"
1416 "github.com/openshift/console-operator/pkg/api"
1517 "github.com/openshift/console-operator/pkg/console/controllers/util"
1618 "github.com/openshift/console-operator/pkg/console/status"
17- serviceaccountssub "github.com/openshift/console-operator/pkg/console/subresource/serviceaccount "
19+ subresourceutil "github.com/openshift/console-operator/pkg/console/subresource/util "
1820 "github.com/openshift/library-go/pkg/controller/factory"
1921 "github.com/openshift/library-go/pkg/operator/events"
2022 "github.com/openshift/library-go/pkg/operator/resource/resourceapply"
23+ "github.com/openshift/library-go/pkg/operator/resource/resourceread"
2124 "github.com/openshift/library-go/pkg/operator/v1helpers"
2225
2326 corev1 "k8s.io/api/core/v1"
@@ -31,6 +34,7 @@ import (
3134
3235type ServiceAccountSyncController struct {
3336 serviceAccountName string
37+ conditionType string
3438 operatorClient v1helpers.OperatorClient
3539 // configs
3640 consoleOperatorLister operatorlistersv1.ConsoleLister
@@ -54,13 +58,12 @@ func NewServiceAccountSyncController(
5458 serviceAccountName string ,
5559 // controllerName,
5660 controllerName string ,
57- // controllerSuffix
58- controllerSuffix string ,
5961) factory.Controller {
6062 configV1Informers := configInformer .Config ().V1 ()
6163
6264 ctrl := & ServiceAccountSyncController {
6365 serviceAccountName : serviceAccountName ,
66+ conditionType : fmt .Sprintf ("%sServiceAccountSync" , controllerName ),
6467 // configs
6568 operatorClient : operatorClient ,
6669 consoleOperatorLister : operatorConfigInformer .Lister (),
@@ -81,32 +84,32 @@ func NewServiceAccountSyncController(
8184 serviceAccountNameFilter ,
8285 serviceAccountInformer .Informer (),
8386 ).ResyncEvery (time .Minute ).WithSync (ctrl .Sync ).
84- ToController (controllerName , recorder .WithComponentSuffix (controllerSuffix ))
87+ ToController (fmt . Sprintf ( "%sServiceAccountController" , strings . Title ( controllerName )) , recorder .WithComponentSuffix (fmt . Sprintf ( "%s-service-account-controller" , controllerName ) ))
8588}
8689
8790func (c * ServiceAccountSyncController ) Sync (ctx context.Context , controllerContext factory.SyncContext ) error {
8891 operatorConfig , err := c .consoleOperatorLister .Get (api .ConfigResourceName )
8992 if err != nil {
90- return err
93+ return fmt . Errorf ( "failed to get console operator config %s: %w" , api . ConfigResourceName , err )
9194 }
9295 operatorConfigCopy := operatorConfig .DeepCopy ()
9396
9497 switch operatorConfigCopy .Spec .ManagementState {
9598 case operatorv1 .Managed :
96- klog .V (4 ).Infoln ("console is in a managed state: syncing service account " )
99+ klog .V (4 ).Infoln ("console is in a managed state: syncing serviceaccount " )
97100 case operatorv1 .Unmanaged :
98- klog .V (4 ).Infoln ("console is in an unmanaged state: skipping service account sync" )
101+ klog .V (4 ).Infoln ("console is in an unmanaged state: skipping serviceaccount sync" )
99102 return nil
100103 case operatorv1 .Removed :
101- klog .V (4 ).Infoln ("console is in a removed state: removing synced service account " )
104+ klog .V (4 ).Infoln ("console is in a removed state: removing synced serviceaccount " )
102105 return c .removeServiceAccount (ctx )
103106 default :
104107 return fmt .Errorf ("unknown state: %v" , operatorConfigCopy .Spec .ManagementState )
105108 }
106109 statusHandler := status .NewStatusHandler (c .operatorClient )
107110
108- _ , _ , serviceAccountErr := c .SyncServiceAccount (ctx , operatorConfigCopy , controllerContext )
109- statusHandler .AddConditions (status .HandleProgressingOrDegraded ("ServiceAccountSync" , "FailedApply" , serviceAccountErr ))
111+ serviceAccountErr := c .SyncServiceAccount (ctx , operatorConfigCopy , controllerContext )
112+ statusHandler .AddConditions (status .HandleProgressingOrDegraded (c . conditionType , "FailedApply" , serviceAccountErr ))
110113 if serviceAccountErr != nil {
111114 return statusHandler .FlushAndReturn (serviceAccountErr )
112115 }
@@ -122,25 +125,33 @@ func (c *ServiceAccountSyncController) removeServiceAccount(ctx context.Context)
122125 return err
123126}
124127
125- func (c * ServiceAccountSyncController ) SyncServiceAccount (ctx context.Context , operatorConfigCopy * operatorv1.Console , controllerContext factory.SyncContext ) (* corev1.ServiceAccount , bool , error ) {
126- requiredServiceAccount , err := serviceaccountssub .DefaultServiceAccountFactory (c .serviceAccountName , operatorConfigCopy )
128+ func (c * ServiceAccountSyncController ) SyncServiceAccount (ctx context.Context , operatorConfigCopy * operatorv1.Console , controllerContext factory.SyncContext ) error {
129+ serviceAccount , err := c .DefaultServiceAccount (operatorConfigCopy )
130+
127131 if err != nil {
128- return nil , false , err
132+ return err
129133 }
130134
131- // if the object has one or more ownerRef objects, then we must
132- // ensure that their controller attribute is set to false.
133- // Only one ownerRef.controller == true .
134- // https://kubernetes.io/docs/concepts/overview/working-with-objects/owners-dependents/#owner-references-in-object-specifications
135+ _ , _ , err = resourceapply .ApplyServiceAccount (ctx ,
136+ c .serviceAccountClient ,
137+ controllerContext .Recorder (),
138+ serviceAccount ,
139+ )
135140
136- for oR := range requiredServiceAccount .OwnerReferences {
137- falseBool := false
138- requiredServiceAccount .OwnerReferences [oR ].Controller = & falseBool
141+ if err != nil {
142+ return fmt .Errorf ("failed to apply service account %s: %w" , c .serviceAccountName , err )
139143 }
140144
141- return resourceapply .ApplyServiceAccount (ctx ,
142- c .serviceAccountClient ,
143- controllerContext .Recorder (),
144- requiredServiceAccount ,
145+ return nil
146+ }
147+
148+ func (c * ServiceAccountSyncController ) DefaultServiceAccount (cr * operatorv1.Console ) (* corev1.ServiceAccount , error ) {
149+ serviceAccount := resourceread .ReadServiceAccountV1OrDie (
150+ bindata .MustAsset (fmt .Sprintf ("assets/serviceaccounts/%s-sa.yaml" , c .serviceAccountName )),
145151 )
152+ if serviceAccount .Name == "" {
153+ return nil , fmt .Errorf ("No service account found for name %v ." , c .serviceAccountName )
154+ }
155+ subresourceutil .AddOwnerRef (serviceAccount , subresourceutil .OwnerRefFrom (cr ))
156+ return serviceAccount , nil
146157}
0 commit comments