Skip to content

Commit 3f7011f

Browse files
simplify controller logic + lock down cr
1 parent 2f12153 commit 3f7011f

5 files changed

Lines changed: 60 additions & 85 deletions

File tree

manifests/03-rbac-role-cluster.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,20 @@ rules:
153153
resources:
154154
- serviceaccounts
155155
verbs:
156-
- get
157-
- list
158-
- delete
159156
- create
160-
- update
157+
- list
161158
- watch
159+
- get
160+
- apiGroups:
161+
- ""
162+
resources:
163+
- serviceaccounts
164+
verbs:
165+
- update
166+
- delete
167+
resourceNames:
168+
- console
169+
- downloads
162170
---
163171
kind: ClusterRole
164172
apiVersion: rbac.authorization.k8s.io/v1

pkg/api/api.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@ const (
5656
DefaultIngressController = "default"
5757
IngressControllerNamespace = "openshift-ingress-operator"
5858

59-
OAuthClientName = OpenShiftConsoleName
60-
OpenShiftConsoleDeploymentName = OpenShiftConsoleName
61-
OpenShiftConsoleDownloadsDeploymentName = DownloadsResourceName
62-
OpenShiftConsoleDownloadsPDBName = DownloadsResourceName
63-
OpenShiftConsoleDownloadsRouteName = DownloadsResourceName
64-
OpenShiftConsoleNamespace = TargetNamespace
65-
OpenShiftConsolePDBName = OpenShiftConsoleName
66-
OpenShiftConsoleRouteName = OpenShiftConsoleName
67-
OpenShiftConsoleServiceName = OpenShiftConsoleName
68-
OpenShiftConsoleDownloadsServiceAccountName = OpenShiftConsoleDownloadsDeploymentName
69-
OpenshiftConsoleServiceAccountName = OpenShiftConsoleDeploymentName
70-
RedirectContainerTargetPort = RedirectContainerPort
71-
OpenShiftConsoleSASyncControllerSuffix = "ConsoleServiceAccountSyncController"
72-
OpenshiftConsoleDownloadsSASyncControllerPrefix = "DownloadsServiceAccountSyncController"
59+
OAuthClientName = OpenShiftConsoleName
60+
OpenShiftConsoleDeploymentName = OpenShiftConsoleName
61+
OpenShiftConsoleDownloadsDeploymentName = DownloadsResourceName
62+
OpenShiftConsoleDownloadsPDBName = DownloadsResourceName
63+
OpenShiftConsoleDownloadsRouteName = DownloadsResourceName
64+
OpenShiftConsoleNamespace = TargetNamespace
65+
OpenShiftConsolePDBName = OpenShiftConsoleName
66+
OpenShiftConsoleRouteName = OpenShiftConsoleName
67+
OpenShiftConsoleServiceName = OpenShiftConsoleName
68+
OpenShiftConsoleDownloadsServiceAccountName = OpenShiftConsoleDownloadsDeploymentName
69+
OpenShiftConsoleServiceAccountName = OpenShiftConsoleDeploymentName
70+
RedirectContainerTargetPort = RedirectContainerPort
7371
)

pkg/console/controllers/serviceaccounts/controller.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package serviceaccounts
33
import (
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

3235
type 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

8790
func (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
}

pkg/console/starter/starter.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,8 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
338338
kubeInformersNamespaced.Core().V1().ServiceAccounts(), // ServiceAccount
339339

340340
recorder,
341-
api.OpenshiftConsoleServiceAccountName,
341+
api.OpenShiftConsoleServiceAccountName,
342342
api.OpenShiftConsoleName, // controller name
343-
api.OpenShiftConsoleSASyncControllerSuffix,
344343
)
345344

346345
downloadsServiceAccountController := serviceaccounts.NewServiceAccountSyncController(
@@ -357,7 +356,6 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
357356

358357
api.OpenShiftConsoleDownloadsServiceAccountName,
359358
api.DownloadsResourceName,
360-
api.OpenshiftConsoleDownloadsSASyncControllerPrefix,
361359
)
362360

363361
downloadsDeploymentController := downloadsdeployment.NewDownloadsDeploymentSyncController(

pkg/console/subresource/serviceaccount/serviceaccount.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)