Skip to content

Commit 216d657

Browse files
simplify controller logic + lock down cr
1 parent 2f12153 commit 216d657

5 files changed

Lines changed: 58 additions & 78 deletions

File tree

manifests/03-rbac-role-cluster.yaml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,20 @@ rules:
153153
resources:
154154
- serviceaccounts
155155
verbs:
156-
- get
157156
- list
158-
- delete
157+
- watch
158+
- get
159+
- apiGroups:
160+
- ""
161+
resources:
162+
- serviceaccounts
163+
verbs:
159164
- create
160165
- update
161-
- watch
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 & 19 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,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

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

pkg/console/starter/starter.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
340340
recorder,
341341
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)