/
managed_namespaces.go
140 lines (124 loc) · 5.18 KB
/
managed_namespaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package atlasdeployment
import (
"context"
"fmt"
"go.mongodb.org/atlas/mongodbatlas"
mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util"
)
func EnsureManagedNamespaces(service *workflow.Context, groupID string, clusterType string, managedNamespace []mdbv1.ManagedNamespace, deploymentName string) workflow.Result {
if clusterType != string(mdbv1.TypeGeoSharded) && managedNamespace != nil {
return workflow.Terminate(workflow.ManagedNamespacesReady, "Managed namespace is only supported by GeoSharded clusters")
}
result := syncManagedNamespaces(context.Background(), service, groupID, deploymentName, managedNamespace)
if !result.IsOk() {
service.SetConditionFromResult(status.ManagedNamespacesReadyType, result)
return result
}
if managedNamespace == nil {
service.UnsetCondition(status.ManagedNamespacesReadyType)
service.EnsureStatusOption(status.AtlasDeploymentManagedNamespacesOption(nil))
} else {
service.SetConditionTrue(status.ManagedNamespacesReadyType)
}
return result
}
func syncManagedNamespaces(ctx context.Context, service *workflow.Context, groupID string, deploymentName string, managedNamespaces []mdbv1.ManagedNamespace) workflow.Result {
logger := service.Log
existingManagedNamespaces, _, err := GetGlobalDeploymentState(ctx, service.Client.GlobalClusters, groupID, deploymentName)
logger.Debugf("Syncing managed namespaces %s", deploymentName)
if err != nil {
return workflow.Terminate(workflow.ManagedNamespacesReady, fmt.Sprintf("Failed to get managed namespaces: %v", err))
}
diff := sortManagedNamespaces(existingManagedNamespaces, managedNamespaces)
logger.Debugw("diff", "To create: %v", diff.ToCreate, "To delete: %v", diff.ToDelete, "To update status: %v", diff.ToUpdateStatus)
err = deleteManagedNamespaces(ctx, service.Client.GlobalClusters, groupID, deploymentName, diff.ToDelete)
if err != nil {
logger.Errorf("failed to delete managed namespaces: %v", err)
return workflow.Terminate(workflow.ManagedNamespacesReady, fmt.Sprintf("Failed to delete managed namespaces: %v", err))
}
nsStatuses := createManagedNamespaces(ctx, service.Client.GlobalClusters, groupID, deploymentName, diff.ToCreate)
for _, ns := range diff.ToUpdateStatus {
nsStatuses = append(nsStatuses, status.NewCreatedManagedNamespaceStatus(ns))
}
logger.Debugw("Managed namespaces statuses", "statuses", nsStatuses)
service.EnsureStatusOption(status.AtlasDeploymentManagedNamespacesOption(nsStatuses))
return checkManagedNamespaceStatus(nsStatuses)
}
func checkManagedNamespaceStatus(managedNamespaces []status.ManagedNamespace) workflow.Result {
for _, ns := range managedNamespaces {
if ns.Status != status.StatusReady {
return workflow.Terminate(workflow.ManagedNamespacesReady, "Managed namespaces are not ready")
}
}
return workflow.OK()
}
func sortManagedNamespaces(existing []mongodbatlas.ManagedNamespace, desired []mdbv1.ManagedNamespace) NamespaceDiff {
var result NamespaceDiff
for _, d := range desired {
found := false
for _, e := range existing {
if isManagedNamespaceStateMatch(e, d) {
found = true
result.ToUpdateStatus = append(result.ToUpdateStatus, d.ToAtlas())
break
}
}
if !found {
result.ToCreate = append(result.ToCreate, d.ToAtlas())
}
}
for _, e := range existing {
found := false
for _, d := range desired {
if isManagedNamespaceStateMatch(e, d) {
found = true
break
}
}
if !found {
result.ToDelete = append(result.ToDelete, e)
}
}
return result
}
func isManagedNamespaceStateMatch(existing mongodbatlas.ManagedNamespace, desired mdbv1.ManagedNamespace) bool {
if existing.Db == desired.Db &&
existing.Collection == desired.Collection &&
existing.CustomShardKey == desired.CustomShardKey &&
util.PtrValuesEqual(existing.IsShardKeyUnique, desired.IsShardKeyUnique) &&
util.PtrValuesEqual(existing.IsCustomShardKeyHashed, desired.IsCustomShardKeyHashed) &&
existing.NumInitialChunks == desired.NumInitialChunks {
return true
}
return false
}
func deleteManagedNamespaces(ctx context.Context, client mongodbatlas.GlobalClustersService, id string, name string, namespaces []mongodbatlas.ManagedNamespace) error {
for i := range namespaces {
_, _, err := client.DeleteManagedNamespace(ctx, id, name, &namespaces[i])
if err != nil {
return err
}
}
return nil
}
func createManagedNamespaces(ctx context.Context, client mongodbatlas.GlobalClustersService, id string, name string, namespaces []mongodbatlas.ManagedNamespace) []status.ManagedNamespace {
var newStatuses []status.ManagedNamespace
for i := range namespaces {
ns := namespaces[i]
_, _, err := client.AddManagedNamespace(ctx, id, name, &ns)
if err != nil {
newStatuses = append(newStatuses, status.NewFailedToCreateManagedNamespaceStatus(ns, err))
} else {
newStatuses = append(newStatuses, status.NewCreatedManagedNamespaceStatus(ns))
}
}
return newStatuses
}
type NamespaceDiff struct {
ToCreate []mongodbatlas.ManagedNamespace
ToDelete []mongodbatlas.ManagedNamespace
ToUpdateStatus []mongodbatlas.ManagedNamespace
}