2021-12-21 14:23:26 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI 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.
|
|
|
|
*/
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-09-13 12:27:45 +00:00
|
|
|
"regexp"
|
2020-09-03 09:34:29 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
apierrs "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/kubernetes/pkg/client/conditions"
|
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2023-02-01 17:06:36 +00:00
|
|
|
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
|
2023-06-13 09:49:01 +00:00
|
|
|
frameworkPod "k8s.io/kubernetes/test/e2e/framework/pod"
|
2020-09-03 09:34:29 +00:00
|
|
|
)
|
|
|
|
|
2022-01-06 15:18:04 +00:00
|
|
|
const errRWOPConflict = "node has pod using PersistentVolumeClaim with the same name and ReadWriteOncePod access mode."
|
|
|
|
|
2021-03-18 13:27:52 +00:00
|
|
|
// getDaemonSetLabelSelector returns labels of daemonset given name and namespace dynamically,
|
|
|
|
// needed since labels are not same for helm and non-helm deployments.
|
|
|
|
func getDaemonSetLabelSelector(f *framework.Framework, ns, daemonSetName string) (string, error) {
|
|
|
|
ds, err := f.ClientSet.AppsV1().DaemonSets(ns).Get(context.TODO(), daemonSetName, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Error getting daemonsets with name %s in namespace %s", daemonSetName, ns)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-18 13:27:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
s, err := metav1.LabelSelectorAsSelector(ds.Spec.Selector)
|
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Error parsing %s daemonset selector in namespace %s", daemonSetName, ns)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-18 13:27:52 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("LabelSelector for %s daemonsets in namespace %s: %s", daemonSetName, ns, s.String())
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-18 13:27:52 +00:00
|
|
|
return s.String(), nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
func waitForDaemonSets(name, ns string, c kubernetes.Interface, t int) error {
|
|
|
|
timeout := time.Duration(t) * time.Minute
|
|
|
|
start := time.Now()
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Waiting up to %v for all daemonsets in namespace '%s' to start", timeout, ns)
|
2020-09-03 09:34:29 +00:00
|
|
|
|
2023-06-05 14:41:04 +00:00
|
|
|
return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) {
|
|
|
|
ds, err := c.AppsV1().DaemonSets(ns).Get(ctx, name, metav1.GetOptions{})
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Error getting daemonsets in namespace: '%s': %v", ns, err)
|
2020-09-03 09:34:29 +00:00
|
|
|
if strings.Contains(err.Error(), "not found") {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-12-17 13:00:02 +00:00
|
|
|
if isRetryableAPIError(err) {
|
2020-09-03 09:34:29 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
dNum := ds.Status.DesiredNumberScheduled
|
|
|
|
ready := ds.Status.NumberReady
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf(
|
2021-06-25 12:48:23 +00:00
|
|
|
"%d / %d pods ready in namespace '%s' in daemonset '%s' (%d seconds elapsed)",
|
|
|
|
ready,
|
|
|
|
dNum,
|
|
|
|
ns,
|
|
|
|
ds.ObjectMeta.Name,
|
|
|
|
int(time.Since(start).Seconds()))
|
2020-09-03 09:34:29 +00:00
|
|
|
if ready != dNum {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-05 10:27:35 +00:00
|
|
|
func findPodAndContainerName(f *framework.Framework, ns, cn string, opt *metav1.ListOptions) (string, string, error) {
|
2022-04-28 08:35:34 +00:00
|
|
|
timeout := time.Duration(deployTimeout) * time.Minute
|
|
|
|
|
|
|
|
var (
|
|
|
|
podList *v1.PodList
|
|
|
|
listErr error
|
|
|
|
)
|
2023-06-05 14:41:04 +00:00
|
|
|
err := wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) {
|
|
|
|
podList, listErr = e2epod.PodClientNS(f, ns).List(ctx, *opt)
|
2022-04-28 08:35:34 +00:00
|
|
|
if listErr != nil {
|
|
|
|
if isRetryableAPIError(listErr) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("failed to list Pods: %w", listErr)
|
|
|
|
}
|
2021-03-05 10:27:35 +00:00
|
|
|
|
2022-04-28 08:35:34 +00:00
|
|
|
if len(podList.Items) == 0 {
|
|
|
|
// retry in case the pods have not been (re)started yet
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", "", fmt.Errorf("failed to find pod for %v: %w", opt, err)
|
2021-03-05 10:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cn != "" {
|
|
|
|
for i := range podList.Items {
|
|
|
|
for j := range podList.Items[i].Spec.Containers {
|
|
|
|
if podList.Items[i].Spec.Containers[j].Name == cn {
|
|
|
|
return podList.Items[i].Name, cn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-05 10:27:35 +00:00
|
|
|
return "", "", errors.New("container name not found")
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-05 10:27:35 +00:00
|
|
|
return podList.Items[0].Name, podList.Items[0].Spec.Containers[0].Name, nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:48:23 +00:00
|
|
|
func getCommandInPodOpts(
|
|
|
|
f *framework.Framework,
|
|
|
|
c, ns, cn string,
|
2022-06-01 10:17:19 +00:00
|
|
|
opt *metav1.ListOptions,
|
2023-02-01 17:06:36 +00:00
|
|
|
) (e2epod.ExecOptions, error) {
|
2020-09-03 09:34:29 +00:00
|
|
|
cmd := []string{"/bin/sh", "-c", c}
|
2021-03-05 10:27:35 +00:00
|
|
|
pName, cName, err := findPodAndContainerName(f, ns, cn, opt)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
return e2epod.ExecOptions{}, err
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
return e2epod.ExecOptions{
|
2020-09-03 09:34:29 +00:00
|
|
|
Command: cmd,
|
2021-03-05 10:27:35 +00:00
|
|
|
PodName: pName,
|
2020-09-03 09:34:29 +00:00
|
|
|
Namespace: ns,
|
2021-03-05 10:27:35 +00:00
|
|
|
ContainerName: cName,
|
2020-09-03 09:34:29 +00:00
|
|
|
Stdin: nil,
|
|
|
|
CaptureStdout: true,
|
|
|
|
CaptureStderr: true,
|
|
|
|
PreserveWhitespace: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:29:18 +00:00
|
|
|
// execCommandInDaemonsetPod executes commands inside given container of a
|
|
|
|
// daemonset pod on a particular node.
|
|
|
|
//
|
|
|
|
// stderr is returned as a string, and err will be set on a failure.
|
2021-06-25 12:48:23 +00:00
|
|
|
func execCommandInDaemonsetPod(
|
|
|
|
f *framework.Framework,
|
2022-06-01 10:17:19 +00:00
|
|
|
c, daemonsetName, nodeName, containerName, ns string,
|
|
|
|
) (string, error) {
|
2023-06-13 09:49:01 +00:00
|
|
|
podName, err := getDaemonsetPodOnNode(f, daemonsetName, nodeName, ns)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := []string{"/bin/sh", "-c", c}
|
|
|
|
podOpt := e2epod.ExecOptions{
|
|
|
|
Command: cmd,
|
|
|
|
Namespace: ns,
|
|
|
|
PodName: podName,
|
|
|
|
ContainerName: containerName,
|
|
|
|
CaptureStdout: true,
|
|
|
|
CaptureStderr: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
_ /* stdout */, stderr, err := execWithRetry(f, &podOpt)
|
|
|
|
|
|
|
|
return stderr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDaemonsetPodOnNode returns the name of a daemonset pod on a particular node.
|
|
|
|
func getDaemonsetPodOnNode(f *framework.Framework, daemonsetName, nodeName, ns string) (string, error) {
|
2021-03-22 04:55:58 +00:00
|
|
|
selector, err := getDaemonSetLabelSelector(f, ns, daemonsetName)
|
|
|
|
if err != nil {
|
2022-04-25 08:29:18 +00:00
|
|
|
return "", err
|
2021-03-22 04:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opt := &metav1.ListOptions{
|
|
|
|
LabelSelector: selector,
|
|
|
|
}
|
|
|
|
pods, err := listPods(f, ns, opt)
|
|
|
|
if err != nil {
|
2022-04-25 08:29:18 +00:00
|
|
|
return "", err
|
2021-03-22 04:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
podName := ""
|
|
|
|
for i := range pods {
|
|
|
|
if pods[i].Spec.NodeName == nodeName {
|
|
|
|
podName = pods[i].Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if podName == "" {
|
2022-04-25 08:29:18 +00:00
|
|
|
return "", fmt.Errorf("%s daemonset pod on node %s in namespace %s not found", daemonsetName, nodeName, ns)
|
2021-03-22 04:55:58 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 09:49:01 +00:00
|
|
|
return podName, nil
|
2021-03-22 04:55:58 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 04:31:25 +00:00
|
|
|
// listPods returns slice of pods matching given ListOptions and namespace.
|
|
|
|
func listPods(f *framework.Framework, ns string, opt *metav1.ListOptions) ([]v1.Pod, error) {
|
2023-02-01 17:06:36 +00:00
|
|
|
podList, err := e2epod.PodClientNS(f, ns).List(context.TODO(), *opt)
|
2021-03-22 04:31:25 +00:00
|
|
|
if len(podList.Items) == 0 {
|
|
|
|
return podList.Items, fmt.Errorf("podlist for label '%s' in namespace %s is empty", opt.LabelSelector, ns)
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-22 04:31:25 +00:00
|
|
|
return podList.Items, err
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
func execWithRetry(f *framework.Framework, opts *e2epod.ExecOptions) (string, string, error) {
|
2022-07-26 13:28:59 +00:00
|
|
|
timeout := time.Duration(deployTimeout) * time.Minute
|
|
|
|
var stdOut, stdErr string
|
2023-06-05 12:01:31 +00:00
|
|
|
err := wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) {
|
2022-07-26 13:28:59 +00:00
|
|
|
var execErr error
|
2023-02-01 17:06:36 +00:00
|
|
|
stdOut, stdErr, execErr = e2epod.ExecWithOptions(f, *opts)
|
2022-07-26 13:28:59 +00:00
|
|
|
if execErr != nil {
|
|
|
|
if isRetryableAPIError(execErr) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("failed to execute command: %v", execErr)
|
2022-07-26 13:28:59 +00:00
|
|
|
|
|
|
|
return false, fmt.Errorf("failed to execute command: %w", execErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return stdOut, stdErr, err
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
func execCommandInPod(f *framework.Framework, c, ns string, opt *metav1.ListOptions) (string, string, error) {
|
2021-03-05 10:27:35 +00:00
|
|
|
podOpt, err := getCommandInPodOpts(f, c, ns, "", opt)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2022-07-26 13:28:59 +00:00
|
|
|
|
|
|
|
stdOut, stdErr, err := execWithRetry(f, &podOpt)
|
2021-03-05 10:27:35 +00:00
|
|
|
if stdErr != "" {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("stdErr occurred: %v", stdErr)
|
2021-03-05 10:27:35 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-03-05 10:27:35 +00:00
|
|
|
return stdOut, stdErr, err
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:48:23 +00:00
|
|
|
func execCommandInContainer(
|
2022-06-01 10:17:19 +00:00
|
|
|
f *framework.Framework, c, ns, cn string, opt *metav1.ListOptions,
|
|
|
|
) (string, string, error) {
|
2021-03-05 10:27:35 +00:00
|
|
|
podOpt, err := getCommandInPodOpts(f, c, ns, cn, opt)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2022-07-26 13:28:59 +00:00
|
|
|
|
|
|
|
stdOut, stdErr, err := execWithRetry(f, &podOpt)
|
2020-09-03 09:34:29 +00:00
|
|
|
if stdErr != "" {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("stdErr occurred: %v", stdErr)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return stdOut, stdErr, err
|
|
|
|
}
|
|
|
|
|
2022-02-15 14:27:40 +00:00
|
|
|
func execCommandInContainerByPodName(
|
|
|
|
f *framework.Framework, shellCmd, namespace, podName, containerName string,
|
|
|
|
) (string, string, error) {
|
|
|
|
cmd := []string{"/bin/sh", "-c", shellCmd}
|
2023-02-01 17:06:36 +00:00
|
|
|
execOpts := e2epod.ExecOptions{
|
2022-02-15 14:27:40 +00:00
|
|
|
Command: cmd,
|
|
|
|
PodName: podName,
|
|
|
|
Namespace: namespace,
|
|
|
|
ContainerName: containerName,
|
|
|
|
Stdin: nil,
|
|
|
|
CaptureStdout: true,
|
|
|
|
CaptureStderr: true,
|
|
|
|
PreserveWhitespace: true,
|
|
|
|
}
|
|
|
|
|
2022-07-26 13:28:59 +00:00
|
|
|
stdOut, stdErr, err := execWithRetry(f, &execOpts)
|
2022-02-15 14:27:40 +00:00
|
|
|
if stdErr != "" {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("stdErr occurred: %v", stdErr)
|
2022-02-15 14:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdOut, stdErr, err
|
|
|
|
}
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
func execCommandInToolBoxPod(f *framework.Framework, c, ns string) (string, string, error) {
|
|
|
|
opt := &metav1.ListOptions{
|
2021-03-18 10:01:16 +00:00
|
|
|
LabelSelector: rookToolBoxPodLabel,
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-03-05 10:27:35 +00:00
|
|
|
podOpt, err := getCommandInPodOpts(f, c, ns, "", opt)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2022-07-26 13:28:59 +00:00
|
|
|
|
|
|
|
stdOut, stdErr, err := execWithRetry(f, &podOpt)
|
2020-09-03 09:34:29 +00:00
|
|
|
if stdErr != "" {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("stdErr occurred: %v", stdErr)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return stdOut, stdErr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func execCommandInPodAndAllowFail(f *framework.Framework, c, ns string, opt *metav1.ListOptions) (string, string) {
|
2021-03-05 10:27:35 +00:00
|
|
|
podOpt, err := getCommandInPodOpts(f, c, ns, "", opt)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err.Error()
|
|
|
|
}
|
2022-07-26 13:28:59 +00:00
|
|
|
|
|
|
|
stdOut, stdErr, err := execWithRetry(f, &podOpt)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("command %s failed: %v", c, err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return stdOut, stdErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadApp(path string) (*v1.Pod, error) {
|
|
|
|
app := v1.Pod{}
|
2021-05-06 09:49:27 +00:00
|
|
|
if err := unmarshal(path, &app); err != nil {
|
2020-09-03 09:34:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-18 03:30:15 +00:00
|
|
|
for i := range app.Spec.Containers {
|
|
|
|
app.Spec.Containers[i].ImagePullPolicy = v1.PullIfNotPresent
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return &app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createApp(c kubernetes.Interface, app *v1.Pod, timeout int) error {
|
|
|
|
_, err := c.CoreV1().Pods(app.Namespace).Create(context.TODO(), app, metav1.CreateOptions{})
|
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to create app: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-15 10:08:51 +00:00
|
|
|
return waitForPodInRunningState(app.Name, app.Namespace, c, timeout, noError)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 10:08:51 +00:00
|
|
|
func createAppErr(c kubernetes.Interface, app *v1.Pod, timeout int, errString string) error {
|
|
|
|
_, err := c.CoreV1().Pods(app.Namespace).Create(context.TODO(), app, metav1.CreateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-15 10:08:51 +00:00
|
|
|
return waitForPodInRunningState(app.Name, app.Namespace, c, timeout, errString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForPodInRunningState(name, ns string, c kubernetes.Interface, t int, expectedError string) error {
|
2020-09-03 09:34:29 +00:00
|
|
|
timeout := time.Duration(t) * time.Minute
|
|
|
|
start := time.Now()
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Waiting up to %v to be in Running state", name)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-06-05 14:41:04 +00:00
|
|
|
return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(ctx context.Context) (bool, error) {
|
|
|
|
pod, err := c.CoreV1().Pods(ns).Get(ctx, name, metav1.GetOptions{})
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
2021-06-29 05:43:53 +00:00
|
|
|
if isRetryableAPIError(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-05-11 09:28:56 +00:00
|
|
|
return false, fmt.Errorf("failed to get app: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
switch pod.Status.Phase {
|
|
|
|
case v1.PodRunning:
|
|
|
|
return true, nil
|
|
|
|
case v1.PodFailed, v1.PodSucceeded:
|
|
|
|
return false, conditions.ErrPodCompleted
|
2021-06-15 10:08:51 +00:00
|
|
|
case v1.PodPending:
|
|
|
|
if expectedError != "" {
|
2023-06-05 14:41:04 +00:00
|
|
|
events, err := c.CoreV1().Events(ns).List(ctx, metav1.ListOptions{
|
2024-04-04 08:49:32 +00:00
|
|
|
FieldSelector: "involvedObject.name=" + name,
|
2021-06-15 10:08:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if strings.Contains(events.String(), expectedError) {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Expected Error %q found successfully", expectedError)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-15 10:08:51 +00:00
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-05 13:10:53 +00:00
|
|
|
case v1.PodUnknown:
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf(
|
2021-07-05 13:10:53 +00:00
|
|
|
"%s app is in %s phase expected to be in Running state (%d seconds elapsed)",
|
|
|
|
name,
|
|
|
|
pod.Status.Phase,
|
|
|
|
int(time.Since(start).Seconds()))
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func deletePod(name, ns string, c kubernetes.Interface, t int) error {
|
|
|
|
timeout := time.Duration(t) * time.Minute
|
2023-06-05 14:41:04 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
err := c.CoreV1().Pods(ns).Delete(ctx, name, metav1.DeleteOptions{})
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to delete app: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
start := time.Now()
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Waiting for pod %v to be deleted", name)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-06-05 14:41:04 +00:00
|
|
|
return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) {
|
|
|
|
_, err := c.CoreV1().Pods(ns).Get(ctx, name, metav1.GetOptions{})
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
2021-06-29 05:43:53 +00:00
|
|
|
if isRetryableAPIError(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if apierrs.IsNotFound(err) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("%s app to be deleted (%d seconds elapsed)", name, int(time.Since(start).Seconds()))
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-05-11 09:28:56 +00:00
|
|
|
return false, fmt.Errorf("failed to get app: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-02 08:59:52 +00:00
|
|
|
//nolint:unparam // currently skipNotFound is always false, this can change in the future
|
2021-06-25 12:48:23 +00:00
|
|
|
func deletePodWithLabel(label, ns string, skipNotFound bool) error {
|
2021-07-28 03:51:18 +00:00
|
|
|
err := retryKubectlArgs(
|
|
|
|
ns,
|
|
|
|
kubectlDelete,
|
|
|
|
deployTimeout,
|
|
|
|
"po",
|
|
|
|
"-l",
|
|
|
|
label,
|
|
|
|
fmt.Sprintf("--ignore-not-found=%t", skipNotFound))
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("failed to delete pod %v", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-09-30 06:38:09 +00:00
|
|
|
|
|
|
|
// calculateSHA512sum returns the sha512sum of a file inside a pod.
|
|
|
|
func calculateSHA512sum(f *framework.Framework, app *v1.Pod, filePath string, opt *metav1.ListOptions) (string, error) {
|
2024-04-04 08:49:32 +00:00
|
|
|
cmd := "sha512sum " + filePath
|
2020-09-30 06:38:09 +00:00
|
|
|
sha512sumOut, stdErr, err := execCommandInPod(f, cmd, app.Namespace, opt)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return "", fmt.Errorf("error: sha512sum could not be calculated %v", stdErr)
|
|
|
|
}
|
|
|
|
// extract checksum from sha512sum output.
|
|
|
|
checkSum := strings.Split(sha512sumOut, "")[0]
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Calculated checksum %s", checkSum)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-30 06:38:09 +00:00
|
|
|
return checkSum, nil
|
|
|
|
}
|
2021-08-24 08:30:24 +00:00
|
|
|
|
2022-04-06 12:05:18 +00:00
|
|
|
func appendToFileInContainer(
|
|
|
|
f *framework.Framework,
|
|
|
|
app *v1.Pod,
|
|
|
|
filePath,
|
|
|
|
toAppend string,
|
|
|
|
opt *metav1.ListOptions,
|
|
|
|
) error {
|
|
|
|
cmd := fmt.Sprintf("echo %q >> %s", toAppend, filePath)
|
|
|
|
_, stdErr, err := execCommandInPod(f, cmd, app.Namespace, opt)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not append to file %s: %w ; stderr: %s", filePath, err, stdErr)
|
|
|
|
}
|
|
|
|
if stdErr != "" {
|
|
|
|
return fmt.Errorf("could not append to file %s: %v", filePath, stdErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-24 08:30:24 +00:00
|
|
|
// getKernelVersionFromDaemonset gets the kernel version from the specified container.
|
|
|
|
func getKernelVersionFromDaemonset(f *framework.Framework, ns, dsn, cn string) (string, error) {
|
|
|
|
selector, err := getDaemonSetLabelSelector(f, ns, dsn)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := metav1.ListOptions{
|
|
|
|
LabelSelector: selector,
|
|
|
|
}
|
|
|
|
|
|
|
|
kernelRelease, stdErr, err := execCommandInContainer(f, "uname -r", ns, cn, &opt)
|
|
|
|
if err != nil || stdErr != "" {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return kernelRelease, nil
|
|
|
|
}
|
2022-01-04 10:41:53 +00:00
|
|
|
|
|
|
|
// recreateCSIPods delete the daemonset and deployment pods based on the selectors passed in.
|
|
|
|
func recreateCSIPods(f *framework.Framework, podLabels, daemonsetName, deploymentName string) error {
|
|
|
|
err := deletePodWithLabel(podLabels, cephCSINamespace, false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete pods with labels (%s): %w", podLabels, err)
|
|
|
|
}
|
|
|
|
// wait for csi pods to come up
|
|
|
|
err = waitForDaemonSets(daemonsetName, cephCSINamespace, f.ClientSet, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("timeout waiting for daemonset pods: %w", err)
|
|
|
|
}
|
|
|
|
err = waitForDeploymentComplete(f.ClientSet, deploymentName, cephCSINamespace, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("timeout waiting for deployment to be in running state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-06 15:18:04 +00:00
|
|
|
|
|
|
|
// validateRWOPPodCreation validates the second pod creation failure scenario with RWOP pvc.
|
|
|
|
func validateRWOPPodCreation(
|
|
|
|
f *framework.Framework,
|
|
|
|
pvc *v1.PersistentVolumeClaim,
|
|
|
|
app *v1.Pod,
|
2022-06-01 10:17:19 +00:00
|
|
|
baseAppName string,
|
|
|
|
) error {
|
2022-01-06 15:18:04 +00:00
|
|
|
var err error
|
|
|
|
// create one more app with same PVC
|
|
|
|
name := fmt.Sprintf("%s%d", f.UniqueName, deployTimeout)
|
|
|
|
app.Name = name
|
|
|
|
|
|
|
|
err = createAppErr(f.ClientSet, app, deployTimeout, errRWOPConflict)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("application should not go to running state due to RWOP access mode: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = deletePod(name, app.Namespace, f.ClientSet, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete application: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Name = baseAppName
|
|
|
|
err = deletePVCAndApp("", f, pvc, app)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete PVC and application: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-13 09:49:01 +00:00
|
|
|
|
|
|
|
// verifySeLinuxMountOption verifies the SeLinux context MountOption added to PV.Spec.MountOption
|
|
|
|
// is successfully used by nodeplugin during mounting by checking for its presence in the
|
|
|
|
// nodeplugin container logs.
|
|
|
|
func verifySeLinuxMountOption(
|
|
|
|
f *framework.Framework,
|
|
|
|
pvcPath, appPath, daemonSetName, cn, ns string,
|
|
|
|
) error {
|
|
|
|
mountOption := "context=\"system_u:object_r:container_file_t:s0:c0,c1\""
|
|
|
|
|
|
|
|
// create PVC
|
|
|
|
pvc, err := loadPVC(pvcPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load pvc: %w", err)
|
|
|
|
}
|
|
|
|
pvc.Namespace = f.UniqueName
|
|
|
|
err = createPVCAndvalidatePV(f.ClientSet, pvc, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create PVC: %w", err)
|
|
|
|
}
|
|
|
|
// modify PV spec.MountOptions
|
|
|
|
pv, err := getBoundPV(f.ClientSet, pvc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get PV: %w", err)
|
|
|
|
}
|
|
|
|
pv.Spec.MountOptions = []string{mountOption}
|
|
|
|
|
|
|
|
// update PV
|
|
|
|
_, err = f.ClientSet.CoreV1().PersistentVolumes().Update(context.TODO(), pv, metav1.UpdateOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update pv: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app, err := loadApp(appPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load application: %w", err)
|
|
|
|
}
|
|
|
|
app.Namespace = f.UniqueName
|
|
|
|
err = createApp(f.ClientSet, app, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create application: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pod, err := f.ClientSet.CoreV1().Pods(f.UniqueName).Get(context.TODO(), app.Name, metav1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
framework.Logf("Error occurred getting pod %s in namespace %s", app.Name, f.UniqueName)
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to get pod: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodepluginPodName, err := getDaemonsetPodOnNode(f, daemonSetName, pod.Spec.NodeName, ns)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get daemonset pod on node: %w", err)
|
|
|
|
}
|
|
|
|
logs, err := frameworkPod.GetPodLogs(context.TODO(), f.ClientSet, ns, nodepluginPodName, cn)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get pod logs from container %s/%s/%s : %w", ns, nodepluginPodName, cn, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(logs, mountOption) {
|
|
|
|
return fmt.Errorf("mount option %s not found in logs: %s", mountOption, logs)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = deletePVCAndApp("", f, pvc, app)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete PVC and application: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-13 12:27:45 +00:00
|
|
|
|
|
|
|
// verifyReadAffinity verifies if read affinity is enabled by checking if read_from_replica
|
|
|
|
// and crush_location options are present in the device config file (/sys/devices/rbd/0/config_info).
|
|
|
|
func verifyReadAffinity(
|
|
|
|
f *framework.Framework,
|
|
|
|
pvcPath, appPath, daemonSetName, cn, ns string,
|
|
|
|
) error {
|
|
|
|
readFromReplicaOption := "read_from_replica=localize"
|
|
|
|
expectedCrushLocationValues := map[string]string{
|
|
|
|
strings.Split(crushLocationRegionLabel, "/")[1]: crushLocationRegionValue,
|
|
|
|
strings.Split(crushLocationZoneLabel, "/")[1]: crushLocationZoneValue,
|
|
|
|
}
|
|
|
|
|
|
|
|
// create PVC
|
|
|
|
pvc, err := loadPVC(pvcPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load pvc: %w", err)
|
|
|
|
}
|
|
|
|
pvc.Namespace = f.UniqueName
|
|
|
|
err = createPVCAndvalidatePV(f.ClientSet, pvc, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create PVC: %w", err)
|
|
|
|
}
|
|
|
|
app, err := loadApp(appPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load application: %w", err)
|
|
|
|
}
|
|
|
|
app.Namespace = f.UniqueName
|
|
|
|
err = createApp(f.ClientSet, app, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create application: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
imageInfo, err := getImageInfoFromPVC(pvc.Namespace, pvc.Name, f)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get imageInfo: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
selector, err := getDaemonSetLabelSelector(f, ns, daemonSetName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get selector label %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := metav1.ListOptions{
|
|
|
|
LabelSelector: selector,
|
|
|
|
}
|
|
|
|
|
|
|
|
command := "cat /sys/devices/rbd/*/config_info"
|
|
|
|
configInfos, _, err := execCommandInContainer(f, command, ns, cn, &opt)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to execute command %s: %w", command, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var configInfo string
|
|
|
|
for _, config := range strings.Split(configInfos, "\n") {
|
|
|
|
if config == "" || !strings.Contains(config, imageInfo.imageName) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
configInfo = config
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if configInfo == "" {
|
|
|
|
return errors.New("failed to get config_info file")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(configInfo, readFromReplicaOption) {
|
|
|
|
return fmt.Errorf("option %s not found in config_info: %s", readFromReplicaOption, configInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
crushLocationPattern := "crush_location=([^,]+)"
|
|
|
|
regex := regexp.MustCompile(crushLocationPattern)
|
|
|
|
match := regex.FindString(configInfo)
|
|
|
|
if match == "" {
|
|
|
|
return fmt.Errorf("option crush_location not found in config_info: %s", configInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
crushLocationValue := strings.Split(match, "=")[1]
|
|
|
|
keyValues := strings.Split(crushLocationValue, "|")
|
|
|
|
actualCrushLocationValues := make(map[string]string)
|
|
|
|
|
|
|
|
for _, keyValue := range keyValues {
|
|
|
|
s := strings.Split(keyValue, ":")
|
|
|
|
actualCrushLocationValues[s[0]] = s[1]
|
|
|
|
}
|
|
|
|
for key, expectedValue := range expectedCrushLocationValues {
|
|
|
|
if actualValue, exists := actualCrushLocationValues[key]; !(exists && actualValue == expectedValue) {
|
|
|
|
return fmt.Errorf("crush location %s:%s not found in config_info : %s", key, expectedValue, configInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = deletePVCAndApp("", f, pvc, app)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete PVC and application: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|