From b66b3f4e5eecfd8e370c2b06fcad6986a8b8981f Mon Sep 17 00:00:00 2001 From: Moritz Clasmeier Date: Wed, 6 May 2026 20:54:20 +0200 Subject: [PATCH] Differentiate between OpenShift4-on-infra and generic OpenShift4 --- cmd/deploy.go | 2 +- internal/deployer/operator.go | 4 ++-- internal/env/env.go | 36 ++++++++++++++++++++-------- internal/env/env_integration_test.go | 2 +- internal/env/env_test.go | 26 +++++++++++--------- 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/cmd/deploy.go b/cmd/deploy.go index f3ea869..a5a6131 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -111,7 +111,7 @@ func runDeploy(cmd *cobra.Command, args []string) error { return errors.New("cannot use both --olm and --konflux flags together (not currently implemented)") } clusterType := env.GetCurrentClusterType() - if clusterType != env.InfraOpenShift4 { + if !clusterType.IsOpenShift() { return fmt.Errorf("--konflux flag is only supported on OpenShift 4 clusters (current cluster type: %s)", clusterType.String()) } } diff --git a/internal/deployer/operator.go b/internal/deployer/operator.go index e66221e..916a9a5 100644 --- a/internal/deployer/operator.go +++ b/internal/deployer/operator.go @@ -201,7 +201,7 @@ func (d *Deployer) getOperatorBundleImage() string { // ensureKonfluxImageRewriting configures image rewriting for Konflux images func (d *Deployer) ensureKonfluxImageRewriting(ctx context.Context) error { - if env.GetCurrentClusterType() != env.InfraOpenShift4 { + if !env.GetCurrentClusterType().IsOpenShift() { return errors.New("image rewriting for Konflux is only supported on OpenShift4 clusters") } @@ -289,7 +289,7 @@ func (d *Deployer) applyImageContentSourcePolicy(ctx context.Context) error { // removeKonfluxImageRewriting removes the ImageContentSourcePolicy for Konflux images if it exists func (d *Deployer) removeKonfluxImageRewriting(ctx context.Context) error { - if env.GetCurrentClusterType() != env.InfraOpenShift4 { + if !env.GetCurrentClusterType().IsOpenShift() { return nil } diff --git a/internal/env/env.go b/internal/env/env.go index d3ae4fa..6b125b1 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -33,6 +33,8 @@ const ( InfraGKE // InfraOpenShift4 represents an OpenShift 4 cluster InfraOpenShift4 + // Generic OpenShift4 cluster (e.g. for prow CI) + OpenShift4 // LocalKind represents a Kind (Kubernetes in Docker) cluster LocalKind ) @@ -115,6 +117,8 @@ func (ct ClusterType) String() string { case InfraGKE: return "GKE" case InfraOpenShift4: + return "OpenShift4 (infra)" + case OpenShift4: return "OpenShift4" case LocalKind: return "Kind" @@ -123,6 +127,10 @@ func (ct ClusterType) String() string { } } +func (ct ClusterType) IsOpenShift() bool { + return ct == InfraOpenShift4 || ct == OpenShift4 +} + // KubeConfig represents a simplified kubectl configuration type KubeConfig struct { CurrentContext string @@ -183,17 +191,12 @@ func detectClusterType(config KubeConfig, apiResources []string) ClusterType { return InfraGKE } - // Check for OpenShift 4 clusters by examining the server hostname - if serverURL := getServerURL(config); serverURL != "" { - if parsedURL, err := url.Parse(serverURL); err == nil { - hostname := parsedURL.Hostname() - if strings.HasSuffix(hostname, ".ocp.infra.rox.systems") { - // Further verify it's OpenShift 4 by checking the API resources - if isOpenShift4(apiResources) { - return InfraOpenShift4 - } - } + // Check for OpenShift 4 clusters. + if isOpenShift4(apiResources) { + if isInfraOpenShift4(config) { + return InfraOpenShift4 } + return OpenShift4 } // Check for Kind clusters @@ -205,6 +208,19 @@ func detectClusterType(config KubeConfig, apiResources []string) ClusterType { return ClusterTypeUnknown } +func isInfraOpenShift4(config KubeConfig) bool { + serverURL := getServerURL(config) + if serverURL == "" { + return false + } + parsedURL, err := url.Parse(serverURL) + if err != nil { + return false + } + hostname := parsedURL.Hostname() + return strings.HasSuffix(hostname, ".ocp.infra.rox.systems") +} + // getServerURL retrieves the server URL from the KubeConfig func getServerURL(config KubeConfig) string { if len(config.Clusters) == 0 { diff --git a/internal/env/env_integration_test.go b/internal/env/env_integration_test.go index f583eb8..e07000f 100644 --- a/internal/env/env_integration_test.go +++ b/internal/env/env_integration_test.go @@ -19,7 +19,7 @@ func TestDetectClusterType_Integration(t *testing.T) { t.Logf("Detected cluster type: %s", clusterType) // The cluster type should never be invalid (even if Unknown) - validTypes := []ClusterType{ClusterTypeUnknown, InfraGKE, InfraOpenShift4, LocalKind} + validTypes := []ClusterType{ClusterTypeUnknown, InfraGKE, InfraOpenShift4, OpenShift4, LocalKind} found := false for _, valid := range validTypes { if clusterType == valid { diff --git a/internal/env/env_test.go b/internal/env/env_test.go index 149825a..abb0a7c 100644 --- a/internal/env/env_test.go +++ b/internal/env/env_test.go @@ -2,6 +2,8 @@ package env import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestDetectClusterType_GKE(t *testing.T) { @@ -40,7 +42,7 @@ func TestDetectClusterType_GKE_ExactMatch(t *testing.T) { } } -func TestDetectClusterType_OpenShift4(t *testing.T) { +func TestDetectClusterType_InfraOpenShift4(t *testing.T) { config := KubeConfig{ CurrentContext: "admin", Clusters: []KubeCluster{ @@ -58,18 +60,16 @@ func TestDetectClusterType_OpenShift4(t *testing.T) { } result := detectClusterType(config, apiResources) - if result != InfraOpenShift4 { - t.Errorf("detectClusterType() = %v (%s), want %v (%s)", result, result.String(), InfraOpenShift4, InfraOpenShift4.String()) - } + assert.Equal(t, InfraOpenShift4, result) } -func TestDetectClusterType_OpenShift4_WrongHostname(t *testing.T) { +func TestDetectClusterType_OpenShift4(t *testing.T) { config := KubeConfig{ - CurrentContext: "admin", + CurrentContext: "some-context-name", Clusters: []KubeCluster{ { - Name: "openshift-cluster", - Server: "https://api.my-cluster.example.com:6443", + Name: "some-other-name", + Server: "https://my-cluster.example.com:6443", }, }, } @@ -77,12 +77,11 @@ func TestDetectClusterType_OpenShift4_WrongHostname(t *testing.T) { "pods", "services", "clusterversions.config.openshift.io", + "clusteroperators.config.openshift.io", } result := detectClusterType(config, apiResources) - if result != ClusterTypeUnknown { - t.Errorf("detectClusterType() = %v (%s), want %v (%s)", result, result.String(), ClusterTypeUnknown, ClusterTypeUnknown.String()) - } + assert.Equal(t, OpenShift4, result) } func TestDetectClusterType_OpenShift4_NoAPIResources(t *testing.T) { @@ -295,6 +294,11 @@ func TestClusterTypeString(t *testing.T) { { name: "InfraOpenShift4", clusterType: InfraOpenShift4, + want: "OpenShift4 (infra)", + }, + { + name: "OpenShift4", + clusterType: OpenShift4, want: "OpenShift4", }, {