Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Sleep Action for PreStop Hook #119026

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions api/openapi-spec/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions api/openapi-spec/v3/api__v1_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2901,6 +2901,14 @@
],
"description": "HTTPGet specifies the http request to perform."
},
"sleep": {
"allOf": [
{
"$ref": "#/components/schemas/io.k8s.api.core.v1.SleepAction"
}
],
"description": "Sleep represents the duration that the container should sleep before being terminated."
},
"tcpSocket": {
"allOf": [
{
Expand Down Expand Up @@ -7357,6 +7365,21 @@
},
"type": "object"
},
"io.k8s.api.core.v1.SleepAction": {
"description": "SleepAction describes a \"sleep\" action.",
"properties": {
"seconds": {
"default": 0,
"description": "Seconds is the number of seconds to sleep.",
"format": "int64",
"type": "integer"
}
},
"required": [
"seconds"
],
"type": "object"
},
"io.k8s.api.core.v1.StorageOSPersistentVolumeSource": {
"description": "Represents a StorageOS persistent volume resource.",
"properties": {
Expand Down
23 changes: 23 additions & 0 deletions api/openapi-spec/v3/apis__apps__v1_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2760,6 +2760,14 @@
],
"description": "HTTPGet specifies the http request to perform."
},
"sleep": {
"allOf": [
{
"$ref": "#/components/schemas/io.k8s.api.core.v1.SleepAction"
}
],
"description": "Sleep represents the duration that the container should sleep before being terminated."
},
"tcpSocket": {
"allOf": [
{
Expand Down Expand Up @@ -4438,6 +4446,21 @@
],
"type": "object"
},
"io.k8s.api.core.v1.SleepAction": {
"description": "SleepAction describes a \"sleep\" action.",
"properties": {
"seconds": {
"default": 0,
"description": "Seconds is the number of seconds to sleep.",
"format": "int64",
"type": "integer"
}
},
"required": [
"seconds"
],
"type": "object"
},
"io.k8s.api.core.v1.StorageOSVolumeSource": {
"description": "Represents a StorageOS persistent volume resource.",
"properties": {
Expand Down
23 changes: 23 additions & 0 deletions api/openapi-spec/v3/apis__batch__v1_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,14 @@
],
"description": "HTTPGet specifies the http request to perform."
},
"sleep": {
"allOf": [
{
"$ref": "#/components/schemas/io.k8s.api.core.v1.SleepAction"
}
],
"description": "Sleep represents the duration that the container should sleep before being terminated."
},
"tcpSocket": {
"allOf": [
{
Expand Down Expand Up @@ -3629,6 +3637,21 @@
],
"type": "object"
},
"io.k8s.api.core.v1.SleepAction": {
"description": "SleepAction describes a \"sleep\" action.",
"properties": {
"seconds": {
"default": 0,
"description": "Seconds is the number of seconds to sleep.",
"format": "int64",
"type": "integer"
}
},
"required": [
"seconds"
],
"type": "object"
},
"io.k8s.api.core.v1.StorageOSVolumeSource": {
"description": "Represents a StorageOS persistent volume resource.",
"properties": {
Expand Down
58 changes: 58 additions & 0 deletions pkg/api/pod/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,64 @@ func dropDisabledFields(
}
// For other types of containers, validateContainers will handle them.
}

if !utilfeature.DefaultFeatureGate.Enabled(features.PodLifecycleSleepAction) && !podLifecycleSleepActionInUse(oldPodSpec) {
for i := range podSpec.Containers {
if podSpec.Containers[i].Lifecycle == nil {
continue
}
if podSpec.Containers[i].Lifecycle.PreStop != nil {
podSpec.Containers[i].Lifecycle.PreStop.Sleep = nil
AxeZhan marked this conversation as resolved.
Show resolved Hide resolved
}
if podSpec.Containers[i].Lifecycle.PostStart != nil {
podSpec.Containers[i].Lifecycle.PostStart.Sleep = nil
}
}
for i := range podSpec.InitContainers {
if podSpec.InitContainers[i].Lifecycle == nil {
continue
}
if podSpec.InitContainers[i].Lifecycle.PreStop != nil {
podSpec.InitContainers[i].Lifecycle.PreStop.Sleep = nil
}
if podSpec.InitContainers[i].Lifecycle.PostStart != nil {
podSpec.InitContainers[i].Lifecycle.PostStart.Sleep = nil
}
}
for i := range podSpec.EphemeralContainers {
if podSpec.EphemeralContainers[i].Lifecycle == nil {
continue
}
if podSpec.EphemeralContainers[i].Lifecycle.PreStop != nil {
podSpec.EphemeralContainers[i].Lifecycle.PreStop.Sleep = nil
}
if podSpec.EphemeralContainers[i].Lifecycle.PostStart != nil {
podSpec.EphemeralContainers[i].Lifecycle.PostStart.Sleep = nil
}
}
}
}

func podLifecycleSleepActionInUse(podSpec *api.PodSpec) bool {
thockin marked this conversation as resolved.
Show resolved Hide resolved
if podSpec == nil {
return false
}
var inUse bool
VisitContainers(podSpec, AllContainers, func(c *api.Container, containerType ContainerType) bool {
if c.Lifecycle == nil {
return true
}
if c.Lifecycle.PreStop != nil && c.Lifecycle.PreStop.Sleep != nil {
inUse = true
return false
}
if c.Lifecycle.PostStart != nil && c.Lifecycle.PostStart.Sleep != nil {
inUse = true
return false
}
return true
})
return inUse
}

// dropDisabledPodStatusFields removes disabled fields from the pod status
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,12 @@ type ExecAction struct {
Command []string
}

// SleepAction describes a "sleep" action.
type SleepAction struct {
// Seconds is the number of seconds to sleep.
Seconds int64
AxeZhan marked this conversation as resolved.
Show resolved Hide resolved
}

// Probe describes a health check to be performed against a container to determine whether it is
// alive or ready to receive traffic.
type Probe struct {
Expand Down Expand Up @@ -2432,6 +2438,10 @@ type LifecycleHandler struct {
// lifecycle hooks will fail in runtime when tcp handler is specified.
// +optional
TCPSocket *TCPSocketAction
// Sleep represents the duration that the container should sleep before being terminated.
// +featureGate=PodLifecycleSleepAction
// +optional
Sleep *SleepAction
}

type GRPCAction struct {
Expand Down
32 changes: 32 additions & 0 deletions pkg/apis/core/v1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.