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

Move Initialized() to e2e framework util #83812

Merged
merged 1 commit into from
Oct 17, 2019
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
22 changes: 0 additions & 22 deletions test/e2e/framework/pod/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,6 @@ func CountRemainingPods(c clientset.Interface, namespace string) (int, int, erro
return numPods, missingTimestamp, nil
}

// Initialized checks the state of all init containers in the pod.
func Initialized(pod *v1.Pod) (ok bool, failed bool, err error) {
allInit := true
initFailed := false
for _, s := range pod.Status.InitContainerStatuses {
switch {
case initFailed && s.State.Waiting == nil:
return allInit, initFailed, fmt.Errorf("container %s is after a failed container but isn't waiting", s.Name)
case allInit && s.State.Waiting == nil:
return allInit, initFailed, fmt.Errorf("container %s is after an initializing container but isn't waiting", s.Name)
case s.State.Terminated == nil:
allInit = false
case s.State.Terminated.ExitCode != 0:
allInit = false
initFailed = true
case !s.Ready:
return allInit, initFailed, fmt.Errorf("container %s initialized but isn't marked as ready", s.Name)
}
}
return allInit, initFailed, nil
}

func podRunning(c clientset.Interface, podName, namespace string) wait.ConditionFunc {
return func() (bool, error) {
pod, err := c.CoreV1().Pods(namespace).Get(podName, metav1.GetOptions{})
Expand Down
30 changes: 26 additions & 4 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,18 +675,18 @@ func ContainerInitInvariant(older, newer runtime.Object) error {
if err := initContainersInvariants(newPod); err != nil {
return err
}
oldInit, _, _ := e2epod.Initialized(oldPod)
newInit, _, _ := e2epod.Initialized(newPod)
oldInit, _, _ := initialized(oldPod)
newInit, _, _ := initialized(newPod)
Comment on lines -678 to +679
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anything, I think we should move this to the e2epod package in order to decouple this functionality from the framework.
The method that calls e2epodInitialized is containerInitInvariant().
This method is used a lot in conjunction with framework.CheckInvariants().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess for future reviews: it is ok to move a method back into the framework if it is only used by the framework (?)
/hold cancel

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think if only consumed by framework it's fine to bring it in

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we should be careful cause it becomes really easy for someone else to export this function later and call it from somewhere else.

if oldInit && !newInit {
// TODO: we may in the future enable resetting Initialized = false if the kubelet needs to restart it
// TODO: we may in the future enable resetting initialized = false if the kubelet needs to restart it
// from scratch
return fmt.Errorf("pod cannot be initialized and then regress to not being initialized")
}
return nil
}

func initContainersInvariants(pod *v1.Pod) error {
allInit, initFailed, err := e2epod.Initialized(pod)
allInit, initFailed, err := initialized(pod)
Comment on lines 688 to +689
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initContainersInvariants should also be refactored along with ContainerInitInvariants() as it is only used by that function.

if err != nil {
return err
}
Expand Down Expand Up @@ -2837,3 +2837,25 @@ func PrettyPrintJSON(metrics interface{}) string {
}
return string(formatted.Bytes())
}

// initialized checks the state of all init containers in the pod.
func initialized(pod *v1.Pod) (ok bool, failed bool, err error) {
allInit := true
initFailed := false
for _, s := range pod.Status.InitContainerStatuses {
switch {
case initFailed && s.State.Waiting == nil:
return allInit, initFailed, fmt.Errorf("container %s is after a failed container but isn't waiting", s.Name)
case allInit && s.State.Waiting == nil:
return allInit, initFailed, fmt.Errorf("container %s is after an initializing container but isn't waiting", s.Name)
case s.State.Terminated == nil:
allInit = false
case s.State.Terminated.ExitCode != 0:
allInit = false
initFailed = true
case !s.Ready:
return allInit, initFailed, fmt.Errorf("container %s initialized but isn't marked as ready", s.Name)
}
}
return allInit, initFailed, nil
}