Skip to content
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

Populate ControlPlaneEndpoint when ManagedCluster update is not needed. #2134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions azure/services/managedclusters/managedclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,11 @@ func (s *Service) Reconcile(ctx context.Context) error {
}

customHeaders := maps.FilterByKeyPrefix(s.Scope.ManagedClusterAnnotations(), azure.CustomHeaderPrefix)
// Use the MC fetched from Azure if no update is needed. This is to ensure the read-only fields like Fqdn from the
// existing MC are used for updating the AzureManagedCluster.
result := existingMC
if isCreate {
managedCluster, err = s.Client.CreateOrUpdate(ctx, managedClusterSpec.ResourceGroupName, managedClusterSpec.Name, managedCluster, customHeaders)
result, err = s.Client.CreateOrUpdate(ctx, managedClusterSpec.ResourceGroupName, managedClusterSpec.Name, managedCluster, customHeaders)
if err != nil {
return fmt.Errorf("failed to create managed cluster, %w", err)
}
Expand Down Expand Up @@ -325,20 +328,23 @@ func (s *Service) Reconcile(ctx context.Context) error {
diff := computeDiffOfNormalizedClusters(managedCluster, existingMC)
if diff != "" {
klog.V(2).Infof("Update required (+new -old):\n%s", diff)
managedCluster, err = s.Client.CreateOrUpdate(ctx, managedClusterSpec.ResourceGroupName, managedClusterSpec.Name, managedCluster, customHeaders)
result, err = s.Client.CreateOrUpdate(ctx, managedClusterSpec.ResourceGroupName, managedClusterSpec.Name, managedCluster, customHeaders)
if err != nil {
return fmt.Errorf("failed to update managed cluster, %w", err)
}
}
}

// Update control plane endpoint.
if managedCluster.ManagedClusterProperties != nil && managedCluster.ManagedClusterProperties.Fqdn != nil {
if result.ManagedClusterProperties != nil && result.ManagedClusterProperties.Fqdn != nil {
endpoint := clusterv1.APIEndpoint{
Host: *managedCluster.ManagedClusterProperties.Fqdn,
Host: *result.ManagedClusterProperties.Fqdn,
Port: 443,
}
s.Scope.SetControlPlaneEndpoint(endpoint)
} else {
// Fail if cluster api endpoint is not available.
return fmt.Errorf("failed to get API endpoint for managed cluster")
}

// Update kubeconfig data
Expand Down
107 changes: 107 additions & 0 deletions azure/services/managedclusters/managedclusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/managedclusters/mock_managedclusters"
gomockinternal "sigs.k8s.io/cluster-api-provider-azure/internal/test/matchers/gomock"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestReconcile(t *testing.T) {
Expand Down Expand Up @@ -123,16 +124,118 @@ func TestReconcile(t *testing.T) {
{
name: "no managedcluster exists",
expectedError: "",
expect: func(m *mock_managedclusters.MockClientMockRecorder, s *mock_managedclusters.MockManagedClusterScopeMockRecorder) {
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-managedcluster", gomock.Any(), gomock.Any()).Return(containerservice.ManagedCluster{ManagedClusterProperties: &containerservice.ManagedClusterProperties{
Fqdn: pointer.String("my-managedcluster-fqdn"),
ProvisioningState: pointer.String("Succeeded"),
}}, nil).Times(1)
m.Get(gomockinternal.AContext(), "my-rg", "my-managedcluster").Return(containerservice.ManagedCluster{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not Found"))
m.GetCredentials(gomockinternal.AContext(), "my-rg", "my-managedcluster").Times(1)
s.ClusterName().AnyTimes().Return("my-managedcluster")
s.ResourceGroup().AnyTimes().Return("my-rg")
s.ManagedClusterAnnotations().Times(1).Return(map[string]string{})
s.ManagedClusterSpec().AnyTimes().Return(azure.ManagedClusterSpec{
Name: "my-managedcluster",
ResourceGroupName: "my-rg",
}, nil)
s.GetAllAgentPoolSpecs(gomockinternal.AContext()).AnyTimes().Return([]azure.AgentPoolSpec{
{
Name: "my-agentpool",
SKU: "Standard_D4s_v3",
Replicas: 1,
OSDiskSizeGB: 0,
},
}, nil)
s.SetControlPlaneEndpoint(gomock.Eq(clusterv1.APIEndpoint{
Host: "my-managedcluster-fqdn",
Port: 443,
})).Times(1)
s.SetKubeConfigData(gomock.Any()).Times(1)
},
},
{
name: "missing fqdn after create",
expectedError: "failed to get API endpoint for managed cluster",
expect: func(m *mock_managedclusters.MockClientMockRecorder, s *mock_managedclusters.MockManagedClusterScopeMockRecorder) {
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-managedcluster", gomock.Any(), gomock.Any()).Return(containerservice.ManagedCluster{ManagedClusterProperties: &containerservice.ManagedClusterProperties{}}, nil)
m.Get(gomockinternal.AContext(), "my-rg", "my-managedcluster").Return(containerservice.ManagedCluster{}, autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: 404}, "Not Found"))
s.ClusterName().AnyTimes().Return("my-managedcluster")
s.ResourceGroup().AnyTimes().Return("my-rg")
s.ManagedClusterAnnotations().Times(1).Return(map[string]string{})
s.ManagedClusterSpec().AnyTimes().Return(azure.ManagedClusterSpec{
Name: "my-managedcluster",
ResourceGroupName: "my-rg",
}, nil)
s.GetAllAgentPoolSpecs(gomockinternal.AContext()).AnyTimes().Return([]azure.AgentPoolSpec{
{
Name: "my-agentpool",
SKU: "Standard_D4s_v3",
Replicas: 1,
OSDiskSizeGB: 0,
},
}, nil)
},
},
{
name: "set correct ControlPlaneEndpoint using fqdn from existing MC after update",
expectedError: "",
expect: func(m *mock_managedclusters.MockClientMockRecorder, s *mock_managedclusters.MockManagedClusterScopeMockRecorder) {
m.CreateOrUpdate(gomockinternal.AContext(), "my-rg", "my-managedcluster", gomock.Any(), gomock.Any()).Return(containerservice.ManagedCluster{
ManagedClusterProperties: &containerservice.ManagedClusterProperties{
Fqdn: pointer.String("my-managedcluster-fqdn"),
ProvisioningState: pointer.String("Succeeded"),
},
}, nil)
m.Get(gomockinternal.AContext(), "my-rg", "my-managedcluster").Return(containerservice.ManagedCluster{
ManagedClusterProperties: &containerservice.ManagedClusterProperties{
Fqdn: pointer.String("my-managedcluster-fqdn"),
ProvisioningState: pointer.String("Succeeded"),
NetworkProfile: &containerservice.NetworkProfile{},
},
}, nil).Times(1)
m.GetCredentials(gomockinternal.AContext(), "my-rg", "my-managedcluster").Times(1)
s.ClusterName().AnyTimes().Return("my-managedcluster")
s.ResourceGroup().AnyTimes().Return("my-rg")
s.ManagedClusterAnnotations().Times(1).Return(map[string]string{})
s.ManagedClusterSpec().AnyTimes().Return(azure.ManagedClusterSpec{
Name: "my-managedcluster",
ResourceGroupName: "my-rg",
}, nil)
s.GetAllAgentPoolSpecs(gomockinternal.AContext()).AnyTimes().Return([]azure.AgentPoolSpec{
{
Name: "my-agentpool",
SKU: "Standard_D4s_v3",
Replicas: 1,
OSDiskSizeGB: 0,
},
}, nil)
s.SetControlPlaneEndpoint(gomock.Eq(clusterv1.APIEndpoint{
Host: "my-managedcluster-fqdn",
Port: 443,
})).Times(1)
s.SetKubeConfigData(gomock.Any()).Times(1)
},
},
{
name: "no update needed - set correct ControlPlaneEndpoint using fqdn from existing MC",
expectedError: "",
expect: func(m *mock_managedclusters.MockClientMockRecorder, s *mock_managedclusters.MockManagedClusterScopeMockRecorder) {
m.Get(gomockinternal.AContext(), "my-rg", "my-managedcluster").Return(containerservice.ManagedCluster{
ManagedClusterProperties: &containerservice.ManagedClusterProperties{
Fqdn: pointer.String("my-managedcluster-fqdn"),
ProvisioningState: pointer.String("Succeeded"),
KubernetesVersion: pointer.String("1.1"),
NetworkProfile: &containerservice.NetworkProfile{},
},
}, nil).Times(1)
m.GetCredentials(gomockinternal.AContext(), "my-rg", "my-managedcluster").Times(1)
s.ClusterName().AnyTimes().Return("my-managedcluster")
s.ResourceGroup().AnyTimes().Return("my-rg")
s.ManagedClusterAnnotations().Times(1).Return(map[string]string{})
s.ManagedClusterSpec().AnyTimes().Return(azure.ManagedClusterSpec{
Name: "my-managedcluster",
ResourceGroupName: "my-rg",
Version: "1.1",
}, nil)
s.GetAllAgentPoolSpecs(gomockinternal.AContext()).AnyTimes().Return([]azure.AgentPoolSpec{
{
Expand All @@ -142,6 +245,10 @@ func TestReconcile(t *testing.T) {
OSDiskSizeGB: 0,
},
}, nil)
s.SetControlPlaneEndpoint(gomock.Eq(clusterv1.APIEndpoint{
Host: "my-managedcluster-fqdn",
Port: 443,
})).Times(1)
s.SetKubeConfigData(gomock.Any()).Times(1)
},
},
Expand Down