|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + g "github.com/onsi/ginkgo/v2" |
| 8 | + configv1 "github.com/openshift/api/config/v1" |
| 9 | + clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned" |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/util/wait" |
| 14 | + "k8s.io/client-go/kubernetes" |
| 15 | + "k8s.io/client-go/rest" |
| 16 | + "k8s.io/client-go/tools/clientcmd" |
| 17 | +) |
| 18 | + |
| 19 | +// IsHypershift checks if running on a HyperShift hosted cluster |
| 20 | +// Refer to https://github.com/openshift/origin/blob/31704414237b8bd5c66ad247c105c94abc9470b1/test/extended/util/framework.go#L2301 |
| 21 | +func IsHypershift(ctx context.Context, restConfig *rest.Config) (bool, error) { |
| 22 | + configClient, err := GetConfigClient(restConfig) |
| 23 | + if err != nil { |
| 24 | + return false, err |
| 25 | + } |
| 26 | + infrastructure, err := configClient.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{}) |
| 27 | + if err != nil { |
| 28 | + // If the Infrastructure resource doesn't exist (e.g., in MicroShift),it's not a HyperShift |
| 29 | + if apierrors.IsNotFound(err) { |
| 30 | + return false, nil |
| 31 | + } |
| 32 | + return false, err |
| 33 | + } |
| 34 | + return infrastructure.Status.ControlPlaneTopology == configv1.ExternalTopologyMode, nil |
| 35 | +} |
| 36 | + |
| 37 | +// SkipIfHypershift skips the test if running on a HyperShift hosted cluster |
| 38 | +func SkipIfHypershift(ctx context.Context, restConfig *rest.Config) error { |
| 39 | + isHypershift, err := IsHypershift(ctx, restConfig) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + if isHypershift { |
| 44 | + g.Skip("Skipping test: running on HyperShift hosted cluster!") |
| 45 | + } |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// IsMicroshift checks if running on a MicroShift cluster |
| 50 | +// Refer to https://github.com/openshift/origin/blob/31704414237b8bd5c66ad247c105c94abc9470b1/test/extended/util/framework.go#L2312 |
| 51 | +func IsMicroshift(ctx context.Context, restConfig *rest.Config) (bool, error) { |
| 52 | + kubeClient, err := GetKubeClient(restConfig) |
| 53 | + if err != nil { |
| 54 | + return false, err |
| 55 | + } |
| 56 | + var cm *corev1.ConfigMap |
| 57 | + duration := 5 * time.Minute |
| 58 | + if err := wait.PollUntilContextTimeout(ctx, 10*time.Second, duration, true, func(ctx context.Context) (bool, error) { |
| 59 | + // MicroShift cluster contains "microshift-version" configmap in "kube-public" namespace |
| 60 | + var err error |
| 61 | + cm, err = kubeClient.CoreV1().ConfigMaps("kube-public").Get(ctx, "microshift-version", metav1.GetOptions{}) |
| 62 | + if err == nil { |
| 63 | + return true, nil |
| 64 | + } |
| 65 | + if apierrors.IsNotFound(err) { |
| 66 | + cm = nil |
| 67 | + return true, nil |
| 68 | + } |
| 69 | + g.GinkgoLogr.Info("error accessing microshift-version configmap", "error", err) |
| 70 | + return false, nil |
| 71 | + }); err != nil { |
| 72 | + g.GinkgoLogr.Info("failed to find microshift-version configmap while polling", "duration", duration, "error", err) |
| 73 | + return false, err |
| 74 | + } |
| 75 | + if cm == nil { |
| 76 | + g.GinkgoLogr.Info("microshift-version configmap not found") |
| 77 | + return false, nil |
| 78 | + } |
| 79 | + g.GinkgoLogr.Info("MicroShift cluster detected", "version", cm.Data["version"]) |
| 80 | + return true, nil |
| 81 | +} |
| 82 | + |
| 83 | +// SkipIfMicroshift skips the test if running on a MicroShift cluster |
| 84 | +func SkipIfMicroshift(ctx context.Context, restConfig *rest.Config) error { |
| 85 | + isMicroshift, err := IsMicroshift(ctx, restConfig) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + if isMicroshift { |
| 90 | + g.Skip("Skipping test: running on MicroShift cluster!") |
| 91 | + } |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// GetRestConfig loads the Kubernetes REST configuration from KUBECONFIG environment variable. |
| 96 | +func GetRestConfig() (*rest.Config, error) { |
| 97 | + return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig() |
| 98 | +} |
| 99 | + |
| 100 | +// GetKubeClient creates a Kubernetes client from the given REST config. |
| 101 | +func GetKubeClient(restConfig *rest.Config) (kubernetes.Interface, error) { |
| 102 | + return kubernetes.NewForConfig(restConfig) |
| 103 | +} |
| 104 | + |
| 105 | +// GetConfigClient creates an OpenShift config client from the given REST config. |
| 106 | +func GetConfigClient(restConfig *rest.Config) (clientconfigv1.Interface, error) { |
| 107 | + return clientconfigv1.NewForConfig(restConfig) |
| 108 | +} |
0 commit comments