Skip to content

Commit 5a0afe2

Browse files
authored
Readiness Probe: Check for acVersion file only if it is present (#994)
1 parent 300d90e commit 5a0afe2

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

cmd/readiness/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"go.uber.org/zap/zapcore"
7-
"gopkg.in/natefinch/lumberjack.v2"
86
"io"
97
"io/ioutil"
108
"os"
119
"time"
1210

11+
"go.uber.org/zap/zapcore"
12+
"gopkg.in/natefinch/lumberjack.v2"
13+
1314
"github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/config"
1415
"github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/headless"
1516
"github.com/mongodb/mongodb-kubernetes-operator/pkg/readiness/health"

controllers/mongodb_tls.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func getPemOrConcatenatedCrtAndKey(getter secret.Getter, mdb mdbv1.MongoDBCommun
142142
certKey := getCertAndKey(getter, mdb, secretName)
143143
pem := getPem(getter, mdb, secretName)
144144
if certKey == "" && pem == "" {
145-
return "", fmt.Errorf(`Neither "%s" nor the pair "%s"/"%s" were present in the TLS secret`, tlsSecretPemName, tlsSecretCertName, tlsSecretKeyName)
145+
return "", fmt.Errorf(`neither "%s" nor the pair "%s"/"%s" were present in the TLS secret`, tlsSecretPemName, tlsSecretCertName, tlsSecretKeyName)
146146
}
147147
if certKey == "" {
148148
return pem, nil
@@ -151,7 +151,7 @@ func getPemOrConcatenatedCrtAndKey(getter secret.Getter, mdb mdbv1.MongoDBCommun
151151
return certKey, nil
152152
}
153153
if certKey != pem {
154-
return "", fmt.Errorf(`If all of "%s", "%s" and "%s" are present in the secret, the entry for "%s" must be equal to the concatenation of "%s" with "%s"`, tlsSecretCertName, tlsSecretKeyName, tlsSecretPemName, tlsSecretPemName, tlsSecretCertName, tlsSecretKeyName)
154+
return "", fmt.Errorf(`if all of "%s", "%s" and "%s" are present in the secret, the entry for "%s" must be equal to the concatenation of "%s" with "%s"`, tlsSecretCertName, tlsSecretKeyName, tlsSecretPemName, tlsSecretPemName, tlsSecretCertName, tlsSecretKeyName)
155155
}
156156
return certKey, nil
157157
}

controllers/mongodb_tls_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func TestPemSupport(t *testing.T) {
276276

277277
err = r.ensureTLSResources(mdb)
278278
assert.Error(t, err)
279-
assert.Contains(t, err.Error(), `If all of "tls.crt", "tls.key" and "tls.pem" are present in the secret, the entry for "tls.pem" must be equal to the concatenation of "tls.crt" with "tls.key"`)
279+
assert.Contains(t, err.Error(), `if all of "tls.crt", "tls.key" and "tls.pem" are present in the secret, the entry for "tls.pem" must be equal to the concatenation of "tls.crt" with "tls.key"`)
280280

281281
})
282282
}

pkg/readiness/headless/headless.go

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package headless
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
67
"strconv"
@@ -12,6 +13,10 @@ import (
1213
"go.uber.org/zap"
1314
)
1415

16+
const (
17+
acVersionPath string = "/var/lib/automation/config/acVersion/version"
18+
)
19+
1520
// performCheckHeadlessMode validates if the Agent has reached the correct goal state
1621
// The state is fetched from K8s automation config Secret directly to avoid flakiness of mounting process
1722
// Dev note: there is an alternative way to get current namespace: to read from
@@ -21,23 +26,29 @@ import (
2126
func PerformCheckHeadlessMode(health health.Status, conf config.Config) (bool, error) {
2227
var targetVersion int64
2328
var err error
29+
2430
targetVersion, err = secret.ReadAutomationConfigVersionFromSecret(conf.Namespace, conf.ClientSet, conf.AutomationConfigSecretName)
2531
if err != nil {
26-
file, err := os.Open("/var/lib/automation/config/acVersion/version")
27-
if err != nil {
28-
return false, err
29-
}
30-
defer file.Close()
31-
data, err := ioutil.ReadAll(file)
32-
33-
if err != nil {
34-
return false, err
35-
}
32+
// this file is expected to be present in case of AppDB, there is no point trying to access it in
33+
// community, it masks the underlying error
34+
if _, pathErr := os.Stat(acVersionPath); !os.IsNotExist(pathErr) {
35+
file, err := os.Open(acVersionPath)
36+
if err != nil {
37+
return false, err
38+
}
39+
defer file.Close()
3640

37-
targetVersion, err = strconv.ParseInt(string(data), 10, 64)
41+
data, err := ioutil.ReadAll(file)
42+
if err != nil {
43+
return false, err
44+
}
3845

39-
if err != nil {
40-
return false, err
46+
targetVersion, err = strconv.ParseInt(string(data), 10, 64)
47+
if err != nil {
48+
return false, err
49+
}
50+
} else {
51+
return false, fmt.Errorf("failed to fetch automation-config secret name: %s, err: %s", conf.AutomationConfigSecretName, err)
4152
}
4253
}
4354

0 commit comments

Comments
 (0)