|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mutators |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + |
| 24 | + asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001" |
| 25 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 26 | + infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1" |
| 27 | + "sigs.k8s.io/cluster-api-provider-azure/util/tele" |
| 28 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + // ErrNoManagedClusterDefined describes an AzureASOManagedControlPlane without a ManagedCluster. |
| 34 | + ErrNoManagedClusterDefined = fmt.Errorf("no %s ManagedCluster defined in AzureASOManagedControlPlane spec.resources", asocontainerservicev1.GroupVersion.Group) |
| 35 | +) |
| 36 | + |
| 37 | +// SetManagedClusterDefaults propagates values defined by Cluster API to an ASO ManagedCluster. |
| 38 | +func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, cluster *clusterv1.Cluster) ResourcesMutator { |
| 39 | + return func(ctx context.Context, us []*unstructured.Unstructured) error { |
| 40 | + ctx, _, done := tele.StartSpanWithLogger(ctx, "mutators.SetManagedClusterDefaults") |
| 41 | + defer done() |
| 42 | + |
| 43 | + var managedCluster *unstructured.Unstructured |
| 44 | + var managedClusterPath string |
| 45 | + for i, u := range us { |
| 46 | + if u.GroupVersionKind().Group == asocontainerservicev1.GroupVersion.Group && |
| 47 | + u.GroupVersionKind().Kind == "ManagedCluster" { |
| 48 | + managedCluster = u |
| 49 | + managedClusterPath = fmt.Sprintf("spec.resources[%d]", i) |
| 50 | + break |
| 51 | + } |
| 52 | + } |
| 53 | + if managedCluster == nil { |
| 54 | + return reconcile.TerminalError(ErrNoManagedClusterDefined) |
| 55 | + } |
| 56 | + |
| 57 | + if err := setManagedClusterKubernetesVersion(ctx, asoManagedControlPlane, managedClusterPath, managedCluster); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + if err := setManagedClusterServiceCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + if err := setManagedClusterPodCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func setManagedClusterKubernetesVersion(ctx context.Context, asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, managedClusterPath string, managedCluster *unstructured.Unstructured) error { |
| 74 | + _, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterKubernetesVersion") |
| 75 | + defer done() |
| 76 | + |
| 77 | + capzK8sVersion := strings.TrimPrefix(asoManagedControlPlane.Spec.Version, "v") |
| 78 | + if capzK8sVersion == "" { |
| 79 | + // When the CAPI contract field isn't set, any value for version in the embedded ASO resource may be specified. |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + k8sVersionPath := []string{"spec", "kubernetesVersion"} |
| 84 | + userK8sVersion, k8sVersionFound, err := unstructured.NestedString(managedCluster.UnstructuredContent(), k8sVersionPath...) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + setK8sVersion := mutation{ |
| 89 | + location: managedClusterPath + "." + strings.Join(k8sVersionPath, "."), |
| 90 | + val: capzK8sVersion, |
| 91 | + reason: "because spec.version is set to " + asoManagedControlPlane.Spec.Version, |
| 92 | + } |
| 93 | + if k8sVersionFound && userK8sVersion != capzK8sVersion { |
| 94 | + return Incompatible{ |
| 95 | + mutation: setK8sVersion, |
| 96 | + userVal: userK8sVersion, |
| 97 | + } |
| 98 | + } |
| 99 | + logMutation(log, setK8sVersion) |
| 100 | + return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capzK8sVersion, k8sVersionPath...) |
| 101 | +} |
| 102 | + |
| 103 | +func setManagedClusterServiceCIDR(ctx context.Context, cluster *clusterv1.Cluster, managedClusterPath string, managedCluster *unstructured.Unstructured) error { |
| 104 | + _, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterServiceCIDR") |
| 105 | + defer done() |
| 106 | + |
| 107 | + if cluster.Spec.ClusterNetwork == nil || |
| 108 | + cluster.Spec.ClusterNetwork.Services == nil || |
| 109 | + len(cluster.Spec.ClusterNetwork.Services.CIDRBlocks) == 0 { |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + capiCIDR := cluster.Spec.ClusterNetwork.Services.CIDRBlocks[0] |
| 114 | + |
| 115 | + // ManagedCluster.v1api20210501.containerservice.azure.com does not contain the plural serviceCidrs field. |
| 116 | + svcCIDRPath := []string{"spec", "networkProfile", "serviceCidr"} |
| 117 | + userSvcCIDR, found, err := unstructured.NestedString(managedCluster.UnstructuredContent(), svcCIDRPath...) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + setSvcCIDR := mutation{ |
| 122 | + location: managedClusterPath + "." + strings.Join(svcCIDRPath, "."), |
| 123 | + val: capiCIDR, |
| 124 | + reason: fmt.Sprintf("because spec.clusterNetwork.services.cidrBlocks[0] in Cluster %s/%s is set to %s", cluster.Namespace, cluster.Name, capiCIDR), |
| 125 | + } |
| 126 | + if found && userSvcCIDR != capiCIDR { |
| 127 | + return Incompatible{ |
| 128 | + mutation: setSvcCIDR, |
| 129 | + userVal: userSvcCIDR, |
| 130 | + } |
| 131 | + } |
| 132 | + logMutation(log, setSvcCIDR) |
| 133 | + return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capiCIDR, svcCIDRPath...) |
| 134 | +} |
| 135 | + |
| 136 | +func setManagedClusterPodCIDR(ctx context.Context, cluster *clusterv1.Cluster, managedClusterPath string, managedCluster *unstructured.Unstructured) error { |
| 137 | + _, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterPodCIDR") |
| 138 | + defer done() |
| 139 | + |
| 140 | + if cluster.Spec.ClusterNetwork == nil || |
| 141 | + cluster.Spec.ClusterNetwork.Pods == nil || |
| 142 | + len(cluster.Spec.ClusterNetwork.Pods.CIDRBlocks) == 0 { |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + capiCIDR := cluster.Spec.ClusterNetwork.Pods.CIDRBlocks[0] |
| 147 | + |
| 148 | + // ManagedCluster.v1api20210501.containerservice.azure.com does not contain the plural podCidrs field. |
| 149 | + podCIDRPath := []string{"spec", "networkProfile", "podCidr"} |
| 150 | + userPodCIDR, found, err := unstructured.NestedString(managedCluster.UnstructuredContent(), podCIDRPath...) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + setPodCIDR := mutation{ |
| 155 | + location: managedClusterPath + "." + strings.Join(podCIDRPath, "."), |
| 156 | + val: capiCIDR, |
| 157 | + reason: fmt.Sprintf("because spec.clusterNetwork.pods.cidrBlocks[0] in Cluster %s/%s is set to %s", cluster.Namespace, cluster.Name, capiCIDR), |
| 158 | + } |
| 159 | + if found && userPodCIDR != capiCIDR { |
| 160 | + return Incompatible{ |
| 161 | + mutation: setPodCIDR, |
| 162 | + userVal: userPodCIDR, |
| 163 | + } |
| 164 | + } |
| 165 | + logMutation(log, setPodCIDR) |
| 166 | + return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capiCIDR, podCIDRPath...) |
| 167 | +} |
0 commit comments