Skip to content

Commit d1f39be

Browse files
author
Yuvaraj Kakaraparthi
committed
address review comments
1 parent 1bd272b commit d1f39be

12 files changed

+239
-232
lines changed

api/v1beta1/machine_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const (
5252
PreTerminateDeleteHookAnnotationPrefix = "pre-terminate.delete.hook.machine.cluster.x-k8s.io"
5353

5454
// MachineCertificatesExpiryDateAnnotation annotation specifies the expiry date of the machine certificates.
55+
// This annotation can be used my MachineHealthCheck to trigger machine rotation before certificates expire.
56+
// This annotation can be set on KubeadmConfig or Machine objects. The value set on the Machine object takes precedence.
5557
MachineCertificatesExpiryDateAnnotation = "machine.cluster.x-k8s.io/certificates-expiry-date"
5658
)
5759

api/v1beta1/machinehealthcheck_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type MachineHealthCheckSpec struct {
7979

8080
// ANCHOR: Certificates
8181

82+
// Certificates contains the information needed to validate the health of machine certificates.
8283
type Certificates struct {
8384
// ExpiresWithinDays is the number of days after which certificates is considered to be expired.
8485
// +optional

api/v1beta1/zz_generated.openapi.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ func (r *KubeadmConfigReconciler) handleClusterNotInitialized(ctx context.Contex
443443
}
444444

445445
// Update the certificate expiration time in the config.
446-
r.reconcileCertificateExpiryTime(ctx, scope)
446+
r.addCertificateExpiryAnnotation(scope.Config)
447447

448448
conditions.MarkTrue(scope.Config, bootstrapv1.CertificatesAvailableCondition)
449449

@@ -626,7 +626,7 @@ func (r *KubeadmConfigReconciler) joinControlplane(ctx context.Context, scope *S
626626
}
627627

628628
// Update the certificate expiration time in the config.
629-
r.reconcileCertificateExpiryTime(ctx, scope)
629+
r.addCertificateExpiryAnnotation(scope.Config)
630630

631631
conditions.MarkTrue(scope.Config, bootstrapv1.CertificatesAvailableCondition)
632632

@@ -1023,16 +1023,16 @@ func (r *KubeadmConfigReconciler) storeBootstrapData(ctx context.Context, scope
10231023
return nil
10241024
}
10251025

1026-
// reconcileCertificateExpiryTime reconciles the certificate expiry time on Kubeadmconfig.
1026+
// addCertificateExpiryAnnotation reconciles the certificate expiry time on Kubeadmconfig.
10271027
// Sets the certificate expiration time as on annotation on the config.
10281028
// If the annotation is already present, there is nothing to do.
1029-
func (r *KubeadmConfigReconciler) reconcileCertificateExpiryTime(_ context.Context, scope *Scope) {
1030-
annotations := scope.Config.GetAnnotations()
1029+
func (r *KubeadmConfigReconciler) addCertificateExpiryAnnotation(config *bootstrapv1.KubeadmConfig) {
1030+
annotations := config.GetAnnotations()
10311031
if annotations == nil {
10321032
annotations = map[string]string{}
10331033
}
10341034
if _, ok := annotations[clusterv1.MachineCertificatesExpiryDateAnnotation]; !ok {
10351035
annotations[clusterv1.MachineCertificatesExpiryDateAnnotation] = now().Add(defaultCertificateExpiryDuration).Format(time.RFC3339)
1036-
scope.Config.SetAnnotations(annotations)
1036+
config.SetAnnotations(annotations)
10371037
}
10381038
}

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ func TestKubeadmConfigReconciler_ReconcileCertificateExpiryTime(t *testing.T) {
21042104
t.Run(tt.name, func(t *testing.T) {
21052105
g := NewWithT(t)
21062106
k := &KubeadmConfigReconciler{}
2107-
k.reconcileCertificateExpiryTime(ctx, &Scope{Config: tt.cfg})
2107+
k.addCertificateExpiryAnnotation(tt.cfg)
21082108
annotations := tt.cfg.GetAnnotations()
21092109
g.Expect(annotations[clusterv1.MachineCertificatesExpiryDateAnnotation]).To(Equal(tt.wantTime))
21102110
})

bootstrap/kubeadm/types/upstreamv1beta1/zz_generated.deepcopy.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bootstrap/kubeadm/types/upstreamv1beta2/zz_generated.deepcopy.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bootstrap/kubeadm/types/upstreamv1beta3/zz_generated.deepcopy.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controllers/machine/machine_controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
267267
r.reconcileInfrastructure,
268268
r.reconcileNode,
269269
r.reconcileInterruptibleNodeLabel,
270+
r.reconcileCertificateExpiry,
270271
}
271272

272273
res := ctrl.Result{}

internal/controllers/machine/machine_controller_phases.go

+43-36
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (r *Reconciler) reconcileBootstrap(ctx context.Context, cluster *clusterv1.
175175
log := ctrl.LoggerFrom(ctx, "cluster", cluster.Name)
176176

177177
// If the bootstrap data is populated, set ready and return.
178-
if m.Spec.Bootstrap.DataSecretName != nil && m.Spec.Bootstrap.ConfigRef == nil {
178+
if m.Spec.Bootstrap.DataSecretName != nil {
179179
m.Status.BootstrapReady = true
180180
conditions.MarkTrue(m, clusterv1.BootstrapReadyCondition)
181181
return ctrl.Result{}, nil
@@ -222,37 +222,6 @@ func (r *Reconciler) reconcileBootstrap(ctx context.Context, cluster *clusterv1.
222222
return ctrl.Result{RequeueAfter: externalReadyWait}, nil
223223
}
224224

225-
// If the machine is a control plane machine, check for certificate expiry information in the following places:
226-
// - As an annotation on the Machine
227-
// - As an annotation on the bootstrap config
228-
// If the certificate expiry information is found, set the Machine's status accordingly.
229-
// Note: If both values are defined the value in the machine annotation should take precedence.
230-
if util.IsControlPlaneMachine(m) {
231-
var annotations map[string]string
232-
233-
// Check for certificate expiry information in the bootstrap config.
234-
annotations = bootstrapConfig.GetAnnotations()
235-
if expiry, ok := annotations[clusterv1.MachineCertificatesExpiryDateAnnotation]; ok {
236-
expiryTime, err := time.Parse(time.RFC3339, expiry)
237-
if err != nil {
238-
return ctrl.Result{}, errors.Wrap(err, "failed to parse expiry date")
239-
}
240-
expTime := metav1.NewTime(expiryTime)
241-
m.Status.CertificatesExpiryDate = &expTime
242-
}
243-
244-
// Check for certificate expiry information in the machine annotation.
245-
annotations = m.GetAnnotations()
246-
if expiry, ok := annotations[clusterv1.MachineCertificatesExpiryDateAnnotation]; ok {
247-
expiryTime, err := time.Parse(time.RFC3339, expiry)
248-
if err != nil {
249-
return ctrl.Result{}, errors.Wrap(err, "failed to parse expiry date")
250-
}
251-
expTime := metav1.NewTime(expiryTime)
252-
m.Status.CertificatesExpiryDate = &expTime
253-
}
254-
}
255-
256225
// Get and set the name of the secret containing the bootstrap data.
257226
secretName, _, err := unstructured.NestedString(bootstrapConfig.Object, "status", "dataSecretName")
258227
if err != nil {
@@ -261,11 +230,8 @@ func (r *Reconciler) reconcileBootstrap(ctx context.Context, cluster *clusterv1.
261230
return ctrl.Result{}, errors.Errorf("retrieved empty dataSecretName from bootstrap provider for Machine %q in namespace %q", m.Name, m.Namespace)
262231
}
263232

264-
if m.Spec.Bootstrap.DataSecretName == nil {
265-
m.Spec.Bootstrap.DataSecretName = pointer.StringPtr(secretName)
266-
}
233+
m.Spec.Bootstrap.DataSecretName = pointer.StringPtr(secretName)
267234
m.Status.BootstrapReady = true
268-
conditions.MarkTrue(m, clusterv1.BootstrapReadyCondition)
269235
return ctrl.Result{}, nil
270236
}
271237

@@ -346,3 +312,44 @@ func (r *Reconciler) reconcileInfrastructure(ctx context.Context, cluster *clust
346312
m.Spec.ProviderID = pointer.StringPtr(providerID)
347313
return ctrl.Result{}, nil
348314
}
315+
316+
func (r *Reconciler) reconcileCertificateExpiry(ctx context.Context, _ *clusterv1.Cluster, m *clusterv1.Machine) (ctrl.Result, error) {
317+
var annotations map[string]string
318+
319+
if !util.IsControlPlaneMachine(m) {
320+
// If the machine is not a control plane machine, return early.
321+
return ctrl.Result{}, nil
322+
}
323+
324+
if m.Spec.Bootstrap.ConfigRef != nil {
325+
bootstrapConfig, err := external.Get(ctx, r.Client, m.Spec.Bootstrap.ConfigRef, m.Namespace)
326+
if err != nil {
327+
return ctrl.Result{}, err
328+
}
329+
330+
// Check for certificate expiry information in the bootstrap config.
331+
annotations = bootstrapConfig.GetAnnotations()
332+
if expiry, ok := annotations[clusterv1.MachineCertificatesExpiryDateAnnotation]; ok {
333+
expiryTime, err := time.Parse(time.RFC3339, expiry)
334+
if err != nil {
335+
return ctrl.Result{}, errors.Wrap(err, "failed to parse expiry date")
336+
}
337+
expTime := metav1.NewTime(expiryTime)
338+
m.Status.CertificatesExpiryDate = &expTime
339+
}
340+
}
341+
342+
// Check for certificate expiry information in the machine annotation.
343+
// This should take precedence over other information.
344+
annotations = m.GetAnnotations()
345+
if expiry, ok := annotations[clusterv1.MachineCertificatesExpiryDateAnnotation]; ok {
346+
expiryTime, err := time.Parse(time.RFC3339, expiry)
347+
if err != nil {
348+
return ctrl.Result{}, errors.Wrap(err, "failed to parse expiry date")
349+
}
350+
expTime := metav1.NewTime(expiryTime)
351+
m.Status.CertificatesExpiryDate = &expTime
352+
}
353+
354+
return ctrl.Result{}, nil
355+
}

0 commit comments

Comments
 (0)