This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
azuremachine.go
64 lines (53 loc) · 2.47 KB
/
azuremachine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package helpers
import (
"context"
"github.com/giantswarm/apiextensions/v6/pkg/annotation"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
capz "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/azure-operator/v8/service/controller/key"
)
func InitAzureMachineAnnotations(ctx context.Context, ctrlClient client.Client, logger micrologger.Logger, azureMachine *capz.AzureMachine) error {
// We want to initialize release.giantswarm.io/last-deployed-version annotation
// and set it to the whatever is the latest deployed release version.
// This will ensure that AzureMachine CRs for existing clusters get Creating
// and Upgrading conditions correctly initialized.
if azureMachine.Annotations == nil {
azureMachine.Annotations = map[string]string{}
}
_, ok := azureMachine.Annotations[annotation.LastDeployedReleaseVersion]
if ok {
return nil
}
logger.Debugf(ctx, "ensuring that azuremachine has release.giantswarm.io/last-deployed-version annotation initialized")
// Initialize annotation only for the CRs that do not have it set already.
// This will ensure that AzureMachine Creating and Upgrading conditions are
// set properly for the first time.
clusterName := key.ClusterName(azureMachine)
cluster, err := util.GetClusterByName(ctx, ctrlClient, azureMachine.Namespace, clusterName)
if err != nil {
return microerror.Mask(err)
}
clusterLastDeployedReleaseVersion, ok := cluster.Annotations[annotation.LastDeployedReleaseVersion]
if !ok {
// Cluster CR does not have release.giantswarm.io/last-deployed-version
// annotation set, which means that the Cluster is probably being
// created, so we don't have to initialize that same annotation for
// AzureMachine CR, as it will be updated by Cluster controller
// clusterupgrade handler.
return nil
}
// Since this will be executed only once (during the first upgrade to the
// release that includes this change), we will ready the annotation value
// from the Cluster CR, as that will be the latest deployed release at that
// moment.
// Few more details:
// - Cluster release label will store newer desired release, so we
// cannot use that.
// - Also ,we cannot know if AzureMachine release label was already updated
// or not, as that is done in Cluster controller.
azureMachine.Annotations[annotation.LastDeployedReleaseVersion] = clusterLastDeployedReleaseVersion
return nil
}