This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
create.go
50 lines (40 loc) · 1.54 KB
/
create.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
package azuremachinemetadata
import (
"context"
"github.com/giantswarm/apiextensions/v6/pkg/annotation"
"github.com/giantswarm/microerror"
"k8s.io/apimachinery/pkg/api/errors"
"github.com/giantswarm/azure-operator/v8/pkg/helpers"
"github.com/giantswarm/azure-operator/v8/service/controller/key"
)
func (r *Resource) EnsureCreated(ctx context.Context, cr interface{}) error {
var err error
azureMachine, err := key.ToAzureMachine(cr)
if err != nil {
return microerror.Mask(err)
}
changed := false
_, annotationWasSet := azureMachine.Annotations[annotation.LastDeployedReleaseVersion]
// We need this for existing clusters, and we do it both in Cluster
// controller (clusterupgrade) and in AzureMachine controller, because we
// do not know which controller/handler will be executed first, and we need
// annotation set correctly in both places.
err = helpers.InitAzureMachineAnnotations(ctx, r.ctrlClient, r.logger, &azureMachine)
if err != nil {
return microerror.Mask(err)
}
_, annotationIsSet := azureMachine.Annotations[annotation.LastDeployedReleaseVersion]
changed = !annotationWasSet && annotationIsSet
if changed {
err = r.ctrlClient.Update(ctx, &azureMachine)
if errors.IsConflict(err) {
r.logger.Debugf(ctx, "conflict trying to save object in k8s API concurrently")
r.logger.Debugf(ctx, "cancelling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
}
r.logger.Debugf(ctx, "ensured that azuremachine has release.giantswarm.io/last-deployed-version annotation initialized")
return nil
}