-
Notifications
You must be signed in to change notification settings - Fork 81
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
CLOUDP-280230: Network peering controller and CRD #1930
Draft
josvazg
wants to merge
29
commits into
main
Choose a base branch
from
CLOUDP-280230/net-peering-controller
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
c5d6c81
CLOUDP-280230: Fix network peering translation layer & contract tests
josvazg 230f3dc
CLOUDP-280230: Add network peering CRD
josvazg 347dc04
CLOUDP-280230: Add network peering controller & tests
josvazg 9815d27
Unit tests draft
josvazg 0f51911
Treat container in use as a good cleanup exit
josvazg 8cd506a
Remove unused reason
josvazg 8623af4
Fix to comply to cluster usage over manager
josvazg 595f862
Add AtlasNetworkContainer CRD
josvazg fbe0993
Refactor dual ref CEL validations
josvazg 3f608f4
Add peering & container CEL validations
josvazg 9e92bbb
add controller draft
josvazg f3e5d52
start network container controller
josvazg 27f89ab
Add indexers and improve their test coverage
josvazg f559531
draft container controller with unit-tests
josvazg 5b648c4
container passing custom resource tests
josvazg c2ba44f
add network container mocks
josvazg 7ec7506
draft
josvazg edbcecb
more on network container controller tests
josvazg 133a93d
fixup
josvazg 7b9cb58
fixup project and kustomization
josvazg bc66bd8
Fix alias of containers clobbing the one from peerings
josvazg b18044a
Remove redundant controller registration
josvazg ae17c3f
Update helm charts with network container support
josvazg 2e98e19
Complete container state machine with unit tests
josvazg 3d95daf
Cover finalizers valdation in unit tests
josvazg 93643f8
Use fixed helm-charts
josvazg 4245a52
tweak state machine due to manual tests
josvazg 5f3ea7d
Manual testsing fixes
josvazg 06c0e1b
Implement greedyness deactivation and skips
josvazg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright 2025 MongoDB. | ||
|
||
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 ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api" | ||
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/status" | ||
) | ||
|
||
func init() { | ||
SchemeBuilder.Register(&AtlasNetworkContainer{}, &AtlasNetworkContainerList{}) | ||
} | ||
|
||
// AtlasNetworkContainer is the Schema for the AtlasNetworkContainer API | ||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
// +kubebuilder:object:root=true | ||
// +kubebuilder:printcolumn:name="Ready",type=string,JSONPath=`.status.conditions[?(@.type=="Ready")].status` | ||
// +kubebuilder:printcolumn:name="Provider",type=string,JSONPath=`.spec.provider` | ||
// +kubebuilder:printcolumn:name="Id",type=string,JSONPath=`.status.id` | ||
// +kubebuilder:printcolumn:name="Provisioned",type=string,JSONPath=`.status.provisioned` | ||
// +kubebuilder:subresource:status | ||
// +groupName:=atlas.mongodb.com | ||
// +kubebuilder:resource:categories=atlas,shortName=anc | ||
type AtlasNetworkContainer struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec AtlasNetworkContainerSpec `json:"spec,omitempty"` | ||
Status status.AtlasNetworkContainerStatus `json:"status,omitempty"` | ||
} | ||
|
||
//+kubebuilder:object:root=true | ||
|
||
// AtlasNetworkContainerList contains a list of AtlasNetworkContainer | ||
type AtlasNetworkContainerList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []AtlasNetworkContainer `json:"items"` | ||
} | ||
|
||
// +kubebuilder:validation:XValidation:rule="(has(self.externalProjectRef) && !has(self.projectRef)) || (!has(self.externalProjectRef) && has(self.projectRef))",message="must define only one project reference through externalProjectRef or projectRef" | ||
// +kubebuilder:validation:XValidation:rule="(has(self.externalProjectRef) && has(self.connectionSecret)) || !has(self.externalProjectRef)",message="must define a local connection secret when referencing an external project" | ||
// +kubebuilder:validation:XValidation:rule="(self.provider == 'GCP' && !has(self.region)) || (self.provider != 'GCP')",message="must not set region for GCP containers" | ||
// +kubebuilder:validation:XValidation:rule="((self.provider == 'AWS' || self.provider == 'AZURE') && has(self.region)) || (self.provider == 'GCP')",message="must set region for AWS and Azure containers" | ||
|
||
// AtlasNetworkContainerSpec defines the desired state of an AtlasNetworkContainer | ||
type AtlasNetworkContainerSpec struct { | ||
ProjectDualReference `json:",inline"` | ||
|
||
// Provider is the name of the cloud provider hosting the network container | ||
// +kubebuilder:validation:Enum=AWS;GCP;AZURE | ||
// +kubebuilder:validation:Required | ||
Provider string `json:"provider"` | ||
|
||
AtlasNetworkContainerConfig `json:",inline"` | ||
} | ||
|
||
// AtlasNetworkContainerConfig defines the Atlas specifics of the desired state of a Network Container | ||
type AtlasNetworkContainerConfig struct { | ||
// ContainerRegion is the provider region name of Atlas network peer container in Atlas region format | ||
// This is required by AWS and Azure, but not used by GCP | ||
// +optional | ||
Region string `json:"region,omitempty"` | ||
|
||
// Atlas CIDR. It needs to be set if ContainerID is not set. | ||
// +required | ||
CIDRBlock string `json:"cidrBlock"` | ||
} | ||
|
||
func (np *AtlasNetworkContainer) GetStatus() api.Status { | ||
return np.Status | ||
} | ||
|
||
func (np *AtlasNetworkContainer) Credentials() *api.LocalObjectReference { | ||
return np.Spec.ConnectionSecret | ||
} | ||
|
||
func (np *AtlasNetworkContainer) ProjectDualRef() *ProjectDualReference { | ||
return &np.Spec.ProjectDualReference | ||
} | ||
|
||
func (np *AtlasNetworkContainer) UpdateStatus(conditions []api.Condition, options ...api.Option) { | ||
np.Status.Conditions = conditions | ||
np.Status.ObservedGeneration = np.ObjectMeta.Generation | ||
|
||
for _, o := range options { | ||
v := o.(status.AtlasNetworkContainerStatusOption) | ||
v(&np.Status) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package v1 | ||
|
||
import ( | ||
"fmt" | ||
"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/api/v1/provider" | ||
"github.com/mongodb/mongodb-atlas-kubernetes/v2/test/helper/cel" | ||
) | ||
|
||
func TestGCPRegionCELCcheck(t *testing.T) { | ||
for _, tc := range []struct { | ||
title string | ||
obj *AtlasNetworkContainer | ||
expectedErrors []string | ||
}{ | ||
{ | ||
title: "GCP fails with a region", | ||
obj: &AtlasNetworkContainer{ | ||
Spec: AtlasNetworkContainerSpec{ | ||
Provider: string(provider.ProviderGCP), | ||
AtlasNetworkContainerConfig: AtlasNetworkContainerConfig{ | ||
Region: "some-region", | ||
}, | ||
}, | ||
}, | ||
expectedErrors: []string{"spec: Invalid value: \"object\": must not set region for GCP containers"}, | ||
}, | ||
{ | ||
title: "GCP succeeds without a region", | ||
obj: &AtlasNetworkContainer{ | ||
Spec: AtlasNetworkContainerSpec{ | ||
Provider: string(provider.ProviderGCP), | ||
}, | ||
}, | ||
}, | ||
{ | ||
title: "AWS succeeds with a region", | ||
obj: &AtlasNetworkContainer{ | ||
Spec: AtlasNetworkContainerSpec{ | ||
Provider: string(provider.ProviderAWS), | ||
AtlasNetworkContainerConfig: AtlasNetworkContainerConfig{ | ||
Region: "some-region", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
title: "Azure succeeds with a region", | ||
obj: &AtlasNetworkContainer{ | ||
Spec: AtlasNetworkContainerSpec{ | ||
Provider: string(provider.ProviderAzure), | ||
AtlasNetworkContainerConfig: AtlasNetworkContainerConfig{ | ||
Region: "some-region", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
title: "AWS fails without a region", | ||
obj: &AtlasNetworkContainer{ | ||
Spec: AtlasNetworkContainerSpec{ | ||
Provider: string(provider.ProviderAWS), | ||
}, | ||
}, | ||
expectedErrors: []string{"spec: Invalid value: \"object\": must set region for AWS and Azure containers"}, | ||
}, | ||
{ | ||
title: "Azure fails without a region", | ||
obj: &AtlasNetworkContainer{ | ||
Spec: AtlasNetworkContainerSpec{ | ||
Provider: string(provider.ProviderAzure), | ||
}, | ||
}, | ||
expectedErrors: []string{"spec: Invalid value: \"object\": must set region for AWS and Azure containers"}, | ||
}, | ||
} { | ||
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"} | ||
unstructuredObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&tc.obj) | ||
require.NoError(t, err) | ||
|
||
crdPath := "../../config/crd/bases/atlas.mongodb.com_atlasnetworkcontainers.yaml" | ||
validator, err := cel.VersionValidatorFromFile(t, crdPath, "v1") | ||
assert.NoError(t, err) | ||
errs := validator(unstructuredObject, nil) | ||
|
||
require.Equal(t, len(tc.expectedErrors), len(errs), fmt.Sprintf("errs: %v", errs)) | ||
|
||
for i, err := range errs { | ||
assert.Equal(t, tc.expectedErrors[i], err.Error()) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: are we following the rules described in https://ahmet.im/blog/crd-generation-pitfalls/index.html#explicit-required-or-optional-on-every-field for all spec fields?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will double check that for when we surface this again, both for the NetworkContainer and the PeeringConnection CRDs.