package framework import ( "context" "fmt" "github.com/onsi/ginkgo" "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/klog/v2" ) // CreateNamespace create Namespace. func CreateNamespace(client kubernetes.Interface, namespace *corev1.Namespace) { ginkgo.By(fmt.Sprintf("Creating Namespace(%s)", namespace.Name), func() { _, err := client.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{}) gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) }) } // RemoveNamespace delete Namespace. func RemoveNamespace(client kubernetes.Interface, name string) { ginkgo.By(fmt.Sprintf("Removing Namespace(%s)", name), func() { err := client.CoreV1().Namespaces().Delete(context.TODO(), name, metav1.DeleteOptions{}) gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) }) } // WaitNamespacePresentOnCluster wait namespace present on cluster until timeout. func WaitNamespacePresentOnCluster(cluster, name string) { clusterClient := GetClusterClient(cluster) gomega.Expect(clusterClient).ShouldNot(gomega.BeNil()) klog.Infof("Waiting for namespace present on cluster(%s)", cluster) gomega.Eventually(func(g gomega.Gomega) (bool, error) { _, err := clusterClient.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{}) g.Expect(err).NotTo(gomega.HaveOccurred()) return true, nil }, pollTimeout, pollInterval).Should(gomega.Equal(true)) } // WaitNamespacePresentOnClusters wait namespace present on clusters until timeout. func WaitNamespacePresentOnClusters(clusters []string, name string) { ginkgo.By(fmt.Sprintf("Check if namespace(%s) present on member clusters", name), func() { for _, clusterName := range clusters { WaitNamespacePresentOnCluster(clusterName, name) } }) } // WaitNamespaceDisappearOnCluster wait namespace disappear on cluster until timeout. func WaitNamespaceDisappearOnCluster(cluster, name string) { clusterClient := GetClusterClient(cluster) gomega.Expect(clusterClient).ShouldNot(gomega.BeNil()) klog.Infof("Waiting for namespace disappear on cluster(%s)", cluster) gomega.Eventually(func() bool { _, err := clusterClient.CoreV1().Namespaces().Get(context.TODO(), name, metav1.GetOptions{}) return apierrors.IsNotFound(err) }, pollTimeout, pollInterval).Should(gomega.Equal(true)) } // WaitNamespaceDisappearOnClusters wait namespace disappear on clusters until timeout. func WaitNamespaceDisappearOnClusters(clusters []string, name string) { ginkgo.By(fmt.Sprintf("Check if namespace(%s) disappear on member clusters", name), func() { for _, clusterName := range clusters { WaitNamespaceDisappearOnCluster(clusterName, name) } }) }