-
Notifications
You must be signed in to change notification settings - Fork 460
ASOAPI: add resource mutator framework #4793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package mutators | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha1" | ||
| "sigs.k8s.io/cluster-api-provider-azure/util/tele" | ||
| clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrNoManagedClusterDefined describes an AzureASOManagedControlPlane without a ManagedCluster. | ||
| ErrNoManagedClusterDefined = fmt.Errorf("no %s ManagedCluster defined in AzureASOManagedControlPlane spec.resources", asocontainerservicev1.GroupVersion.Group) | ||
| ) | ||
|
|
||
| // SetManagedClusterDefaults propagates values defined by Cluster API to an ASO ManagedCluster. | ||
| func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, cluster *clusterv1.Cluster) ResourcesMutator { | ||
| return func(ctx context.Context, us []*unstructured.Unstructured) error { | ||
| ctx, _, done := tele.StartSpanWithLogger(ctx, "mutators.SetManagedClusterDefaults") | ||
| defer done() | ||
|
|
||
| var managedCluster *unstructured.Unstructured | ||
| var managedClusterPath string | ||
| for i, u := range us { | ||
| if u.GroupVersionKind().Group == asocontainerservicev1.GroupVersion.Group && | ||
| u.GroupVersionKind().Kind == "ManagedCluster" { | ||
| managedCluster = u | ||
| managedClusterPath = fmt.Sprintf("spec.resources[%d]", i) | ||
| break | ||
| } | ||
| } | ||
| if managedCluster == nil { | ||
| return reconcile.TerminalError(ErrNoManagedClusterDefined) | ||
| } | ||
|
|
||
| if err := setManagedClusterKubernetesVersion(ctx, asoManagedControlPlane, managedClusterPath, managedCluster); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := setManagedClusterServiceCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := setManagedClusterPodCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func setManagedClusterKubernetesVersion(ctx context.Context, asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane, managedClusterPath string, managedCluster *unstructured.Unstructured) error { | ||
| _, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterKubernetesVersion") | ||
| defer done() | ||
|
|
||
| capzK8sVersion := strings.TrimPrefix(asoManagedControlPlane.Spec.Version, "v") | ||
| if capzK8sVersion == "" { | ||
| // When the CAPI contract field isn't set, any value for version in the embedded ASO resource may be specified. | ||
| return nil | ||
| } | ||
|
|
||
| k8sVersionPath := []string{"spec", "kubernetesVersion"} | ||
| userK8sVersion, k8sVersionFound, err := unstructured.NestedString(managedCluster.UnstructuredContent(), k8sVersionPath...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| setK8sVersion := mutation{ | ||
| location: managedClusterPath + "." + strings.Join(k8sVersionPath, "."), | ||
| val: capzK8sVersion, | ||
| reason: "because spec.version is set to " + asoManagedControlPlane.Spec.Version, | ||
| } | ||
| if k8sVersionFound && userK8sVersion != capzK8sVersion { | ||
| return Incompatible{ | ||
| mutation: setK8sVersion, | ||
| userVal: userK8sVersion, | ||
| } | ||
| } | ||
| logMutation(log, setK8sVersion) | ||
| return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capzK8sVersion, k8sVersionPath...) | ||
| } | ||
|
|
||
| func setManagedClusterServiceCIDR(ctx context.Context, cluster *clusterv1.Cluster, managedClusterPath string, managedCluster *unstructured.Unstructured) error { | ||
| _, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterServiceCIDR") | ||
| defer done() | ||
|
|
||
| if cluster.Spec.ClusterNetwork == nil || | ||
| cluster.Spec.ClusterNetwork.Services == nil || | ||
| len(cluster.Spec.ClusterNetwork.Services.CIDRBlocks) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| capiCIDR := cluster.Spec.ClusterNetwork.Services.CIDRBlocks[0] | ||
|
|
||
| // ManagedCluster.v1api20210501.containerservice.azure.com does not contain the plural serviceCidrs field. | ||
| svcCIDRPath := []string{"spec", "networkProfile", "serviceCidr"} | ||
| userSvcCIDR, found, err := unstructured.NestedString(managedCluster.UnstructuredContent(), svcCIDRPath...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| setSvcCIDR := mutation{ | ||
| location: managedClusterPath + "." + strings.Join(svcCIDRPath, "."), | ||
| val: capiCIDR, | ||
| reason: fmt.Sprintf("because spec.clusterNetwork.services.cidrBlocks[0] in Cluster %s/%s is set to %s", cluster.Namespace, cluster.Name, capiCIDR), | ||
| } | ||
| if found && userSvcCIDR != capiCIDR { | ||
| return Incompatible{ | ||
| mutation: setSvcCIDR, | ||
| userVal: userSvcCIDR, | ||
| } | ||
| } | ||
| logMutation(log, setSvcCIDR) | ||
| return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capiCIDR, svcCIDRPath...) | ||
| } | ||
|
|
||
| func setManagedClusterPodCIDR(ctx context.Context, cluster *clusterv1.Cluster, managedClusterPath string, managedCluster *unstructured.Unstructured) error { | ||
| _, log, done := tele.StartSpanWithLogger(ctx, "mutators.setManagedClusterPodCIDR") | ||
| defer done() | ||
|
|
||
| if cluster.Spec.ClusterNetwork == nil || | ||
| cluster.Spec.ClusterNetwork.Pods == nil || | ||
| len(cluster.Spec.ClusterNetwork.Pods.CIDRBlocks) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| capiCIDR := cluster.Spec.ClusterNetwork.Pods.CIDRBlocks[0] | ||
|
|
||
| // ManagedCluster.v1api20210501.containerservice.azure.com does not contain the plural podCidrs field. | ||
| podCIDRPath := []string{"spec", "networkProfile", "podCidr"} | ||
| userPodCIDR, found, err := unstructured.NestedString(managedCluster.UnstructuredContent(), podCIDRPath...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| setPodCIDR := mutation{ | ||
| location: managedClusterPath + "." + strings.Join(podCIDRPath, "."), | ||
| val: capiCIDR, | ||
| reason: fmt.Sprintf("because spec.clusterNetwork.pods.cidrBlocks[0] in Cluster %s/%s is set to %s", cluster.Namespace, cluster.Name, capiCIDR), | ||
| } | ||
| if found && userPodCIDR != capiCIDR { | ||
| return Incompatible{ | ||
| mutation: setPodCIDR, | ||
| userVal: userPodCIDR, | ||
| } | ||
| } | ||
| logMutation(log, setPodCIDR) | ||
| return unstructured.SetNestedField(managedCluster.UnstructuredContent(), capiCIDR, podCIDRPath...) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.