Skip to content

Commit a2c6b11

Browse files
authored
fix: lint subcommand for workload ref rollout (argoproj#1328)
Signed-off-by: Hui Kang <[email protected]>
1 parent 2339ddc commit a2c6b11

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

pkg/apis/rollouts/validation/validation.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ func ValidateRolloutSpec(rollout *v1alpha1.Rollout, fldPath *field.Path) field.E
7171
replicas := defaults.GetReplicasOrDefault(spec.Replicas)
7272
allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(replicas), fldPath.Child("replicas"))...)
7373

74-
if spec.Selector == nil {
75-
message := fmt.Sprintf(MissingFieldMessage, ".spec.selector")
76-
allErrs = append(allErrs, field.Required(fldPath.Child("selector"), message))
77-
} else {
78-
allErrs = append(allErrs, unversionedvalidation.ValidateLabelSelector(spec.Selector, fldPath.Child("selector"))...)
79-
if len(spec.Selector.MatchLabels)+len(spec.Selector.MatchExpressions) == 0 {
80-
allErrs = append(allErrs, field.Invalid(fldPath.Child("selector"), spec.Selector, "empty selector is invalid for deployment"))
74+
if spec.WorkloadRef == nil {
75+
if spec.Selector == nil {
76+
message := fmt.Sprintf(MissingFieldMessage, ".spec.selector")
77+
allErrs = append(allErrs, field.Required(fldPath.Child("selector"), message))
78+
} else {
79+
allErrs = append(allErrs, unversionedvalidation.ValidateLabelSelector(spec.Selector, fldPath.Child("selector"))...)
80+
if len(spec.Selector.MatchLabels)+len(spec.Selector.MatchExpressions) == 0 {
81+
allErrs = append(allErrs, field.Invalid(fldPath.Child("selector"), spec.Selector, "empty selector is invalid for deployment"))
82+
}
8183
}
8284
}
8385

@@ -119,9 +121,12 @@ func ValidateRolloutSpec(rollout *v1alpha1.Rollout, fldPath *field.Path) field.E
119121
AllowMultipleHugePageResources: true,
120122
AllowDownwardAPIHugePages: true,
121123
}
122-
allErrs = append(allErrs, validation.ValidatePodTemplateSpecForReplicaSet(&template, selector, replicas, fldPath.Child("template"), opts)...)
123-
}
124124

125+
// Skip validating empty template for rollout resolved from ref
126+
if rollout.Spec.TemplateResolvedFromRef || spec.WorkloadRef == nil {
127+
allErrs = append(allErrs, validation.ValidatePodTemplateSpecForReplicaSet(&template, selector, replicas, fldPath.Child("template"), opts)...)
128+
}
129+
}
125130
allErrs = append(allErrs, apivalidation.ValidateNonnegativeField(int64(spec.MinReadySeconds), fldPath.Child("minReadySeconds"))...)
126131

127132
revisionHistoryLimit := defaults.GetRevisionHistoryLimitOrDefault(rollout)

pkg/apis/rollouts/validation/validation_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ func TestValidateRollout(t *testing.T) {
4747
assert.Equal(t, message, allErrs[0].Detail)
4848
})
4949

50+
t.Run("empty selector", func(t *testing.T) {
51+
invalidRo := ro.DeepCopy()
52+
invalidRo.Spec.Selector = &metav1.LabelSelector{}
53+
allErrs := ValidateRollout(invalidRo)
54+
assert.Equal(t, "empty selector is invalid for deployment", allErrs[0].Detail)
55+
})
56+
5057
t.Run("invalid progressDeadlineSeconds", func(t *testing.T) {
5158
invalidRo := ro.DeepCopy()
5259
invalidRo.Spec.MinReadySeconds = defaults.GetProgressDeadlineSecondsOrDefault(invalidRo) + 1
@@ -341,8 +348,20 @@ func TestWorkloadRefWithTemplate(t *testing.T) {
341348
t.Run("workload reference with template", func(t *testing.T) {
342349
ro := ro.DeepCopy()
343350
allErrs := ValidateRollout(ro)
344-
assert.Equal(t, 2, len(allErrs))
351+
assert.Equal(t, 1, len(allErrs))
345352
assert.EqualError(t, allErrs[0], "spec.template: Internal error: template must be empty for workload reference rollout")
346-
assert.EqualError(t, allErrs[1], "spec.template.spec.containers: Required value")
353+
})
354+
t.Run("valid workload reference with selector", func(t *testing.T) {
355+
ro := ro.DeepCopy()
356+
ro.Spec.Template = corev1.PodTemplateSpec{}
357+
allErrs := ValidateRollout(ro)
358+
assert.Equal(t, 0, len(allErrs))
359+
})
360+
t.Run("valid workload reference without selector", func(t *testing.T) {
361+
ro := ro.DeepCopy()
362+
ro.Spec.Selector = nil
363+
ro.Spec.Template = corev1.PodTemplateSpec{}
364+
allErrs := ValidateRollout(ro)
365+
assert.Equal(t, 0, len(allErrs))
347366
})
348367
}

pkg/kubectl-argo-rollouts/cmd/lint/lint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestLintValidRollout(t *testing.T) {
1515
cmd := NewCmdLint(o)
1616
cmd.PersistentPreRunE = o.PersistentPreRunE
1717

18-
for _, filename := range []string{"testdata/valid.yml", "testdata/valid-with-another-empty-object.yml"} {
18+
for _, filename := range []string{"testdata/valid.yml", "testdata/valid-workload-ref.yaml", "testdata/valid-with-another-empty-object.yml"} {
1919
cmd.SetArgs([]string{"-f", filename})
2020
err := cmd.Execute()
2121
assert.NoError(t, err)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: argoproj.io/v1alpha1 # Create a rollout resource
2+
kind: Rollout
3+
metadata:
4+
name: rollout-ref-deployment
5+
spec:
6+
replicas: 5
7+
workloadRef: # Reference an existing Deployment using workloadRef field
8+
apiVersion: apps/v1
9+
kind: Deployment
10+
name: rollout-ref-deployment
11+
strategy:
12+
canary:
13+
steps:
14+
- setWeight: 20
15+
- pause: {duration: 10s}

0 commit comments

Comments
 (0)