Skip to content

Commit

Permalink
Add validation for r.TargetName
Browse files Browse the repository at this point in the history
  • Loading branch information
maboehm committed Aug 2, 2024
1 parent b6b0005 commit 8b06482
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/flux/v1alpha1/validation/fluxconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
v1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"

Expand Down Expand Up @@ -125,6 +126,9 @@ func ValidateAdditionalSecretResources(additionalResources []fluxv1alpha1.Additi
}

for i, r := range additionalResources {
if ptr.Deref(r.TargetName, "") != "" && len(validation.IsDNS1123Subdomain(*r.TargetName)) > 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Index(i).Child("targetName"), *r.TargetName, "must be a valid resource name"))
}
allErrs = append(allErrs, validateSecretResource(shoot.Spec.Resources, fldPath.Index(i).Child("name"), r.Name)...)
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/apis/flux/v1alpha1/validation/fluxconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ var _ = Describe("FluxConfig validation", func() {
{Name: "valid"},
{Name: "wrong-kind"},
{Name: "no-ref"},
{Name: "valid", TargetName: ptr.To("invalid-name-")},
}
shoot.Spec.Resources = []gardencorev1beta1.NamedResourceReference{
{
Expand All @@ -374,14 +375,19 @@ var _ = Describe("FluxConfig validation", func() {
).To(ConsistOf(
PointTo(MatchFields(IgnoreExtras, Fields{
"Type": Equal(field.ErrorTypeInvalid),
"Field": Equal("root[1]"),
"Field": Equal("root[1].name"),
"Detail": ContainSubstring("is not a secret"),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"Type": Equal(field.ErrorTypeInvalid),
"Field": Equal("root[2]"),
"Field": Equal("root[2].name"),
"Detail": ContainSubstring("does not match any of the resource names"),
})),
PointTo(MatchFields(IgnoreExtras, Fields{
"Type": Equal(field.ErrorTypeInvalid),
"Field": Equal("root[3].targetName"),
"Detail": ContainSubstring("must be a valid resource name"),
})),
))
})
})
Expand Down

0 comments on commit 8b06482

Please sign in to comment.