-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
add new extra component to --wait=all to validate a healthy cluster #10424
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c7d2e0
fix WaitForPod by waiting for component Ready instead of pod Running …
prezha 93e98de
wait CorePodsList components to be Ready only if --wait=all
prezha ec97f1d
fix integration.validateExtraConfig test adding --wait=all
prezha 90cd9c3
wait kubelet stabilise after restart
prezha b8052fe
replace sleep with retry.Expo()
prezha 8099f75
cleanup: remove unused code, make internal-only func private
prezha 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
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,175 @@ | ||
/* | ||
Copyright 2021 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 kverify verifies a running Kubernetes cluster is healthy | ||
package kverify | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
core "k8s.io/api/core/v1" | ||
meta "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/klog/v2" | ||
kconst "k8s.io/kubernetes/cmd/kubeadm/app/constants" | ||
) | ||
|
||
// WaitExtra calls WaitForPodReadyByLabel for each pod in labels list and returns any errors occurred. | ||
func WaitExtra(cs *kubernetes.Clientset, labels []string, timeout time.Duration) error { | ||
klog.Infof("extra waiting for kube-system core pods %s to be Ready ...", labels) | ||
start := time.Now() | ||
defer func() { | ||
klog.Infof("duration metric: took %s for extra waiting for kube-system core pods to be Ready ...", time.Since(start)) | ||
}() | ||
|
||
var errs []string | ||
for _, label := range labels { | ||
if err := WaitForPodReadyByLabel(cs, label, "kube-system", timeout); err != nil { | ||
errs = append(errs, fmt.Sprintf("%q: %q", label, err.Error())) | ||
} | ||
} | ||
if errs != nil { | ||
return fmt.Errorf(strings.Join(errs, ", ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// WaitForPodReadyByLabel waits for pod with label ([key:]val) in a namespace to be in Ready condition. | ||
// If namespace is not provided, it defaults to "kube-system". | ||
// If label key is not provided, it will try with "component" and "k8s-app". | ||
func WaitForPodReadyByLabel(cs *kubernetes.Clientset, label, namespace string, timeout time.Duration) error { | ||
klog.Infof("waiting %v for pod with %q label in %q namespace to be Ready ...", timeout, label, namespace) | ||
start := time.Now() | ||
defer func() { | ||
klog.Infof("duration metric: took %v to run WaitForPodReadyByLabel for pod with %q label in %q namespace ...", time.Since(start), label, namespace) | ||
}() | ||
|
||
if namespace == "" { | ||
namespace = "kube-system" | ||
} | ||
|
||
lkey := "" | ||
lval := "" | ||
l := strings.Split(label, ":") | ||
switch len(l) { | ||
case 1: // treat as no label key provided, just val | ||
lval = strings.TrimSpace(l[0]) | ||
case 2: | ||
lkey = strings.TrimSpace(l[0]) | ||
lval = strings.TrimSpace(l[1]) | ||
default: | ||
return fmt.Errorf("pod label %q is malformed", label) | ||
} | ||
|
||
lap := time.Now() | ||
checkReady := func() (bool, error) { | ||
if time.Since(start) > timeout { | ||
return false, fmt.Errorf("wait for pod with %q label in %q namespace to be Ready timed out", label, namespace) | ||
} | ||
pods, err := cs.CoreV1().Pods(namespace).List(meta.ListOptions{}) | ||
if err != nil { | ||
klog.Infof("error listing pods in %q namespace, will retry: %v", namespace, err) | ||
return false, nil | ||
} | ||
for _, pod := range pods.Items { | ||
for k, v := range pod.ObjectMeta.Labels { | ||
if ((lkey == "" && (k == "component" || k == "k8s-app")) || lkey == k) && v == lval { | ||
ready, reason := IsPodReady(&pod) | ||
if ready { | ||
klog.Info(reason) | ||
return true, nil | ||
} | ||
// reduce log spam | ||
if time.Since(lap) > (1 * time.Second) { | ||
klog.Info(reason) | ||
lap = time.Now() | ||
} | ||
return false, nil | ||
} | ||
} | ||
} | ||
klog.Infof("pod with %q label in %q namespace was not found, will retry", label, namespace) | ||
return false, nil | ||
} | ||
if err := wait.PollImmediate(kconst.APICallRetryInterval, kconst.DefaultControlPlaneTimeout, checkReady); err != nil { | ||
return errors.Wrapf(err, "wait pod Ready") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// WaitForPodReadyByName waits for pod with name in a namespace to be in Ready condition. | ||
// If namespace is not provided, it defaults to "kube-system". | ||
func WaitForPodReadyByName(cs *kubernetes.Clientset, name, namespace string, timeout time.Duration) error { | ||
klog.Infof("waiting %v for pod %q in %q namespace to be Ready ...", timeout, name, namespace) | ||
start := time.Now() | ||
defer func() { | ||
klog.Infof("duration metric: took %v to run WaitForPodReadyByName for pod %q in %q namespace ...", time.Since(start), name, namespace) | ||
}() | ||
|
||
if namespace == "" { | ||
namespace = "kube-system" | ||
} | ||
|
||
lap := time.Now() | ||
checkReady := func() (bool, error) { | ||
if time.Since(start) > timeout { | ||
return false, fmt.Errorf("wait for pod %q in %q namespace to be Ready timed out", name, namespace) | ||
} | ||
pod, err := cs.CoreV1().Pods(namespace).Get(name, meta.GetOptions{}) | ||
if err != nil { | ||
klog.Infof("error getting pod %q in %q namespace, will retry: %v", name, namespace, err) | ||
return false, nil | ||
} | ||
ready, reason := IsPodReady(pod) | ||
if ready { | ||
klog.Info(reason) | ||
return true, nil | ||
} | ||
// reduce log spam | ||
if time.Since(lap) > (1 * time.Second) { | ||
klog.Info(reason) | ||
lap = time.Now() | ||
} | ||
return false, nil | ||
} | ||
if err := wait.PollImmediate(kconst.APICallRetryInterval, kconst.DefaultControlPlaneTimeout, checkReady); err != nil { | ||
return errors.Wrapf(err, "wait pod Ready") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IsPodReady returns if pod is Ready and verbose reason. | ||
func IsPodReady(pod *core.Pod) (ready bool, reason string) { | ||
if pod.Status.Phase != core.PodRunning { | ||
return false, fmt.Sprintf("pod %q in %q namespace is not Running: %+v", pod.Name, pod.Namespace, pod.Status) | ||
} | ||
for _, c := range pod.Status.Conditions { | ||
if c.Type == core.PodReady { | ||
if c.Status != core.ConditionTrue { | ||
return false, fmt.Sprintf("pod %q in %q namespace is not Ready: %+v", pod.Name, pod.Namespace, c) | ||
} | ||
return true, fmt.Sprintf("pod %q in %q namespace is Ready: %+v", pod.Name, pod.Namespace, c) | ||
} | ||
} | ||
return false, fmt.Sprintf("pod %q in %q namespace does not have %q status: %+v", pod.Name, pod.Namespace, core.PodReady, pod.Status) | ||
} |
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.
this func is not used outside, make private.
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.
done!