Skip to content

Commit 7c81e59

Browse files
feat: make reconciliation interval and token lifetime configurable (#108)
* feat: make reconciliation interval and token lifetime configurable * add new flag to helm chart * feat: release v0.1.15 * add default fluxTokenLifetime value * remove obsolete error * fix tests
1 parent 26a51d3 commit 7c81e59

File tree

11 files changed

+37
-28
lines changed

11 files changed

+37
-28
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.1.14-dev
1+
v0.1.15

charts/control-plane-operator/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ apiVersion: v2
22
name: control-plane-operator
33
description: A Helm chart for the Cloud Orchestration Control Plane Operator
44
type: application
5-
version: v0.1.14
6-
appVersion: v0.1.14
5+
version: v0.1.15
6+
appVersion: v0.1.15
77
home: https://github.com/openmcp-project/control-plane-operator
88
sources:
99
- https://github.com/openmcp-project/control-plane-operator

charts/control-plane-operator/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ spec:
125125
{{ if .Values.syncPeriod }}
126126
- "--sync-period={{ .Values.syncPeriod }}"
127127
{{ end }}
128+
{{ if .Values.fluxTokenLifetime }}
129+
- "--flux-token-lifetime={{ .Values.fluxTokenLifetime }}"
130+
{{ end }}
128131
ports:
129132
{{- if .Values.webhooks.listen }}
130133
- name: webhooks-https

charts/control-plane-operator/values.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ image:
88
repository: ghcr.io/openmcp-project/images/control-plane-operator
99
pullPolicy: IfNotPresent
1010
# Overrides the image tag whose default is the chart appVersion.
11-
tag: v0.1.14
11+
tag: v0.1.15
1212

1313
imagePullSecrets: []
1414
nameOverride: ""
1515
fullnameOverride: ""
1616
syncPeriod: 1m
17+
fluxTokenLifetime: 1h
1718

1819
serviceAccount:
1920
# Specifies whether a service account should be created

charts/control-plane-operator/values.yaml.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ imagePullSecrets: []
1414
nameOverride: ""
1515
fullnameOverride: ""
1616
syncPeriod: 1m
17+
fluxTokenLifetime: 1h
1718

1819
serviceAccount:
1920
# Specifies whether a service account should be created

cmd/control-plane-operator/main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ func main() {
109109

110110
var syncPeriod string
111111
flag.StringVar(&syncPeriod, "sync-period", "1m", "The period at which the controller will sync the resources.")
112+
var fluxTokenLifetimeStr string
113+
flag.StringVar(&fluxTokenLifetimeStr, "flux-token-lifetime", "1h", "The desired lifetime of the flux service account token used to access the MCP.")
112114

113115
// component flags
114116
var webhookMiddlewareName string
@@ -178,11 +180,18 @@ func main() {
178180
}
179181
setupLog.Info("sync period set to", "syncPeriod", reconcilePeriod)
180182

183+
fluxTokenLifetime, errFluxTokenLifetime := time.ParseDuration(fluxTokenLifetimeStr)
184+
if errFluxTokenLifetime != nil {
185+
fluxTokenLifetime = 1 * time.Hour
186+
}
187+
setupLog.Info("flux token lifetime set to", "fluxTokenLifetime", fluxTokenLifetime)
188+
181189
if err = (&controller.ControlPlaneReconciler{
182190
Client: mgr.GetClient(),
183191
Scheme: mgr.GetScheme(),
184192
Kubeconfiggen: &kubeconfiggen.Default{},
185193
FluxSecretResolver: fluxSecretResolver,
194+
FluxTokenLifetime: fluxTokenLifetime,
186195
WebhookMiddleware: types.NamespacedName{
187196
Namespace: webhookMiddlewareNamespace,
188197
Name: webhookMiddlewareName,
@@ -196,8 +205,9 @@ func main() {
196205
os.Exit(1)
197206
}
198207
if err = (&controller.SecretReconciler{
199-
Client: mgr.GetClient(),
200-
Scheme: mgr.GetScheme(),
208+
Client: mgr.GetClient(),
209+
Scheme: mgr.GetScheme(),
210+
ReconcilePeriod: reconcilePeriod,
201211
}).SetupWithManager(mgr); err != nil {
202212
setupLog.Error(err, "unable to create controller", "controller", "Secret")
203213
os.Exit(1)

internal/controller/controlplane_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ import (
5959
)
6060

6161
const (
62-
requeueAfter = 1 * time.Minute
63-
requeueAfterError = 5 * time.Second
62+
requeueAfterError = 10 * time.Second
6463

6564
cpNamespacePrefix = "cp-"
6665
cpNamespaceMaxLen = 63
@@ -92,6 +91,7 @@ type ControlPlaneReconciler struct {
9291
FluxSecretResolver secretresolver.SecretResolver
9392
WebhookMiddleware types.NamespacedName
9493
ReconcilePeriod time.Duration
94+
FluxTokenLifetime time.Duration
9595
RemoteConfigBuilder RemoteConfigBuilder
9696
EmbeddedCRDs embed.FS
9797
}

internal/controller/controlplane_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func TestControlPlaneReconciler_Reconcile(t *testing.T) {
288288
assert.Equal(t, "Uninstalled", cond.Reason)
289289
return nil
290290
},
291-
expectedResult: ctrl.Result{RequeueAfter: 5 * time.Second},
291+
expectedResult: ctrl.Result{RequeueAfter: requeueAfterError},
292292
expectedErr: nil,
293293
},
294294
{
@@ -352,6 +352,7 @@ func TestControlPlaneReconciler_Reconcile(t *testing.T) {
352352
Scheme: c.Scheme(),
353353
Kubeconfiggen: &kubeconfiggen.Default{},
354354
FluxSecretResolver: testSecretResolver,
355+
FluxTokenLifetime: 1 * time.Hour,
355356
WebhookMiddleware: types.NamespacedName{},
356357
ReconcilePeriod: time.Second * 30,
357358
Recorder: record.NewFakeRecorder(100),

internal/controller/kubeconfigs.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package controller
22

33
import (
44
"context"
5-
"errors"
65
"time"
76

87
corev1 "k8s.io/api/core/v1"
@@ -19,20 +18,9 @@ import (
1918
const (
2019
keyKubeconfig = "kubeconfig"
2120
keyExpiration = "expiresAt"
22-
23-
kubeconfigExpiration = 10 * time.Minute
24-
kubeconfigBuffer = 3 * requeueAfter
25-
)
26-
27-
var (
28-
errInvalidExpirationOrBuffer = errors.New("desired expiration and buffer are incompatible. make sure that desired expiration is greater than the buffer")
2921
)
3022

3123
func (r *ControlPlaneReconciler) ensureKubeconfig(ctx context.Context, remoteCfg *rest.Config, namespace string, secretName string, svcaccountRef corev1beta1.ServiceAccountReference) (*corev1.SecretReference, error) {
32-
if kubeconfigBuffer >= kubeconfigExpiration {
33-
return nil, errInvalidExpirationOrBuffer
34-
}
35-
3624
secret := &corev1.Secret{
3725
ObjectMeta: metav1.ObjectMeta{
3826
Name: secretName,
@@ -53,13 +41,14 @@ func (r *ControlPlaneReconciler) ensureKubeconfig(ctx context.Context, remoteCfg
5341
return nil, err
5442
}
5543

56-
if time.Now().Before(expiration.Add(-kubeconfigBuffer)) {
44+
// check if token would expire before next planned reconciliation
45+
if time.Now().Before(expiration.Add(-r.ReconcilePeriod)) {
5746
// kubeconfig is still valid
5847
return &corev1.SecretReference{Name: secret.Name, Namespace: secret.Namespace}, nil
5948
}
6049
}
6150

62-
kubeconfig, expiration, err := r.Kubeconfiggen.ForServiceAccount(ctx, remoteCfg, svcaccountRef, kubeconfigExpiration)
51+
kubeconfig, expiration, err := r.Kubeconfiggen.ForServiceAccount(ctx, remoteCfg, svcaccountRef, r.FluxTokenLifetime)
6352
if err != nil {
6453
return nil, err
6554
}

internal/controller/secret_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"strings"
7+
"time"
78

89
"github.com/openmcp-project/control-plane-operator/pkg/constants"
910

@@ -33,7 +34,8 @@ var (
3334
// SecretReconciler reconciles a Secret object
3435
type SecretReconciler struct {
3536
client.Client
36-
Scheme *runtime.Scheme
37+
Scheme *runtime.Scheme
38+
ReconcilePeriod time.Duration
3739
}
3840

3941
// Reconcile is part of the main kubernetes reconciliation loop which aims to
@@ -136,7 +138,7 @@ func (r *SecretReconciler) handleSync(ctx context.Context, secret *corev1.Secret
136138
}
137139
}
138140

139-
return ctrl.Result{RequeueAfter: requeueAfter}, nil
141+
return ctrl.Result{RequeueAfter: r.ReconcilePeriod}, nil
140142
}
141143

142144
func (r *SecretReconciler) shouldReconcile(o client.Object) bool {

0 commit comments

Comments
 (0)