Skip to content

Commit

Permalink
Merge pull request #388 from cybozu-go/d-kuro/containername-validate
Browse files Browse the repository at this point in the history
Validate that container name is non-nil in validation webhook
  • Loading branch information
masa213f authored Mar 10, 2022
2 parents b6d5ea4 + 162f714 commit 74f2ef6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/v1beta2/mysqlcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (s MySQLClusterSpec) validateCreate() field.ErrorList {
mysqldIndex := -1
for i, container := range s.PodTemplate.Spec.Containers {
if container.Name == nil {
allErrs = append(allErrs, field.Forbidden(pp.Index(i), "container name is required"))
continue
}

Expand Down Expand Up @@ -192,6 +193,7 @@ func (s MySQLClusterSpec) validateCreate() field.ErrorList {
pp = p.Child("initContainers")
for i, container := range s.PodTemplate.Spec.InitContainers {
if container.Name == nil {
allErrs = append(allErrs, field.Forbidden(pp.Index(i), "init container name is required"))
continue
}

Expand Down
18 changes: 18 additions & 0 deletions api/v1beta2/mysqlcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ var _ = Describe("MySQLCluster Webhook", func() {
Expect(err).To(HaveOccurred())
})

It("should deny without container name", func() {
r := makeMySQLCluster()
spec := (corev1ac.PodSpecApplyConfiguration)(r.Spec.PodTemplate.Spec)
spec.WithContainers(corev1ac.Container().WithImage("image:dev"))
r.Spec.PodTemplate.Spec = (mocov1beta2.PodSpecApplyConfiguration)(spec)
err := k8sClient.Create(ctx, r)
Expect(err).To(HaveOccurred())
})

It("should deny without init container name", func() {
r := makeMySQLCluster()
spec := (corev1ac.PodSpecApplyConfiguration)(r.Spec.PodTemplate.Spec)
spec.WithInitContainers(corev1ac.Container().WithImage("image:dev"))
r.Spec.PodTemplate.Spec = (mocov1beta2.PodSpecApplyConfiguration)(spec)
err := k8sClient.Create(ctx, r)
Expect(err).To(HaveOccurred())
})

It("should deny mysqld container using reserved port", func() {
for _, port := range []*corev1ac.ContainerPortApplyConfiguration{
corev1ac.ContainerPort().WithContainerPort(constants.MySQLPort),
Expand Down

0 comments on commit 74f2ef6

Please sign in to comment.