Skip to content

Commit 909a20c

Browse files
author
Moritz Clasmeier
committed
Merge branch 'backup/mc/new-config-2' into mc/better-waiting-3
2 parents 5519be3 + 5fb1130 commit 909a20c

13 files changed

Lines changed: 303 additions & 191 deletions

File tree

cmd/deploy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ func configureConfig(log *logger.Logger, components component.Component, deployS
478478
}
479479
if deploySettings.SecuredCluster.ResourceProfile == types.ResourceProfileAuto {
480480
profile := clusterdefaults.ResolveAutoResourceProfile(clusterType)
481+
log.Dimf("Selecting resource profile %v for SecuredCluster", profile)
481482
deploySettings.SecuredCluster.ResourceProfile = profile
482483
}
483484

@@ -557,7 +558,7 @@ func deployValidate(components component.Component, deploySettings *deployer.Con
557558
return errors.New("using Konflux images while deploying operator via OLM is not supported")
558559
}
559560
clusterType := env.GetCurrentClusterType()
560-
if clusterType != types.ClusterTypeInfraOpenShift4 {
561+
if !clusterType.IsOpenShift() {
561562
return fmt.Errorf("--konflux flag is only supported on OpenShift 4 clusters (current cluster type: %s)", clusterType.String())
562563
}
563564
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/stackrox/roxie
33
go 1.25.6
44

55
require (
6+
dario.cat/mergo v1.0.2
67
github.com/fatih/color v1.16.0
78
github.com/google/go-containerregistry v0.21.0
89
github.com/spf13/cobra v1.10.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
22
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
3+
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
4+
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
35
github.com/containerd/stargz-snapshotter/estargz v0.18.2 h1:yXkZFYIzz3eoLwlTUZKz2iQ4MrckBxJjkmD16ynUTrw=
46
github.com/containerd/stargz-snapshotter/estargz v0.18.2/go.mod h1:XyVU5tcJ3PRpkA9XS2T5us6Eg35yM0214Y+wvrZTBrY=
57
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=

internal/clusterdefaults/clusterdefaults.go

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package clusterdefaults
22

33
import (
4+
"fmt"
5+
6+
"dario.cat/mergo"
47
"github.com/stackrox/roxie/internal/deployer"
5-
"github.com/stackrox/roxie/internal/helpers"
68
"github.com/stackrox/roxie/internal/types"
7-
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/utils/ptr"
810
)
911

1012
// ApplyClusterDefaults detects the cluster type and applies appropriate defaults to the
@@ -13,7 +15,7 @@ import (
1315
func ApplyClusterDefaults(
1416
clusterType types.ClusterType,
1517
config *deployer.Config,
16-
) (map[string]interface{}, error) {
18+
) (*deployer.Config, error) {
1719
if config == nil {
1820
panic("applying cluster defaults to nil config")
1921
}
@@ -23,75 +25,33 @@ func ApplyClusterDefaults(
2325
}
2426

2527
// Make a copy.
26-
defaultsCopy := runtime.DeepCopyJSON(defaults)
27-
28-
configMap, err := helpers.StructToMap(config)
28+
defaultsCopy, err := defaults.DeepCopy()
2929
if err != nil {
30-
return nil, err
31-
}
32-
mergeResult := make(map[string]interface{})
33-
if err = helpers.DeepMerge(mergeResult, defaults); err != nil {
34-
return nil, err
35-
}
36-
if err := helpers.DeepMerge(mergeResult, configMap); err != nil {
37-
return nil, err
30+
return nil, fmt.Errorf("deep-copying cluster defaults: %w", err)
3831
}
3932

40-
if err := helpers.MapToStruct(mergeResult, config); err != nil {
41-
return nil, err
33+
if err := mergo.Merge(config, defaultsCopy, mergo.WithoutDereference); err != nil {
34+
return nil, fmt.Errorf("merging-in cluster defaults: %w", err)
4235
}
36+
4337
return defaultsCopy, nil
4438
}
4539

46-
// getDefaultsForClusterType returns the recommended defaults for a given cluster type.
47-
func getDefaultsForClusterType(clusterType types.ClusterType) map[string]interface{} {
40+
func getDefaultsForClusterType(clusterType types.ClusterType) *deployer.Config {
4841
switch clusterType {
49-
case types.ClusterTypeKind:
50-
// Kind clusters are local, lightweight, and don't support LoadBalancer.
51-
return map[string]interface{}{
52-
"central": map[string]interface{}{
53-
"exposure": types.ExposureNone.String(),
54-
"portForwarding": true,
42+
case types.ClusterTypeKind, types.ClusterTypeMinikube, types.ClusterTypeK3s, types.ClusterTypeCRC:
43+
return &deployer.Config{
44+
Central: deployer.CentralConfig{
45+
Exposure: ptr.To(types.ExposureNone),
46+
PortForwarding: ptr.To(true),
5547
},
5648
}
5749

58-
case types.ClusterTypeMinikube:
59-
return map[string]interface{}{
60-
"central": map[string]interface{}{
61-
"exposure": types.ExposureNone.String(),
62-
"portForwarding": true,
63-
},
64-
}
65-
66-
case types.ClusterTypeK3s:
67-
return map[string]interface{}{
68-
"central": map[string]interface{}{
69-
"exposure": types.ExposureNone.String(),
70-
"portForwarding": true,
71-
},
72-
}
73-
74-
case types.ClusterTypeCRC:
75-
return map[string]interface{}{
76-
"central": map[string]interface{}{
77-
"exposure": types.ExposureNone.String(),
78-
"portForwarding": true,
79-
},
80-
}
81-
82-
case types.ClusterTypeInfraGKE:
83-
return map[string]interface{}{
84-
"central": map[string]interface{}{
85-
"exposure": types.ExposureLoadBalancer.String(),
86-
"portForwarding": false,
87-
},
88-
}
89-
90-
case types.ClusterTypeInfraOpenShift4:
91-
return map[string]interface{}{
92-
"central": map[string]interface{}{
93-
"exposure": types.ExposureLoadBalancer.String(),
94-
"portForwarding": false,
50+
case types.ClusterTypeInfraGKE, types.ClusterTypeInfraOpenShift4:
51+
return &deployer.Config{
52+
Central: deployer.CentralConfig{
53+
Exposure: ptr.To(types.ExposureLoadBalancer),
54+
PortForwarding: ptr.To(false),
9555
},
9656
}
9757

internal/clusterdefaults/clusterdefaults_test.go

Lines changed: 137 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,96 +12,176 @@ import (
1212

1313
func TestClusterDefaults(t *testing.T) {
1414
tests := []struct {
15-
name string
16-
clusterType types.ClusterType
17-
wantResourceProfile types.ResourceProfile
18-
wantExposure *types.Exposure
19-
wantPortForwarding *bool
15+
name string
16+
clusterType types.ClusterType
17+
config deployer.Config
18+
wantConfig deployer.Config
2019
}{
2120
{
22-
name: "kind cluster with default params",
23-
clusterType: types.ClusterTypeKind,
24-
wantResourceProfile: types.ResourceProfileSmall,
25-
wantExposure: ptr.To(types.ExposureNone),
26-
wantPortForwarding: ptr.To(true),
21+
name: "kind cluster with default params",
22+
clusterType: types.ClusterTypeKind,
23+
wantConfig: deployer.Config{
24+
Central: deployer.CentralConfig{
25+
Exposure: ptr.To(types.ExposureNone),
26+
PortForwarding: ptr.To(true),
27+
},
28+
},
2729
},
2830
{
29-
name: "kind cluster with already correct params",
30-
clusterType: types.ClusterTypeKind,
31-
wantResourceProfile: types.ResourceProfileSmall,
32-
wantExposure: ptr.To(types.ExposureNone),
33-
wantPortForwarding: ptr.To(true),
31+
name: "kind cluster with already correct params",
32+
clusterType: types.ClusterTypeKind,
33+
wantConfig: deployer.Config{
34+
Central: deployer.CentralConfig{
35+
Exposure: ptr.To(types.ExposureNone),
36+
PortForwarding: ptr.To(true),
37+
},
38+
},
3439
},
3540
{
36-
name: "kind cluster with partial match",
37-
clusterType: types.ClusterTypeKind,
38-
wantResourceProfile: types.ResourceProfileSmall,
39-
wantExposure: ptr.To(types.ExposureNone),
40-
wantPortForwarding: ptr.To(true),
41+
name: "kind cluster with partial match",
42+
clusterType: types.ClusterTypeKind,
43+
wantConfig: deployer.Config{
44+
Central: deployer.CentralConfig{
45+
Exposure: ptr.To(types.ExposureNone),
46+
PortForwarding: ptr.To(true),
47+
},
48+
},
4149
},
4250
{
43-
name: "unknown cluster type",
44-
clusterType: types.ClusterTypeUnknown,
45-
wantResourceProfile: types.ResourceProfileAcsDefaults,
46-
wantExposure: nil,
47-
wantPortForwarding: nil,
51+
name: "unknown cluster type",
52+
clusterType: types.ClusterTypeUnknown,
53+
wantConfig: deployer.Config{},
4854
},
4955
{
50-
name: "minikube cluster",
51-
clusterType: types.ClusterTypeMinikube,
52-
wantResourceProfile: types.ResourceProfileSmall,
53-
wantExposure: ptr.To(types.ExposureNone),
54-
wantPortForwarding: ptr.To(true),
56+
name: "minikube cluster",
57+
clusterType: types.ClusterTypeMinikube,
58+
wantConfig: deployer.Config{
59+
Central: deployer.CentralConfig{
60+
Exposure: ptr.To(types.ExposureNone),
61+
PortForwarding: ptr.To(true),
62+
},
63+
},
5564
},
5665
{
57-
name: "crc cluster",
58-
clusterType: types.ClusterTypeCRC,
59-
wantResourceProfile: types.ResourceProfileSmall,
60-
wantExposure: ptr.To(types.ExposureNone),
61-
wantPortForwarding: ptr.To(true),
66+
name: "crc cluster",
67+
clusterType: types.ClusterTypeCRC,
68+
wantConfig: deployer.Config{
69+
Central: deployer.CentralConfig{
70+
Exposure: ptr.To(types.ExposureNone),
71+
PortForwarding: ptr.To(true),
72+
},
73+
},
6274
},
6375
{
64-
name: "gke cluster",
65-
clusterType: types.ClusterTypeInfraGKE,
66-
wantResourceProfile: types.ResourceProfileMedium,
67-
wantExposure: ptr.To(types.ExposureLoadBalancer),
68-
wantPortForwarding: ptr.To(false),
76+
name: "gke cluster",
77+
clusterType: types.ClusterTypeInfraGKE,
78+
wantConfig: deployer.Config{
79+
Central: deployer.CentralConfig{
80+
Exposure: ptr.To(types.ExposureLoadBalancer),
81+
PortForwarding: ptr.To(false),
82+
},
83+
},
6984
},
7085
{
71-
name: "openshift cluster",
72-
clusterType: types.ClusterTypeInfraOpenShift4,
73-
wantResourceProfile: types.ResourceProfileMedium,
74-
wantExposure: ptr.To(types.ExposureLoadBalancer),
75-
wantPortForwarding: ptr.To(false),
86+
name: "openshift cluster",
87+
clusterType: types.ClusterTypeInfraOpenShift4,
88+
wantConfig: deployer.Config{
89+
Central: deployer.CentralConfig{
90+
Exposure: ptr.To(types.ExposureLoadBalancer),
91+
PortForwarding: ptr.To(false),
92+
},
93+
},
94+
},
95+
{
96+
name: "cluster does not override existing values",
97+
clusterType: types.ClusterTypeInfraGKE,
98+
config: deployer.Config{
99+
Central: deployer.CentralConfig{
100+
Exposure: ptr.To(types.ExposureNone),
101+
PortForwarding: ptr.To(true),
102+
},
103+
},
104+
wantConfig: deployer.Config{
105+
Central: deployer.CentralConfig{
106+
Exposure: ptr.To(types.ExposureNone),
107+
PortForwarding: ptr.To(true),
108+
},
109+
},
76110
},
77111
}
78112

79113
for _, tt := range tests {
80114
t.Run(tt.name, func(t *testing.T) {
81-
config := deployer.NewConfig()
115+
config := tt.config
82116
_, err := ApplyClusterDefaults(tt.clusterType, &config)
83117
require.NoError(t, err)
84118

85-
gotResourceProfile := ResolveAutoResourceProfile(tt.clusterType)
86-
if gotResourceProfile != tt.wantResourceProfile {
87-
t.Errorf("Apply() resources = %v, want %v", gotResourceProfile, tt.wantResourceProfile)
88-
}
89-
90-
if tt.wantExposure == nil {
119+
if tt.wantConfig.Central.Exposure == nil {
91120
assert.Nil(t, config.Central.Exposure, "central exposure is not nil")
92121
} else {
93122
require.NotNil(t, config.Central.Exposure, "central exposure is nil")
94-
assert.Equal(t, *tt.wantExposure, *config.Central.Exposure,
95-
"exposure = %v, want %v", *config.Central.Exposure, *tt.wantExposure)
123+
assert.Equal(t, *tt.wantConfig.Central.Exposure, *config.Central.Exposure,
124+
"exposure = %v, want %v", *config.Central.Exposure, *tt.wantConfig.Central.Exposure)
96125
}
97126

98-
if tt.wantPortForwarding == nil {
127+
if tt.wantConfig.Central.PortForwarding == nil {
99128
assert.Nil(t, config.Central.PortForwarding, "central port forwarding is not nil")
100129
} else {
101130
require.NotNil(t, config.Central.PortForwarding, "central port forwarding is nil")
102-
assert.Equal(t, *tt.wantPortForwarding, *config.Central.PortForwarding,
103-
"portForward = %v, want %v", *config.Central.PortForwarding, *tt.wantPortForwarding)
131+
assert.Equal(t, *tt.wantConfig.Central.PortForwarding, *config.Central.PortForwarding,
132+
"portForward = %v, want %v", *config.Central.PortForwarding, *tt.wantConfig.Central.PortForwarding)
104133
}
105134
})
106135
}
107136
}
137+
138+
func TestResolveAutoResourceProfile(t *testing.T) {
139+
tests := []struct {
140+
name string
141+
clusterType types.ClusterType
142+
want types.ResourceProfile
143+
}{
144+
{
145+
name: "kind cluster",
146+
clusterType: types.ClusterTypeKind,
147+
want: types.ResourceProfileSmall,
148+
},
149+
{
150+
name: "minikube cluster",
151+
clusterType: types.ClusterTypeMinikube,
152+
want: types.ResourceProfileSmall,
153+
},
154+
{
155+
name: "k3s cluster",
156+
clusterType: types.ClusterTypeK3s,
157+
want: types.ResourceProfileSmall,
158+
},
159+
{
160+
name: "crc cluster",
161+
clusterType: types.ClusterTypeCRC,
162+
want: types.ResourceProfileSmall,
163+
},
164+
{
165+
name: "gke cluster",
166+
clusterType: types.ClusterTypeInfraGKE,
167+
want: types.ResourceProfileMedium,
168+
},
169+
{
170+
name: "openshift cluster",
171+
clusterType: types.ClusterTypeInfraOpenShift4,
172+
want: types.ResourceProfileMedium,
173+
},
174+
{
175+
name: "unknown cluster type",
176+
clusterType: types.ClusterTypeUnknown,
177+
want: types.ResourceProfileAcsDefaults,
178+
},
179+
}
180+
181+
for _, tt := range tests {
182+
t.Run(tt.name, func(t *testing.T) {
183+
got := ResolveAutoResourceProfile(tt.clusterType)
184+
assert.Equal(t, tt.want, got)
185+
})
186+
}
187+
}

0 commit comments

Comments
 (0)