Skip to content

Commit

Permalink
changes per reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
roothorp committed Jan 16, 2025
1 parent 91f259b commit 060b90b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
7 changes: 7 additions & 0 deletions api/v1/atlasdeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ func (c *AtlasDeployment) ProjectDualRef() *ProjectDualReference {

type FlexSpec struct {
// Human-readable label that identifies the instance.
// +required
Name string `json:"name"`

// List that contains key-value pairs between 1 to 255 characters in length for tagging and categorizing the instance.
Expand All @@ -552,19 +553,25 @@ type FlexSpec struct {
// Flag that indicates whether termination protection is enabled on the cluster.
// If set to true, MongoDB Cloud won't delete the cluster. If set to false, MongoDB Cloud will delete the cluster.
// +kubebuilder:default:=false
// +optional
TerminationProtectionEnabled bool `json:"terminationProtectionEnabled,omitempty"`

// Group of cloud provider settings that configure the provisioned MongoDB flex cluster.
// +required
ProviderSettings *FlexProviderSettings `json:"providerSettings"`
}

type FlexProviderSettings struct {
// Cloud service provider on which MongoDB Atlas provisions the flex cluster.
// +kubebuilder:validation:Enum=AWS;GCP;AZURE
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Backing Provider cannot be modified after cluster creation"
// +required
BackingProviderName string `json:"backingProviderName,omitempty"`

// Human-readable label that identifies the geographic location of your MongoDB flex cluster.
// The region you choose can affect network latency for clients accessing your databases.
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Region Name cannot be modified after cluster creation"
// +required
RegionName string `json:"regionName,omitempty"`
}

Expand Down
10 changes: 10 additions & 0 deletions config/crd/bases/atlas.mongodb.com_atlasdeployments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,21 @@ spec:
- GCP
- AZURE
type: string
x-kubernetes-validations:
- message: Backing Provider cannot be modified after cluster
creation
rule: self == oldSelf
regionName:
description: |-
Human-readable label that identifies the geographic location of your MongoDB flex cluster.
The region you choose can affect network latency for clients accessing your databases.
type: string
x-kubernetes-validations:
- message: Region Name cannot be modified after cluster creation
rule: self == oldSelf
required:
- backingProviderName
- regionName
type: object
tags:
description: List that contains key-value pairs between 1 to 255
Expand Down
36 changes: 26 additions & 10 deletions internal/controller/validate/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,43 @@ import (
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/provider"
)

const (
DeploymentSet = 1 << iota
ServerlessSet
FlexSet
)

func deploymentSpecMask(atlasDeployment *akov2.AtlasDeployment) int {
mask := 0
if atlasDeployment.Spec.DeploymentSpec != nil {
mask = mask + DeploymentSet
}
if atlasDeployment.Spec.ServerlessSpec != nil {
mask = mask + ServerlessSet
}
if atlasDeployment.Spec.FlexSpec != nil {
mask = mask + FlexSet
}
return mask
}

func AtlasDeployment(atlasDeployment *akov2.AtlasDeployment, isGov bool, regionUsageRestrictions string) error {
isRegularDeployment := atlasDeployment.Spec.DeploymentSpec != nil
isServerlessDeployment := atlasDeployment.Spec.ServerlessSpec != nil
isFlexDeployment := atlasDeployment.Spec.FlexSpec != nil
var err error
var tagsSpec []*akov2.TagSpec

switch {
case !isRegularDeployment && !isServerlessDeployment && !isFlexDeployment:
switch deploymentSpecMask(atlasDeployment) {
case 0:
return errors.New("expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but none were")
case isRegularDeployment && !isServerlessDeployment && !isFlexDeployment:
case DeploymentSet:
tagsSpec = atlasDeployment.Spec.DeploymentSpec.Tags
err = regularDeployment(atlasDeployment.Spec.DeploymentSpec, isGov, regionUsageRestrictions)
case !isRegularDeployment && isServerlessDeployment && !isFlexDeployment:
case ServerlessSet:
tagsSpec = atlasDeployment.Spec.ServerlessSpec.Tags
err = serverlessDeployment(atlasDeployment.Spec.ServerlessSpec)
case !isRegularDeployment && !isServerlessDeployment && isFlexDeployment:
case FlexSet:
tagsSpec = atlasDeployment.Spec.FlexSpec.Tags
err = flexDeployment(atlasDeployment.Spec.FlexSpec)
default:
return errors.New("expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but none were")
return errors.New("expected exactly one of spec.deploymentSpec or spec.serverlessSpec or spec.flexSpec to be present, but multiple were")
}

if err != nil {
Expand Down
15 changes: 9 additions & 6 deletions test/helper/contract/ako.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func DefaultAtlasProject(name string) client.Object {
}

func newVersionedClient(ctx context.Context) (*admin.APIClient, error) {
domain := os.Getenv("MCLI_OPS_MANAGER_URL")
pubKey := os.Getenv("MCLI_PUBLIC_API_KEY")
prvKey := os.Getenv("MCLI_PRIVATE_API_KEY")
domain, pubKey, prvKey := getEnvSecrets()
client, err := atlas.NewClient(domain, pubKey, prvKey)
if err != nil {
return nil, fmt.Errorf("failed to setup Atlas Client: %w", err)
Expand All @@ -47,9 +45,7 @@ func mustCreateVersionedAtlasClient(ctx context.Context) *admin.APIClient {
}

func mustCreateVersionedAtlasClientSet(ctx context.Context) *atlas.ClientSet {
domain := os.Getenv("MCLI_OPS_MANAGER_URL")
pubKey := os.Getenv("MCLI_PUBLIC_API_KEY")
prvKey := os.Getenv("MCLI_PRIVATE_API_KEY")
domain, pubKey, prvKey := getEnvSecrets()
c2024, err := adminv20241113001.NewClient(
adminv20241113001.UseBaseURL(domain),
adminv20241113001.UseDigestAuth(pubKey, prvKey),
Expand Down Expand Up @@ -83,3 +79,10 @@ func globalSecret(namespace string) client.Object {
},
}
}

func getEnvSecrets() (string, string, string) {
domain := os.Getenv("MCLI_OPS_MANAGER_URL")
pubKey := os.Getenv("MCLI_PUBLIC_API_KEY")
prvKey := os.Getenv("MCLI_PRIVATE_API_KEY")
return domain, pubKey, prvKey
}

0 comments on commit 060b90b

Please sign in to comment.