@@ -8162,14 +8162,43 @@ func TestValidateInitContainers(t *testing.T) {
81628162 ImagePullPolicy: "IfNotPresent",
81638163 TerminationMessagePolicy: "File",
81648164 }, {
8165- Name: "container-3-restart-always-with-startup-probe ",
8165+ Name: "container-3-restart-always-with-lifecycle-hook-and-probes ",
81668166 Image: "image",
81678167 ImagePullPolicy: "IfNotPresent",
81688168 TerminationMessagePolicy: "File",
81698169 RestartPolicy: &containerRestartPolicyAlways,
8170+ Lifecycle: &core.Lifecycle{
8171+ PostStart: &core.LifecycleHandler{
8172+ Exec: &core.ExecAction{
8173+ Command: []string{"echo", "post start"},
8174+ },
8175+ },
8176+ PreStop: &core.LifecycleHandler{
8177+ Exec: &core.ExecAction{
8178+ Command: []string{"echo", "pre stop"},
8179+ },
8180+ },
8181+ },
8182+ LivenessProbe: &core.Probe{
8183+ ProbeHandler: core.ProbeHandler{
8184+ TCPSocket: &core.TCPSocketAction{
8185+ Port: intstr.FromInt32(80),
8186+ },
8187+ },
8188+ SuccessThreshold: 1,
8189+ },
8190+ ReadinessProbe: &core.Probe{
8191+ ProbeHandler: core.ProbeHandler{
8192+ TCPSocket: &core.TCPSocketAction{
8193+ Port: intstr.FromInt32(80),
8194+ },
8195+ },
8196+ },
81708197 StartupProbe: &core.Probe{
81718198 ProbeHandler: core.ProbeHandler{
8172- TCPSocket: &core.TCPSocketAction{Port: intstr.FromInt(80)},
8199+ TCPSocket: &core.TCPSocketAction{
8200+ Port: intstr.FromInt(80),
8201+ },
81738202 },
81748203 SuccessThreshold: 1,
81758204 },
@@ -8390,6 +8419,165 @@ func TestValidateInitContainers(t *testing.T) {
83908419 },
83918420 }},
83928421 field.ErrorList{{Type: field.ErrorTypeInvalid, Field: "initContainers[0].startupProbe.successThreshold", BadValue: int32(2)}},
8422+ }, {
8423+ "invalid readiness probe, terminationGracePeriodSeconds set.",
8424+ line(),
8425+ []core.Container{{
8426+ Name: "life-123",
8427+ Image: "image",
8428+ ImagePullPolicy: "IfNotPresent",
8429+ TerminationMessagePolicy: "File",
8430+ RestartPolicy: &containerRestartPolicyAlways,
8431+ ReadinessProbe: &core.Probe{
8432+ ProbeHandler: core.ProbeHandler{
8433+ TCPSocket: &core.TCPSocketAction{
8434+ Port: intstr.FromInt32(80),
8435+ },
8436+ },
8437+ TerminationGracePeriodSeconds: utilpointer.Int64(10),
8438+ },
8439+ }},
8440+ field.ErrorList{{Type: field.ErrorTypeInvalid, Field: "initContainers[0].readinessProbe.terminationGracePeriodSeconds", BadValue: utilpointer.Int64(10)}},
8441+ }, {
8442+ "invalid liveness probe, successThreshold != 1",
8443+ line(),
8444+ []core.Container{{
8445+ Name: "live-123",
8446+ Image: "image",
8447+ ImagePullPolicy: "IfNotPresent",
8448+ TerminationMessagePolicy: "File",
8449+ RestartPolicy: &containerRestartPolicyAlways,
8450+ LivenessProbe: &core.Probe{
8451+ ProbeHandler: core.ProbeHandler{
8452+ TCPSocket: &core.TCPSocketAction{
8453+ Port: intstr.FromInt32(80),
8454+ },
8455+ },
8456+ SuccessThreshold: 2,
8457+ },
8458+ }},
8459+ field.ErrorList{{Type: field.ErrorTypeInvalid, Field: "initContainers[0].livenessProbe.successThreshold", BadValue: int32(2)}},
8460+ }, {
8461+ "invalid lifecycle, no exec command.",
8462+ line(),
8463+ []core.Container{{
8464+ Name: "life-123",
8465+ Image: "image",
8466+ ImagePullPolicy: "IfNotPresent",
8467+ TerminationMessagePolicy: "File",
8468+ RestartPolicy: &containerRestartPolicyAlways,
8469+ Lifecycle: &core.Lifecycle{
8470+ PreStop: &core.LifecycleHandler{
8471+ Exec: &core.ExecAction{},
8472+ },
8473+ },
8474+ }},
8475+ field.ErrorList{{Type: field.ErrorTypeRequired, Field: "initContainers[0].lifecycle.preStop.exec.command", BadValue: ""}},
8476+ }, {
8477+ "invalid lifecycle, no http path.",
8478+ line(),
8479+ []core.Container{{
8480+ Name: "life-123",
8481+ Image: "image",
8482+ ImagePullPolicy: "IfNotPresent",
8483+ TerminationMessagePolicy: "File",
8484+ RestartPolicy: &containerRestartPolicyAlways,
8485+ Lifecycle: &core.Lifecycle{
8486+ PreStop: &core.LifecycleHandler{
8487+ HTTPGet: &core.HTTPGetAction{
8488+ Port: intstr.FromInt32(80),
8489+ Scheme: "HTTP",
8490+ },
8491+ },
8492+ },
8493+ }},
8494+ field.ErrorList{{Type: field.ErrorTypeRequired, Field: "initContainers[0].lifecycle.preStop.httpGet.path", BadValue: ""}},
8495+ }, {
8496+ "invalid lifecycle, no http port.",
8497+ line(),
8498+ []core.Container{{
8499+ Name: "life-123",
8500+ Image: "image",
8501+ ImagePullPolicy: "IfNotPresent",
8502+ TerminationMessagePolicy: "File",
8503+ RestartPolicy: &containerRestartPolicyAlways,
8504+ Lifecycle: &core.Lifecycle{
8505+ PreStop: &core.LifecycleHandler{
8506+ HTTPGet: &core.HTTPGetAction{
8507+ Path: "/",
8508+ Scheme: "HTTP",
8509+ },
8510+ },
8511+ },
8512+ }},
8513+ field.ErrorList{{Type: field.ErrorTypeInvalid, Field: "initContainers[0].lifecycle.preStop.httpGet.port", BadValue: 0}},
8514+ }, {
8515+ "invalid lifecycle, no http scheme.",
8516+ line(),
8517+ []core.Container{{
8518+ Name: "life-123",
8519+ Image: "image",
8520+ ImagePullPolicy: "IfNotPresent",
8521+ TerminationMessagePolicy: "File",
8522+ RestartPolicy: &containerRestartPolicyAlways,
8523+ Lifecycle: &core.Lifecycle{
8524+ PreStop: &core.LifecycleHandler{
8525+ HTTPGet: &core.HTTPGetAction{
8526+ Path: "/",
8527+ Port: intstr.FromInt32(80),
8528+ },
8529+ },
8530+ },
8531+ }},
8532+ field.ErrorList{{Type: field.ErrorTypeNotSupported, Field: "initContainers[0].lifecycle.preStop.httpGet.scheme", BadValue: core.URIScheme("")}},
8533+ }, {
8534+ "invalid lifecycle, no tcp socket port.",
8535+ line(),
8536+ []core.Container{{
8537+ Name: "life-123",
8538+ Image: "image",
8539+ ImagePullPolicy: "IfNotPresent",
8540+ TerminationMessagePolicy: "File",
8541+ RestartPolicy: &containerRestartPolicyAlways,
8542+ Lifecycle: &core.Lifecycle{
8543+ PreStop: &core.LifecycleHandler{
8544+ TCPSocket: &core.TCPSocketAction{},
8545+ },
8546+ },
8547+ }},
8548+ field.ErrorList{{Type: field.ErrorTypeInvalid, Field: "initContainers[0].lifecycle.preStop.tcpSocket.port", BadValue: 0}},
8549+ }, {
8550+ "invalid lifecycle, zero tcp socket port.",
8551+ line(),
8552+ []core.Container{{
8553+ Name: "life-123",
8554+ Image: "image",
8555+ ImagePullPolicy: "IfNotPresent",
8556+ TerminationMessagePolicy: "File",
8557+ RestartPolicy: &containerRestartPolicyAlways,
8558+ Lifecycle: &core.Lifecycle{
8559+ PreStop: &core.LifecycleHandler{
8560+ TCPSocket: &core.TCPSocketAction{
8561+ Port: intstr.FromInt32(0),
8562+ },
8563+ },
8564+ },
8565+ }},
8566+ field.ErrorList{{Type: field.ErrorTypeInvalid, Field: "initContainers[0].lifecycle.preStop.tcpSocket.port", BadValue: 0}},
8567+ }, {
8568+ "invalid lifecycle, no action.",
8569+ line(),
8570+ []core.Container{{
8571+ Name: "life-123",
8572+ Image: "image",
8573+ ImagePullPolicy: "IfNotPresent",
8574+ TerminationMessagePolicy: "File",
8575+ RestartPolicy: &containerRestartPolicyAlways,
8576+ Lifecycle: &core.Lifecycle{
8577+ PreStop: &core.LifecycleHandler{},
8578+ },
8579+ }},
8580+ field.ErrorList{{Type: field.ErrorTypeRequired, Field: "initContainers[0].lifecycle.preStop", BadValue: ""}},
83938581 },
83948582 }
83958583 for _, tc := range errorCases {
0 commit comments