Skip to content

Commit c0d3d41

Browse files
author
Anton
authored
CLOUDP-84457: fix for global secret (#164)
1 parent 10c6cb5 commit c0d3d41

File tree

7 files changed

+73
-27
lines changed

7 files changed

+73
-27
lines changed

config/manager/manager.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ spec:
5555
cpu: 100m
5656
memory: 20Mi
5757
env:
58-
- name: OPERATOR_NAME
58+
- name: OPERATOR_POD_NAME
5959
valueFrom:
6060
fieldRef:
6161
fieldPath: metadata.name

main.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlasdatabaseuser"
3838
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/atlasproject"
3939
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/watch"
40+
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/kube"
4041
// +kubebuilder:scaffold:imports
4142
)
4243

@@ -76,14 +77,14 @@ func main() {
7677
os.Exit(1)
7778
}
7879

79-
operatorPod := operatorPodObjectKey()
80+
operatorPod := operatorDeploymentObjectKey()
8081

8182
if err = (&atlascluster.AtlasClusterReconciler{
82-
Client: mgr.GetClient(),
83-
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
84-
Scheme: mgr.GetScheme(),
85-
AtlasDomain: config.AtlasDomain,
86-
OperatorPod: operatorPod,
83+
Client: mgr.GetClient(),
84+
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
85+
Scheme: mgr.GetScheme(),
86+
AtlasDomain: config.AtlasDomain,
87+
OperatorDeploymentName: operatorPod,
8788
}).SetupWithManager(mgr); err != nil {
8889
setupLog.Error(err, "unable to create controller", "controller", "AtlasCluster")
8990
os.Exit(1)
@@ -159,15 +160,18 @@ func parseConfiguration(log *zap.SugaredLogger) Config {
159160
return config
160161
}
161162

162-
func operatorPodObjectKey() client.ObjectKey {
163-
operatorName := os.Getenv("OPERATOR_NAME")
164-
if operatorName == "" {
165-
log.Fatal(`"OPERATOR_NAME" environment variable must be set!`)
163+
func operatorDeploymentObjectKey() client.ObjectKey {
164+
operatorPodName := os.Getenv("OPERATOR_POD_NAME")
165+
if operatorPodName == "" {
166+
log.Fatal(`"OPERATOR_POD_NAME" environment variable must be set!`)
166167
}
167168
operatorNamespace := os.Getenv("OPERATOR_NAMESPACE")
168169
if operatorNamespace == "" {
169170
log.Fatal(`"OPERATOR_NAMESPACE" environment variable must be set!`)
170171
}
171-
172-
return client.ObjectKey{Namespace: operatorNamespace, Name: operatorName}
172+
deploymentName, err := kube.ParseDeploymentNameFromPodName(operatorPodName)
173+
if err != nil {
174+
log.Fatalf(`Failed to get Operator Deployment name from "OPERATOR_POD_NAME" environment variable: %s`, err.Error())
175+
}
176+
return client.ObjectKey{Namespace: operatorNamespace, Name: deploymentName}
173177
}

pkg/controller/atlas/connection.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ type Connection struct {
2626

2727
// ReadConnection reads Atlas API connection parameters from AtlasProject Secret or from the default Operator one if the
2828
// former is not specified
29-
func ReadConnection(log *zap.SugaredLogger, kubeClient client.Client, operatorPodObjectKey client.ObjectKey, projectOverrideSecretRef *client.ObjectKey) (Connection, error) {
29+
func ReadConnection(log *zap.SugaredLogger, kubeClient client.Client, operatorDeployment client.ObjectKey, projectOverrideSecretRef *client.ObjectKey) (Connection, error) {
3030
if projectOverrideSecretRef != nil {
3131
// TODO is it possible that part of connection (like orgID is still in the Operator level secret and needs to get merged?)
3232
log.Infof("Reading Atlas API credentials from the AtlasProject Secret %s", projectOverrideSecretRef)
3333
return readAtlasConnectionFromSecret(kubeClient, *projectOverrideSecretRef)
3434
}
3535

36-
log.Debug("AtlasProject connection Secret is not specified - using the Operator one")
37-
return readAtlasConnectionFromSecret(kubeClient, kube.ObjectKey(operatorPodObjectKey.Namespace, operatorPodObjectKey.Name+"-api-key"))
36+
operatorAPISecret := kube.ObjectKey(operatorDeployment.Namespace, operatorDeployment.Name+"-api-key")
37+
log.Debugf("AtlasProject connection Secret is not specified - using the Operator one: %v", operatorAPISecret)
38+
return readAtlasConnectionFromSecret(kubeClient, operatorAPISecret)
3839
}
3940

4041
func readAtlasConnectionFromSecret(kubeClient client.Client, secretRef client.ObjectKey) (Connection, error) {

pkg/controller/atlascluster/atlascluster_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ import (
4343

4444
// AtlasClusterReconciler reconciles an AtlasCluster object
4545
type AtlasClusterReconciler struct {
46-
Client client.Client
47-
Log *zap.SugaredLogger
48-
Scheme *runtime.Scheme
49-
AtlasDomain string
50-
OperatorPod client.ObjectKey
46+
Client client.Client
47+
Log *zap.SugaredLogger
48+
Scheme *runtime.Scheme
49+
AtlasDomain string
50+
OperatorDeploymentName client.ObjectKey
5151
}
5252

5353
// +kubebuilder:rbac:groups=atlas.mongodb.com,resources=atlasclusters,verbs=get;list;watch;create;update;patch;delete
@@ -77,7 +77,7 @@ func (r *AtlasClusterReconciler) Reconcile(context context.Context, req ctrl.Req
7777
return result.ReconcileResult(), nil
7878
}
7979

80-
connection, err := atlas.ReadConnection(log, r.Client, r.OperatorPod, project.ConnectionSecretObjectKey())
80+
connection, err := atlas.ReadConnection(log, r.Client, r.OperatorDeploymentName, project.ConnectionSecretObjectKey())
8181
if err != nil {
8282
result := workflow.Terminate(workflow.AtlasCredentialsNotProvided, err.Error())
8383
ctx.SetConditionFromResult(status.ClusterReadyType, result)
@@ -154,7 +154,7 @@ func (r *AtlasClusterReconciler) Delete(e event.DeleteEvent) error {
154154

155155
log = log.With("projectID", project.Status.ID, "clusterName", cluster.Spec.Name)
156156

157-
connection, err := atlas.ReadConnection(log, r.Client, r.OperatorPod, project.ConnectionSecretObjectKey())
157+
connection, err := atlas.ReadConnection(log, r.Client, r.OperatorDeploymentName, project.ConnectionSecretObjectKey())
158158
if err != nil {
159159
return err
160160
}

pkg/util/kube/kube.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kube
22

33
import (
4+
"fmt"
45
"regexp"
56
"strings"
67

@@ -46,6 +47,20 @@ func NormalizeLabelValue(name string) string {
4647
return normalize(name, 63, nonLabelRegexp)
4748
}
4849

50+
// ParseDeploymentNameFromPodName returns the name of Deployment by Pod Name. The Pods for Deployments have two hashes
51+
// parts as the first one is generated for the ReplicaSet resource created and the second - for the Pod itself.
52+
// Example:
53+
// - Deployment: "prometheus-adapter"
54+
// - ReplicaSet: "prometheus-adapter-65c6cb864f"
55+
// - Pod: "prometheus-adapter-797f946f88-97f2q"
56+
func ParseDeploymentNameFromPodName(podName string) (string, error) {
57+
parts := strings.Split(podName, "-")
58+
if len(parts) <= 2 {
59+
return "", fmt.Errorf(`the Pod name must follow the format "<deployment_name>-797f946f88-97f2q" but got %s`, podName)
60+
}
61+
return strings.Join(parts[0:len(parts)-2], "-"), nil
62+
}
63+
4964
// Dev note: the algorithm tries to replace the invalid characters with '-' (or simply omit it replacing is not possible)
5065
// Note, that this algorithm is not ideal - e.g. it won't fix the following: "a.#b" ("a._b" is still not a valid output - as
5166
// nonalphanumeric symbols cannot go together) though this doesn't intend to work in ALL the cases but in the MAJORITY instead

pkg/util/kube/kube_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,29 @@ func TestNormalizeLabelValue(t *testing.T) {
8181
}
8282
})
8383
}
84+
85+
func TestParseDeploymentNameFromPodName(t *testing.T) {
86+
testCases := []struct {
87+
in string
88+
out string
89+
}{
90+
{in: "prometheus-adapter-797f946f88-97f2q", out: "prometheus-adapter"},
91+
{in: "cluster-monitoring-operator-686555c948-z2xrh", out: "cluster-monitoring-operator"},
92+
{in: "mongodb-atlas-operator-cd75dc789-tdhvp", out: "mongodb-atlas-operator"},
93+
{in: "somenondashed-cd75dc789-tdhvp", out: "somenondashed"},
94+
{in: "notadeploymentpod-cd75dc789", out: ""},
95+
{in: "notadeploymentpod", out: ""},
96+
{in: "notadeploymentpod_cd75dc789", out: ""},
97+
{in: "notadeploymentpod.cd75dc789", out: ""},
98+
}
99+
for _, tc := range testCases {
100+
out, err := ParseDeploymentNameFromPodName(tc.in)
101+
if tc.out != "" {
102+
assert.Equal(t, tc.out, out, "in: %q, out: %q, (expected %q)", tc.in, out, tc.out)
103+
assert.Nil(t, err)
104+
}
105+
if tc.out == "" {
106+
assert.Error(t, err)
107+
}
108+
}
109+
}

test/int/integration_suite_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ func prepareControllers() {
189189
Expect(err).ToNot(HaveOccurred())
190190

191191
err = (&atlascluster.AtlasClusterReconciler{
192-
Client: k8sManager.GetClient(),
193-
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
194-
AtlasDomain: "https://cloud-qa.mongodb.com",
195-
OperatorPod: kube.ObjectKey(namespace.Name, "atlas-operator"),
192+
Client: k8sManager.GetClient(),
193+
Log: logger.Named("controllers").Named("AtlasCluster").Sugar(),
194+
AtlasDomain: "https://cloud-qa.mongodb.com",
195+
OperatorDeploymentName: kube.ObjectKey(namespace.Name, "atlas-operator"),
196196
}).SetupWithManager(k8sManager)
197197
Expect(err).ToNot(HaveOccurred())
198198

0 commit comments

Comments
 (0)