|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package file |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + |
| 25 | + "k8s.io/client-go/rest" |
| 26 | + "k8s.io/client-go/tools/clientcmd" |
| 27 | + "k8s.io/client-go/tools/clientcmd/api" |
| 28 | + |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 30 | + |
| 31 | + mcacceptance "sigs.k8s.io/multicluster-runtime/pkg/acceptance" |
| 32 | + mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" |
| 33 | + "sigs.k8s.io/multicluster-runtime/pkg/multicluster" |
| 34 | + |
| 35 | + . "github.com/onsi/ginkgo/v2" |
| 36 | + . "github.com/onsi/gomega" |
| 37 | +) |
| 38 | + |
| 39 | +var _ = Describe("Provider File Acceptance", Ordered, func() { |
| 40 | + var discoverDir string |
| 41 | + var provider multicluster.Provider |
| 42 | + var manager mcmanager.Manager |
| 43 | + var generateCluster mcacceptance.ClusterGenerator |
| 44 | + |
| 45 | + BeforeAll(func() { |
| 46 | + discoverDir = GinkgoT().TempDir() |
| 47 | + |
| 48 | + By("Creating a temporary directory for kubeconfig files", func() { |
| 49 | + err := os.MkdirAll(discoverDir, 0755) |
| 50 | + Expect(err).NotTo(HaveOccurred(), "Failed to create kubeconfig file") |
| 51 | + }) |
| 52 | + |
| 53 | + By("Creating a new provider", func() { |
| 54 | + var err error |
| 55 | + provider, err = New(Options{ |
| 56 | + KubeconfigDirs: []string{discoverDir}, |
| 57 | + }) |
| 58 | + Expect(err).NotTo(HaveOccurred()) |
| 59 | + }) |
| 60 | + |
| 61 | + By("Creating a new manager", func() { |
| 62 | + var err error |
| 63 | + manager, err = mcmanager.New(localCfg, provider, mcmanager.Options{}) |
| 64 | + Expect(err).NotTo(HaveOccurred(), "Failed to create manager") |
| 65 | + }) |
| 66 | + |
| 67 | + By("Defining the ClusterGenerator", func() { |
| 68 | + generateCluster = func(ctx context.Context, errHandler mcacceptance.ErrorHandler) (string, *rest.Config, error) { |
| 69 | + testenv := &envtest.Environment{} |
| 70 | + cfg, err := testenv.Start() |
| 71 | + if err != nil { |
| 72 | + return "", nil, fmt.Errorf("failed to start envtest: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + name := mcacceptance.RandomClusterName() |
| 76 | + kubeconfigPath := filepath.Join(discoverDir, name+".kubeconfig") |
| 77 | + kubeconfig := restToKubeconfig(cfg) |
| 78 | + if err := clientcmd.WriteToFile(*kubeconfig, kubeconfigPath); err != nil { |
| 79 | + return "", nil, fmt.Errorf("failed to write kubeconfig file: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + go func() { |
| 83 | + <-ctx.Done() |
| 84 | + errHandler(os.Remove(kubeconfigPath)) |
| 85 | + errHandler(testenv.Stop()) |
| 86 | + }() |
| 87 | + return kubeconfigPath + "+default-context", cfg, nil |
| 88 | + } |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + It("Should run the acceptance tests", func() { |
| 93 | + mcacceptance.Provider(GinkgoTB(), generateCluster, manager) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +func restToKubeconfig(cfg *rest.Config) *api.Config { |
| 98 | + cluster := api.NewCluster() |
| 99 | + |
| 100 | + cluster.Server = cfg.Host |
| 101 | + cluster.CertificateAuthorityData = cfg.TLSClientConfig.CAData |
| 102 | + cluster.InsecureSkipTLSVerify = cfg.TLSClientConfig.Insecure |
| 103 | + |
| 104 | + authInfo := api.NewAuthInfo() |
| 105 | + authInfo.ClientCertificateData = cfg.TLSClientConfig.CertData |
| 106 | + authInfo.ClientKeyData = cfg.TLSClientConfig.KeyData |
| 107 | + authInfo.Token = cfg.BearerToken |
| 108 | + authInfo.Username = cfg.Username |
| 109 | + authInfo.Password = cfg.Password |
| 110 | + authInfo.AuthProvider = cfg.AuthProvider |
| 111 | + authInfo.Exec = cfg.ExecProvider |
| 112 | + |
| 113 | + context := api.NewContext() |
| 114 | + context.Cluster = "default-cluster" |
| 115 | + context.AuthInfo = "default-user" |
| 116 | + context.Namespace = "default" |
| 117 | + |
| 118 | + config := api.NewConfig() |
| 119 | + config.Clusters["default-cluster"] = cluster |
| 120 | + config.AuthInfos["default-user"] = authInfo |
| 121 | + config.Contexts["default-context"] = context |
| 122 | + config.CurrentContext = "default-context" |
| 123 | + |
| 124 | + return config |
| 125 | +} |
0 commit comments