@@ -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,16 +12,17 @@ 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
23- corev1 "k8s.io/api/core/v1"
2426 apierrors "k8s.io/apimachinery/pkg/api/errors"
2527 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628 coreinformersv1 "k8s.io/client-go/informers/core/v1"
@@ -54,8 +56,6 @@ func NewServiceAccountSyncController(
5456 serviceAccountName string ,
5557 // controllerName,
5658 controllerName string ,
57- // controllerSuffix
58- controllerSuffix string ,
5959) factory.Controller {
6060 configV1Informers := configInformer .Config ().V1 ()
6161
@@ -81,7 +81,7 @@ func NewServiceAccountSyncController(
8181 serviceAccountNameFilter ,
8282 serviceAccountInformer .Informer (),
8383 ).ResyncEvery (time .Minute ).WithSync (ctrl .Sync ).
84- ToController (controllerName , recorder .WithComponentSuffix (controllerSuffix ))
84+ ToController (fmt . Sprintf ( "%sServiceAccountController" , strings . Title ( controllerName )) , recorder .WithComponentSuffix (fmt . Sprintf ( "%s-service-account-controller" , controllerName ) ))
8585}
8686
8787func (c * ServiceAccountSyncController ) Sync (ctx context.Context , controllerContext factory.SyncContext ) error {
@@ -93,19 +93,19 @@ func (c *ServiceAccountSyncController) Sync(ctx context.Context, controllerConte
9393
9494 switch operatorConfigCopy .Spec .ManagementState {
9595 case operatorv1 .Managed :
96- klog .V (4 ).Infoln ("console is in a managed state: syncing service account " )
96+ klog .V (4 ).Infoln ("console is in a managed state: syncing serviceaccount " )
9797 case operatorv1 .Unmanaged :
98- klog .V (4 ).Infoln ("console is in an unmanaged state: skipping service account sync" )
98+ klog .V (4 ).Infoln ("console is in an unmanaged state: skipping serviceaccount sync" )
9999 return nil
100100 case operatorv1 .Removed :
101- klog .V (4 ).Infoln ("console is in a removed state: removing synced service account " )
101+ klog .V (4 ).Infoln ("console is in a removed state: removing synced serviceaccount " )
102102 return c .removeServiceAccount (ctx )
103103 default :
104104 return fmt .Errorf ("unknown state: %v" , operatorConfigCopy .Spec .ManagementState )
105105 }
106106 statusHandler := status .NewStatusHandler (c .operatorClient )
107107
108- _ , _ , serviceAccountErr := c .SyncServiceAccount (ctx , operatorConfigCopy , controllerContext )
108+ serviceAccountErr := c .SyncServiceAccount (ctx , operatorConfigCopy , controllerContext )
109109 statusHandler .AddConditions (status .HandleProgressingOrDegraded ("ServiceAccountSync" , "FailedApply" , serviceAccountErr ))
110110 if serviceAccountErr != nil {
111111 return statusHandler .FlushAndReturn (serviceAccountErr )
@@ -122,25 +122,41 @@ func (c *ServiceAccountSyncController) removeServiceAccount(ctx context.Context)
122122 return err
123123}
124124
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 )
127- if err != nil {
128- return nil , false , err
125+ func (c * ServiceAccountSyncController ) SyncServiceAccount (ctx context.Context , operatorConfigCopy * operatorv1.Console , controllerContext factory.SyncContext ) error {
126+ serviceAccount := resourceread .ReadServiceAccountV1OrDie (
127+ bindata .MustAsset (fmt .Sprintf ("assets/serviceaccounts/%s-sa.yaml" , c .serviceAccountName )),
128+ )
129+ if serviceAccount .Name == "" {
130+ return fmt .Errorf ("No service account found for name %v ." , c .serviceAccountName )
129131 }
130132
131- // if the object has one or more ownerRef objects, then we must
132- // ensure that their controller attribute is set to false.
133133 // Only one ownerRef.controller == true .
134134 // https://kubernetes.io/docs/concepts/overview/working-with-objects/owners-dependents/#owner-references-in-object-specifications
135+ ownerRefs := serviceAccount .GetOwnerReferences ()
135136
136- for oR := range requiredServiceAccount . OwnerReferences {
137+ for oR := range ownerRefs {
137138 falseBool := false
138- requiredServiceAccount . OwnerReferences [oR ].Controller = & falseBool
139+ ownerRefs [oR ].Controller = & falseBool
139140 }
140141
141- return resourceapply .ApplyServiceAccount (ctx ,
142+ // Add owner reference from the Console operator config as the single controller owner.
143+ if ownerRef := subresourceutil .OwnerRefFrom (operatorConfigCopy ); ownerRef != nil {
144+ truthy := true
145+ ownerRef .Controller = & truthy
146+ ownerRefs = append (ownerRefs , * ownerRef )
147+ }
148+
149+ serviceAccount .SetOwnerReferences (ownerRefs )
150+
151+ _ , _ , err := resourceapply .ApplyServiceAccount (ctx ,
142152 c .serviceAccountClient ,
143153 controllerContext .Recorder (),
144- requiredServiceAccount ,
154+ serviceAccount ,
145155 )
156+
157+ if err != nil {
158+ return err
159+ }
160+
161+ return nil
146162}
0 commit comments