Skip to content

Commit

Permalink
Merge pull request #6 from cmurphy/np-delete
Browse files Browse the repository at this point in the history
Don't delete node pools for imported clusters
  • Loading branch information
cbron authored May 14, 2021
2 parents cdf6a51 + e6355d7 commit e1ac1b5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
88 changes: 45 additions & 43 deletions controller/aks-cluster-config-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,58 +635,60 @@ func (h *Handler) updateUpstreamClusterState(ctx context.Context, secretsCache w
}
}

agentPoolClient, err := aks.NewAgentPoolClient(credentials)
if err != nil {
return config, err
}
if config.Spec.NodePools != nil {
agentPoolClient, err := aks.NewAgentPoolClient(credentials)
if err != nil {
return config, err
}

downstreamNodePools, err := utils.BuildNodePoolMap(config.Spec.NodePools, config.Spec.ClusterName)
if err != nil {
return config, err
}
downstreamNodePools, err := utils.BuildNodePoolMap(config.Spec.NodePools, config.Spec.ClusterName)
if err != nil {
return config, err
}

// check for updated NodePools
upstreamNodePools, _ := utils.BuildNodePoolMap(upstreamSpec.NodePools, config.Spec.ClusterName)
updateNodePool := false
for npName, np := range downstreamNodePools {
upstreamNodePool, ok := upstreamNodePools[npName]
if ok {
// There is a matching node pool in the cluster already, so update it if needed
if to.Int32(np.Count) != to.Int32(upstreamNodePool.Count) {
logrus.Infof("Updating node count in node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
updateNodePool = true
}
if np.EnableAutoScaling != nil && to.Bool(np.EnableAutoScaling) != to.Bool(upstreamNodePool.EnableAutoScaling) {
logrus.Infof("Updating autoscaling in node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
updateNodePool = true
}
if np.OrchestratorVersion != nil && to.String(np.OrchestratorVersion) != to.String(upstreamNodePool.OrchestratorVersion) {
logrus.Infof("Updating orchestrator version in node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
// check for updated NodePools
upstreamNodePools, _ := utils.BuildNodePoolMap(upstreamSpec.NodePools, config.Spec.ClusterName)
updateNodePool := false
for npName, np := range downstreamNodePools {
upstreamNodePool, ok := upstreamNodePools[npName]
if ok {
// There is a matching node pool in the cluster already, so update it if needed
if to.Int32(np.Count) != to.Int32(upstreamNodePool.Count) {
logrus.Infof("Updating node count in node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
updateNodePool = true
}
if np.EnableAutoScaling != nil && to.Bool(np.EnableAutoScaling) != to.Bool(upstreamNodePool.EnableAutoScaling) {
logrus.Infof("Updating autoscaling in node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
updateNodePool = true
}
if np.OrchestratorVersion != nil && to.String(np.OrchestratorVersion) != to.String(upstreamNodePool.OrchestratorVersion) {
logrus.Infof("Updating orchestrator version in node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
updateNodePool = true
}
} else {
logrus.Infof("Adding node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
updateNodePool = true
}
} else {
logrus.Infof("Adding node pool [%s] for cluster [%s]", to.String(np.Name), config.Spec.ClusterName)
updateNodePool = true
}

if updateNodePool {
err = aks.CreateOrUpdateAgentPool(ctx, agentPoolClient, &config.Spec, np)
if err != nil {
return config, fmt.Errorf("failed to update cluster: %v", err)
if updateNodePool {
err = aks.CreateOrUpdateAgentPool(ctx, agentPoolClient, &config.Spec, np)
if err != nil {
return config, fmt.Errorf("failed to update cluster: %v", err)
}
return h.enqueueUpdate(config)
}
return h.enqueueUpdate(config)
}
}

// check for removed NodePools
for npName := range upstreamNodePools {
if _, ok := downstreamNodePools[npName]; !ok {
logrus.Infof("Removing node pool [%s] from cluster [%s]", npName, config.Spec.ClusterName)
err = aks.RemoveAgentPool(ctx, agentPoolClient, &config.Spec, upstreamNodePools[npName])
if err != nil {
return config, fmt.Errorf("failed to remove node pool: %v", err)
// check for removed NodePools
for npName := range upstreamNodePools {
if _, ok := downstreamNodePools[npName]; !ok {
logrus.Infof("Removing node pool [%s] from cluster [%s]", npName, config.Spec.ClusterName)
err = aks.RemoveAgentPool(ctx, agentPoolClient, &config.Spec, upstreamNodePools[npName])
if err != nil {
return config, fmt.Errorf("failed to remove node pool: %v", err)
}
return h.enqueueUpdate(config)
}
return h.enqueueUpdate(config)
}
}

Expand Down
2 changes: 2 additions & 0 deletions examples/import-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ spec:
clusterName: my-import-cluster
tenantId: "REPLACE_WITH_TENANT_ID"
azureCredentialSecret: "REPLACE_WITH_K8S_SECRETS_NAME"
resourceGroup: "my-group"
resourceLocation: "westus"
imported: true
status: {}

0 comments on commit e1ac1b5

Please sign in to comment.