Skip to content

Commit befa352

Browse files
authored
Make cluster type overwritable (#184)
1 parent fc4e05e commit befa352

13 files changed

Lines changed: 146 additions & 52 deletions

cmd/deploy.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,18 @@ func runDeploy(cmd *cobra.Command, args []string) error {
347347
}
348348

349349
func configureConfig(log *logger.Logger, components component.Component, deploySettings *deployer.Config) error {
350-
clusterType := env.GetCurrentClusterType()
351-
log.Dimf("Detected cluster type: %v", clusterType)
352-
defaults, err := clusterdefaults.ApplyClusterDefaults(clusterType, deploySettings)
350+
if deploySettings.Roxie.ClusterType == types.ClusterTypeUnknown {
351+
clusterType := env.GetAutoDetectedClusterType()
352+
log.Dimf("Detected cluster type: %v", clusterType)
353+
deploySettings.Roxie.ClusterType = clusterType
354+
}
355+
clusterType := deploySettings.Roxie.ClusterType
356+
defaults, err := clusterdefaults.ApplyClusterDefaults(deploySettings)
353357
if err != nil {
354-
return fmt.Errorf("applying defaults for cluster type %v: %w", clusterType, err)
358+
return err
355359
}
356360
if verbose {
357-
log.Dimf("Applying the following defaults based on detected cluster type %v:", clusterType)
361+
log.Dimf("Applying the following defaults based on cluster type %v:", clusterType)
358362
helpers.LogMultilineYaml(log, defaults)
359363
}
360364

@@ -409,6 +413,8 @@ func deployValidate(components component.Component, deploySettings *deployer.Con
409413
return errors.New("running without a controlling terminal requires --envrc to be set")
410414
}
411415

416+
clusterType := deploySettings.Roxie.ClusterType
417+
412418
if env.RunningInRoxieContainer {
413419
// For running containerized we have specific requirements.
414420
if deploySettings.Central.PortForwardingEnabled() {
@@ -419,7 +425,7 @@ func deployValidate(components component.Component, deploySettings *deployer.Con
419425
}
420426

421427
// On infra OpenShift we already get image pull secrets for Quay automatically.
422-
if clusterType := env.GetCurrentClusterType(); clusterType != types.ClusterTypeInfraOpenShift4 {
428+
if clusterType.NeedsPullSecrets() {
423429
if os.Getenv("REGISTRY_USERNAME") == "" || os.Getenv("REGISTRY_PASSWORD") == "" {
424430
return fmt.Errorf("containerized mode requires REGISTRY_USERNAME and REGISTRY_PASSWORD environment variables for clusters of type %s", clusterType)
425431
}
@@ -437,9 +443,8 @@ func deployValidate(components component.Component, deploySettings *deployer.Con
437443
if deploySettings.Operator.DeployViaOlm {
438444
return errors.New("using Konflux images while deploying operator via OLM is not supported")
439445
}
440-
clusterType := env.GetCurrentClusterType()
441446
if !clusterType.IsOpenShift() {
442-
return fmt.Errorf("--konflux flag is only supported on OpenShift 4 clusters (current cluster type: %s)", clusterType.String())
447+
return fmt.Errorf("--konflux flag is only supported on OpenShift 4 clusters (current cluster type: %s)", clusterType)
443448
}
444449
}
445450

cmd/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func runEnv(cmd *cobra.Command, args []string) error {
3232
fmt.Printf("Kube config: %s\n", os.Getenv("KUBECONFIG"))
3333
fmt.Printf("Running in roxie container: %v\n", env.RunningInRoxieContainer)
3434
fmt.Printf("Current Context: %s\n", env.GetCurrentContext())
35-
fmt.Printf("Cluster Type: %s\n", env.GetCurrentClusterType().String())
35+
fmt.Printf("Cluster Type: %s\n", env.GetAutoDetectedClusterType().String())
3636

3737
return nil
3838
}

internal/clusterdefaults/clusterdefaults.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
// provided deployer.Config.
1414
// Returns *just* the assembled defaults for the given cluster type for logging purposes.
1515
func ApplyClusterDefaults(
16-
clusterType types.ClusterType,
1716
config *deployer.Config,
1817
) (*deployer.Config, error) {
1918
if config == nil {
2019
panic("applying cluster defaults to nil config")
2120
}
21+
clusterType := config.Roxie.ClusterType
2222
defaults := getDefaultsForClusterType(clusterType)
2323
if defaults == nil {
2424
return nil, nil
@@ -27,11 +27,11 @@ func ApplyClusterDefaults(
2727
// Make a copy.
2828
defaultsCopy, err := defaults.DeepCopy()
2929
if err != nil {
30-
return nil, fmt.Errorf("deep-copying cluster defaults: %w", err)
30+
return nil, fmt.Errorf("deep-copying cluster defaults for cluster type %s: %w", clusterType, err)
3131
}
3232

3333
if err := mergo.Merge(config, defaultsCopy, mergo.WithoutDereference); err != nil {
34-
return nil, fmt.Errorf("merging-in cluster defaults: %w", err)
34+
return nil, fmt.Errorf("merging-in cluster defaults for cluster type %s: %w", clusterType, err)
3535
}
3636

3737
return defaultsCopy, nil

internal/clusterdefaults/clusterdefaults_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ func TestClusterDefaults(t *testing.T) {
113113
for _, tt := range tests {
114114
t.Run(tt.name, func(t *testing.T) {
115115
config := tt.config
116-
_, err := ApplyClusterDefaults(tt.clusterType, &config)
116+
config.Roxie.ClusterType = tt.clusterType
117+
_, err := ApplyClusterDefaults(&config)
117118
require.NoError(t, err)
118119

119120
if tt.wantConfig.Central.Exposure == nil {

internal/deployer/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ func (c *Config) DeepCopy() (*Config, error) {
4444

4545
// RoxieConfig holds roxie-level settings such as version and feature flags.
4646
type RoxieConfig struct {
47-
Version string `yaml:"version,omitempty"`
48-
KonfluxImages bool `yaml:"konfluxImages,omitempty"`
49-
FeatureFlags map[string]bool `yaml:"featureFlags,omitempty"`
47+
Version string `yaml:"version,omitempty"`
48+
KonfluxImages bool `yaml:"konfluxImages,omitempty"`
49+
FeatureFlags map[string]bool `yaml:"featureFlags,omitempty"`
50+
ClusterType types.ClusterType `yaml:"clusterType,omitempty"`
5051
}
5152

5253
// NewRoxieConfig returns a RoxieConfig with initialized defaults.

internal/deployer/deploy_via_operator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (d *Deployer) ensureOperatorDeployed(ctx context.Context) error {
119119
func (d *Deployer) deployCentralOperator(ctx context.Context) error {
120120
d.logger.Info("🚀 Deploying Central via Operator...")
121121

122-
needPullSecrets := env.GetCurrentClusterType() != types.ClusterTypeInfraOpenShift4
122+
needPullSecrets := d.config.Roxie.ClusterType.NeedsPullSecrets()
123123
if err := d.prepareNamespace(ctx, d.config.Central.Namespace, needPullSecrets); err != nil {
124124
return fmt.Errorf("failed to prepare namespace: %w", err)
125125
}
@@ -655,7 +655,7 @@ func (d *Deployer) configureCentralEndpoint(ctx context.Context) error {
655655
func (d *Deployer) deploySecuredClusterOperator(ctx context.Context) error {
656656
d.logger.Info("🚀 Deploying SecuredCluster via Operator...")
657657

658-
needPullSecrets := env.GetCurrentClusterType() != types.ClusterTypeInfraOpenShift4
658+
needPullSecrets := d.config.Roxie.ClusterType.NeedsPullSecrets()
659659
if err := d.prepareNamespace(ctx, d.config.SecuredCluster.Namespace, needPullSecrets); err != nil {
660660
return fmt.Errorf("failed to prepare namespace: %w", err)
661661
}

internal/deployer/deployer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func (d *Deployer) stopDetachedPortForward() {
298298
// Deploy deploys the specified components to the cluster.
299299
func (d *Deployer) Deploy(ctx context.Context, components component.Component) error {
300300
// Prepare and verify credentials early to fail fast.
301-
if env.GetCurrentClusterType() != types.ClusterTypeInfraOpenShift4 {
301+
if d.config.Roxie.ClusterType.NeedsPullSecrets() {
302302
if err := d.prepareCredentials(); err != nil {
303303
return fmt.Errorf("failed to prepare credentials: %w", err)
304304
}
@@ -778,7 +778,7 @@ func (d *Deployer) PrintCentralDeploymentSummary() {
778778

779779
// Deployment details
780780
log.Info(cyan.Sprint("│") + createRow("Component", component))
781-
log.Info(cyan.Sprint("│") + createRow("Cluster Type", env.GetCurrentClusterType().String()))
781+
log.Info(cyan.Sprint("│") + createRow("Cluster Type", d.config.Roxie.ClusterType.String()))
782782
log.Info(cyan.Sprint("│") + createRow("Main Tag", mainImageTag))
783783
log.Info(cyan.Sprint("│") + createRow("Kubernetes Context", kubeContext))
784784

@@ -957,7 +957,7 @@ func (d *Deployer) PrintSecuredClusterDeploymentSummary() {
957957

958958
// Deployment details
959959
log.Info(cyan.Sprint("│") + createRow("Component", component))
960-
log.Info(cyan.Sprint("│") + createRow("Cluster Type", env.GetCurrentClusterType().String()))
960+
log.Info(cyan.Sprint("│") + createRow("Cluster Type", d.config.Roxie.ClusterType.String()))
961961
log.Info(cyan.Sprint("│") + createRow("Main Tag", mainImageTag))
962962
log.Info(cyan.Sprint("│") + createRow("Kubernetes Context", kubeContext))
963963

internal/deployer/operator.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ import (
1414

1515
"gopkg.in/yaml.v3"
1616

17-
"github.com/stackrox/roxie/internal/env"
1817
"github.com/stackrox/roxie/internal/k8s"
1918
"github.com/stackrox/roxie/internal/ocihelper"
20-
"github.com/stackrox/roxie/internal/types"
2119
)
2220

2321
const (
@@ -202,7 +200,7 @@ func (d *Deployer) getOperatorBundleImage() string {
202200

203201
// ensureKonfluxImageRewriting configures image rewriting for Konflux images
204202
func (d *Deployer) ensureKonfluxImageRewriting(ctx context.Context) error {
205-
if !env.GetCurrentClusterType().IsOpenShift() {
203+
if !d.config.Roxie.ClusterType.IsOpenShift() {
206204
return errors.New("image rewriting for Konflux is only supported on OpenShift4 clusters")
207205
}
208206

@@ -290,7 +288,7 @@ func (d *Deployer) applyImageContentSourcePolicy(ctx context.Context) error {
290288

291289
// removeKonfluxImageRewriting removes the ImageContentSourcePolicy for Konflux images if it exists
292290
func (d *Deployer) removeKonfluxImageRewriting(ctx context.Context) error {
293-
if !env.GetCurrentClusterType().IsOpenShift() {
291+
if !d.config.Roxie.ClusterType.IsOpenShift() {
294292
return nil
295293
}
296294

@@ -320,7 +318,7 @@ func (d *Deployer) deployOperatorFromCSV(ctx context.Context, bundleDir string)
320318
}
321319

322320
serviceAccountName := deploymentSpec["service_account"].(string)
323-
d.useOperatorPullSecrets = d.config.Roxie.KonfluxImages && env.GetCurrentClusterType() != types.ClusterTypeInfraOpenShift4
321+
d.useOperatorPullSecrets = d.config.Roxie.KonfluxImages && d.config.Roxie.ClusterType.NeedsPullSecrets()
324322

325323
d.logger.Info("📋 Operator deployment plan:")
326324
d.logger.Dim(fmt.Sprintf(" • Namespace: %s", operatorNamespace))

internal/env/env.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var (
2626

2727
var (
2828
// currentClusterType holds the detected cluster type for the current kubectl context
29-
// This is lazily populated on first access via GetCurrentClusterType()
29+
// This is lazily populated on first access via GetAutoDetectedClusterType()
3030
currentClusterType types.ClusterType
3131

3232
// currentContext holds the name of the current kubectl context
@@ -78,8 +78,8 @@ func ensureInitialized(log *logger.Logger) error {
7878
return nil
7979
}
8080

81-
// GetCurrentClusterType returns the current cluster type, initializing if needed
82-
func GetCurrentClusterType() types.ClusterType {
81+
// GetAutoDetectedClusterType returns the current cluster type, initializing if needed
82+
func GetAutoDetectedClusterType() types.ClusterType {
8383
return currentClusterType
8484
}
8585

internal/env/env_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestDetectClusterType_Integration(t *testing.T) {
1616

1717
// This test uses the current kubectl context
1818
// The result will depend on the active cluster
19-
clusterType := GetCurrentClusterType()
19+
clusterType := GetAutoDetectedClusterType()
2020

2121
t.Logf("Detected cluster type: %s", clusterType)
2222

0 commit comments

Comments
 (0)