|
| 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 acceptance |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/rand" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | + |
| 26 | + "k8s.io/client-go/rest" |
| 27 | + |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/cluster" |
| 29 | + |
| 30 | + mcmanager "sigs.k8s.io/multicluster-runtime/pkg/manager" |
| 31 | +) |
| 32 | + |
| 33 | +// ClusterGenerator is a function that generates a new cluster. |
| 34 | +// The cluster is expected to be available through the provider. |
| 35 | +// The return values are the cluster name, the rest.Config to access the |
| 36 | +// cluster, and an error if the cluster could not be created. |
| 37 | +// |
| 38 | +// The context is cancelled when the cluster is to be removed. |
| 39 | +// |
| 40 | +// The ErrorHandler can be used to report errors from goroutines started |
| 41 | +// by the generator. |
| 42 | +type ClusterGenerator func(context.Context, ErrorHandler) (string, *rest.Config, error) |
| 43 | + |
| 44 | +// UnknownClusterName is a random cluster name used to test the |
| 45 | +// return of a correct error for non-existing clusters. |
| 46 | +// Providers may use this in their test to verify their generated |
| 47 | +// names do not accidentally collide with this name. |
| 48 | +var UnknownClusterName = rand.Text() |
| 49 | + |
| 50 | +// RandomClusterName generates a random cluster name that is not |
| 51 | +// UnknownClusterName. |
| 52 | +func RandomClusterName() string { |
| 53 | + name := rand.Text() |
| 54 | + if name == UnknownClusterName { |
| 55 | + return RandomClusterName() |
| 56 | + } |
| 57 | + return name |
| 58 | +} |
| 59 | + |
| 60 | +func createCluster(t testing.TB, clusterGenerator ClusterGenerator) (string, *rest.Config) { |
| 61 | + t.Helper() |
| 62 | + clusterName, clusterCfg, err := clusterGenerator(t.Context(), errorHandler(t, "cluster generator")) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Failed to create cluster: %v", err) |
| 65 | + } |
| 66 | + return clusterName, clusterCfg |
| 67 | +} |
| 68 | + |
| 69 | +func getCluster(t testing.TB, manager mcmanager.Manager, clusterName string, clusterCfg *rest.Config) cluster.Cluster { |
| 70 | + t.Helper() |
| 71 | + t.Logf("Retrieving cluster %q", clusterName) |
| 72 | + |
| 73 | + var cl cluster.Cluster |
| 74 | + eventually(t, func() error { |
| 75 | + var err error |
| 76 | + cl, err = manager.GetCluster(t.Context(), clusterName) |
| 77 | + return err |
| 78 | + }, WaitTimeout, PollInterval, "cluster %q not found", clusterName) |
| 79 | + |
| 80 | + cfg := rest.CopyConfig(cl.GetConfig()) |
| 81 | + // These values are not persisted in the kubeconfig |
| 82 | + cfg.QPS = clusterCfg.QPS |
| 83 | + cfg.Burst = clusterCfg.Burst |
| 84 | + |
| 85 | + if diff := cmp.Diff(clusterCfg, cfg); diff != "" { |
| 86 | + t.Errorf("Cluster config mismatch: %s", diff) |
| 87 | + } |
| 88 | + |
| 89 | + return cl |
| 90 | +} |
0 commit comments