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"
|
|
|
|
"fmt"
|
2022-01-21 09:32:34 +00:00
|
|
|
"os"
|
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"
|
2023-02-01 17:06:36 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2020-09-03 09:34:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createNamespace(c kubernetes.Interface, name string) error {
|
|
|
|
timeout := time.Duration(deployTimeout) * time.Minute
|
|
|
|
ns := &v1.Namespace{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
}
|
2023-06-05 14:41:04 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
_, err := c.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil && !apierrs.IsAlreadyExists(err) {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to create namespace: %w", err)
|
2020-09-03 09:34:29 +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().Namespaces().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 namespace: '%s': %v", name, err)
|
2020-09-03 09:34:29 +00:00
|
|
|
if apierrs.IsNotFound(err) {
|
|
|
|
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
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteNamespace(c kubernetes.Interface, name string) error {
|
|
|
|
timeout := time.Duration(deployTimeout) * time.Minute
|
2023-06-05 14:41:04 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
err := c.CoreV1().Namespaces().Delete(ctx, name, metav1.DeleteOptions{})
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil && !apierrs.IsNotFound(err) {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to delete namespace: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
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().Namespaces().Get(ctx, name, metav1.GetOptions{})
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
if apierrs.IsNotFound(err) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Error getting namespace: '%s': %v", name, err)
|
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
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func replaceNamespaceInTemplate(filePath string) (string, error) {
|
2022-01-21 09:32:34 +00:00
|
|
|
read, err := os.ReadFile(filePath)
|
2020-09-03 09:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2024-03-14 13:40:24 +00:00
|
|
|
// template can contain "default" as namespace, with or without ".
|
|
|
|
templ := strings.ReplaceAll(string(read), "namespace: default", fmt.Sprintf("namespace: %s", cephCSINamespace))
|
|
|
|
templ = strings.ReplaceAll(templ, "namespace: \"default\"", fmt.Sprintf("namespace: %s", cephCSINamespace))
|
|
|
|
|
|
|
|
return templ, nil
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|