diff --git a/pkg/apis/elasticsearch/v1/name.go b/pkg/apis/elasticsearch/v1/name.go index bfb37c8ec3..4d2dd1c036 100644 --- a/pkg/apis/elasticsearch/v1/name.go +++ b/pkg/apis/elasticsearch/v1/name.go @@ -54,8 +54,14 @@ func validateNames(es *Elasticsearch) error { if len(es.Name) > common_name.MaxResourceNameLength { return errors.Errorf("name exceeds maximum allowed length of %d", common_name.MaxResourceNameLength) } + nodeSetNames := map[string]struct{}{} // validate ssets for _, nodeSet := range es.Spec.NodeSets { + if _, ok := nodeSetNames[nodeSet.Name]; ok { + return errors.Errorf("duplicated nodeSet name: '%s'", nodeSet.Name) + } + nodeSetNames[nodeSet.Name] = struct{}{} + if errs := apimachineryvalidation.NameIsDNSSubdomain(nodeSet.Name, false); len(errs) > 0 { return errors.Errorf("invalid nodeSet name '%s': [%s]", nodeSet.Name, strings.Join(errs, ",")) } diff --git a/pkg/apis/elasticsearch/v1/name_test.go b/pkg/apis/elasticsearch/v1/name_test.go index 83bbe150bb..22688b0c62 100644 --- a/pkg/apis/elasticsearch/v1/name_test.go +++ b/pkg/apis/elasticsearch/v1/name_test.go @@ -46,6 +46,13 @@ func TestValidate(t *testing.T) { wantErr: true, wantErrMsg: "invalid nodeSet name", }, + { + name: "duplicated nodeSet names", + esName: "test-es", + nodeSpecNames: []string{"default", "default"}, + wantErr: true, + wantErrMsg: "duplicated nodeSet name", + }, } for _, tc := range testCases { diff --git a/pkg/apis/elasticsearch/v1beta1/name.go b/pkg/apis/elasticsearch/v1beta1/name.go index 2fd8bb0bf7..be79615025 100644 --- a/pkg/apis/elasticsearch/v1beta1/name.go +++ b/pkg/apis/elasticsearch/v1beta1/name.go @@ -54,8 +54,14 @@ func validateNames(es *Elasticsearch) error { if len(es.Name) > common_name.MaxResourceNameLength { return errors.Errorf("name exceeds maximum allowed length of %d", common_name.MaxResourceNameLength) } + nodeSetNames := map[string]struct{}{} // validate ssets for _, nodeSet := range es.Spec.NodeSets { + if _, ok := nodeSetNames[nodeSet.Name]; ok { + return errors.Errorf("duplicated nodeSet name: '%s'", nodeSet.Name) + } + + nodeSetNames[nodeSet.Name] = struct{}{} if errs := apimachineryvalidation.NameIsDNSSubdomain(nodeSet.Name, false); len(errs) > 0 { return errors.Errorf("invalid nodeSet name '%s': [%s]", nodeSet.Name, strings.Join(errs, ",")) } diff --git a/pkg/apis/elasticsearch/v1beta1/name_test.go b/pkg/apis/elasticsearch/v1beta1/name_test.go index 09f8b77a7f..876108ff06 100644 --- a/pkg/apis/elasticsearch/v1beta1/name_test.go +++ b/pkg/apis/elasticsearch/v1beta1/name_test.go @@ -46,6 +46,13 @@ func TestValidate(t *testing.T) { wantErr: true, wantErrMsg: "invalid nodeSet name", }, + { + name: "duplicated nodeSet names", + esName: "test-es", + nodeSpecNames: []string{"default", "default"}, + wantErr: true, + wantErrMsg: "duplicated nodeSet name", + }, } for _, tc := range testCases {