Skip to content

Commit

Permalink
Set ref at generation in a single step
Browse files Browse the repository at this point in the history
  • Loading branch information
josvazg committed Oct 29, 2024
1 parent 9c8c896 commit c55f7af
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 66 deletions.
23 changes: 9 additions & 14 deletions internal/kubernetes/operator/config_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/resources"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/streamsprocessing"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
"go.mongodb.org/atlas-sdk/v20240805004/admin"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
Expand Down Expand Up @@ -168,7 +167,6 @@ func (e *ConfigExporter) Run() (string, error) {
r = append(r, streamProcessingResources...)

for _, res := range r {
res = filterResource(res, e.independentResources, projectName+credentialSuffix)
if e.patcher != nil {
err = e.patcher.Patch(res)
if err != nil {
Expand Down Expand Up @@ -225,10 +223,11 @@ func (e *ConfigExporter) exportProject() ([]runtime.Object, string, error) {
r = append(r, t)
}

credentialsName := credentialsName(projectData.Project.Name)
// Project secret with credentials
r = append(r, project.BuildProjectNamedConnectionSecret(
e.credsProvider,
projectData.Project.Name+credentialSuffix,
credentialsName,
projectData.Project.Namespace,
e.orgID,
e.includeSecretsData,
Expand All @@ -241,8 +240,10 @@ func (e *ConfigExporter) exportProject() ([]runtime.Object, string, error) {
e.projectID,
projectData.Project.Name,
e.targetNamespace,
credentialsName,
e.dictionaryForAtlasNames,
e.operatorVersion)
e.operatorVersion,
e.independentResources)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -272,9 +273,10 @@ func (e *ConfigExporter) exportDeployments(projectName string) ([]runtime.Object
e.clusterNames = clusters
}

credentials := credentialsName(projectName)
for _, deploymentName := range e.clusterNames {
// Try advanced cluster first
if advancedCluster, err := deployment.BuildAtlasAdvancedDeployment(e.dataProvider, e.featureValidator, e.projectID, projectName, deploymentName, e.targetNamespace, e.dictionaryForAtlasNames, e.operatorVersion); err == nil {
if advancedCluster, err := deployment.BuildAtlasAdvancedDeployment(e.dataProvider, e.featureValidator, e.projectID, projectName, deploymentName, e.targetNamespace, credentials, e.dictionaryForAtlasNames, e.operatorVersion, e.independentResources); err == nil {
if advancedCluster != nil {
// Append deployment to result
result = append(result, advancedCluster.Deployment)
Expand Down Expand Up @@ -459,13 +461,6 @@ func (e *ConfigExporter) exportAtlasFederatedAuth(projectName string) ([]runtime
return append(result, federatedAuthentification), nil
}

func filterResource(obj runtime.Object, independentResource bool, credentials string) runtime.Object {
switch r := obj.(type) {
case *akov2.AtlasDatabaseUser:
return dbusers.FixReference(r, independentResource, credentials)
case *akov2.AtlasDeployment:
return deployment.FixReference(r, independentResource, credentials)
default:
return obj
}
func credentialsName(projectName string) string {
return projectName + credentialSuffix
}
43 changes: 22 additions & 21 deletions internal/kubernetes/operator/dbusers/dbusers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

const timeFormatISO8601 = "2006-01-02T15:04:05.999Z"

func BuildDBUsers(provider store.OperatorDBUsersStore, projectID, projectName, targetNamespace string, dictionary map[string]string, version string) ([]*akov2.AtlasDatabaseUser, []*corev1.Secret, error) {
func BuildDBUsers(provider store.OperatorDBUsersStore, projectID, projectName, targetNamespace, credentials string, dictionary map[string]string, version string, independentResource bool) ([]*akov2.AtlasDatabaseUser, []*corev1.Secret, error) {
users, err := provider.DatabaseUsers(projectID, &store.ListOptions{})
if err != nil {
return nil, nil, err
Expand All @@ -57,7 +57,7 @@ func BuildDBUsers(provider store.OperatorDBUsersStore, projectID, projectName, t
}
scopes := convertUserScopes(user)

mappedUsers[resourceName] = &akov2.AtlasDatabaseUser{
dbu := &akov2.AtlasDatabaseUser{
TypeMeta: metav1.TypeMeta{
Kind: "AtlasDatabaseUser",
APIVersion: "atlas.mongodb.com/v1",
Expand All @@ -70,13 +70,6 @@ func BuildDBUsers(provider store.OperatorDBUsersStore, projectID, projectName, t
},
},
Spec: akov2.AtlasDatabaseUserSpec{
Project: &akov2common.ResourceRefNamespaced{
Name: resources.NormalizeAtlasName(projectName, dictionary),
Namespace: targetNamespace,
},
ExternalProjectRef: &akov2.ExternalProjectReference{
ID: projectID,
},
DatabaseName: user.DatabaseName,
DeleteAfterDate: getDeleteAfterDate(user),
Labels: labels,
Expand All @@ -91,6 +84,9 @@ func BuildDBUsers(provider store.OperatorDBUsersStore, projectID, projectName, t
},
},
}
normalizedProjectName := resources.NormalizeAtlasName(projectName, dictionary)
dbu = setReference(dbu, independentResource, projectID, normalizedProjectName, targetNamespace, credentials)
mappedUsers[resourceName] = dbu

if user.GetX509Type() != "MANAGED" {
secret := buildUserSecret(resourceName, targetNamespace, projectID, projectName, dictionary)
Expand All @@ -110,6 +106,23 @@ func BuildDBUsers(provider store.OperatorDBUsersStore, projectID, projectName, t
return result, relatedSecrets, nil
}

func setReference(dbUser *akov2.AtlasDatabaseUser, independentResource bool, projectID, projectName, namespace string, credentials string) *akov2.AtlasDatabaseUser {
if independentResource {
dbUser.Spec.ExternalProjectRef = &akov2.ExternalProjectReference{
ID: projectID,
}
dbUser.Spec.ConnectionSecret = &akoapi.LocalObjectReference{
Name: credentials,
}
return dbUser
}
dbUser.Spec.Project = &akov2common.ResourceRefNamespaced{
Name: projectName,
Namespace: namespace,
}
return dbUser
}

func getDeleteAfterDate(user *atlasv2.CloudDatabaseUser) string {
if user.DeleteAfterDate != nil {
return user.DeleteAfterDate.Format(timeFormatISO8601)
Expand Down Expand Up @@ -180,15 +193,3 @@ func suggestResourceName(

return resourceName
}

func FixReference(dbUser *akov2.AtlasDatabaseUser, independentResource bool, credentials string) *akov2.AtlasDatabaseUser {
if independentResource {
dbUser.Spec.Project = nil
dbUser.Spec.ConnectionSecret = &akoapi.LocalObjectReference{
Name: credentials,
}
return dbUser
}
dbUser.Spec.ExternalProjectRef = nil
return dbUser
}
15 changes: 9 additions & 6 deletions internal/kubernetes/operator/dbusers/dbusers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const resourceVersion = "x.y.z"
const (
resourceVersion = "x.y.z"

credentialSuffix = "-credentials"
)

func Test_convertUserLabels(t *testing.T) {
t.Run("Can convert user labels from Atlas to the Operator format", func(t *testing.T) {
Expand Down Expand Up @@ -175,7 +179,8 @@ func TestBuildDBUsers(t *testing.T) {
},
}, nil)

users, relatedSecrets, err := BuildDBUsers(mockUserStore, projectID, projectName, targetNamespace, dictionary, resourceVersion)
creds := projectName + credentialSuffix
users, relatedSecrets, err := BuildDBUsers(mockUserStore, projectID, projectName, targetNamespace, creds, dictionary, resourceVersion, false)
if err != nil {
t.Fatalf("%v", err)
}
Expand All @@ -197,9 +202,6 @@ func TestBuildDBUsers(t *testing.T) {
Name: resources.NormalizeAtlasName(projectName, dictionary),
Namespace: targetNamespace,
},
ExternalProjectRef: &akov2.ExternalProjectReference{
ID: projectID,
},
DatabaseName: user.DatabaseName,
DeleteAfterDate: user.DeleteAfterDate.Format(timeFormatISO8601),
Labels: []akov2common.LabelSpec{
Expand Down Expand Up @@ -326,7 +328,8 @@ func TestBuildDBUsers(t *testing.T) {
listOptions := &store.ListOptions{}
mockUserStore.EXPECT().DatabaseUsers(projectID, listOptions).Return(&atlasUsers, nil)

users, relatedSecrets, err := BuildDBUsers(mockUserStore, projectID, projectName, targetNamespace, dictionary, resourceVersion)
creds := projectName + credentialSuffix
users, relatedSecrets, err := BuildDBUsers(mockUserStore, projectID, projectName, targetNamespace, creds, dictionary, resourceVersion, false)
if err != nil {
t.Fatalf("%v", err)
}
Expand Down
40 changes: 20 additions & 20 deletions internal/kubernetes/operator/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type AtlasDeploymentResult struct {
BackupPolicies []*akov2.AtlasBackupPolicy
}

func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, validator features.FeatureValidator, projectID, projectName, clusterID, targetNamespace string, dictionary map[string]string, version string) (*AtlasDeploymentResult, error) {
func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, validator features.FeatureValidator, projectID, projectName, clusterID, targetNamespace string, credentials string, dictionary map[string]string, version string, independentResource bool) (*AtlasDeploymentResult, error) {
deployment, err := deploymentStore.AtlasCluster(projectID, clusterID)
if err != nil {
return nil, err
Expand Down Expand Up @@ -124,13 +124,6 @@ func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, va
},
},
Spec: akov2.AtlasDeploymentSpec{
Project: &akov2common.ResourceRefNamespaced{
Name: resources.NormalizeAtlasName(projectName, dictionary),
Namespace: targetNamespace,
},
ExternalProjectRef: &akov2.ExternalProjectReference{
ID: projectID,
},
DeploymentSpec: advancedSpec,
ServerlessSpec: nil,
ProcessArgs: nil,
Expand All @@ -141,6 +134,8 @@ func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, va
},
},
}
normalizedProjectName := resources.NormalizeAtlasName(projectName, dictionary)
atlasDeployment = setReference(atlasDeployment, independentResource, projectID, normalizedProjectName, targetNamespace, credentials)

deploymentResult := &AtlasDeploymentResult{
Deployment: atlasDeployment,
Expand Down Expand Up @@ -189,6 +184,23 @@ func BuildAtlasAdvancedDeployment(deploymentStore store.OperatorClusterStore, va
return deploymentResult, nil
}

func setReference(deployment *akov2.AtlasDeployment, independentResource bool, projectID, projectName, namespace string, credentials string) *akov2.AtlasDeployment {
if independentResource {
deployment.Spec.ExternalProjectRef = &akov2.ExternalProjectReference{
ID: projectID,
}
deployment.Spec.ConnectionSecret = &akoapi.LocalObjectReference{
Name: credentials,
}
return deployment
}
deployment.Spec.Project = &akov2common.ResourceRefNamespaced{
Name: projectName,
Namespace: namespace,
}
return deployment
}

func hasTenantRegionConfig(out *akov2.AtlasDeployment) bool {
if out.Spec.DeploymentSpec == nil {
return false
Expand Down Expand Up @@ -549,15 +561,3 @@ func buildServerlessPrivateEndpoints(deploymentStore store.ServerlessPrivateEndp
}
return result, nil
}

func FixReference(deployment *akov2.AtlasDeployment, independentResource bool, credentials string) *akov2.AtlasDeployment {
if independentResource {
deployment.Spec.Project = nil
deployment.Spec.ConnectionSecret = &akoapi.LocalObjectReference{
Name: credentials,
}
return deployment
}
deployment.Spec.ExternalProjectRef = nil
return deployment
}
12 changes: 7 additions & 5 deletions internal/kubernetes/operator/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const resourceVersion = "x.y.z"
const (
resourceVersion = "x.y.z"

credentialSuffix = "-credentials"
)

func TestBuildAtlasAdvancedDeployment(t *testing.T) {
ctl := gomock.NewController(t)
Expand Down Expand Up @@ -223,9 +227,6 @@ func TestBuildAtlasAdvancedDeployment(t *testing.T) {
Name: strings.ToLower(projectName),
Namespace: targetNamespace,
},
ExternalProjectRef: &akov2.ExternalProjectReference{
ID: projectID,
},
DeploymentSpec: &akov2.AdvancedDeploymentSpec{
MongoDBMajorVersion: "5.0",
BackupEnabled: cluster.BackupEnabled,
Expand Down Expand Up @@ -410,7 +411,8 @@ func TestBuildAtlasAdvancedDeployment(t *testing.T) {
featureValidator.EXPECT().FeatureExist(features.ResourceAtlasDeployment, featureBackupSchedule).Return(true)
featureValidator.EXPECT().FeatureExist(features.ResourceAtlasDeployment, featureGlobalDeployments).Return(true)

got, err := BuildAtlasAdvancedDeployment(clusterStore, featureValidator, projectID, projectName, clusterName, targetNamespace, dictionary, resourceVersion)
creds := projectName + credentialSuffix
got, err := BuildAtlasAdvancedDeployment(clusterStore, featureValidator, projectID, projectName, clusterName, targetNamespace, creds, dictionary, resourceVersion, false)
if err != nil {
t.Fatalf("%v", err)
}
Expand Down

0 comments on commit c55f7af

Please sign in to comment.