Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/deployer/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
}

Expand Down
36 changes: 26 additions & 10 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/env/env_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 15 additions & 11 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package env

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDetectClusterType_GKE(t *testing.T) {
Expand Down Expand Up @@ -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{
Expand All @@ -58,31 +60,28 @@ 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",
},
},
}
apiResources := []string{
"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) {
Expand Down Expand Up @@ -295,6 +294,11 @@ func TestClusterTypeString(t *testing.T) {
{
name: "InfraOpenShift4",
clusterType: InfraOpenShift4,
want: "OpenShift4 (infra)",
},
{
name: "OpenShift4",
clusterType: OpenShift4,
want: "OpenShift4",
},
{
Expand Down
Loading