From d67f42c0bb5ce88ec4a5f545fb821d0de932c7c7 Mon Sep 17 00:00:00 2001 From: Aniruddha Basak Date: Sun, 18 Aug 2024 14:55:20 +0200 Subject: [PATCH] Implement skip preflight flag Signed-off-by: Aniruddha Basak --- cmd/root.go | 4 +- pkg/conformance/deploy.go | 78 ++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index bac9020..bb62ef8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -41,6 +41,7 @@ var ( runListImages bool runConformance bool continueConformance bool + skipPreflight string conformanceFocus string ) @@ -74,6 +75,7 @@ func New() *cobra.Command { rootCmd.Flags().BoolVar(&runCleanup, "cleanup", false, "cleanup resources (pods, namespaces etc).") rootCmd.Flags().BoolVar(&runListImages, "list-images", false, "list all images that will be used during conformance tests.") rootCmd.Flags().BoolVar(&runConformance, "conformance", false, "run conformance tests.") + rootCmd.Flags().StringVar(&skipPreflight, "skip-preflight", "", "skip namespace check, use the specified namespace.") rootCmd.Flags().BoolVar(&continueConformance, "continue", false, "connect to an already running conformance test pod.") rootCmd.Flags().StringVar(&conformanceFocus, "focus", "", "focus runs a specific e2e test. e.g. - sig-auth. allows regular expressions.") @@ -147,7 +149,7 @@ func action(ctx context.Context, config *types.Configuration) error { if continueConformance { log.Println("Attempting to continue with already running tests...") } else { - if err := testRunner.Deploy(ctx, conformanceFocus, verboseGinkgo, config.StartupTimeout); err != nil { + if err := testRunner.Deploy(ctx, conformanceFocus, skipPreflight, verboseGinkgo, config.StartupTimeout); err != nil { return fmt.Errorf("failed to deploy tests: %w", err) } } diff --git a/pkg/conformance/deploy.go b/pkg/conformance/deploy.go index 2a57937..429f110 100644 --- a/pkg/conformance/deploy.go +++ b/pkg/conformance/deploy.go @@ -33,7 +33,7 @@ import ( ) // Deploy sets up the necessary resources and runs E2E conformance tests. -func (r *TestRunner) Deploy(ctx context.Context, focus string, verboseGinkgo bool, timeout time.Duration) error { +func (r *TestRunner) Deploy(ctx context.Context, focus, skipPreflight string, verboseGinkgo bool, timeout time.Duration) error { conformanceNS := corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: r.config.Namespace, @@ -195,46 +195,66 @@ func (r *TestRunner) Deploy(ctx context.Context, focus string, verboseGinkgo boo ns, err := r.clientset.CoreV1().Namespaces().Create(ctx, &conformanceNS, metav1.CreateOptions{}) if err != nil { if errors.IsAlreadyExists(err) { - //nolint:stylecheck // error message references a Kubernetes resource type. - err = fmt.Errorf("Namespace %s already exist, please run --cleanup first", conformanceNS.Name) + if skipPreflight != "" { + log.Printf("Using existing namespace: %s", r.config.Namespace) + } else { + //nolint:stylecheck // error message references a Kubernetes resource type. + return fmt.Errorf("namespace %s already exists, please run with --cleanup first", conformanceNS.Name) + } + } else { + return fmt.Errorf("failed to create namespace: %w", err) } - - return err + } else { + log.Printf("Created namespace %s.", ns.Name) } - log.Printf("Created Namespace %s.", ns.Name) - sa, err := r.clientset.CoreV1().ServiceAccounts(ns.Name).Create(ctx, &conformanceSA, metav1.CreateOptions{}) + sa, err := r.clientset.CoreV1().ServiceAccounts(r.config.Namespace).Create(ctx, &conformanceSA, metav1.CreateOptions{}) if err != nil { if errors.IsAlreadyExists(err) { - //nolint:stylecheck // error message references a Kubernetes resource type. - err = fmt.Errorf("ServiceAccount %s already exist, please run --cleanup first", conformanceSA.Name) + if skipPreflight != "" { + log.Printf("using existing ServiceAccount: %s/%s", r.config.Namespace, ServiceAccountName) + } else { + //nolint:stylecheck // error message references a Kubernetes resource type. + return fmt.Errorf("ServiceAccount %s already exist, please run --cleanup first", conformanceSA.Name) + } + } else { + return fmt.Errorf("failed to create ServiceAccount: %w", err) } - - return err + } else { + log.Printf("Created ServiceAccount %s.", sa.Name) } - log.Printf("Created ServiceAccount %s.", sa.Name) clusterRole, err := r.clientset.RbacV1().ClusterRoles().Create(ctx, &conformanceClusterRole, metav1.CreateOptions{}) if err != nil { if errors.IsAlreadyExists(err) { - //nolint:stylecheck // error message references a Kubernetes resource type. - err = fmt.Errorf("ClusterRole %s already exist, please run --cleanup first", conformanceClusterRole.Name) + if skipPreflight != "" { + log.Printf("using existing ClusterRole: %s/%s", r.config.Namespace, r.namespacedName(ClusterRoleName)) + } else { + //nolint:stylecheck // error message references a Kubernetes resource type. + return fmt.Errorf("ClusterRole %s already exist, please run --cleanup first", conformanceClusterRole.Name) + } + } else { + return fmt.Errorf("failed to create ClusterRole: %w", err) } - - return err + } else { + log.Printf("Created ClusterRole %s.", clusterRole.Name) } - log.Printf("Created Clusterrole %s.", clusterRole.Name) clusterRoleBinding, err := r.clientset.RbacV1().ClusterRoleBindings().Create(ctx, &conformanceClusterRoleBinding, metav1.CreateOptions{}) if err != nil { if errors.IsAlreadyExists(err) { - //nolint:stylecheck // error message references a Kubernetes resource type. - err = fmt.Errorf("ClusterRoleBinding %s already exist, please run --cleanup first", conformanceClusterRoleBinding.Name) + if skipPreflight != "" { + log.Printf("using existing ClusterRoleBinding: %s/%s", r.config.Namespace, r.namespacedName(ClusterRoleBindingName)) + } else { + //nolint:stylecheck // error message references a Kubernetes resource type. + return fmt.Errorf("ClusterRoleBinding %s already exist, please run --cleanup first", conformanceClusterRoleBinding.Name) + } + } else { + return fmt.Errorf("failed to create ClusterRoleBinding: %w", err) } - - return err + } else { + log.Printf("Created ClusterRoleBinding %s.", clusterRoleBinding.Name) } - log.Printf("Created ClusterRoleBinding %s.", clusterRoleBinding.Name) if filename := r.config.TestRepoList; filename != "" { repoListData, err := os.ReadFile(filename) @@ -298,11 +318,17 @@ func (r *TestRunner) Deploy(ctx context.Context, focus string, verboseGinkgo boo pod, err := common.CreatePod(ctx, r.clientset, &conformancePod, timeout) if err != nil { if errors.IsAlreadyExists(err) { - //nolint:stylecheck // error message references a Kubernetes resource type. - err = fmt.Errorf("Pod %s already exist, please run --cleanup first", pod.Name) + if skipPreflight != "" { + log.Printf("using existing Pod: %s/%s", r.config.Namespace, "e2e-conformance-test") + } else { + //nolint:stylecheck // error message references a Kubernetes resource type. + return fmt.Errorf("Pod %s already exist, please run --cleanup first", conformanceClusterRoleBinding.Name) + } + } else { + return fmt.Errorf("failed to create Pod: %w", err) } - - return err + } else { + log.Printf("Created ConformancePod %s.", pod.Name) } return nil