Skip to content

Commit

Permalink
ASOAPI: propagate Cluster spec.clusterNetwork to ManagedCluster
Browse files Browse the repository at this point in the history
  • Loading branch information
nojnhuh committed May 1, 2024
1 parent 7b75af6 commit dc69aa6
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 3 deletions.
2 changes: 1 addition & 1 deletion exp/controllers/azureasomanagedcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (r *AzureASOManagedControlPlaneReconciler) reconcileNormal(ctx context.Cont
return ctrl.Result{Requeue: true}, nil
}

resources, err := mutators.ApplyMutators(ctx, asoManagedControlPlane.Spec.Resources, mutators.SetManagedClusterDefaults(asoManagedControlPlane))
resources, err := mutators.ApplyMutators(ctx, asoManagedControlPlane.Spec.Resources, mutators.SetManagedClusterDefaults(asoManagedControlPlane, cluster))
if err != nil {
return ctrl.Result{}, err
}
Expand Down
77 changes: 76 additions & 1 deletion exp/mutators/azureasomanagedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"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"
)

Expand All @@ -34,7 +35,7 @@ var (
)

// SetManagedClusterDefaults propagates values defined by Cluster API to an ASO ManagedCluster.
func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManagedControlPlane) ResourcesMutator {
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()
Expand All @@ -57,6 +58,14 @@ func SetManagedClusterDefaults(asoManagedControlPlane *infrav1exp.AzureASOManage
return err

Check warning on line 58 in exp/mutators/azureasomanagedcontrolplane.go

View check run for this annotation

Codecov / codecov/patch

exp/mutators/azureasomanagedcontrolplane.go#L58

Added line #L58 was not covered by tests
}

if err := setManagedClusterServiceCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil {
return err

Check warning on line 62 in exp/mutators/azureasomanagedcontrolplane.go

View check run for this annotation

Codecov / codecov/patch

exp/mutators/azureasomanagedcontrolplane.go#L62

Added line #L62 was not covered by tests
}

if err := setManagedClusterPodCIDR(ctx, cluster, managedClusterPath, managedCluster); err != nil {
return err

Check warning on line 66 in exp/mutators/azureasomanagedcontrolplane.go

View check run for this annotation

Codecov / codecov/patch

exp/mutators/azureasomanagedcontrolplane.go#L66

Added line #L66 was not covered by tests
}

return nil
}
}
Expand Down Expand Up @@ -90,3 +99,69 @@ func setManagedClusterKubernetesVersion(ctx context.Context, asoManagedControlPl
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

Check warning on line 119 in exp/mutators/azureasomanagedcontrolplane.go

View check run for this annotation

Codecov / codecov/patch

exp/mutators/azureasomanagedcontrolplane.go#L119

Added line #L119 was not covered by tests
}
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

Check warning on line 152 in exp/mutators/azureasomanagedcontrolplane.go

View check run for this annotation

Codecov / codecov/patch

exp/mutators/azureasomanagedcontrolplane.go#L152

Added line #L152 was not covered by tests
}
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...)
}
Loading

0 comments on commit dc69aa6

Please sign in to comment.