This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
/
create.go
61 lines (51 loc) · 1.93 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
51
52
53
54
55
56
57
58
59
60
61
package azureclusterconfig
import (
"context"
"fmt"
corev1alpha1 "github.com/giantswarm/apiextensions/v6/pkg/apis/core/v1alpha1"
"github.com/giantswarm/microerror"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/giantswarm/azure-operator/v8/pkg/label"
"github.com/giantswarm/azure-operator/v8/service/controller/key"
)
func (r *Resource) EnsureCreated(ctx context.Context, obj interface{}) error {
azureCluster, err := key.ToAzureCluster(obj)
if err != nil {
return microerror.Mask(err)
}
var azureClusterConfig corev1alpha1.AzureClusterConfig
{
nsName := types.NamespacedName{
Name: clusterConfigName(key.ClusterName(&azureCluster)),
Namespace: metav1.NamespaceDefault,
}
err = r.ctrlClient.Get(ctx, nsName, &azureClusterConfig)
if errors.IsNotFound(err) {
r.logger.Debugf(ctx, "azureclusterconfig did not exist, nothing to do")
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
}
r.logger.Debugf(ctx, "ensuring azureclusterconfig's %q label is up to date", label.ClusterOperatorVersion)
{
currentClusterOperatorVersion := azureClusterConfig.GetLabels()[label.ClusterOperatorVersion]
desiredClusterOperatorVersion := azureCluster.GetLabels()[label.ClusterOperatorVersion]
if currentClusterOperatorVersion != desiredClusterOperatorVersion {
r.logger.Debugf(ctx, "updating %q label on azureclusterconfig", label.ClusterOperatorVersion)
azureClusterConfig.Labels[label.ClusterOperatorVersion] = desiredClusterOperatorVersion
err = r.ctrlClient.Update(ctx, &azureClusterConfig)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "updated %q label on azureclusterconfig", label.ClusterOperatorVersion)
}
}
return nil
}
func clusterConfigName(clusterID string) string {
return fmt.Sprintf("%s-%s", clusterID, "azure-cluster-config")
}