From 686cc08dcc244b9cb931af9a28f410daa03a0c91 Mon Sep 17 00:00:00 2001 From: Martin Gencur Date: Mon, 15 Jun 2026 09:02:16 +0200 Subject: [PATCH] test(controlplane-component): add RestartDateAnnotation propagation tests Verify that setDefaultOptions propagates RestartDateAnnotation from HCP to the pod template when set, and omits it when absent. This covers the mechanism that triggers rolling restarts via pod template annotation changes. Rolling out the new ReplicaSet is then Kubernetes' concern. The annotation propagation from HostedCluster to HostedControlPlane is already tested in TestReconcileHostedControlPlaneAnnotations. Co-Authored-By: Claude Opus 4.6 --- .../controlplane-component/defaults_test.go | 70 ++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/support/controlplane-component/defaults_test.go b/support/controlplane-component/defaults_test.go index e02e29e730f1..4be8663b1b31 100644 --- a/support/controlplane-component/defaults_test.go +++ b/support/controlplane-component/defaults_test.go @@ -523,9 +523,15 @@ func TestApplyNonOvercommitableResourceLimits(t *testing.T) { func TestSetDefaultOptions(t *testing.T) { scheme := runtime.NewScheme() - _ = hyperv1.AddToScheme(scheme) - _ = corev1.AddToScheme(scheme) - _ = appsv1.AddToScheme(scheme) + if err := hyperv1.AddToScheme(scheme); err != nil { + t.Fatalf("failed to add hyperv1 to scheme: %v", err) + } + if err := corev1.AddToScheme(scheme); err != nil { + t.Fatalf("failed to add corev1 to scheme: %v", err) + } + if err := appsv1.AddToScheme(scheme); err != nil { + t.Fatalf("failed to add appsv1 to scheme: %v", err) + } t.Run("When SetDefaultSecurityContext is true it should set RunAsUser and FSGroup", func(t *testing.T) { t.Parallel() @@ -649,4 +655,62 @@ func TestSetDefaultOptions(t *testing.T) { g.Expect(deployment.Spec.Template.Spec.Containers[0].Resources).To(Equal(test.expectedResources)) }) } + + annotationTests := []struct { + name string + hcpAnnotations map[string]string + expectSet bool + }{ + { + name: "When HCP has RestartDateAnnotation it should propagate it to the pod template", + hcpAnnotations: map[string]string{ + hyperv1.RestartDateAnnotation: "2024-01-15T12:00:00Z", + }, + expectSet: true, + }, + { + name: "When HCP has no RestartDateAnnotation it should not set it on the pod template", + expectSet: false, + }, + } + + for _, test := range annotationTests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + g := NewGomegaWithT(t) + + workload := &controlPlaneWorkload[*appsv1.Deployment]{ + name: "kube-apiserver", + workloadProvider: &deploymentProvider{}, + ComponentOptions: &testComponent{}, + } + deployment := &appsv1.Deployment{ + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Name: "kube-apiserver", Image: "hyperkube"}, + }, + }, + }, + }, + } + hcp := &hyperv1.HostedControlPlane{} + hcp.Annotations = test.hcpAnnotations + + err := workload.setDefaultOptions(ControlPlaneContext{ + HCP: hcp, + Client: fake.NewClientBuilder().WithScheme(scheme).Build(), + ReleaseImageProvider: releaseProvider, + }, deployment, nil) + g.Expect(err).NotTo(HaveOccurred()) + + if test.expectSet { + g.Expect(deployment.Spec.Template.Annotations).To(HaveKeyWithValue( + hyperv1.RestartDateAnnotation, test.hcpAnnotations[hyperv1.RestartDateAnnotation])) + } else { + g.Expect(deployment.Spec.Template.Annotations).NotTo(HaveKey(hyperv1.RestartDateAnnotation)) + } + }) + } }