Skip to content

Commit 9ac01a8

Browse files
committed
fix update strategy default handling
Signed-off-by: Zakhar Dvurechensky <72825626+Zakharden@users.noreply.github.com>
1 parent e7d5baa commit 9ac01a8

3 files changed

Lines changed: 55 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: 6 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
},

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)