Skip to content

Commit c038dec

Browse files
aniruddha2000janiskemper
authored andcommitted
✨ Add manual upgrade if no k8s version is changed (#33)
Add manual upgrade if no k8s version is changed Signed-off-by: Aniruddha Basak <[email protected]>
1 parent b50680f commit c038dec

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

api/v1alpha1/clusteraddon_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ type ClusterAddonStatus struct {
6868
// +optional
6969
CurrentHook string `json:"currentHook,omitempty"`
7070

71+
// KubernetesVersion is the kubernetes version of the current cluster stack release.
72+
// +optional
73+
KubernetesVersion string `json:"kubernetesVersion,omitempty"`
74+
7175
// HelmChartStatus defines the status of helm chart in the cluster addon.
7276
// +optional
7377
HelmChartStatus map[string]HelmChartStatusConditions `json:"helmChartStatus,omitempty"`

config/crd/bases/clusterstack.x-k8s.io_clusteraddons.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ spec:
179179
description: HelmChartStatus defines the status of helm chart in the
180180
cluster addon.
181181
type: object
182+
kubernetesVersion:
183+
description: KubernetesVersion is the kubernetes version of the current
184+
cluster stack release.
185+
type: string
182186
ready:
183187
default: false
184188
type: boolean

internal/controller/clusteraddon_controller.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
278278
clusterAddon.Status.Ready = false
279279
}
280280

281+
// In case the Kubernetes version stays the same, the hook server does not trigger.
282+
// Therefore, we have to check whether the ClusterStack is upgraded and if that is the case, the ClusterAddons have to be upgraded as well.
283+
if clusterAddon.Spec.ClusterStack != cluster.Spec.Topology.Class && clusterAddon.Status.KubernetesVersion == releaseAsset.Meta.Versions.Kubernetes {
284+
if clusterAddon.Spec.Version != releaseAsset.Meta.Versions.Components.ClusterAddon {
285+
clusterAddon.Status.HelmChartStatus = make(map[string]csov1alpha1.HelmChartStatusConditions)
286+
clusterAddon.Status.CurrentHook = clusterAddon.Spec.Hook
287+
clusterAddon.Status.Ready = false
288+
conditions.Delete(clusterAddon, csov1alpha1.HelmChartAppliedCondition)
289+
} else {
290+
// If the cluster addon version don't change we don't want to apply helm charts again.
291+
clusterAddon.Spec.ClusterStack = cluster.Spec.Topology.Class
292+
clusterAddon.Status.Ready = true
293+
}
294+
}
295+
296+
clusterAddon.Spec.ClusterStack = cluster.Spec.Topology.Class
297+
clusterAddon.Spec.Version = releaseAsset.Meta.Versions.Components.ClusterAddon
298+
281299
if clusterAddon.Status.Ready {
282300
return reconcile.Result{}, nil
283301
}
@@ -293,8 +311,38 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
293311
return reconcile.Result{}, fmt.Errorf("failed to parse clusteraddon.yaml config: %w", err)
294312
}
295313

296-
for _, stage := range clusterAddonConfig.AddonStages[in.clusterAddon.Spec.Hook] {
297-
shouldRequeue, err := executeStage(ctx, stage, in)
314+
// In case the Kubernetes version stayed the same during an upgrade, the hook server does not trigger and
315+
// we just take the Helm charts that are supposed to be installed in the BeforeClusterUpgrade hook and apply them.
316+
if clusterAddon.Status.KubernetesVersion == releaseAsset.Meta.Versions.Kubernetes {
317+
clusterAddon.Spec.Hook = "BeforeClusterUpgrade"
318+
for _, stage := range clusterAddonConfig.AddonStages["BeforeClusterUpgrade"] {
319+
shouldRequeue, err := r.executeStage(ctx, stage, in)
320+
if err != nil {
321+
return reconcile.Result{}, fmt.Errorf("failed to execute stage: %w", err)
322+
}
323+
if shouldRequeue {
324+
return reconcile.Result{RequeueAfter: 20 * time.Second}, nil
325+
}
326+
}
327+
328+
// Helm chart has been applied successfully
329+
// clusterAddon.Spec.Version = metadata.Versions.Components.ClusterAddon
330+
conditions.MarkTrue(clusterAddon, csov1alpha1.HelmChartAppliedCondition)
331+
332+
// store the release kubernetes version and current hook
333+
clusterAddon.Status.Ready = true
334+
335+
return ctrl.Result{}, nil
336+
}
337+
338+
// If hook is empty we can don't want to proceed executing staged according to current hook
339+
// hence we can return.
340+
if clusterAddon.Spec.Hook == "" {
341+
return reconcile.Result{}, nil
342+
}
343+
344+
for _, stage := range clusterAddonConfig.AddonStages[clusterAddon.Spec.Hook] {
345+
shouldRequeue, err := r.executeStage(ctx, stage, in)
298346
if err != nil {
299347
return reconcile.Result{}, fmt.Errorf("failed to execute stage: %w", err)
300348
}
@@ -414,7 +462,7 @@ func (r *ClusterAddonReconciler) templateAndApplyClusterAddonHelmChart(ctx conte
414462
return shouldRequeue, nil
415463
}
416464

417-
func executeStage(ctx context.Context, stage clusteraddon.Stage, in templateAndApplyClusterAddonInput) (bool, error) {
465+
func (r *ClusterAddonReconciler) executeStage(ctx context.Context, stage clusteraddon.Stage, in templateAndApplyClusterAddonInput) (bool, error) {
418466
var (
419467
newResources []*csov1alpha1.Resource
420468
shouldRequeue bool
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
metrics-server:
2+
commonLabels:
3+
domain: "{{ .Cluster.spec.controlPlaneEndpoint.host }}"
4+
clusterAddonVersion: "v2"

0 commit comments

Comments
 (0)