From 0d67aa971fbe6379697cffbbdb676030b3737e71 Mon Sep 17 00:00:00 2001 From: Moritz Clasmeier Date: Mon, 20 Jul 2026 23:36:48 +0200 Subject: [PATCH] Support non-Infra cluster type and also support specifying 'Unknown' as a generic type explicitly --- cmd/deploy.go | 2 +- internal/clusterdefaults/clusterdefaults.go | 24 +++------- .../clusterdefaults/clusterdefaults_test.go | 12 ++++- internal/env/env.go | 3 ++ internal/env/env_test.go | 46 +++++++++++++------ internal/types/cluster_type.go | 12 +++-- internal/types/cluster_type_test.go | 4 +- 7 files changed, 65 insertions(+), 38 deletions(-) diff --git a/cmd/deploy.go b/cmd/deploy.go index 99db4b1c..40325e15 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -346,7 +346,7 @@ func runDeploy(cmd *cobra.Command, args []string) error { } func configureConfig(log *logger.Logger, components component.Component, deploySettings *deployer.Config) error { - if deploySettings.Roxie.ClusterType == types.ClusterTypeUnknown { + if deploySettings.Roxie.ClusterType == "" { clusterType := env.GetAutoDetectedClusterType() log.Dimf("Detected cluster type: %v", clusterType) deploySettings.Roxie.ClusterType = clusterType diff --git a/internal/clusterdefaults/clusterdefaults.go b/internal/clusterdefaults/clusterdefaults.go index 739a9810..bda3f59d 100644 --- a/internal/clusterdefaults/clusterdefaults.go +++ b/internal/clusterdefaults/clusterdefaults.go @@ -44,8 +44,8 @@ func ApplyClusterDefaults( // in the Config struct. Otherwise, `ApplyClusterDefaults` would not apply those to the caller-provided // configuration. func getDefaultsForClusterType(clusterType types.ClusterType) *deployer.Config { - switch clusterType { - case types.ClusterTypeKind, types.ClusterTypeMinikube, types.ClusterTypeK3s, types.ClusterTypeCRC: + switch { + case clusterType.IsLocal(): return &deployer.Config{ Central: deployer.CentralConfig{ Exposure: ptr.To(types.ExposureNone), @@ -53,7 +53,7 @@ func getDefaultsForClusterType(clusterType types.ClusterType) *deployer.Config { }, } - case types.ClusterTypeInfraGKE, types.ClusterTypeInfraOpenShift4: + case clusterType.IsGKE() || clusterType.IsOpenShift(): return &deployer.Config{ Central: deployer.CentralConfig{ Exposure: ptr.To(types.ExposureLoadBalancer), @@ -68,23 +68,11 @@ func getDefaultsForClusterType(clusterType types.ClusterType) *deployer.Config { // ResolveAutoResourceProfile resolves the "auto" resource profile depending on the cluster type. func ResolveAutoResourceProfile(clusterType types.ClusterType) types.ResourceProfile { - switch clusterType { - case types.ClusterTypeKind: + switch { + case clusterType.IsLocal(): return types.ResourceProfileSmall - case types.ClusterTypeMinikube: - return types.ResourceProfileSmall - - case types.ClusterTypeK3s: - return types.ResourceProfileSmall - - case types.ClusterTypeCRC: - return types.ResourceProfileSmall - - case types.ClusterTypeInfraOpenShift4: - return types.ResourceProfileMedium - - case types.ClusterTypeInfraGKE: + case clusterType.IsGKE() || clusterType.IsOpenShift(): return types.ResourceProfileMedium default: diff --git a/internal/clusterdefaults/clusterdefaults_test.go b/internal/clusterdefaults/clusterdefaults_test.go index 78b59e13..3607da5a 100644 --- a/internal/clusterdefaults/clusterdefaults_test.go +++ b/internal/clusterdefaults/clusterdefaults_test.go @@ -73,7 +73,7 @@ func TestClusterDefaults(t *testing.T) { }, }, { - name: "gke cluster", + name: "infra gke cluster", clusterType: types.ClusterTypeInfraGKE, wantConfig: deployer.Config{ Central: deployer.CentralConfig{ @@ -82,6 +82,16 @@ func TestClusterDefaults(t *testing.T) { }, }, }, + { + name: "gke cluster", + clusterType: types.ClusterTypeGKE, + wantConfig: deployer.Config{ + Central: deployer.CentralConfig{ + Exposure: ptr.To(types.ExposureLoadBalancer), + PortForwarding: ptr.To(false), + }, + }, + }, { name: "openshift cluster", clusterType: types.ClusterTypeInfraOpenShift4, diff --git a/internal/env/env.go b/internal/env/env.go index 37603bb1..fa041f23 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -147,6 +147,9 @@ func DetectClusterType(config KubeConfig, apiResources []string) types.ClusterTy if strings.HasPrefix(config.CurrentContext, "gke_acs-team-temp-dev") { return types.ClusterTypeInfraGKE } + if strings.HasPrefix(config.CurrentContext, "gke_") { + return types.ClusterTypeGKE + } // Minikube clusters typically have context name "minikube". if contextLower == "minikube" || strings.HasPrefix(contextLower, "minikube-") { diff --git a/internal/env/env_test.go b/internal/env/env_test.go index b5cf6c1d..04ad89b5 100644 --- a/internal/env/env_test.go +++ b/internal/env/env_test.go @@ -8,7 +8,7 @@ import ( "github.com/stackrox/roxie/internal/types" ) -func TestDetectClusterType_GKE(t *testing.T) { +func TestDetectClusterType_InfraGKE(t *testing.T) { config := KubeConfig{ CurrentContext: "gke_acs-team-temp-dev_us-central1-a_my-cluster", Clusters: []KubeCluster{ @@ -26,7 +26,7 @@ func TestDetectClusterType_GKE(t *testing.T) { } } -func TestDetectClusterType_GKE_ExactMatch(t *testing.T) { +func TestDetectClusterType_InfraGKE_ExactMatch(t *testing.T) { config := KubeConfig{ CurrentContext: "gke_acs-team-temp-dev", Clusters: []KubeCluster{ @@ -172,20 +172,33 @@ func TestDetectClusterType_Minikube(t *testing.T) { } func TestDetectClusterType_GKE_DifferentProject(t *testing.T) { - config := KubeConfig{ - CurrentContext: "gke_other-project_us-west1_cluster", - Clusters: []KubeCluster{ - { - Name: "gke_cluster", - Server: "https://34.1.2.3", - }, + tests := []struct { + name string + context string + }{ + { + name: "arbitrary project", + context: "gke_other-project_us-west1_cluster", + }, + { + name: "CI direct-gke via gke.sh (acs-san-stackroxci)", + context: "gke_acs-san-stackroxci_us-east4-b_rox-ci-qa-e2e-12345678", }, } - apiResources := []string{"pods"} - - result := DetectClusterType(config, apiResources) - if result != types.ClusterTypeUnknown { - t.Errorf("DetectClusterType() = %v (%s), want %v", result, result.String(), types.ClusterTypeUnknown) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + config := KubeConfig{ + CurrentContext: tt.context, + Clusters: []KubeCluster{ + { + Name: "gke_cluster", + Server: "https://34.1.2.3", + }, + }, + } + result := DetectClusterType(config, []string{"pods"}) + assert.Equal(t, types.ClusterTypeGKE, result) + }) } } @@ -288,6 +301,11 @@ func TestClusterTypeString(t *testing.T) { clusterType types.ClusterType want string }{ + { + name: "types.ClusterTypeGKE", + clusterType: types.ClusterTypeGKE, + want: "GKE", + }, { name: "types.ClusterTypeInfraGKE", clusterType: types.ClusterTypeInfraGKE, diff --git a/internal/types/cluster_type.go b/internal/types/cluster_type.go index 5621665f..fce6b8f8 100644 --- a/internal/types/cluster_type.go +++ b/internal/types/cluster_type.go @@ -7,7 +7,9 @@ type ClusterType string const ( // ClusterTypeUnknown represents an unidentified cluster type - ClusterTypeUnknown ClusterType = "" + ClusterTypeUnknown ClusterType = "Unknown" + // ClusterTypeGKE represents a generic GKE cluster. + ClusterTypeGKE ClusterType = "GKE" // ClusterTypeInfraGKE represents a GKE cluster created via Infra. ClusterTypeInfraGKE ClusterType = "InfraGKE" // ClusterTypeInfraOpenShift4 represents an OpenShift 4 cluster @@ -24,6 +26,10 @@ const ( ClusterTypeCRC ClusterType = "CRC" ) +func (ct ClusterType) IsGKE() bool { + return ct == ClusterTypeInfraGKE || ct == ClusterTypeGKE +} + func (ct ClusterType) IsOpenShift() bool { return ct == ClusterTypeInfraOpenShift4 || ct == ClusterTypeOpenShift4 } @@ -35,8 +41,6 @@ func (ct ClusterType) String() string { return "GKE (infra)" case ClusterTypeInfraOpenShift4: return "OpenShift4 (infra)" - case ClusterTypeUnknown: - return "Unknown" default: return string(ct) } @@ -44,6 +48,8 @@ func (ct ClusterType) String() string { func AllClusterTypes() []ClusterType { return []ClusterType{ + ClusterTypeUnknown, + ClusterTypeGKE, ClusterTypeInfraGKE, ClusterTypeKind, ClusterTypeMinikube, diff --git a/internal/types/cluster_type_test.go b/internal/types/cluster_type_test.go index b97ff6b5..40d623b1 100644 --- a/internal/types/cluster_type_test.go +++ b/internal/types/cluster_type_test.go @@ -13,6 +13,7 @@ func TestClusterTypeMarshalYAML(t *testing.T) { clusterType ClusterType expected string }{ + {ClusterTypeGKE, "GKE"}, {ClusterTypeInfraGKE, "InfraGKE"}, {ClusterTypeInfraOpenShift4, "InfraOpenShift4"}, {ClusterTypeOpenShift4, "OpenShift4"}, @@ -35,6 +36,7 @@ func TestClusterTypeUnmarshalYAML(t *testing.T) { input string expected ClusterType }{ + {"GKE", ClusterTypeGKE}, {"InfraGKE", ClusterTypeInfraGKE}, {"InfraOpenShift4", ClusterTypeInfraOpenShift4}, {"OpenShift4", ClusterTypeOpenShift4}, @@ -42,7 +44,7 @@ func TestClusterTypeUnmarshalYAML(t *testing.T) { {"Minikube", ClusterTypeMinikube}, {"K3s", ClusterTypeK3s}, {"CRC", ClusterTypeCRC}, - {"", ClusterTypeUnknown}, + {"Unknown", ClusterTypeUnknown}, } for _, tt := range tests { t.Run(tt.input, func(t *testing.T) {