forked from LaconicNetwork/kompose
Add readiness healthcheck generation by label (#1366)
This commit is contained in:
@@ -510,30 +510,53 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
|
||||
template.Spec.NodeSelector = service.Placement
|
||||
// Configure the HealthCheck
|
||||
// We check to see if it's blank
|
||||
if !reflect.DeepEqual(service.HealthChecks, kobject.HealthCheck{}) {
|
||||
if !reflect.DeepEqual(service.HealthChecks.Liveness, kobject.HealthCheck{}) {
|
||||
probe := api.Probe{}
|
||||
|
||||
if len(service.HealthChecks.Test) > 0 {
|
||||
if len(service.HealthChecks.Liveness.Test) > 0 {
|
||||
probe.Handler = api.Handler{
|
||||
Exec: &api.ExecAction{
|
||||
Command: service.HealthChecks.Test,
|
||||
Command: service.HealthChecks.Liveness.Test,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return errors.New("Health check must contain a command")
|
||||
}
|
||||
|
||||
probe.TimeoutSeconds = service.HealthChecks.Timeout
|
||||
probe.PeriodSeconds = service.HealthChecks.Interval
|
||||
probe.FailureThreshold = service.HealthChecks.Retries
|
||||
probe.TimeoutSeconds = service.HealthChecks.Liveness.Timeout
|
||||
probe.PeriodSeconds = service.HealthChecks.Liveness.Interval
|
||||
probe.FailureThreshold = service.HealthChecks.Liveness.Retries
|
||||
|
||||
// See issue: https://github.com/docker/cli/issues/116
|
||||
// StartPeriod has been added to docker/cli however, it is not yet added
|
||||
// to compose. Once the feature has been implemented, this will automatically work
|
||||
probe.InitialDelaySeconds = service.HealthChecks.StartPeriod
|
||||
probe.InitialDelaySeconds = service.HealthChecks.Liveness.StartPeriod
|
||||
|
||||
template.Spec.Containers[0].LivenessProbe = &probe
|
||||
}
|
||||
if !reflect.DeepEqual(service.HealthChecks.Readiness, kobject.HealthCheck{}) {
|
||||
probeHealthCheckReadiness := api.Probe{}
|
||||
if len(service.HealthChecks.Readiness.Test) > 0 {
|
||||
probeHealthCheckReadiness.Handler = api.Handler{
|
||||
Exec: &api.ExecAction{
|
||||
Command: service.HealthChecks.Readiness.Test,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return errors.New("Health check must contain a command")
|
||||
}
|
||||
|
||||
probeHealthCheckReadiness.TimeoutSeconds = service.HealthChecks.Readiness.Timeout
|
||||
probeHealthCheckReadiness.PeriodSeconds = service.HealthChecks.Readiness.Interval
|
||||
probeHealthCheckReadiness.FailureThreshold = service.HealthChecks.Readiness.Retries
|
||||
|
||||
// See issue: https://github.com/docker/cli/issues/116
|
||||
// StartPeriod has been added to docker/cli however, it is not yet added
|
||||
// to compose. Once the feature has been implemented, this will automatically work
|
||||
probeHealthCheckReadiness.InitialDelaySeconds = service.HealthChecks.Readiness.StartPeriod
|
||||
|
||||
template.Spec.Containers[0].ReadinessProbe = &probeHealthCheckReadiness
|
||||
}
|
||||
|
||||
if service.StopGracePeriod != "" {
|
||||
template.Spec.TerminationGracePeriodSeconds, err = DurationStrToSecondsInt(service.StopGracePeriod)
|
||||
|
||||
@@ -359,6 +359,45 @@ func TestIsDir(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestServiceWithoutPort this tests if Headless Service is created for services without Port.
|
||||
func TestServiceWithHealthCheck(t *testing.T) {
|
||||
service := kobject.ServiceConfig{
|
||||
ContainerName: "name",
|
||||
Image: "image",
|
||||
ServiceType: "Headless",
|
||||
HealthChecks: kobject.HealthChecks{
|
||||
Readiness: kobject.HealthCheck{
|
||||
Test: []string{"arg1", "arg2"},
|
||||
Timeout: 10,
|
||||
Interval: 5,
|
||||
Retries: 3,
|
||||
StartPeriod: 60,
|
||||
},
|
||||
Liveness: kobject.HealthCheck{
|
||||
Test: []string{"arg1", "arg2"},
|
||||
Timeout: 11,
|
||||
Interval: 6,
|
||||
Retries: 4,
|
||||
StartPeriod: 61,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
komposeObject := kobject.KomposeObject{
|
||||
ServiceConfigs: map[string]kobject.ServiceConfig{"app": service},
|
||||
}
|
||||
k := Kubernetes{}
|
||||
|
||||
objects, err := k.Transform(komposeObject, kobject.ConvertOptions{CreateD: true, Replicas: 1})
|
||||
if err != nil {
|
||||
t.Error(errors.Wrap(err, "k.Transform failed"))
|
||||
}
|
||||
if err := testutils.CheckForHealthCheckLivenessAndReadiness(objects); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TestServiceWithoutPort this tests if Headless Service is created for services without Port.
|
||||
func TestServiceWithoutPort(t *testing.T) {
|
||||
service := kobject.ServiceConfig{
|
||||
|
||||
Reference in New Issue
Block a user