Skip to content

Commit 3c34a42

Browse files
mclasmeierMoritz Clasmeier
andauthored
Differentiate between OpenShift4-on-infra and generic OpenShift4 (#155)
Co-authored-by: Moritz Clasmeier <mclasmeier@redhat.com>
1 parent 597d792 commit 3c34a42

5 files changed

Lines changed: 45 additions & 25 deletions

File tree

cmd/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func runDeploy(cmd *cobra.Command, args []string) error {
111111
return errors.New("cannot use both --olm and --konflux flags together (not currently implemented)")
112112
}
113113
clusterType := env.GetCurrentClusterType()
114-
if clusterType != env.InfraOpenShift4 {
114+
if !clusterType.IsOpenShift() {
115115
return fmt.Errorf("--konflux flag is only supported on OpenShift 4 clusters (current cluster type: %s)", clusterType.String())
116116
}
117117
}

internal/deployer/operator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (d *Deployer) getOperatorBundleImage() string {
201201

202202
// ensureKonfluxImageRewriting configures image rewriting for Konflux images
203203
func (d *Deployer) ensureKonfluxImageRewriting(ctx context.Context) error {
204-
if env.GetCurrentClusterType() != env.InfraOpenShift4 {
204+
if !env.GetCurrentClusterType().IsOpenShift() {
205205
return errors.New("image rewriting for Konflux is only supported on OpenShift4 clusters")
206206
}
207207

@@ -289,7 +289,7 @@ func (d *Deployer) applyImageContentSourcePolicy(ctx context.Context) error {
289289

290290
// removeKonfluxImageRewriting removes the ImageContentSourcePolicy for Konflux images if it exists
291291
func (d *Deployer) removeKonfluxImageRewriting(ctx context.Context) error {
292-
if env.GetCurrentClusterType() != env.InfraOpenShift4 {
292+
if !env.GetCurrentClusterType().IsOpenShift() {
293293
return nil
294294
}
295295

internal/env/env.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const (
3333
InfraGKE
3434
// InfraOpenShift4 represents an OpenShift 4 cluster
3535
InfraOpenShift4
36+
// Generic OpenShift4 cluster (e.g. for prow CI)
37+
OpenShift4
3638
// LocalKind represents a Kind (Kubernetes in Docker) cluster
3739
LocalKind
3840
)
@@ -115,6 +117,8 @@ func (ct ClusterType) String() string {
115117
case InfraGKE:
116118
return "GKE"
117119
case InfraOpenShift4:
120+
return "OpenShift4 (infra)"
121+
case OpenShift4:
118122
return "OpenShift4"
119123
case LocalKind:
120124
return "Kind"
@@ -123,6 +127,10 @@ func (ct ClusterType) String() string {
123127
}
124128
}
125129

130+
func (ct ClusterType) IsOpenShift() bool {
131+
return ct == InfraOpenShift4 || ct == OpenShift4
132+
}
133+
126134
// KubeConfig represents a simplified kubectl configuration
127135
type KubeConfig struct {
128136
CurrentContext string
@@ -183,17 +191,12 @@ func detectClusterType(config KubeConfig, apiResources []string) ClusterType {
183191
return InfraGKE
184192
}
185193

186-
// Check for OpenShift 4 clusters by examining the server hostname
187-
if serverURL := getServerURL(config); serverURL != "" {
188-
if parsedURL, err := url.Parse(serverURL); err == nil {
189-
hostname := parsedURL.Hostname()
190-
if strings.HasSuffix(hostname, ".ocp.infra.rox.systems") {
191-
// Further verify it's OpenShift 4 by checking the API resources
192-
if isOpenShift4(apiResources) {
193-
return InfraOpenShift4
194-
}
195-
}
194+
// Check for OpenShift 4 clusters.
195+
if isOpenShift4(apiResources) {
196+
if isInfraOpenShift4(config) {
197+
return InfraOpenShift4
196198
}
199+
return OpenShift4
197200
}
198201

199202
// Check for Kind clusters
@@ -205,6 +208,19 @@ func detectClusterType(config KubeConfig, apiResources []string) ClusterType {
205208
return ClusterTypeUnknown
206209
}
207210

211+
func isInfraOpenShift4(config KubeConfig) bool {
212+
serverURL := getServerURL(config)
213+
if serverURL == "" {
214+
return false
215+
}
216+
parsedURL, err := url.Parse(serverURL)
217+
if err != nil {
218+
return false
219+
}
220+
hostname := parsedURL.Hostname()
221+
return strings.HasSuffix(hostname, ".ocp.infra.rox.systems")
222+
}
223+
208224
// getServerURL retrieves the server URL from the KubeConfig
209225
func getServerURL(config KubeConfig) string {
210226
if len(config.Clusters) == 0 {

internal/env/env_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestDetectClusterType_Integration(t *testing.T) {
1919
t.Logf("Detected cluster type: %s", clusterType)
2020

2121
// The cluster type should never be invalid (even if Unknown)
22-
validTypes := []ClusterType{ClusterTypeUnknown, InfraGKE, InfraOpenShift4, LocalKind}
22+
validTypes := []ClusterType{ClusterTypeUnknown, InfraGKE, InfraOpenShift4, OpenShift4, LocalKind}
2323
found := false
2424
for _, valid := range validTypes {
2525
if clusterType == valid {

internal/env/env_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package env
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestDetectClusterType_GKE(t *testing.T) {
@@ -40,7 +42,7 @@ func TestDetectClusterType_GKE_ExactMatch(t *testing.T) {
4042
}
4143
}
4244

43-
func TestDetectClusterType_OpenShift4(t *testing.T) {
45+
func TestDetectClusterType_InfraOpenShift4(t *testing.T) {
4446
config := KubeConfig{
4547
CurrentContext: "admin",
4648
Clusters: []KubeCluster{
@@ -58,31 +60,28 @@ func TestDetectClusterType_OpenShift4(t *testing.T) {
5860
}
5961

6062
result := detectClusterType(config, apiResources)
61-
if result != InfraOpenShift4 {
62-
t.Errorf("detectClusterType() = %v (%s), want %v (%s)", result, result.String(), InfraOpenShift4, InfraOpenShift4.String())
63-
}
63+
assert.Equal(t, InfraOpenShift4, result)
6464
}
6565

66-
func TestDetectClusterType_OpenShift4_WrongHostname(t *testing.T) {
66+
func TestDetectClusterType_OpenShift4(t *testing.T) {
6767
config := KubeConfig{
68-
CurrentContext: "admin",
68+
CurrentContext: "some-context-name",
6969
Clusters: []KubeCluster{
7070
{
71-
Name: "openshift-cluster",
72-
Server: "https://api.my-cluster.example.com:6443",
71+
Name: "some-other-name",
72+
Server: "https://my-cluster.example.com:6443",
7373
},
7474
},
7575
}
7676
apiResources := []string{
7777
"pods",
7878
"services",
7979
"clusterversions.config.openshift.io",
80+
"clusteroperators.config.openshift.io",
8081
}
8182

8283
result := detectClusterType(config, apiResources)
83-
if result != ClusterTypeUnknown {
84-
t.Errorf("detectClusterType() = %v (%s), want %v (%s)", result, result.String(), ClusterTypeUnknown, ClusterTypeUnknown.String())
85-
}
84+
assert.Equal(t, OpenShift4, result)
8685
}
8786

8887
func TestDetectClusterType_OpenShift4_NoAPIResources(t *testing.T) {
@@ -295,6 +294,11 @@ func TestClusterTypeString(t *testing.T) {
295294
{
296295
name: "InfraOpenShift4",
297296
clusterType: InfraOpenShift4,
297+
want: "OpenShift4 (infra)",
298+
},
299+
{
300+
name: "OpenShift4",
301+
clusterType: OpenShift4,
298302
want: "OpenShift4",
299303
},
300304
{

0 commit comments

Comments
 (0)