Skip to content

CLOUDP-330314: Disallow project/deployment renaming #2441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/v1/atlasdeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type AdvancedDeploymentSpec struct {
// Can only contain ASCII letters, numbers, and hyphens.
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern:=^[a-zA-Z0-9][a-zA-Z0-9-]*$
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Name cannot be modified after deployment creation"
Name string `json:"name,omitempty"`
// Flag that indicates whether the deployment should be paused.
Paused *bool `json:"paused,omitempty"`
Expand Down
69 changes: 69 additions & 0 deletions api/v1/atlasdeployment_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2025 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/common"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/cel"
)

func TestDeploymentCELChecks(t *testing.T) {
for _, tc := range []struct {
title string
old, obj *AtlasDeployment
expectedErrors []string
}{
{
title: "Cannot rename a deployment",
old: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
DeploymentSpec: &AdvancedDeploymentSpec{
Name: "name-old",
},
},
},
obj: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
DeploymentSpec: &AdvancedDeploymentSpec{
Name: "name-new",
},
},
},
expectedErrors: []string{"spec.deploymentSpec.name: Invalid value: \"string\": Name cannot be modified after deployment creation"},
},
} {
t.Run(tc.title, func(t *testing.T) {
// inject a project to avoid other CEL validations being hit
tc.obj.Spec.ProjectRef = &common.ResourceRefNamespaced{Name: "some-project"}
unstructuredOldObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&tc.old)
require.NoError(t, err)
unstructuredObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&tc.obj)
require.NoError(t, err)

crdPath := "../../config/crd/bases/atlas.mongodb.com_atlasdeployments.yaml"
validator, err := cel.VersionValidatorFromFile(t, crdPath, "v1")
assert.NoError(t, err)
errs := validator(unstructuredObject, unstructuredOldObject)

require.Equal(t, tc.expectedErrors, cel.ErrorListAsStrings(errs))
})
}
}
1 change: 1 addition & 0 deletions api/v1/atlasproject_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
type AtlasProjectSpec struct {

// Name is the name of the Project that is created in Atlas by the Operator if it doesn't exist yet.
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Name cannot be modified after project creation"
Name string `json:"name"`

// RegionUsageRestrictions designate the project's AWS region when using Atlas for Government.
Expand Down
41 changes: 41 additions & 0 deletions api/v1/atlasproject_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/atlas-sdk/v20250312002/admin"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/yaml"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/common"
internalcmp "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/cmp"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/cel"
)

func TestSpecEquality(t *testing.T) {
Expand Down Expand Up @@ -138,3 +142,40 @@ func mustMarshal(t *testing.T, what any) string {
}
return string(result)
}

func TestProjectCELChecks(t *testing.T) {
for _, tc := range []struct {
title string
old, obj *AtlasProject
expectedErrors []string
}{
{
title: "Cannot rename a project",
old: &AtlasProject{
Spec: AtlasProjectSpec{
Name: "name-old",
},
},
obj: &AtlasProject{
Spec: AtlasProjectSpec{
Name: "name-new",
},
},
expectedErrors: []string{"spec.name: Invalid value: \"string\": Name cannot be modified after project creation"},
},
} {
t.Run(tc.title, func(t *testing.T) {
unstructuredOldObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&tc.old)
require.NoError(t, err)
unstructuredObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&tc.obj)
require.NoError(t, err)

crdPath := "../../config/crd/bases/atlas.mongodb.com_atlasprojects.yaml"
validator, err := cel.VersionValidatorFromFile(t, crdPath, "v1")
assert.NoError(t, err)
errs := validator(unstructuredObject, unstructuredOldObject)

require.Equal(t, tc.expectedErrors, cel.ErrorListAsStrings(errs))
})
}
}
3 changes: 3 additions & 0 deletions config/crd/bases/atlas.mongodb.com_atlasdeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ spec:
Can only contain ASCII letters, numbers, and hyphens.
pattern: ^[a-zA-Z0-9][a-zA-Z0-9-]*$
type: string
x-kubernetes-validations:
- message: Name cannot be modified after deployment creation
rule: self == oldSelf
paused:
description: Flag that indicates whether the deployment should
be paused.
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/atlas.mongodb.com_atlasprojects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,9 @@ spec:
description: Name is the name of the Project that is created in Atlas
by the Operator if it doesn't exist yet.
type: string
x-kubernetes-validations:
- message: Name cannot be modified after project creation
rule: self == oldSelf
networkPeers:
description: NetworkPeers is a list of Network Peers configured for
the current Project.
Expand Down
Loading