-
Notifications
You must be signed in to change notification settings - Fork 158
OCPBUGS-65622: add controller for downloads service account deletion logic #1111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 8 commits into
openshift:main
from
ehearne-redhat:downloads-service-account-controller
Mar 26, 2026
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0504779
add controller for downloads service account deletion logic
ehearne-redhat 4fa2f2e
moved service account logic to downloads deployment
ehearne-redhat 9612471
ensure ownerrefs have one or less controller true
ehearne-redhat 480f7f2
use dedicated sa controller for console and downloads sa management
ehearne-redhat 860016b
ensure ownerref doesn't get overwritten
ehearne-redhat 9a3e679
move ownerref logic to sa controller
ehearne-redhat 2f12153
add downloads sa to test
ehearne-redhat 1ee9a0a
simplify controller logic + lock down cr
ehearne-redhat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| apiVersion: v1 | ||
| kind: ServiceAccount | ||
| metadata: | ||
| name: downloads | ||
| namespace: openshift-console | ||
| annotations: | ||
| include.release.openshift.io/hypershift: "true" | ||
| include.release.openshift.io/ibm-cloud-managed: "true" | ||
| include.release.openshift.io/self-managed-high-availability: "true" | ||
| include.release.openshift.io/single-node-developer: "true" | ||
| capability.openshift.io/name: Console |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
pkg/console/controllers/downloadsserviceaccount/controller.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package downloadsserviceaccount | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| coreinformersv1 "k8s.io/client-go/informers/core/v1" | ||
| coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
| "k8s.io/klog/v2" | ||
|
|
||
| operatorv1 "github.com/openshift/api/operator/v1" | ||
| operatorinformerv1 "github.com/openshift/client-go/operator/informers/externalversions/operator/v1" | ||
| operatorlistersv1 "github.com/openshift/client-go/operator/listers/operator/v1" | ||
| "github.com/openshift/console-operator/pkg/api" | ||
| "github.com/openshift/console-operator/pkg/console/controllers/util" | ||
| "github.com/openshift/console-operator/pkg/console/status" | ||
| serviceaccountsub "github.com/openshift/console-operator/pkg/console/subresource/serviceaccount" | ||
| "github.com/openshift/library-go/pkg/controller/factory" | ||
| "github.com/openshift/library-go/pkg/operator/events" | ||
| "github.com/openshift/library-go/pkg/operator/resource/resourceapply" | ||
| "github.com/openshift/library-go/pkg/operator/v1helpers" | ||
| ) | ||
|
|
||
| type DownloadsServiceAccountSyncController struct { | ||
| operatorClient v1helpers.OperatorClient | ||
| // configs | ||
| consoleOperatorLister operatorlistersv1.ConsoleLister | ||
| // core kube | ||
| serviceAccountClient coreclientv1.ServiceAccountsGetter | ||
| } | ||
|
|
||
| func NewDownloadsServiceAccountSyncController( | ||
| // clients | ||
| operatorClient v1helpers.OperatorClient, | ||
| // informer | ||
| operatorConfigInformer operatorinformerv1.ConsoleInformer, | ||
| // core kube | ||
| serviceAccountClient coreclientv1.ServiceAccountsGetter, | ||
| serviceAccountInformer coreinformersv1.ServiceAccountInformer, | ||
| // events | ||
| recorder events.Recorder, | ||
| ) factory.Controller { | ||
| ctrl := &DownloadsServiceAccountSyncController{ | ||
| // configs | ||
| operatorClient: operatorClient, | ||
| consoleOperatorLister: operatorConfigInformer.Lister(), | ||
| // client | ||
| serviceAccountClient: serviceAccountClient, | ||
| } | ||
|
|
||
| configNameFilter := util.IncludeNamesFilter(api.ConfigResourceName) | ||
| downloadsNameFilter := util.IncludeNamesFilter(api.DownloadsResourceName) | ||
|
|
||
| return factory.New(). | ||
| WithFilteredEventsInformers( // configs | ||
| configNameFilter, | ||
| operatorConfigInformer.Informer(), | ||
| ).WithFilteredEventsInformers( // downloads service account | ||
| downloadsNameFilter, | ||
| serviceAccountInformer.Informer(), | ||
| ).ResyncEvery(time.Minute).WithSync(ctrl.Sync). | ||
| ToController("ConsoleDownloadsServiceAccountSyncController", recorder.WithComponentSuffix("console-downloads-service-account-controller")) | ||
| } | ||
|
|
||
| func (c *DownloadsServiceAccountSyncController) Sync(ctx context.Context, controllerContext factory.SyncContext) error { | ||
| operatorConfig, err := c.consoleOperatorLister.Get(api.ConfigResourceName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| operatorConfigCopy := operatorConfig.DeepCopy() | ||
|
|
||
| switch operatorConfigCopy.Spec.ManagementState { | ||
| case operatorv1.Managed: | ||
| klog.V(4).Infoln("console is in a managed state: syncing downloads service account") | ||
| case operatorv1.Unmanaged: | ||
| klog.V(4).Infoln("console is in an unmanaged state: skipping downloads service account sync") | ||
| return nil | ||
| case operatorv1.Removed: | ||
| klog.V(4).Infoln("console is in a removed state: removing downloads service account") | ||
| return c.removeDownloadsServiceAccount(ctx) | ||
| default: | ||
| return fmt.Errorf("unknown state: %v", operatorConfigCopy.Spec.ManagementState) | ||
| } | ||
| statusHandler := status.NewStatusHandler(c.operatorClient) | ||
|
|
||
| _, _, serviceAccountErr := c.SyncDownloadsServiceAccount(ctx, operatorConfigCopy, controllerContext) | ||
| statusHandler.AddConditions(status.HandleProgressingOrDegraded("DownloadsServiceAccountSync", "FailedApply", serviceAccountErr)) | ||
| if serviceAccountErr != nil { | ||
| return statusHandler.FlushAndReturn(serviceAccountErr) | ||
| } | ||
|
|
||
| return statusHandler.FlushAndReturn(nil) | ||
| } | ||
|
|
||
| func (c *DownloadsServiceAccountSyncController) SyncDownloadsServiceAccount(ctx context.Context, operatorConfigCopy *operatorv1.Console, controllerContext factory.SyncContext) (*corev1.ServiceAccount, bool, error) { | ||
| requiredDownloadsServiceAccount := serviceaccountsub.DefaultDownloadsServiceAccount(operatorConfigCopy) | ||
|
|
||
| return resourceapply.ApplyServiceAccount(ctx, | ||
| c.serviceAccountClient, | ||
| controllerContext.Recorder(), | ||
| requiredDownloadsServiceAccount, | ||
| ) | ||
| } | ||
|
|
||
| func (c *DownloadsServiceAccountSyncController) removeDownloadsServiceAccount(ctx context.Context) error { | ||
| err := c.serviceAccountClient.ServiceAccounts(api.OpenShiftConsoleNamespace).Delete(ctx, api.DownloadsResourceName, metav1.DeleteOptions{}) | ||
| if apierrors.IsNotFound(err) { | ||
| return nil | ||
| } | ||
| return err | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package serviceaccount | ||
|
|
||
| import ( | ||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| operatorv1 "github.com/openshift/api/operator/v1" | ||
| "github.com/openshift/console-operator/bindata" | ||
| "github.com/openshift/console-operator/pkg/console/subresource/util" | ||
| "github.com/openshift/library-go/pkg/operator/resource/resourceread" | ||
| ) | ||
|
|
||
| func DefaultDownloadsServiceAccount(operatorConfig *operatorv1.Console) *corev1.ServiceAccount { | ||
| serviceAccount := resourceread.ReadServiceAccountV1OrDie( | ||
| bindata.MustAsset("assets/serviceaccounts/downloads-sa.yaml"), | ||
| ) | ||
| util.AddOwnerRef(serviceAccount, util.OwnerRefFrom(operatorConfig)) | ||
| return serviceAccount | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We just need one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking here , if we did squash it all together then we may have issues with looking for service accounts. I kept it separate so we can list, watch and get any service accounts. However, there is no reason why we cannot merge the first two rules.
From what I understand, the minimum number of rules we can have is two. We can move
list,watchandgetto the third rule. However, we cannot restrictcreate.As for the third rule, if we wanted to make modifications to the service account, we wouldn't want for the controller to be able to delete just any service account, so having
resourceNamesspecified here allows us to restrict where possible.So, the compromise we can do is keep
createwithout restriction, and we can restrict everything else. I'll go ahead and update to reflect this, and feel free to correct me if I misunderstood anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also need to double check if the
getused in the controller looks for these service accounts specifically or not and make adjustments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @jhadvig - sadly we cannot use it like this. Found this in console-operator logs
This is the current rule formation:
I will probably need to move it to this
Let me know if this suits. :)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
its a bit weird that we have the
createaction but notdelete.if anything I would add the
createto the second list, sicne we are only creating these two SAs