-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Delete outdated deployments. #2690
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
nilo19:qi-delete-outdated-deployments
Dec 28, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,14 +30,17 @@ import ( | |
"github.com/Azure/go-autorest/autorest/to" | ||
|
||
apiv1 "k8s.io/api/core/v1" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider" | ||
"k8s.io/autoscaler/cluster-autoscaler/config/dynamic" | ||
"k8s.io/klog" | ||
schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo" | ||
) | ||
|
||
var ( | ||
vmInstancesRefreshPeriod = 5 * time.Minute | ||
const ( | ||
vmInstancesRefreshPeriod = 5 * time.Minute | ||
clusterAutoscalerDeploymentPrefix = `cluster-autoscaler-` | ||
defaultMaxDeploymentsCount = 10 | ||
) | ||
|
||
var virtualMachinesStatusCache struct { | ||
|
@@ -209,6 +212,62 @@ func (as *AgentPool) TargetSize() (int, error) { | |
return int(size), nil | ||
} | ||
|
||
func (as *AgentPool) getAllSucceededAndFailedDeployments() (succeededAndFailedDeployments []resources.DeploymentExtended, err error) { | ||
ctx, cancel := getContextWithCancel() | ||
defer cancel() | ||
|
||
deploymentsFilter := "provisioningState eq 'Succeeded' or provisioningState eq 'Failed'" | ||
succeededAndFailedDeployments, err = as.manager.azClient.deploymentsClient.List(ctx, as.manager.config.ResourceGroup, deploymentsFilter, nil) | ||
if err != nil { | ||
klog.Errorf("getAllSucceededAndFailedDeployments: failed to list succeeded or failed deployments with error: %v", err) | ||
return nil, err | ||
} | ||
|
||
return succeededAndFailedDeployments, err | ||
} | ||
|
||
// deleteOutdatedDeployments keeps the newest deployments in the resource group and delete others, | ||
// since Azure resource group deployments have a hard cap of 800, outdated deployments must be deleted | ||
// to prevent the `DeploymentQuotaExceeded` error. see: issue #2154. | ||
func (as *AgentPool) deleteOutdatedDeployments() (err error) { | ||
deployments, err := as.getAllSucceededAndFailedDeployments() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for i := len(deployments) - 1; i >= 0; i-- { | ||
klog.V(4).Infof("deleteOutdatedDeployments: found deployments[i].Name: %s", *deployments[i].Name) | ||
if deployments[i].Name != nil && !strings.HasPrefix(*deployments[i].Name, clusterAutoscalerDeploymentPrefix) { | ||
deployments = append(deployments[:i], deployments[i+1:]...) | ||
} | ||
} | ||
|
||
if int64(len(deployments)) <= as.manager.config.MaxDeploymentsCount { | ||
klog.V(4).Infof("deleteOutdatedDeployments: the number of deployments (%d) is under threshold, skip deleting", len(deployments)) | ||
return err | ||
} | ||
|
||
sort.Slice(deployments, func(i, j int) bool { | ||
return deployments[i].Properties.Timestamp.Time.After(deployments[j].Properties.Timestamp.Time) | ||
}) | ||
|
||
toBeDeleted := deployments[as.manager.config.MaxDeploymentsCount:] | ||
|
||
ctx, cancel := getContextWithCancel() | ||
defer cancel() | ||
|
||
errList := make([]error, 0) | ||
for _, deployment := range toBeDeleted { | ||
klog.V(4).Infof("deleteOutdatedDeployments: starts deleting outdated deployment (%s)", *deployment.Name) | ||
_, err := as.manager.azClient.deploymentsClient.Delete(ctx, as.manager.config.ResourceGroup, *deployment.Name) | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aggregate the errors and continue to delete the next error. refer |
||
errList = append(errList, err) | ||
} | ||
} | ||
|
||
return utilerrors.NewAggregate(errList) | ||
} | ||
|
||
// IncreaseSize increases agent pool size | ||
func (as *AgentPool) IncreaseSize(delta int) error { | ||
as.mutex.Lock() | ||
|
@@ -218,6 +277,11 @@ func (as *AgentPool) IncreaseSize(delta int) error { | |
return fmt.Errorf("size increase must be positive") | ||
} | ||
|
||
err := as.deleteOutdatedDeployments() | ||
if err != nil { | ||
klog.Warningf("IncreaseSize: failed to cleanup outdated deployments with err: %v.", err) | ||
} | ||
|
||
indexes, _, err := as.GetVMIndexes() | ||
if err != nil { | ||
return err | ||
|
@@ -404,6 +468,11 @@ func (as *AgentPool) DeleteNodes(nodes []*apiv1.Node) error { | |
refs = append(refs, ref) | ||
} | ||
|
||
err = as.deleteOutdatedDeployments() | ||
if err != nil { | ||
klog.Warningf("DeleteNodes: failed to cleanup outdated deployments with err: %v.", err) | ||
} | ||
|
||
return as.DeleteInstances(refs) | ||
} | ||
|
||
|
107 changes: 107 additions & 0 deletions
107
cluster-autoscaler/cloudprovider/azure/azure_agent_pool_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
Copyright 2019 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 azure | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources" | ||
"github.com/Azure/go-autorest/autorest/date" | ||
"github.com/Azure/go-autorest/autorest/to" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func newTestAgentPool(manager *AzureManager, name string) *AgentPool { | ||
return &AgentPool{ | ||
azureRef: azureRef{ | ||
Name: name, | ||
}, | ||
manager: manager, | ||
minSize: 1, | ||
maxSize: 5, | ||
} | ||
} | ||
|
||
func TestDeleteOutdatedDeployments(t *testing.T) { | ||
timeLayout := "2006-01-02 15:04:05" | ||
timeBenchMark, _ := time.Parse(timeLayout, "2000-01-01 00:00:00") | ||
|
||
testCases := []struct { | ||
deployments map[string]resources.DeploymentExtended | ||
expectedDeploymentsNames map[string]bool | ||
expectedErr error | ||
desc string | ||
}{ | ||
{ | ||
deployments: map[string]resources.DeploymentExtended{ | ||
"non-cluster-autoscaler-0000": { | ||
Name: to.StringPtr("non-cluster-autoscaler-0000"), | ||
Properties: &resources.DeploymentPropertiesExtended{ | ||
ProvisioningState: to.StringPtr("Succeeded"), | ||
Timestamp: &date.Time{Time: timeBenchMark.Add(2 * time.Minute)}, | ||
}, | ||
}, | ||
"cluster-autoscaler-0000": { | ||
Name: to.StringPtr("cluster-autoscaler-0000"), | ||
Properties: &resources.DeploymentPropertiesExtended{ | ||
ProvisioningState: to.StringPtr("Succeeded"), | ||
Timestamp: &date.Time{Time: timeBenchMark}, | ||
}, | ||
}, | ||
"cluster-autoscaler-0001": { | ||
Name: to.StringPtr("cluster-autoscaler-0001"), | ||
Properties: &resources.DeploymentPropertiesExtended{ | ||
ProvisioningState: to.StringPtr("Succeeded"), | ||
Timestamp: &date.Time{Time: timeBenchMark.Add(time.Minute)}, | ||
}, | ||
}, | ||
"cluster-autoscaler-0002": { | ||
Name: to.StringPtr("cluster-autoscaler-0002"), | ||
Properties: &resources.DeploymentPropertiesExtended{ | ||
ProvisioningState: to.StringPtr("Succeeded"), | ||
Timestamp: &date.Time{Time: timeBenchMark.Add(2 * time.Minute)}, | ||
}, | ||
}, | ||
}, | ||
expectedDeploymentsNames: map[string]bool{ | ||
"non-cluster-autoscaler-0000": true, | ||
"cluster-autoscaler-0001": true, | ||
"cluster-autoscaler-0002": true, | ||
}, | ||
expectedErr: nil, | ||
desc: "cluster autoscaler provider azure should delete outdated deployments created by cluster autoscaler", | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
testAS := newTestAgentPool(newTestAzureManager(t), "testAS") | ||
testAS.manager.azClient.deploymentsClient = &DeploymentsClientMock{ | ||
FakeStore: test.deployments, | ||
} | ||
|
||
err := testAS.deleteOutdatedDeployments() | ||
assert.Equal(t, test.expectedErr, err, test.desc) | ||
existedDeployments, err := testAS.manager.azClient.deploymentsClient.List(context.Background(), "", "", to.Int32Ptr(0)) | ||
existedDeploymentsNames := make(map[string]bool) | ||
for _, deployment := range existedDeployments { | ||
existedDeploymentsNames[*deployment.Name] = true | ||
} | ||
assert.Equal(t, test.expectedDeploymentsNames, existedDeploymentsNames, test.desc) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we move the filter here and only return the autoscaler managed deployments?