-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add secret expiration reconciler #588
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
vprashar2929
merged 1 commit into
sustainable-computing-io:v1alpha1
from
KaiyiLiu1234:token-expiration
Oct 6, 2025
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
|
|
||
| // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) | ||
| // to ensure that exec-entrypoint and run can make use of them. | ||
|
|
@@ -71,6 +72,8 @@ func main() { | |
| var enableHTTP2 bool | ||
| var tlsOpts []func(*tls.Config) | ||
| var additionalNamespaces stringList | ||
| var tokenRefreshInterval time.Duration | ||
| var tokenTTL time.Duration | ||
|
|
||
| flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to."+ | ||
| "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.") | ||
|
|
@@ -92,6 +95,12 @@ func main() { | |
| flag.BoolVar(&openshift, "openshift", false, | ||
| "Indicate if the operator is running on an OpenShift cluster.") | ||
|
|
||
| flag.DurationVar(&tokenRefreshInterval, "exp.reconciler.token.refresh-interval", controller.Config.TokenRefreshInterval, | ||
| "Interval at which the token expiry reconciler requeues for reconciliation.") | ||
|
|
||
| flag.DurationVar(&tokenTTL, "exp.uwm.token.ttl", controller.Config.TokenTTL, | ||
| "Time-to-live duration for user workload monitoring tokens.") | ||
|
|
||
| // NOTE: RELATED_IMAGE_KEPLER can be set as env or flag, flag takes precedence over env | ||
| keplerImage := os.Getenv("RELATED_IMAGE_KEPLER") | ||
| flag.StringVar(&controller.Config.Image, "kepler.image", keplerImage, "kepler image") | ||
|
|
@@ -136,6 +145,10 @@ func main() { | |
| } | ||
| } | ||
|
|
||
| controller.Config.TokenRefreshInterval = tokenRefreshInterval | ||
| controller.Config.TokenTTL = tokenTTL | ||
| powermonitor.TokenTTL = tokenTTL | ||
|
|
||
| // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server. | ||
| // More info: | ||
| // - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/server | ||
|
|
@@ -200,6 +213,13 @@ func main() { | |
| os.Exit(1) | ||
| } | ||
|
|
||
| if err = (&controller.TokenExpiryReconciler{ | ||
| Client: mgr.GetClient(), | ||
| Scheme: mgr.GetScheme(), | ||
| }).SetupWithManager(mgr); err != nil { | ||
| setupLog.Error(err, "unable to create controller", "controller", "token-expiry") | ||
| os.Exit(1) | ||
| } | ||
| if err = (&controller.PowerMonitorReconciler{ | ||
| Client: mgr.GetClient(), | ||
| Scheme: mgr.GetScheme(), | ||
|
|
||
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
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
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,147 @@ | ||
| // SPDX-FileCopyrightText: 2025 The Kepler Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "sigs.k8s.io/controller-runtime/pkg/builder" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
|
||
| powermonitor "github.com/sustainable.computing.io/kepler-operator/pkg/components/power-monitor" | ||
| "github.com/sustainable.computing.io/kepler-operator/pkg/reconciler" | ||
|
|
||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| ) | ||
|
|
||
| type TokenExpiryReconciler struct { | ||
| client.Client | ||
| Scheme *runtime.Scheme | ||
| logger logr.Logger | ||
| } | ||
|
|
||
| // RBAC for TokenExpirationReconciler | ||
| //+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;delete | ||
|
|
||
| // SetupWithManager sets up the controller with the Manager. | ||
| func (r *TokenExpiryReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
| secretPredicate := builder.WithPredicates(predicate.Funcs{ | ||
| CreateFunc: func(e event.CreateEvent) bool { | ||
| return r.inPowerMonitorNamespace(e.Object) && r.isPrometheusUserWorkloadToken(e.Object) | ||
| }, | ||
| UpdateFunc: func(e event.UpdateEvent) bool { | ||
| return r.inPowerMonitorNamespace(e.ObjectNew) && r.isPrometheusUserWorkloadToken(e.ObjectNew) | ||
| }, | ||
| DeleteFunc: func(e event.DeleteEvent) bool { | ||
| return false | ||
| }, | ||
| GenericFunc: func(e event.GenericEvent) bool { | ||
| return r.inPowerMonitorNamespace(e.Object) && r.isPrometheusUserWorkloadToken(e.Object) | ||
| }, | ||
| }) | ||
|
|
||
| return ctrl.NewControllerManagedBy(mgr). | ||
| For(&corev1.Secret{}, secretPredicate). | ||
| Complete(r) | ||
| } | ||
|
|
||
| // inPowerMonitorNamespace checks if object is in the PowerMonitorDeploymentNS namespace | ||
| func (r *TokenExpiryReconciler) inPowerMonitorNamespace(obj client.Object) bool { | ||
| return obj.GetNamespace() == PowerMonitorDeploymentNS | ||
| } | ||
|
|
||
| // isPrometheusUserWorkloadToken checks if the secret is the prometheus-user-workload-token | ||
| func (r *TokenExpiryReconciler) isPrometheusUserWorkloadToken(obj client.Object) bool { | ||
| return obj.GetName() == powermonitor.SecretUWMTokenName | ||
| } | ||
|
|
||
| // hasExpirationAnnotation checks if the secret has an expiration annotation | ||
| func (r *TokenExpiryReconciler) hasExpirationAnnotation(obj client.Object) bool { | ||
| secret, ok := obj.(*corev1.Secret) | ||
| if !ok { | ||
| return false | ||
| } | ||
|
|
||
| annotations := secret.GetAnnotations() | ||
| if annotations == nil { | ||
| return false | ||
| } | ||
|
|
||
| _, exists := annotations[powermonitor.SecretTokenExpirationAnnotation] | ||
| return exists | ||
| } | ||
|
|
||
| // deleteResources is a helper function that creates and runs deleter reconcilers for the given resources | ||
| func (r *TokenExpiryReconciler) deleteResources(ctx context.Context, resources ...client.Object) (ctrl.Result, error) { | ||
| reconcilers := resourceReconcilers(deleteResource, resources...) | ||
|
|
||
| return reconciler.Runner{ | ||
| Reconcilers: reconcilers, | ||
| Client: r.Client, | ||
| Scheme: r.Scheme, | ||
| Logger: r.logger, | ||
| }.Run(ctx) | ||
| } | ||
|
|
||
| func (r *TokenExpiryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
| logger := log.FromContext(ctx) | ||
| r.logger = logger | ||
|
|
||
| logger.Info("Start of reconcile") | ||
| defer logger.Info("End of reconcile") | ||
|
|
||
| secret := &corev1.Secret{} | ||
| err := r.Get(ctx, req.NamespacedName, secret) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| r.logger.Info("secret not found, continue without error") | ||
| return ctrl.Result{}, nil | ||
| } | ||
| r.logger.Error(err, "failed to retrieve secret") | ||
| return ctrl.Result{}, err | ||
| } | ||
|
|
||
| if !r.hasExpirationAnnotation(secret) { | ||
| r.logger.Info("prometheus-user-workload-token does not have expiration annotation, deleting it") | ||
| return r.deleteResources(ctx, secret) | ||
| } | ||
|
|
||
| expired, expirationTime, err := r.isSecretExpired(secret) | ||
| if err != nil { | ||
| r.logger.Error(err, "failed to extract expiration time") | ||
| return ctrl.Result{RequeueAfter: time.Minute * 5}, nil | ||
| } | ||
|
|
||
| if expired { | ||
| r.logger.Info("secret has expired, reconciling", "expiration-time", expirationTime) | ||
| return r.deleteResources(ctx, secret) | ||
| } | ||
|
|
||
| timeUntilExpiration := time.Until(expirationTime) | ||
| r.logger.Info("secret not expired yet, requeuing", "expiration-time", expirationTime, "time-until-expiration", timeUntilExpiration) | ||
|
|
||
| return ctrl.Result{RequeueAfter: Config.TokenRefreshInterval}, nil | ||
| } | ||
|
|
||
| // isSecretExpired checks if the secret has expired according to the expiration annotation | ||
| func (r *TokenExpiryReconciler) isSecretExpired(secret *corev1.Secret) (bool, time.Time, error) { | ||
| expirationTime, err := powermonitor.GetExpirationFromAnnotation(&secret.ObjectMeta, powermonitor.SecretTokenExpirationAnnotation) | ||
| if err != nil { | ||
| return false, time.Time{}, err | ||
| } | ||
| if expirationTime == nil { | ||
| return false, time.Time{}, nil | ||
| } | ||
| return time.Now().After(expirationTime.Add(-(Config.TokenRefreshInterval * 2))), *expirationTime, nil | ||
vprashar2929 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.