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

add AKS resource health to AzureManagedControlPlane #2738

Merged
merged 1 commit into from
Dec 16, 2022
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
2 changes: 2 additions & 0 deletions api/v1beta1/conditions_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ const (
ManagedClusterRunningCondition clusterv1.ConditionType = "ManagedClusterRunning"
// AgentPoolsReadyCondition means the AKS agent pools exist and are ready to be used.
AgentPoolsReadyCondition clusterv1.ConditionType = "AgentPoolsReady"
// AzureResourceAvailableCondition means the AKS cluster is healthy according to Azure's Resource Health API.
AzureResourceAvailableCondition clusterv1.ConditionType = "AzureResourceAvailable"
)

// Azure Services Conditions and Reasons.
Expand Down
69 changes: 69 additions & 0 deletions azure/converters/resourcehealth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2022 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 converters

import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/resourcehealth/mgmt/2020-05-01/resourcehealth"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
)

// SDKAvailabilityStatusToCondition converts an Azure Resource Health availability status to a status condition.
func SDKAvailabilityStatusToCondition(availStatus resourcehealth.AvailabilityStatus) *clusterv1.Condition {
if availStatus.Properties == nil {
return conditions.FalseCondition(infrav1.AzureResourceAvailableCondition, "", "", "")
}

state := availStatus.Properties.AvailabilityState

if state == resourcehealth.AvailabilityStateValuesAvailable {
return conditions.TrueCondition(infrav1.AzureResourceAvailableCondition)
}

var reason strings.Builder
if availStatus.Properties.ReasonType != nil {
// CAPI specifies Reason should be CamelCase, though the Azure API
// response may include spaces (e.g. "Customer Initiated")
words := strings.Split(*availStatus.Properties.ReasonType, " ")
for _, word := range words {
if len(word) > 0 {
reason.WriteString(strings.ToTitle(word[:1]))
}
if len(word) > 1 {
reason.WriteString(word[1:])
}
}
}

var severity clusterv1.ConditionSeverity
switch availStatus.Properties.AvailabilityState {
case resourcehealth.AvailabilityStateValuesUnavailable:
severity = clusterv1.ConditionSeverityError
case resourcehealth.AvailabilityStateValuesDegraded, resourcehealth.AvailabilityStateValuesUnknown:
severity = clusterv1.ConditionSeverityWarning
}

var message string
if availStatus.Properties.Summary != nil {
message = *availStatus.Properties.Summary
}

return conditions.FalseCondition(infrav1.AzureResourceAvailableCondition, reason.String(), severity, message)
}
101 changes: 101 additions & 0 deletions azure/converters/resourcehealth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2022 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 converters

import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/resourcehealth/mgmt/2020-05-01/resourcehealth"
"github.com/Azure/go-autorest/autorest/to"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestAzureAvailabilityStatusToCondition(t *testing.T) {
tests := []struct {
name string
avail resourcehealth.AvailabilityStatus
expected *clusterv1.Condition
}{
{
name: "empty",
avail: resourcehealth.AvailabilityStatus{},
expected: &clusterv1.Condition{
Status: corev1.ConditionFalse,
},
},
{
name: "available",
avail: resourcehealth.AvailabilityStatus{
Properties: &resourcehealth.AvailabilityStatusProperties{
AvailabilityState: resourcehealth.AvailabilityStateValuesAvailable,
},
},
expected: &clusterv1.Condition{
Status: corev1.ConditionTrue,
},
},
{
name: "unavailable",
avail: resourcehealth.AvailabilityStatus{
Properties: &resourcehealth.AvailabilityStatusProperties{
AvailabilityState: resourcehealth.AvailabilityStateValuesUnavailable,
ReasonType: to.StringPtr("this Is a reason "),
Summary: to.StringPtr("The Summary"),
},
},
expected: &clusterv1.Condition{
Status: corev1.ConditionFalse,
Severity: clusterv1.ConditionSeverityError,
Reason: "ThisIsAReason",
Message: "The Summary",
},
},
{
name: "degraded",
avail: resourcehealth.AvailabilityStatus{
Properties: &resourcehealth.AvailabilityStatusProperties{
AvailabilityState: resourcehealth.AvailabilityStateValuesDegraded,
ReasonType: to.StringPtr("TheReason"),
Summary: to.StringPtr("The Summary"),
},
},
expected: &clusterv1.Condition{
Status: corev1.ConditionFalse,
Severity: clusterv1.ConditionSeverityWarning,
Reason: "TheReason",
Message: "The Summary",
},
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)
t.Parallel()

cond := SDKAvailabilityStatusToCondition(test.avail)

g.Expect(cond.Status).To(Equal(test.expected.Status))
g.Expect(cond.Severity).To(Equal(test.expected.Severity))
g.Expect(cond.Reason).To(Equal(test.expected.Reason))
g.Expect(cond.Message).To(Equal(test.expected.Message))
})
}
}
5 changes: 5 additions & 0 deletions azure/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ func VirtualNetworkLinkID(subscriptionID, resourceGroup, privateDNSZoneName, vir
return fmt.Sprintf("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/privateDnsZones/%s/virtualNetworkLinks/%s", subscriptionID, resourceGroup, privateDNSZoneName, virtualNetworkLinkName)
}

// ManagedClusterID returns the azure resource ID for a given managed cluster.
func ManagedClusterID(subscriptionID, resourceGroup, managedClusterName string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerService/managedClusters/%s", subscriptionID, resourceGroup, managedClusterName)
}

// GetBootstrappingVMExtension returns the CAPZ Bootstrapping VM extension.
// The CAPZ Bootstrapping extension is a simple clone of https://github.com/Azure/custom-script-extension-linux for Linux or
// https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows for Windows.
Expand Down
24 changes: 24 additions & 0 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"strings"
"time"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
Expand All @@ -44,6 +45,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const resourceHealthWarningInitialGracePeriod = 1 * time.Hour

// ManagedControlPlaneScopeParams defines the input parameters used to create a new managed
// control plane.
type ManagedControlPlaneScopeParams struct {
Expand Down Expand Up @@ -197,6 +200,7 @@ func (s *ManagedControlPlaneScope) PatchObject(ctx context.Context) error {
infrav1.SubnetsReadyCondition,
infrav1.ManagedClusterRunningCondition,
infrav1.AgentPoolsReadyCondition,
infrav1.AzureResourceAvailableCondition,
}})
}

Expand Down Expand Up @@ -655,3 +659,23 @@ func (s *ManagedControlPlaneScope) TagsSpecs() []azure.TagsSpec {
},
}
}

// AvailabilityStatusResource refers to the AzureManagedControlPlane.
func (s *ManagedControlPlaneScope) AvailabilityStatusResource() conditions.Setter {
return s.ControlPlane
}

// AvailabilityStatusResourceURI constructs the ID of the underlying AKS resource.
func (s *ManagedControlPlaneScope) AvailabilityStatusResourceURI() string {
return azure.ManagedClusterID(s.SubscriptionID(), s.ResourceGroup(), s.ControlPlane.Name)
}

// AvailabilityStatusFilter ignores the health metrics connection error that
// occurs on startup for every AKS cluster.
func (s *ManagedControlPlaneScope) AvailabilityStatusFilter(cond *clusterv1.Condition) *clusterv1.Condition {
if time.Since(s.ControlPlane.CreationTimestamp.Time) < resourceHealthWarningInitialGracePeriod &&
cond.Severity == clusterv1.ConditionSeverityWarning {
return conditions.TrueCondition(infrav1.AzureResourceAvailableCondition)
}
return cond
}
57 changes: 57 additions & 0 deletions azure/services/resourcehealth/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2022 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 resourcehealth

import (
"context"

"github.com/Azure/azure-sdk-for-go/services/resourcehealth/mgmt/2020-05-01/resourcehealth"
"github.com/Azure/go-autorest/autorest"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)

// client wraps go-sdk.
type client interface {
GetByResource(context.Context, string) (resourcehealth.AvailabilityStatus, error)
}

// azureClient contains the Azure go-sdk Client.
type azureClient struct {
availabilityStatuses resourcehealth.AvailabilityStatusesClient
}

// newClient creates a new resource health client from subscription ID.
func newClient(auth azure.Authorizer) *azureClient {
c := newResourceHealthClient(auth.SubscriptionID(), auth.BaseURI(), auth.Authorizer())
return &azureClient{c}
}

// newResourceHealthClient creates a new resource health client from subscription ID.
func newResourceHealthClient(subscriptionID string, baseURI string, authorizer autorest.Authorizer) resourcehealth.AvailabilityStatusesClient {
healthClient := resourcehealth.NewAvailabilityStatusesClientWithBaseURI(baseURI, subscriptionID)
azure.SetAutoRestClientDefaults(&healthClient.Client, authorizer)
return healthClient
}

// GetByResource gets the availability status for the specified resource.
func (ac *azureClient) GetByResource(ctx context.Context, resourceURI string) (resourcehealth.AvailabilityStatus, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "resourcehealth.AzureClient.GetByResource")
defer done()

return ac.availabilityStatuses.GetByResource(ctx, resourceURI, "", "")
}
67 changes: 67 additions & 0 deletions azure/services/resourcehealth/mock_resourcehealth/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions azure/services/resourcehealth/mock_resourcehealth/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2022 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.
*/

// Run go generate to regenerate this mock.
//
//go:generate ../../../../hack/tools/bin/mockgen -destination client_mock.go -package mock_resourcehealth -source ../client.go Client
//go:generate ../../../../hack/tools/bin/mockgen -destination resourcehealth_mock.go -package mock_resourcehealth -source ../resourcehealth.go ResourceHealthScope,AvailabilityStatusFilterer
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt client_mock.go > _client_mock.go && mv _client_mock.go client_mock.go"
//go:generate /usr/bin/env bash -c "cat ../../../../hack/boilerplate/boilerplate.generatego.txt resourcehealth_mock.go > _resourcehealth_mock.go && mv _resourcehealth_mock.go resourcehealth_mock.go"
package mock_resourcehealth
Loading