sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/kubernetes/namespace/namespace.go (about) 1 //go:build e2e 2 // +build e2e 3 4 /* 5 Copyright 2020 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package namespace 21 22 import ( 23 "context" 24 "log" 25 "time" 26 27 . "github.com/onsi/gomega" 28 corev1 "k8s.io/api/core/v1" 29 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 "k8s.io/client-go/kubernetes" 31 ) 32 33 const ( 34 namespaceOperationTimeout = 30 * time.Second 35 namespaceOperationSleepBetweenRetries = 3 * time.Second 36 ) 37 38 // Create a namespace with the given name 39 func Create(ctx context.Context, clientset *kubernetes.Clientset, name string, labels map[string]string) (*corev1.Namespace, error) { 40 ns := &corev1.Namespace{ 41 ObjectMeta: metav1.ObjectMeta{ 42 Name: name, 43 Labels: labels, 44 }, 45 } 46 47 var namespace *corev1.Namespace 48 Eventually(func(g Gomega) { 49 var err error 50 namespace, err = clientset.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{}) 51 if err != nil { 52 log.Printf("failed trying to create namespace (%s):%s\n", name, err.Error()) 53 } 54 g.Expect(err).NotTo(HaveOccurred()) 55 }, namespaceOperationTimeout, namespaceOperationSleepBetweenRetries).Should(Succeed()) 56 return namespace, nil 57 } 58 59 // CreateNamespaceDeleteIfExist creates a namespace, deletes it first if it already exists 60 func CreateNamespaceDeleteIfExist(ctx context.Context, clientset *kubernetes.Clientset, name string, labels map[string]string) (*corev1.Namespace, error) { 61 n, err := Get(ctx, clientset, name) 62 if err == nil { 63 // Delete existing namespace if exists to avoid dirty exit in last round of test 64 log.Printf("namespace %s already exist", n) 65 err := clientset.CoreV1().Namespaces().Delete(ctx, name, metav1.DeleteOptions{}) 66 if err != nil { 67 log.Printf("Error trying to delete namespace (%s):%s\n", name, err.Error()) 68 return nil, err 69 } 70 } 71 log.Printf("namespace %s does not exist, creating...", name) 72 return Create(ctx, clientset, name, labels) 73 } 74 75 // Get returns a namespace for with a given name 76 func Get(ctx context.Context, clientset *kubernetes.Clientset, name string) (*corev1.Namespace, error) { 77 opts := metav1.GetOptions{} 78 namespace, err := clientset.CoreV1().Namespaces().Get(ctx, name, opts) 79 if err != nil { 80 log.Printf("failed trying to get namespace (%s):%s\n", name, err.Error()) 81 return nil, err 82 } 83 84 return namespace, nil 85 }