Skip to content

Commit 9884d34

Browse files
authored
fix update strategy default handling (#1179)
* fix update strategy default handling Signed-off-by: Zakhar Dvurechensky <72825626+Zakharden@users.noreply.github.com> * test update strategy defaults --------- Signed-off-by: Zakhar Dvurechensky <72825626+Zakharden@users.noreply.github.com>
1 parent 9aa05d4 commit 9884d34

3 files changed

Lines changed: 112 additions & 8 deletions

File tree

pkg/templates/updateconfig/template.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"golang.stackrox.io/kube-linter/pkg/templates"
1818
"golang.stackrox.io/kube-linter/pkg/templates/updateconfig/internal/params"
1919

20+
ocsAppsv1 "github.com/openshift/api/apps/v1"
21+
appsv1 "k8s.io/api/apps/v1"
2022
"k8s.io/apimachinery/pkg/util/intstr"
2123
)
2224

@@ -111,6 +113,21 @@ func needsRollingUpdateDefinition(p params.Params) bool {
111113
p.MinSurge != "" || p.MaxSurge != "")
112114
}
113115

116+
func defaultStrategyType(object lintcontext.Object) (string, bool) {
117+
switch object.K8sObject.(type) {
118+
case *appsv1.Deployment:
119+
return string(appsv1.RollingUpdateDeploymentStrategyType), true
120+
case *appsv1.DaemonSet:
121+
return string(appsv1.RollingUpdateDaemonSetStrategyType), true
122+
case *appsv1.StatefulSet:
123+
return string(appsv1.RollingUpdateStatefulSetStrategyType), true
124+
case *ocsAppsv1.DeploymentConfig:
125+
return string(ocsAppsv1.DeploymentStrategyTypeRolling), true
126+
default:
127+
return "", false
128+
}
129+
}
130+
114131
func init() {
115132
templates.Register(check.Template{
116133
HumanName: "Update configuration",
@@ -165,10 +182,16 @@ func init() {
165182
if !strategy.TypeExists {
166183
return nil
167184
}
168-
if !compiledRegex.MatchString(strategy.Type) {
185+
strategyType := strategy.Type
186+
if strategyType == "" {
187+
if defaultType, ok := defaultStrategyType(object); ok {
188+
strategyType = defaultType
189+
}
190+
}
191+
if !compiledRegex.MatchString(strategyType) {
169192
newD := diagnostic.Diagnostic{
170193
Message: fmt.Sprintf("object has %s strategy type but must match regex %s",
171-
stringutils.Ternary(strategy.Type != "", strategy.Type, "no"), p.StrategyTypeRegex)}
194+
stringutils.Ternary(strategyType != "", strategyType, "no"), p.StrategyTypeRegex)}
172195
diagnostics = append(diagnostics, newD)
173196
}
174197
if !strategy.RollingConfigExists {

pkg/templates/updateconfig/template_test.go

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (s *UpgradeConfigTestSuite) addReplicationControllerWithReplicas(name strin
6464

6565
func (s *UpgradeConfigTestSuite) TestInvalidStrategyType() {
6666
const (
67-
noExplicitStrategy = "no-explicit-strategy"
67+
noExplicitDeployment = "no-explicit-deployment-strategy"
68+
noExplicitDeploymentConfig = "no-explicit-deployment-config-strategy"
6869
deploymentWithStrategy = "deployment-strategy-recreate"
6970
daemonSetWithStrategy = "daemon-set-strategy-on-delete"
7071
deploymentConfigWithStrategy = "deployment-config-strategy-recreate"
@@ -74,7 +75,8 @@ func (s *UpgradeConfigTestSuite) TestInvalidStrategyType() {
7475
deploymentConfigStrategyType = ocsAppsv1.DeploymentStrategyTypeRecreate
7576
strategyRegex = "^(RollingUpdate|Rolling)$"
7677
)
77-
s.ctx.AddMockDeployment(s.T(), noExplicitStrategy)
78+
s.ctx.AddMockDeployment(s.T(), noExplicitDeployment)
79+
s.ctx.AddMockDeploymentConfig(s.T(), noExplicitDeploymentConfig)
7880
s.addDeploymentWithStrategy(deploymentWithStrategy, appsv1.DeploymentStrategy{Type: deploymentStrategyType})
7981
s.addDaemonSetWithStrategy(daemonSetWithStrategy, appsv1.DaemonSetUpdateStrategy{Type: daemonSetStrategyType})
8082
s.addDeploymentConfigWithStrategy(deploymentConfigWithStrategy, ocsAppsv1.DeploymentStrategy{Type: deploymentConfigStrategyType})
@@ -89,9 +91,8 @@ func (s *UpgradeConfigTestSuite) TestInvalidStrategyType() {
8991
StrategyTypeRegex: strategyRegex,
9092
},
9193
Diagnostics: map[string][]diagnostic.Diagnostic{
92-
noExplicitStrategy: {
93-
{Message: fmt.Sprintf("object has no strategy type but must match regex %s", strategyRegex)},
94-
},
94+
noExplicitDeployment: {},
95+
noExplicitDeploymentConfig: {},
9596
deploymentWithStrategy: {
9697
{Message: deploymentErrorMsg},
9798
},
@@ -108,6 +109,63 @@ func (s *UpgradeConfigTestSuite) TestInvalidStrategyType() {
108109
})
109110
}
110111

112+
func (s *UpgradeConfigTestSuite) TestDefaultStrategyType() {
113+
tests := map[string]struct {
114+
addObject func(name string)
115+
expectedType string
116+
expectedFound bool
117+
}{
118+
"deployment": {
119+
addObject: func(name string) {
120+
s.ctx.AddMockDeployment(s.T(), name)
121+
},
122+
expectedType: string(appsv1.RollingUpdateDeploymentStrategyType),
123+
expectedFound: true,
124+
},
125+
"daemon set": {
126+
addObject: func(name string) {
127+
s.ctx.AddMockDaemonSet(s.T(), name)
128+
},
129+
expectedType: string(appsv1.RollingUpdateDaemonSetStrategyType),
130+
expectedFound: true,
131+
},
132+
"stateful set": {
133+
addObject: func(name string) {
134+
s.ctx.AddObject(name, &appsv1.StatefulSet{})
135+
},
136+
expectedType: string(appsv1.RollingUpdateStatefulSetStrategyType),
137+
expectedFound: true,
138+
},
139+
"deployment config": {
140+
addObject: func(name string) {
141+
s.ctx.AddMockDeploymentConfig(s.T(), name)
142+
},
143+
expectedType: string(ocsAppsv1.DeploymentStrategyTypeRolling),
144+
expectedFound: true,
145+
},
146+
"replication controller": {
147+
addObject: func(name string) {
148+
s.addReplicationControllerWithReplicas(name, 2)
149+
},
150+
expectedFound: false,
151+
},
152+
}
153+
154+
for name, test := range tests {
155+
s.Run(name, func() {
156+
s.SetupTest()
157+
test.addObject(name)
158+
159+
objects := s.ctx.Objects()
160+
s.Require().Len(objects, 1)
161+
defaultType, found := defaultStrategyType(objects[0])
162+
163+
s.Equal(test.expectedFound, found)
164+
s.Equal(test.expectedType, defaultType)
165+
})
166+
}
167+
}
168+
111169
func (s *UpgradeConfigTestSuite) TestValidStrategyType() {
112170
const (
113171
deploymentWithStrategy = "deployment-strategy-recreate"

tests/checks/no-rolling-update-strategy.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,27 @@ metadata:
1313
name: app2
1414
spec:
1515
strategy:
16-
type: Other
16+
type: Other
17+
---
18+
apiVersion: apps/v1
19+
kind: Deployment
20+
metadata:
21+
name: app3
22+
spec:
23+
selector:
24+
matchLabels:
25+
app: app3
26+
template:
27+
metadata:
28+
labels:
29+
app: app3
30+
spec:
31+
containers:
32+
- name: app
33+
image: nginx
34+
---
35+
apiVersion: apps.openshift.io/v1
36+
kind: DeploymentConfig
37+
metadata:
38+
name: app4
39+
spec: {}

0 commit comments

Comments
 (0)