Skip to content

Commit

Permalink
Add Functions to get Default Probes (#579)
Browse files Browse the repository at this point in the history
Signed-off-by: Mohammad Fahim Abrar <fahimabrar@appscode.com>
  • Loading branch information
Mohammad Fahim Abrar committed Sep 4, 2020
1 parent 76ac9bc commit ecd1d17
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 additions & 41 deletions apis/kubedb/v1alpha1/mongodb_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,31 +457,17 @@ func (m *MongoDB) setDefaultTLSConfig() {
},
})
}

// setDefaultProbes sets defaults only when probe fields are nil.
// In operator, check if the value of probe fields is "{}".
// For "{}", ignore readinessprobe or livenessprobe in statefulset.
// ref: https://github.com/helm/charts/blob/345ba987722350ffde56ec34d2928c0b383940aa/stable/mongodb/templates/deployment-standalone.yaml#L93
func (m *MongoDB) setDefaultProbes(podTemplate *ofst.PodTemplateSpec, mgVersion *v1alpha1.MongoDBVersion) {
if podTemplate == nil {
return
}
func (m *MongoDB) getCmdForProbes(mgVersion *v1alpha1.MongoDBVersion) []string {
var sslArgs string
if m.Spec.SSLMode == SSLModeRequireSSL {
sslArgs = fmt.Sprintf("--tls --tlsCAFile=%v/%v --tlsCertificateKeyFile=%v/%v",
MongoCertDirectory, TLSCACertFileName, MongoCertDirectory, MongoClientFileName)

breakingVer, err := version.NewVersion("4.1")
if err != nil {
return
}
exceptionVer, err := version.NewVersion("4.1.4")
if err != nil {
return
}
breakingVer, _ := version.NewVersion("4.1")
exceptionVer, _ := version.NewVersion("4.1.4")
currentVer, err := version.NewVersion(mgVersion.Spec.Version)
if err != nil {
return
panic(fmt.Errorf("MongoDB %s/%s: unable to parse version. reason: %s", m.Namespace, m.Name, err.Error()))
}
if currentVer.Equal(exceptionVer) {
sslArgs = fmt.Sprintf("--tls --tlsCAFile=%v/%v --tlsPEMKeyFile=%v/%v", MongoCertDirectory, TLSCACertFileName, MongoCertDirectory, MongoClientFileName)
Expand All @@ -490,40 +476,58 @@ func (m *MongoDB) setDefaultProbes(podTemplate *ofst.PodTemplateSpec, mgVersion
}
}

cmd := []string{
return []string{
"bash",
"-c",
fmt.Sprintf(`set -x; if [[ $(mongo admin --host=localhost %v --username=$MONGO_INITDB_ROOT_USERNAME --password=$MONGO_INITDB_ROOT_PASSWORD --authenticationDatabase=admin --quiet --eval "db.adminCommand('ping').ok" ) -eq "1" ]]; then
exit 0
fi
exit 1`, sslArgs),
}
}

if podTemplate.Spec.LivenessProbe == nil {
podTemplate.Spec.LivenessProbe = &core.Probe{
Handler: core.Handler{
Exec: &core.ExecAction{
Command: cmd,
},
func (m *MongoDB) GetDefaultLivenessProbeSpec(mgVersion *v1alpha1.MongoDBVersion) *core.Probe {
return &core.Probe{
Handler: core.Handler{
Exec: &core.ExecAction{
Command: m.getCmdForProbes(mgVersion),
},
FailureThreshold: 3,
PeriodSeconds: 10,
SuccessThreshold: 1,
TimeoutSeconds: 5,
}
},
FailureThreshold: 3,
PeriodSeconds: 10,
SuccessThreshold: 1,
TimeoutSeconds: 5,
}
if podTemplate.Spec.ReadinessProbe == nil {
podTemplate.Spec.ReadinessProbe = &core.Probe{
Handler: core.Handler{
Exec: &core.ExecAction{
Command: cmd,
},
}

func (m *MongoDB) GetDefaultReadinessProbeSpec(mgVersion *v1alpha1.MongoDBVersion) *core.Probe {
return &core.Probe{
Handler: core.Handler{
Exec: &core.ExecAction{
Command: m.getCmdForProbes(mgVersion),
},
FailureThreshold: 3,
PeriodSeconds: 10,
SuccessThreshold: 1,
TimeoutSeconds: 1,
}
},
FailureThreshold: 3,
PeriodSeconds: 10,
SuccessThreshold: 1,
TimeoutSeconds: 1,
}
}

// setDefaultProbes sets defaults only when probe fields are nil.
// In operator, check if the value of probe fields is "{}".
// For "{}", ignore readinessprobe or livenessprobe in statefulset.
// ref: https://github.com/helm/charts/blob/345ba987722350ffde56ec34d2928c0b383940aa/stable/mongodb/templates/deployment-standalone.yaml#L93
func (m *MongoDB) setDefaultProbes(podTemplate *ofst.PodTemplateSpec, mgVersion *v1alpha1.MongoDBVersion) {
if podTemplate == nil {
return
}

if podTemplate.Spec.LivenessProbe == nil {
podTemplate.Spec.LivenessProbe = m.GetDefaultLivenessProbeSpec(mgVersion)
}
if podTemplate.Spec.ReadinessProbe == nil {
podTemplate.Spec.ReadinessProbe = m.GetDefaultReadinessProbeSpec(mgVersion)
}
}

Expand Down

0 comments on commit ecd1d17

Please sign in to comment.