Skip to content

Commit

Permalink
Use dual ref on deployments
Browse files Browse the repository at this point in the history
Signed-off-by: jose.vazquez <[email protected]>
  • Loading branch information
josvazg committed Jan 4, 2025
1 parent 11a6c72 commit a0aa658
Show file tree
Hide file tree
Showing 26 changed files with 296 additions and 670 deletions.
10 changes: 6 additions & 4 deletions config/crd/bases/atlas.mongodb.com_atlasdeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,9 @@ spec:
- name
type: object
externalProjectRef:
description: ExternalProjectRef holds the Atlas project ID the user
belongs to
description: |-
"externalProjectRef" holds the parent Atlas project ID.
Mutually exclusive with the "projectRef" field
properties:
id:
description: ID is the Atlas project ID
Expand Down Expand Up @@ -655,8 +656,9 @@ spec:
type: integer
type: object
projectRef:
description: Project is a reference to AtlasProject resource the deployment
belongs to
description: |-
"projectRef" is a reference to the parent AtlasProject resource.
Mutually exclusive with the "externalProjectRef" field
properties:
name:
description: Name is the name of the Kubernetes Resource
Expand Down
19 changes: 0 additions & 19 deletions pkg/api/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,3 @@ type ObjectWithCredentials interface {
client.Object
CredentialsProvider
}

// +k8s:deepcopy-gen=false

// ResourceWithCredentials is to be implemented by all CRDs using custom local credentials
type ResourceWithCredentials interface {
CredentialsProvider
GetName() string
GetNamespace() string
}

// LocalCredentialHolder is to be embedded by Specs of CRDs using custom local credentials
type LocalCredentialHolder struct {
// Name of the secret containing Atlas API private and public keys
ConnectionSecret *LocalObjectReference `json:"connectionSecret,omitempty"`
}

func (ch *LocalCredentialHolder) Credentials() *LocalObjectReference {
return ch.ConnectionSecret
}
30 changes: 14 additions & 16 deletions pkg/api/v1/atlasdeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ const (
// +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"
type AtlasDeploymentSpec struct {
api.LocalCredentialHolder `json:",inline"`

// Project is a reference to AtlasProject resource the deployment belongs to
Project *common.ResourceRefNamespaced `json:"projectRef,omitempty"`
// ExternalProjectRef holds the Atlas project ID the user belongs to
ExternalProjectRef *ExternalProjectReference `json:"externalProjectRef,omitempty"`
// ProjectReference is the dual external or kubernetes reference with access credentials
ProjectDualReference `json:",inline"`

// Configuration for the advanced (v1.5) deployment API https://www.mongodb.com/docs/atlas/reference/api/clusters/
// +optional
Expand Down Expand Up @@ -505,10 +501,10 @@ type AtlasDeploymentList struct {

func (c AtlasDeployment) AtlasProjectObjectKey() client.ObjectKey {
ns := c.Namespace
if c.Spec.Project.Namespace != "" {
ns = c.Spec.Project.Namespace
if c.Spec.ProjectRef.Namespace != "" {
ns = c.Spec.ProjectRef.Namespace
}
return kube.ObjectKey(ns, c.Spec.Project.Name)
return kube.ObjectKey(ns, c.Spec.ProjectRef.Name)
}

func (c *AtlasDeployment) GetStatus() api.Status {
Expand All @@ -527,7 +523,11 @@ func (c *AtlasDeployment) UpdateStatus(conditions []api.Condition, options ...ap
}

func (c *AtlasDeployment) Credentials() *api.LocalObjectReference {
return c.Spec.Credentials()
return c.Spec.ConnectionSecret
}

func (c *AtlasDeployment) ProjectDualRef() *ProjectDualReference {
return &c.Spec.ProjectDualReference
}

// ************************************ Builder methods *************************************************
Expand Down Expand Up @@ -633,7 +633,7 @@ func (c *AtlasDeployment) WithAtlasName(name string) *AtlasDeployment {
}

func (c *AtlasDeployment) WithProjectName(projectName string) *AtlasDeployment {
c.Spec.Project = &common.ResourceRefNamespaced{Name: projectName}
c.Spec.ProjectRef = &common.ResourceRefNamespaced{Name: projectName}
return c
}

Expand Down Expand Up @@ -689,14 +689,12 @@ func (c *AtlasDeployment) WithSearchNodes(instanceSize string, count uint8) *Atl
}

func (c *AtlasDeployment) WithExternaLProject(projectID, credentialsName string) *AtlasDeployment {
c.Spec.Project = nil
c.Spec.ProjectRef = nil
c.Spec.ExternalProjectRef = &ExternalProjectReference{
ID: projectID,
}
c.Spec.LocalCredentialHolder = api.LocalCredentialHolder{
ConnectionSecret: &api.LocalObjectReference{
Name: credentialsName,
},
c.Spec.ConnectionSecret = &api.LocalObjectReference{
Name: credentialsName,
}

return c
Expand Down
52 changes: 31 additions & 21 deletions pkg/api/v1/atlasdeployment_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ func TestDeploymentProjectReference(t *testing.T) {
"both project references are set": {
object: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
Project: &common.ResourceRefNamespaced{
Name: "my-project",
},
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
ProjectDualReference: ProjectDualReference{
ProjectRef: &common.ResourceRefNamespaced{
Name: "my-project",
},
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
},
},
},
},
Expand All @@ -34,8 +36,10 @@ func TestDeploymentProjectReference(t *testing.T) {
"external project references is set": {
object: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
ProjectDualReference: ProjectDualReference{
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
},
},
},
},
Expand All @@ -46,8 +50,10 @@ func TestDeploymentProjectReference(t *testing.T) {
"kubernetes project references is set": {
object: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
Project: &common.ResourceRefNamespaced{
Name: "my-project",
ProjectDualReference: ProjectDualReference{
ProjectRef: &common.ResourceRefNamespaced{
Name: "my-project",
},
},
},
},
Expand All @@ -62,8 +68,10 @@ func TestDeploymentExternalProjectReferenceConnectionSecret(t *testing.T) {
"external project references is set without connection secret": {
object: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
ProjectDualReference: ProjectDualReference{
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
},
},
},
},
Expand All @@ -74,10 +82,10 @@ func TestDeploymentExternalProjectReferenceConnectionSecret(t *testing.T) {
"external project references is set with connection secret": {
object: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
},
LocalCredentialHolder: api.LocalCredentialHolder{
ProjectDualReference: ProjectDualReference{
ExternalProjectRef: &ExternalProjectReference{
ID: "my-project-id",
},
ConnectionSecret: &api.LocalObjectReference{
Name: "my-dbuser-connection-secret",
},
Expand All @@ -88,19 +96,21 @@ func TestDeploymentExternalProjectReferenceConnectionSecret(t *testing.T) {
"kubernetes project references is set without connection secret": {
object: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
Project: &common.ResourceRefNamespaced{
Name: "my-project",
ProjectDualReference: ProjectDualReference{
ProjectRef: &common.ResourceRefNamespaced{
Name: "my-project",
},
},
},
},
},
"kubernetes project references is set with connection secret": {
object: &AtlasDeployment{
Spec: AtlasDeploymentSpec{
Project: &common.ResourceRefNamespaced{
Name: "my-project",
},
LocalCredentialHolder: api.LocalCredentialHolder{
ProjectDualReference: ProjectDualReference{
ProjectRef: &common.ResourceRefNamespaced{
Name: "my-project",
},
ConnectionSecret: &api.LocalObjectReference{
Name: "my-dbuser-connection-secret",
},
Expand Down
12 changes: 1 addition & 11 deletions pkg/api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions pkg/api/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a0aa658

Please sign in to comment.