|
| 1 | +// Copyright 2024 Red Hat, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/onsi/ginkgo/v2" |
| 27 | + "github.com/onsi/gomega" |
| 28 | + corev1 "k8s.io/api/core/v1" |
| 29 | + kclientset "k8s.io/client-go/kubernetes" |
| 30 | + "k8s.io/client-go/tools/clientcmd" |
| 31 | + "k8s.io/kubernetes/openshift-hack/e2e" |
| 32 | + conformancetestdata "k8s.io/kubernetes/test/conformance/testdata" |
| 33 | + "k8s.io/kubernetes/test/e2e/framework" |
| 34 | + "k8s.io/kubernetes/test/e2e/framework/testfiles" |
| 35 | + e2etestingmanifests "k8s.io/kubernetes/test/e2e/testing-manifests" |
| 36 | + testfixtures "k8s.io/kubernetes/test/fixtures" |
| 37 | + |
| 38 | + // this appears to inexplicably auto-register global flags. |
| 39 | + _ "k8s.io/kubernetes/test/e2e/storage/drivers" |
| 40 | + |
| 41 | + // these are loading important global flags that we need to get and set. |
| 42 | + _ "k8s.io/kubernetes/test/e2e" |
| 43 | + _ "k8s.io/kubernetes/test/e2e/lifecycle" |
| 44 | +) |
| 45 | + |
| 46 | +var ( |
| 47 | + errProviderJSONInvalid = errors.New("provider must be a JSON object with the 'type' key at a minimum") |
| 48 | + errProviderMissingType = errors.New("provider must be a JSON object with the 'type' key") |
| 49 | + errProviderConfigInvalid = errors.New("provider must decode into the ClusterConfig object") |
| 50 | + errUnableToSetArtifactDir = errors.New("unable to set ARTIFACT_DIR") |
| 51 | + errClientConfigInvalid = errors.New("failed to create client config") |
| 52 | +) |
| 53 | + |
| 54 | +// copied directly from github.com/openshift/kubernetes/openshift-hack/cmd/k8s-tests-ext/provider.go |
| 55 | +// I attempted to use the clusterdiscovery.InitializeTestFramework in origin but it has too many additional parameters |
| 56 | +// that as an test-ext, I felt we shouldn't have to load all that. Hopefully origin's test-ext frameworks gets enhanced |
| 57 | +// to have a simple way to initialize all this w/o having to copy/pasta like the openshift/kubernetes project did. |
| 58 | +func initializeTestFramework(provider string) error { |
| 59 | + config, err := parseProviderConfig(provider) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + if err := setupTestContext(config); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + if err := setupKubernetesClient(); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + setupFrameworkDefaults() |
| 73 | + setupIPFamily(config) |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func parseProviderConfig(provider string) (*ClusterConfiguration, error) { |
| 79 | + providerInfo := &ClusterConfiguration{} |
| 80 | + if err := json.Unmarshal([]byte(provider), &providerInfo); err != nil { |
| 81 | + return nil, fmt.Errorf("%w: %w", errProviderJSONInvalid, err) |
| 82 | + } |
| 83 | + |
| 84 | + if len(providerInfo.ProviderName) == 0 { |
| 85 | + return nil, errProviderMissingType |
| 86 | + } |
| 87 | + |
| 88 | + config := &ClusterConfiguration{} |
| 89 | + if err := json.Unmarshal([]byte(provider), config); err != nil { |
| 90 | + return nil, fmt.Errorf("%w: %w", errProviderConfigInvalid, err) |
| 91 | + } |
| 92 | + |
| 93 | + return config, nil |
| 94 | +} |
| 95 | + |
| 96 | +func setupTestContext(config *ClusterConfiguration) error { |
| 97 | + testContext := &framework.TestContext |
| 98 | + testContext.Provider = config.ProviderName |
| 99 | + testContext.CloudConfig = framework.CloudConfig{ |
| 100 | + ProjectID: config.ProjectID, |
| 101 | + Region: config.Region, |
| 102 | + Zone: config.Zone, |
| 103 | + Zones: config.Zones, |
| 104 | + NumNodes: config.NumNodes, |
| 105 | + MultiMaster: config.MultiMaster, |
| 106 | + MultiZone: config.MultiZone, |
| 107 | + ConfigFile: config.ConfigFile, |
| 108 | + } |
| 109 | + testContext.AllowedNotReadyNodes = -1 |
| 110 | + testContext.MinStartupPods = -1 |
| 111 | + testContext.MaxNodesToGather = 0 |
| 112 | + testContext.KubeConfig = os.Getenv("KUBECONFIG") |
| 113 | + |
| 114 | + if ad := os.Getenv("ARTIFACT_DIR"); len(strings.TrimSpace(ad)) == 0 { |
| 115 | + if err := os.Setenv("ARTIFACT_DIR", filepath.Join(os.TempDir(), "artifacts")); err != nil { |
| 116 | + return fmt.Errorf("%w: %w", errUnableToSetArtifactDir, err) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + testContext.DeleteNamespace = os.Getenv("DELETE_NAMESPACE") != "false" |
| 121 | + testContext.VerifyServiceAccount = true |
| 122 | + |
| 123 | + setupFileSources() |
| 124 | + |
| 125 | + testContext.KubectlPath = "kubectl" |
| 126 | + testContext.KubeConfig = os.Getenv("KUBECONFIG") |
| 127 | + testContext.NodeOSDistro = "custom" |
| 128 | + testContext.MasterOSDistro = "custom" |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func setupFileSources() { |
| 134 | + testfiles.AddFileSource(e2etestingmanifests.GetE2ETestingManifestsFS()) |
| 135 | + testfiles.AddFileSource(testfixtures.GetTestFixturesFS()) |
| 136 | + testfiles.AddFileSource(conformancetestdata.GetConformanceTestdataFS()) |
| 137 | +} |
| 138 | + |
| 139 | +func setupKubernetesClient() error { |
| 140 | + testContext := &framework.TestContext |
| 141 | + clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&clientcmd.ClientConfigLoadingRules{ExplicitPath: testContext.KubeConfig}, &clientcmd.ConfigOverrides{}) |
| 142 | + |
| 143 | + cfg, err := clientConfig.ClientConfig() |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("%w: %w", errClientConfigInvalid, err) |
| 146 | + } |
| 147 | + |
| 148 | + testContext.Host = cfg.Host |
| 149 | + testContext.CreateTestingNS = func(ctx context.Context, baseName string, c kclientset.Interface, labels map[string]string) (*corev1.Namespace, error) { |
| 150 | + return e2e.CreateTestingNS(ctx, baseName, c, labels, true) |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func setupFrameworkDefaults() { |
| 157 | + testContext := &framework.TestContext |
| 158 | + |
| 159 | + gomega.RegisterFailHandler(ginkgo.Fail) |
| 160 | + framework.AfterReadingAllFlags(testContext) |
| 161 | + testContext.DumpLogsOnFailure = true |
| 162 | + testContext.ReportDir = os.Getenv("TEST_JUNIT_DIR") |
| 163 | +} |
| 164 | + |
| 165 | +func setupIPFamily(config *ClusterConfiguration) { |
| 166 | + testContext := &framework.TestContext |
| 167 | + testContext.IPFamily = "ipv4" |
| 168 | + |
| 169 | + if config.HasIPv6 && !config.HasIPv4 { |
| 170 | + testContext.IPFamily = "ipv6" |
| 171 | + } |
| 172 | +} |
0 commit comments