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

Updated AddOrUpdateTolerationInPod to return bool only. #42964

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,15 @@ const (

// AddOrUpdateTolerationInPod tries to add a toleration to the pod's toleration list.
// Returns true if something was updated, false otherwise.
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error) {
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) bool {
podTolerations := pod.Spec.Tolerations

var newTolerations []Toleration
updated := false
for i := range podTolerations {
if toleration.MatchToleration(&podTolerations[i]) {
if Semantic.DeepEqual(toleration, podTolerations[i]) {
return false, nil
return false
}
newTolerations = append(newTolerations, *toleration)
updated = true
Expand All @@ -490,7 +490,7 @@ func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error)
}

pod.Spec.Tolerations = newTolerations
return true, nil
return true
}

// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,17 @@ const (
AffinityAnnotationKey string = "scheduler.alpha.kubernetes.io/affinity"
)

// Tries to add a toleration to annotations list. Returns true if something was updated
// false otherwise.
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error) {
// AddOrUpdateTolerationInPod tries to add a toleration to the pod's toleration list.
// Returns true if something was updated, false otherwise.
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) bool {
podTolerations := pod.Spec.Tolerations

var newTolerations []Toleration
updated := false
for i := range podTolerations {
if toleration.MatchToleration(&podTolerations[i]) {
if api.Semantic.DeepEqual(toleration, podTolerations[i]) {
return false, nil
return false
}
newTolerations = append(newTolerations, *toleration)
updated = true
Expand All @@ -301,7 +301,7 @@ func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error)
}

pod.Spec.Tolerations = newTolerations
return true, nil
return true
}

// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
Expand Down
10 changes: 2 additions & 8 deletions pkg/controller/daemon/daemoncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,27 +844,21 @@ func (dsc *DaemonSetsController) nodeShouldRunDaemonPod(node *v1.Node, ds *exten
// Add infinite toleration for taint notReady:NoExecute here
// to survive taint-based eviction enforced by NodeController
// when node turns not ready.
_, err = v1.AddOrUpdateTolerationInPod(newPod, &v1.Toleration{
v1.AddOrUpdateTolerationInPod(newPod, &v1.Toleration{
Key: metav1.TaintNodeNotReady,
Operator: v1.TolerationOpExists,
Effect: v1.TaintEffectNoExecute,
})
if err != nil {
return false, false, false, err
}

// DaemonSet pods shouldn't be deleted by NodeController in case of node problems.
// Add infinite toleration for taint unreachable:NoExecute here
// to survive taint-based eviction enforced by NodeController
// when node turns unreachable.
_, err = v1.AddOrUpdateTolerationInPod(newPod, &v1.Toleration{
v1.AddOrUpdateTolerationInPod(newPod, &v1.Toleration{
Key: metav1.TaintNodeUnreachable,
Operator: v1.TolerationOpExists,
Effect: v1.TaintEffectNoExecute,
})
if err != nil {
return false, false, false, err
}

pods := []*v1.Pod{}

Expand Down
12 changes: 2 additions & 10 deletions plugin/pkg/admission/defaulttolerationseconds/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,21 @@ func (p *plugin) Admit(attributes admission.Attributes) (err error) {
}

if !toleratesNodeNotReady {
_, err := api.AddOrUpdateTolerationInPod(pod, &api.Toleration{
api.AddOrUpdateTolerationInPod(pod, &api.Toleration{
Key: metav1.TaintNodeNotReady,
Operator: api.TolerationOpExists,
Effect: api.TaintEffectNoExecute,
TolerationSeconds: defaultNotReadyTolerationSeconds,
})
if err != nil {
return admission.NewForbidden(attributes,
fmt.Errorf("failed to add default tolerations for taints `notReady:NoExecute` and `unreachable:NoExecute`, err: %v", err))
}
}

if !toleratesNodeUnreachable {
_, err := api.AddOrUpdateTolerationInPod(pod, &api.Toleration{
api.AddOrUpdateTolerationInPod(pod, &api.Toleration{
Key: metav1.TaintNodeUnreachable,
Operator: api.TolerationOpExists,
Effect: api.TaintEffectNoExecute,
TolerationSeconds: defaultUnreachableTolerationSeconds,
})
if err != nil {
return admission.NewForbidden(attributes,
fmt.Errorf("failed to add default tolerations for taints `notReady:NoExecute` and `unreachable:NoExecute`, err: %v", err))
}
}
return nil
}
6 changes: 3 additions & 3 deletions staging/src/k8s.io/client-go/pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,15 @@ const (

// AddOrUpdateTolerationInPod tries to add a toleration to the pod's toleration list.
// Returns true if something was updated, false otherwise.
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error) {
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) bool {
podTolerations := pod.Spec.Tolerations

var newTolerations []Toleration
updated := false
for i := range podTolerations {
if toleration.MatchToleration(&podTolerations[i]) {
if Semantic.DeepEqual(toleration, podTolerations[i]) {
return false, nil
return false
}
newTolerations = append(newTolerations, *toleration)
updated = true
Expand All @@ -490,7 +490,7 @@ func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error)
}

pod.Spec.Tolerations = newTolerations
return true, nil
return true
}

// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
Expand Down
10 changes: 5 additions & 5 deletions staging/src/k8s.io/client-go/pkg/api/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,17 @@ const (
AffinityAnnotationKey string = "scheduler.alpha.kubernetes.io/affinity"
)

// Tries to add a toleration to annotations list. Returns true if something was updated
// false otherwise.
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error) {
// AddOrUpdateTolerationInPod tries to add a toleration to the pod's toleration list.
// Returns true if something was updated, false otherwise.
func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) bool {
podTolerations := pod.Spec.Tolerations

var newTolerations []Toleration
updated := false
for i := range podTolerations {
if toleration.MatchToleration(&podTolerations[i]) {
if api.Semantic.DeepEqual(toleration, podTolerations[i]) {
return false, nil
return false
}
newTolerations = append(newTolerations, *toleration)
updated = true
Expand All @@ -301,7 +301,7 @@ func AddOrUpdateTolerationInPod(pod *Pod, toleration *Toleration) (bool, error)
}

pod.Spec.Tolerations = newTolerations
return true, nil
return true
}

// MatchToleration checks if the toleration matches tolerationToMatch. Tolerations are unique by <key,effect,operator,value>,
Expand Down