-
Notifications
You must be signed in to change notification settings - Fork 74
/
provider.go
183 lines (149 loc) · 5.13 KB
/
provider.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package atlas
import (
"context"
"fmt"
"net/http"
"net/url"
"runtime"
"strings"
"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas/mongodbatlas"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/httputil"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api"
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/version"
)
const (
govAtlasDomain = "mongodbgov.com"
orgIDKey = "orgId"
publicAPIKey = "publicApiKey"
privateAPIKey = "privateApiKey"
)
type Provider interface {
Client(ctx context.Context, secretRef *client.ObjectKey, log *zap.SugaredLogger) (*mongodbatlas.Client, string, error)
SdkClient(ctx context.Context, secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error)
IsCloudGov() bool
IsResourceSupported(resource api.AtlasCustomResource) bool
}
type ProductionProvider struct {
k8sClient client.Client
domain string
globalSecretRef client.ObjectKey
}
type credentialsSecret struct {
OrgID string
PublicKey string
PrivateKey string
}
func NewProductionProvider(atlasDomain string, globalSecretRef client.ObjectKey, k8sClient client.Client) *ProductionProvider {
return &ProductionProvider{
k8sClient: k8sClient,
domain: atlasDomain,
globalSecretRef: globalSecretRef,
}
}
func (p *ProductionProvider) IsCloudGov() bool {
domainURL, err := url.Parse(p.domain)
if err != nil {
return false
}
return strings.HasSuffix(domainURL.Hostname(), govAtlasDomain)
}
func (p *ProductionProvider) IsResourceSupported(resource api.AtlasCustomResource) bool {
if !p.IsCloudGov() {
return true
}
switch atlasResource := resource.(type) {
case *akov2.AtlasProject,
*akov2.AtlasTeam,
*akov2.AtlasBackupSchedule,
*akov2.AtlasBackupPolicy,
*akov2.AtlasDatabaseUser,
*akov2.AtlasSearchIndexConfig,
*akov2.AtlasBackupCompliancePolicy,
*akov2.AtlasFederatedAuth:
return true
case *akov2.AtlasDataFederation,
*akov2.AtlasStreamInstance,
*akov2.AtlasStreamConnection:
return false
case *akov2.AtlasDeployment:
hasSearchNodes := atlasResource.Spec.DeploymentSpec != nil && len(atlasResource.Spec.DeploymentSpec.SearchNodes) > 0
isServerless := atlasResource.Spec.ServerlessSpec != nil
return !(isServerless || hasSearchNodes)
}
return false
}
func (p *ProductionProvider) Client(ctx context.Context, secretRef *client.ObjectKey, log *zap.SugaredLogger) (*mongodbatlas.Client, string, error) {
secretData, err := getSecrets(ctx, p.k8sClient, secretRef, &p.globalSecretRef)
if err != nil {
return nil, "", err
}
clientCfg := []httputil.ClientOpt{
httputil.Digest(secretData.PublicKey, secretData.PrivateKey),
httputil.LoggingTransport(log),
}
httpClient, err := httputil.DecorateClient(&http.Client{Transport: http.DefaultTransport}, clientCfg...)
if err != nil {
return nil, "", err
}
c, err := mongodbatlas.New(httpClient, mongodbatlas.SetBaseURL(p.domain), mongodbatlas.SetUserAgent(operatorUserAgent()))
return c, secretData.OrgID, err
}
func (p *ProductionProvider) SdkClient(ctx context.Context, secretRef *client.ObjectKey, log *zap.SugaredLogger) (*admin.APIClient, string, error) {
secretData, err := getSecrets(ctx, p.k8sClient, secretRef, &p.globalSecretRef)
if err != nil {
return nil, "", err
}
// TODO review we need add a custom logger to http client
//httpClientWithCustomLogger := http.DefaultClient
//err = httputil.LoggingTransport(log)(http.DefaultClient)
//if err != nil {
// return nil, "", err
//}
c, err := NewClient(p.domain, secretData.PublicKey, secretData.PrivateKey)
if err != nil {
return nil, "", err
}
return c, secretData.OrgID, nil
}
func getSecrets(ctx context.Context, k8sClient client.Client, secretRef, fallbackRef *client.ObjectKey) (*credentialsSecret, error) {
if secretRef == nil {
secretRef = fallbackRef
}
secret := &corev1.Secret{}
if err := k8sClient.Get(ctx, *secretRef, secret); err != nil {
return nil, fmt.Errorf("failed to read Atlas API credentials from the secret %s: %w", secretRef.String(), err)
}
secretData := credentialsSecret{
OrgID: string(secret.Data[orgIDKey]),
PublicKey: string(secret.Data[publicAPIKey]),
PrivateKey: string(secret.Data[privateAPIKey]),
}
if missingFields, valid := validateSecretData(&secretData); !valid {
return nil, fmt.Errorf("the following fields are missing in the secret %v: %v", secretRef, missingFields)
}
return &secretData, nil
}
func validateSecretData(secretData *credentialsSecret) ([]string, bool) {
missingFields := make([]string, 0, 3)
if secretData.OrgID == "" {
missingFields = append(missingFields, orgIDKey)
}
if secretData.PublicKey == "" {
missingFields = append(missingFields, publicAPIKey)
}
if secretData.PrivateKey == "" {
missingFields = append(missingFields, privateAPIKey)
}
if len(missingFields) > 0 {
return missingFields, false
}
return nil, true
}
func operatorUserAgent() string {
return fmt.Sprintf("%s/%s (%s;%s)", "MongoDBAtlasKubernetesOperator", version.Version, runtime.GOOS, runtime.GOARCH)
}