Skip to content

Commit

Permalink
feat: implement agent pool resource information
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Oct 14, 2021
1 parent 9d572b4 commit 5481e4c
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
56 changes: 54 additions & 2 deletions aks/upgrade/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,25 @@ func createEngineDemoCommand() *cobra.Command {
rules.RulesSet{
upgradePDBRuleProvider,
rules.NewRule(
"upgrade/armtest",
"upgrade/armtest-managed-cluster",
func(
ctx context.Context,
clusterCtx rules.ClusterContext,
) ([]*rules.CheckResult, error) {
// load the cluster from cluster context
cluster, err := clusterCtx.GetManagedClusterInformation(ctx)
if err != nil {
return nil, err
}

// load the ARM representation of the cluster
// the model details: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-08-01/containerservice#ManagedCluster
latestModel, err := cluster.GetLatestModel(ctx)
if err != nil {
return nil, err
}

// check provisioning state
category := rules.Healthy
provisionState := to.String(latestModel.ProvisioningState)
if provisionState != "Succeeded" {
Expand All @@ -103,7 +107,7 @@ func createEngineDemoCommand() *cobra.Command {

return []*rules.CheckResult{
{
RuleID: "upgrade/armtest",
RuleID: "upgrade/armtest-managed-cluster",
Category: category,
Description: fmt.Sprintf(
"Got details from cluster: %s - state: %s (%s)",
Expand All @@ -115,6 +119,54 @@ func createEngineDemoCommand() *cobra.Command {
}, nil
},
),
rules.NewRule(
"upgrade/armtest-agent-pool",
func(
ctx context.Context,
clusterCtx rules.ClusterContext,
) ([]*rules.CheckResult, error) {
cluster, err := clusterCtx.GetManagedClusterInformation(ctx)
if err != nil {
return nil, err
}

clusterModel, err := cluster.GetLatestModel(ctx)
if err != nil {
return nil, err
}

ap, err := cluster.GetAgentPoolInformation(ctx, to.String((*clusterModel.AgentPoolProfiles)[0].Name))
if err != nil {
return nil, err
}

// load the ARM representation of the cluster
// the model details: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2021-08-01/containerservice#AgentPool
latestModel, err := ap.GetLatestModel(ctx)
if err != nil {
return nil, err
}

category := rules.Healthy
provisionState := to.String(latestModel.ProvisioningState)
if provisionState != "Succeeded" {
category = rules.Warning
}

return []*rules.CheckResult{
{
RuleID: "upgrade/armtest-agent-pool",
Category: category,
Description: fmt.Sprintf(
"Got details from agent pool: %s - state: %s (%s)",
ap.GetResourceName(),
provisionState,
ap.GetManagedClusterName(),
),
},
}, nil
},
),
demoRule{
ruleID: "demo/upgrade/subnet",
category: rules.Warning,
Expand Down
10 changes: 10 additions & 0 deletions pkg/azure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ func aksManagedClusterClient(
return client
}

func aksAgentPoolClient(
authorizer autorest.Authorizer,
subscriptionID string,
) containerservice.AgentPoolsClient {
client := containerservice.NewAgentPoolsClient(subscriptionID)
client.Authorizer = authorizer

return client
}

type ARMResourceID struct {
Subscription string
ResourceGroup string
Expand Down
72 changes: 69 additions & 3 deletions pkg/azure/managedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func (m *nilManagedClusterAgentPoolInformation) GetResourceName() string {

func (m *nilManagedClusterAgentPoolInformation) GetLatestModel(
ctx context.Context,
) (containerservice.ManagedClusterAgentPoolProfile, error) {
return containerservice.ManagedClusterAgentPoolProfile{}, ErrNotAvilable
) (containerservice.AgentPool, error) {
return containerservice.AgentPool{}, ErrNotAvilable
}

type managedClusterInfomration struct {
Expand Down Expand Up @@ -163,5 +163,71 @@ func (m *managedClusterInfomration) GetKubeConfig(ctx context.Context) (string,
func (m *managedClusterInfomration) GetAgentPoolInformation(
ctx context.Context, agentPoolName string,
) (ManagedClusterAgentPoolInformation, error) {
return nil, ErrNotAvilable
apInformation := &managedClusterAgentPoolInformation{
azureAuthorizer: m.azureAuthorizer,
clusterResourceID: m.resourceID,
agentPoolName: agentPoolName,
mutex: &sync.RWMutex{},
}
if _, err := apInformation.GetLatestModel(ctx); err != nil {
return &nilManagedClusterAgentPoolInformation{}, err
}

return apInformation, nil
}

type managedClusterAgentPoolInformation struct {
azureAuthorizer autorest.Authorizer

clusterResourceID *ARMResourceID
agentPoolName string

mutex *sync.RWMutex // protects the following fields
model *containerservice.AgentPool
}

var _ ManagedClusterAgentPoolInformation = &managedClusterAgentPoolInformation{}

func (m *managedClusterAgentPoolInformation) IsAvailable() bool {
return true
}

func (m *managedClusterAgentPoolInformation) GetResourceID() string {
m.mutex.RLock()
defer m.mutex.RUnlock()

return to.String(m.model.ID)
}

func (m *managedClusterAgentPoolInformation) GetSubscriptionID() string {
return m.clusterResourceID.Subscription
}

func (m *managedClusterAgentPoolInformation) GetResourceGroup() string {
return m.clusterResourceID.ResourceGroup
}

func (m *managedClusterAgentPoolInformation) GetManagedClusterName() string {
return m.clusterResourceID.ResourceName
}

func (m *managedClusterAgentPoolInformation) GetResourceName() string {
return m.agentPoolName
}

func (m *managedClusterAgentPoolInformation) GetLatestModel(
ctx context.Context,
) (containerservice.AgentPool, error) {
client := aksAgentPoolClient(m.azureAuthorizer, m.clusterResourceID.Subscription)
ap, err := client.Get(ctx, m.clusterResourceID.ResourceGroup, m.clusterResourceID.ResourceName, m.agentPoolName)
if err != nil {
return containerservice.AgentPool{}, err
}

// update record
m.mutex.Lock()
m.model = &ap
m.mutex.Unlock()

return ap, nil
}
2 changes: 1 addition & 1 deletion pkg/azure/managedclusterinformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type ManagedClusterAgentPoolInformation interface {
GetResourceName() string

// GetLatestModel returns the latest properties of the managed cluster agent pool.
GetLatestModel(ctx context.Context) (containerservice.ManagedClusterAgentPoolProfile, error)
GetLatestModel(ctx context.Context) (containerservice.AgentPool, error)

// TODO: GetVMSSName()
}
Expand Down

0 comments on commit 5481e4c

Please sign in to comment.