Skip to content

Commit 0504779

Browse files
add controller for downloads service account deletion logic
1 parent 0ec9d85 commit 0504779

7 files changed

Lines changed: 169 additions & 13 deletions

File tree

bindata/assets/deployments/downloads-deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ metadata:
88
component: downloads
99
annotations: {}
1010
spec:
11-
serviceAccountName: downloads
1211
selector:
1312
matchLabels:
1413
app: console
@@ -25,6 +24,7 @@ spec:
2524
target.workload.openshift.io/management: '{"effect": "PreferredDuringScheduling"}'
2625
openshift.io/required-scc: restricted-v2
2726
spec:
27+
serviceAccountName: downloads
2828
nodeSelector:
2929
kubernetes.io/os: linux
3030
node-role.kubernetes.io/master: ""
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: downloads
5+
namespace: openshift-console
6+
annotations:
7+
include.release.openshift.io/hypershift: "true"
8+
include.release.openshift.io/ibm-cloud-managed: "true"
9+
include.release.openshift.io/self-managed-high-availability: "true"
10+
include.release.openshift.io/single-node-developer: "true"
11+
capability.openshift.io/name: Console

manifests/03-rbac-role-cluster.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ rules:
148148
- list
149149
- watch
150150
- delete
151+
- apiGroups:
152+
- ""
153+
resources:
154+
- serviceaccounts
155+
verbs:
156+
- get
157+
- list
158+
- delete
159+
- create
160+
- update
161+
- watch
151162
---
152163
kind: ClusterRole
153164
apiVersion: rbac.authorization.k8s.io/v1

manifests/06-sa.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,4 @@ metadata:
2121
include.release.openshift.io/self-managed-high-availability: "true"
2222
include.release.openshift.io/single-node-developer: "true"
2323
capability.openshift.io/name: Console
24-
---
25-
apiVersion: v1
26-
kind: ServiceAccount
27-
metadata:
28-
name: downloads
29-
namespace: openshift-console
30-
annotations:
31-
include.release.openshift.io/hypershift: "true"
32-
include.release.openshift.io/ibm-cloud-managed: "true"
33-
include.release.openshift.io/self-managed-high-availability: "true"
34-
include.release.openshift.io/single-node-developer: "true"
35-
capability.openshift.io/name: Console
3624
---
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package downloadsserviceaccount
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
corev1 "k8s.io/api/core/v1"
9+
apierrors "k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
coreinformersv1 "k8s.io/client-go/informers/core/v1"
12+
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
13+
"k8s.io/klog/v2"
14+
15+
operatorv1 "github.com/openshift/api/operator/v1"
16+
operatorinformerv1 "github.com/openshift/client-go/operator/informers/externalversions/operator/v1"
17+
operatorlistersv1 "github.com/openshift/client-go/operator/listers/operator/v1"
18+
"github.com/openshift/console-operator/pkg/api"
19+
"github.com/openshift/console-operator/pkg/console/controllers/util"
20+
"github.com/openshift/console-operator/pkg/console/status"
21+
serviceaccountsub "github.com/openshift/console-operator/pkg/console/subresource/serviceaccount"
22+
"github.com/openshift/library-go/pkg/controller/factory"
23+
"github.com/openshift/library-go/pkg/operator/events"
24+
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
25+
"github.com/openshift/library-go/pkg/operator/v1helpers"
26+
)
27+
28+
type DownloadsServiceAccountSyncController struct {
29+
operatorClient v1helpers.OperatorClient
30+
// configs
31+
consoleOperatorLister operatorlistersv1.ConsoleLister
32+
// core kube
33+
serviceAccountClient coreclientv1.ServiceAccountsGetter
34+
}
35+
36+
func NewDownloadsServiceAccountSyncController(
37+
// clients
38+
operatorClient v1helpers.OperatorClient,
39+
// informer
40+
operatorConfigInformer operatorinformerv1.ConsoleInformer,
41+
// core kube
42+
serviceAccountClient coreclientv1.ServiceAccountsGetter,
43+
serviceAccountInformer coreinformersv1.ServiceAccountInformer,
44+
// events
45+
recorder events.Recorder,
46+
) factory.Controller {
47+
ctrl := &DownloadsServiceAccountSyncController{
48+
// configs
49+
operatorClient: operatorClient,
50+
consoleOperatorLister: operatorConfigInformer.Lister(),
51+
// client
52+
serviceAccountClient: serviceAccountClient,
53+
}
54+
55+
configNameFilter := util.IncludeNamesFilter(api.ConfigResourceName)
56+
downloadsNameFilter := util.IncludeNamesFilter(api.DownloadsResourceName)
57+
58+
return factory.New().
59+
WithFilteredEventsInformers( // configs
60+
configNameFilter,
61+
operatorConfigInformer.Informer(),
62+
).WithFilteredEventsInformers( // downloads service account
63+
downloadsNameFilter,
64+
serviceAccountInformer.Informer(),
65+
).ResyncEvery(time.Minute).WithSync(ctrl.Sync).
66+
ToController("ConsoleDownloadsServiceAccountSyncController", recorder.WithComponentSuffix("console-downloads-service-account-controller"))
67+
}
68+
69+
func (c *DownloadsServiceAccountSyncController) Sync(ctx context.Context, controllerContext factory.SyncContext) error {
70+
operatorConfig, err := c.consoleOperatorLister.Get(api.ConfigResourceName)
71+
if err != nil {
72+
return err
73+
}
74+
operatorConfigCopy := operatorConfig.DeepCopy()
75+
76+
switch operatorConfigCopy.Spec.ManagementState {
77+
case operatorv1.Managed:
78+
klog.V(4).Infoln("console is in a managed state: syncing downloads service account")
79+
case operatorv1.Unmanaged:
80+
klog.V(4).Infoln("console is in an unmanaged state: skipping downloads service account sync")
81+
return nil
82+
case operatorv1.Removed:
83+
klog.V(4).Infoln("console is in a removed state: removing downloads service account")
84+
return c.removeDownloadsServiceAccount(ctx)
85+
default:
86+
return fmt.Errorf("unknown state: %v", operatorConfigCopy.Spec.ManagementState)
87+
}
88+
statusHandler := status.NewStatusHandler(c.operatorClient)
89+
90+
_, _, serviceAccountErr := c.SyncDownloadsServiceAccount(ctx, operatorConfigCopy, controllerContext)
91+
statusHandler.AddConditions(status.HandleProgressingOrDegraded("DownloadsServiceAccountSync", "FailedApply", serviceAccountErr))
92+
if serviceAccountErr != nil {
93+
return statusHandler.FlushAndReturn(serviceAccountErr)
94+
}
95+
96+
return statusHandler.FlushAndReturn(nil)
97+
}
98+
99+
func (c *DownloadsServiceAccountSyncController) SyncDownloadsServiceAccount(ctx context.Context, operatorConfigCopy *operatorv1.Console, controllerContext factory.SyncContext) (*corev1.ServiceAccount, bool, error) {
100+
requiredDownloadsServiceAccount := serviceaccountsub.DefaultDownloadsServiceAccount(operatorConfigCopy)
101+
102+
return resourceapply.ApplyServiceAccount(ctx,
103+
c.serviceAccountClient,
104+
controllerContext.Recorder(),
105+
requiredDownloadsServiceAccount,
106+
)
107+
}
108+
109+
func (c *DownloadsServiceAccountSyncController) removeDownloadsServiceAccount(ctx context.Context) error {
110+
err := c.serviceAccountClient.ServiceAccounts(api.OpenShiftConsoleNamespace).Delete(ctx, api.DownloadsResourceName, metav1.DeleteOptions{})
111+
if apierrors.IsNotFound(err) {
112+
return nil
113+
}
114+
return err
115+
}

pkg/console/starter/starter.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/openshift/console-operator/pkg/console/controllers/clidownloads"
3131
"github.com/openshift/console-operator/pkg/console/controllers/clioidcclientstatus"
3232
"github.com/openshift/console-operator/pkg/console/controllers/downloadsdeployment"
33+
"github.com/openshift/console-operator/pkg/console/controllers/downloadsserviceaccount"
3334
"github.com/openshift/console-operator/pkg/console/controllers/healthcheck"
3435
"github.com/openshift/console-operator/pkg/console/controllers/migration"
3536
"github.com/openshift/console-operator/pkg/console/controllers/oauthclients"
@@ -338,6 +339,17 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
338339
recorder,
339340
)
340341

342+
downloadsServiceAccountController := downloadsserviceaccount.NewDownloadsServiceAccountSyncController(
343+
// clients
344+
operatorClient,
345+
// operator
346+
operatorConfigInformers.Operator().V1().Consoles(),
347+
// service accounts
348+
kubeClient.CoreV1(),
349+
kubeInformersNamespaced.Core().V1().ServiceAccounts(),
350+
recorder,
351+
)
352+
341353
cliDownloadsController := clidownloads.NewCLIDownloadsSyncController(
342354
// top level config
343355
configClient.ConfigV1(),
@@ -639,6 +651,7 @@ func RunOperator(ctx context.Context, controllerContext *controllercmd.Controlle
639651
consoleOperator,
640652
cliDownloadsController,
641653
downloadsDeploymentController,
654+
downloadsServiceAccountController,
642655
consoleRouteHealthCheckController,
643656
consolePDBController,
644657
downloadsPDBController,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package serviceaccount
2+
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
6+
operatorv1 "github.com/openshift/api/operator/v1"
7+
"github.com/openshift/console-operator/bindata"
8+
"github.com/openshift/console-operator/pkg/console/subresource/util"
9+
"github.com/openshift/library-go/pkg/operator/resource/resourceread"
10+
)
11+
12+
func DefaultDownloadsServiceAccount(operatorConfig *operatorv1.Console) *corev1.ServiceAccount {
13+
serviceAccount := resourceread.ReadServiceAccountV1OrDie(
14+
bindata.MustAsset("assets/serviceaccounts/downloads-sa.yaml"),
15+
)
16+
util.AddOwnerRef(serviceAccount, util.OwnerRefFrom(operatorConfig))
17+
return serviceAccount
18+
}

0 commit comments

Comments
 (0)