@@ -29,7 +29,9 @@ import (
2929 metal3v1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
3030 "github.com/openstack-k8s-operators/lib-common/modules/common/labels"
3131 "k8s.io/apimachinery/pkg/api/equality"
32+ apierrors "k8s.io/apimachinery/pkg/api/errors"
3233 "k8s.io/apimachinery/pkg/runtime"
34+ "k8s.io/apimachinery/pkg/runtime/schema"
3335 "k8s.io/apimachinery/pkg/util/validation/field"
3436 ctrl "sigs.k8s.io/controller-runtime"
3537 goClient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -72,8 +74,17 @@ func (r *OpenStackBaremetalSet) ValidateCreate() (admission.Warnings, error) {
7274 field .NewPath ("Name" ),
7375 r .Name ,
7476 fmt .Sprintf ("Error validating OpenStackBaremetalSet name %s, name must follow RFC1123" , r .Name )))
77+ return nil , apierrors .NewInvalid (
78+ schema.GroupKind {Group : "baremetal.openstack.org" , Kind : "OpenStackBaremetalSet" },
79+ r .Name ,
80+ errors )
7581 }
7682
83+ // Validate userData and networkData secrets namespace
84+ err := r .ValidateCloudInitSecrets ()
85+ if err != nil {
86+ return nil , err
87+ }
7788 //
7889 // Validate that there are enough available BMHs for the initial requested count
7990 //
@@ -94,6 +105,26 @@ func (r *OpenStackBaremetalSet) ValidateCreate() (admission.Warnings, error) {
94105 return nil , nil
95106}
96107
108+ // ValidateCloudInitSecrets checks if userData and networkData secrets are in the same namespace as bmh
109+ func (r * OpenStackBaremetalSet ) ValidateCloudInitSecrets () error {
110+ var secretsWithIssue []string
111+
112+ for _ , host := range r .Spec .BaremetalHosts {
113+ if host .NetworkData != nil && host .NetworkData .Namespace != r .Spec .BmhNamespace {
114+ secretsWithIssue = append (secretsWithIssue , host .NetworkData .Name )
115+ }
116+ if host .UserData != nil && host .UserData .Namespace != r .Spec .BmhNamespace {
117+ secretsWithIssue = append (secretsWithIssue , host .UserData .Name )
118+ }
119+ }
120+
121+ if len (secretsWithIssue ) > 0 {
122+ return fmt .Errorf ("userData and networkData secrets %v should exist in the bmh namespace %s" ,
123+ secretsWithIssue , r .Spec .BmhNamespace )
124+ }
125+ return nil
126+ }
127+
97128// Validate implements OpenStackBaremetalSetTemplateSpec validation
98129func (spec OpenStackBaremetalSetTemplateSpec ) ValidateTemplate (oldCount int , oldSpec OpenStackBaremetalSetTemplateSpec ) error {
99130 if oldCount > 0 &&
0 commit comments