Skip to content

Commit 2e2b206

Browse files
author
Moritz Clasmeier
committed
Make cluster type overwritable, e.g. for testing purposes
1 parent 844328f commit 2e2b206

8 files changed

Lines changed: 71 additions & 22 deletions

File tree

cmd/deploy.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,15 @@ 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.GetCurrentClusterType()
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 {
357361
log.Dimf("Applying the following defaults based on detected cluster type %v:", clusterType)
@@ -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 != types.ClusterTypeInfraOpenShift4 {
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,7 +443,6 @@ 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() {
442447
return fmt.Errorf("--konflux flag is only supported on OpenShift 4 clusters (current cluster type: %s)", clusterType.String())
443448
}

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 != types.ClusterTypeInfraOpenShift4
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 != types.ClusterTypeInfraOpenShift4
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 != types.ClusterTypeInfraOpenShift4 {
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ 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"
2019
"github.com/stackrox/roxie/internal/types"
@@ -202,7 +201,7 @@ func (d *Deployer) getOperatorBundleImage() string {
202201

203202
// ensureKonfluxImageRewriting configures image rewriting for Konflux images
204203
func (d *Deployer) ensureKonfluxImageRewriting(ctx context.Context) error {
205-
if !env.GetCurrentClusterType().IsOpenShift() {
204+
if !d.config.Roxie.ClusterType.IsOpenShift() {
206205
return errors.New("image rewriting for Konflux is only supported on OpenShift4 clusters")
207206
}
208207

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

291290
// removeKonfluxImageRewriting removes the ImageContentSourcePolicy for Konflux images if it exists
292291
func (d *Deployer) removeKonfluxImageRewriting(ctx context.Context) error {
293-
if !env.GetCurrentClusterType().IsOpenShift() {
292+
if !d.config.Roxie.ClusterType.IsOpenShift() {
294293
return nil
295294
}
296295

@@ -320,7 +319,7 @@ func (d *Deployer) deployOperatorFromCSV(ctx context.Context, bundleDir string)
320319
}
321320

322321
serviceAccountName := deploymentSpec["service_account"].(string)
323-
d.useOperatorPullSecrets = d.config.Roxie.KonfluxImages && env.GetCurrentClusterType() != types.ClusterTypeInfraOpenShift4
322+
d.useOperatorPullSecrets = d.config.Roxie.KonfluxImages && d.config.Roxie.ClusterType != types.ClusterTypeInfraOpenShift4
324323

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

internal/types/cluster_type.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package types
22

3+
import "fmt"
4+
35
// ClusterType represents different types of Kubernetes clusters
46
type ClusterType int
57

@@ -22,6 +24,27 @@ const (
2224
ClusterTypeCRC
2325
)
2426

27+
var (
28+
clusterTypeToIdentifier = map[ClusterType]string{
29+
ClusterTypeUnknown: "Unknown",
30+
ClusterTypeInfraGKE: "InfraGKE",
31+
ClusterTypeInfraOpenShift4: "InfraOpenShift4",
32+
ClusterTypeOpenShift4: "OpenShift4",
33+
ClusterTypeKind: "Kind",
34+
ClusterTypeMinikube: "Minikube",
35+
ClusterTypeK3s: "K3s",
36+
ClusterTypeCRC: "CRC",
37+
}
38+
39+
identifierToClusterType = func() map[string]ClusterType {
40+
m := make(map[string]ClusterType, len(clusterTypeToIdentifier))
41+
for k, v := range clusterTypeToIdentifier {
42+
m[v] = k
43+
}
44+
return m
45+
}()
46+
)
47+
2548
func (ct ClusterType) IsOpenShift() bool {
2649
return ct == ClusterTypeInfraOpenShift4 || ct == ClusterTypeOpenShift4
2750
}
@@ -59,3 +82,23 @@ func AllClusterTypes() []ClusterType {
5982
ClusterTypeOpenShift4,
6083
}
6184
}
85+
86+
func (ct ClusterType) MarshalYAML() (any, error) {
87+
if id, ok := clusterTypeToIdentifier[ct]; ok {
88+
return id, nil
89+
}
90+
return nil, fmt.Errorf("unknown cluster type: %d", ct)
91+
}
92+
93+
func (ct *ClusterType) UnmarshalYAML(unmarshal func(any) error) error {
94+
var s string
95+
if err := unmarshal(&s); err != nil {
96+
return err
97+
}
98+
parsed, ok := identifierToClusterType[s]
99+
if !ok {
100+
return fmt.Errorf("unknown cluster type identifier: %q", s)
101+
}
102+
*ct = parsed
103+
return nil
104+
}

0 commit comments

Comments
 (0)