forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#22384 from janetkuo/skip-not-found-label
When retrying updating RSes and pods, ignore the not found error
- Loading branch information
Showing
3 changed files
with
158 additions
and
70 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
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 replicaset | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
"k8s.io/kubernetes/pkg/api/errors" | ||
"k8s.io/kubernetes/pkg/apis/extensions" | ||
unversionedextensions "k8s.io/kubernetes/pkg/client/typed/generated/extensions/unversioned" | ||
"k8s.io/kubernetes/pkg/util/wait" | ||
) | ||
|
||
// TODO: use client library instead when it starts to support update retries | ||
// see https://github.com/kubernetes/kubernetes/issues/21479 | ||
type updateRSFunc func(rs *extensions.ReplicaSet) | ||
|
||
// UpdateRSWithRetries updates a RS with given applyUpdate function. Note that RS not found error is ignored. | ||
// The returned bool value can be used to tell if the RS is actually updated. | ||
func UpdateRSWithRetries(rsClient unversionedextensions.ReplicaSetInterface, rs *extensions.ReplicaSet, applyUpdate updateRSFunc) (*extensions.ReplicaSet, bool, error) { | ||
var err error | ||
var rsUpdated bool | ||
oldRs := rs | ||
if err = wait.Poll(10*time.Millisecond, 1*time.Minute, func() (bool, error) { | ||
rs, err = rsClient.Get(oldRs.Name) | ||
if err != nil { | ||
return false, err | ||
} | ||
// Apply the update, then attempt to push it to the apiserver. | ||
// TODO: add precondition for update | ||
applyUpdate(rs) | ||
if rs, err = rsClient.Update(rs); err == nil { | ||
// Update successful. | ||
return true, nil | ||
} | ||
// TODO: don't retry on perm-failed errors and handle them gracefully | ||
// Update could have failed due to conflict error. Try again. | ||
return false, nil | ||
}); err == nil { | ||
// When there's no error, we've updated this RS. | ||
rsUpdated = true | ||
} | ||
|
||
if err == wait.ErrWaitTimeout { | ||
err = fmt.Errorf("timed out trying to update RS: %+v", oldRs) | ||
} | ||
// Ignore the RS not found error, but the RS isn't updated. | ||
if errors.IsNotFound(err) { | ||
glog.V(4).Infof("%s %s/%s is not found, skip updating it.", oldRs.Kind, oldRs.Namespace, oldRs.Name) | ||
err = nil | ||
} | ||
// If the error is non-nil the returned controller cannot be trusted, if it is nil, the returned | ||
// controller contains the applied update. | ||
return rs, rsUpdated, err | ||
} |