Skip to content

Commit 4513285

Browse files
committed
apis: add WebHook action and job lifecycle events
Add support for WebHook action in LifecyclePolicy to enable HTTP callbacks when job state changes. This includes: - New Events: JobTerminated, JobCompleted, JobAborted, JobFailed - New Action: WebHook with configurable URL, method, headers, body and timeout - WebHookConfig struct for webhook configuration - Fix build constraint syntax in hack/tools.go for Go compatibility This enables users to configure webhook notifications for job completion states through the existing Policies mechanism. Signed-off-by: chuqing1997 <[email protected]>
1 parent 59e96f6 commit 4513285

File tree

18 files changed

+292
-230
lines changed

18 files changed

+292
-230
lines changed

hack/tools.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//go:build tools
12
// +build tools
3+
24
package tools
35

46
import _ "k8s.io/code-generator"

pkg/apis/batch/v1alpha1/job.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ const (
175175
)
176176

177177
// LifecyclePolicy specifies the lifecycle and error handling of task and job.
178+
// +kubebuilder:validation:XValidation:rule="self.action != 'WebHook' || has(self.webhookConfig)",message="webhookConfig is required when action is WebHook"
178179
type LifecyclePolicy struct {
179180
// The action that will be taken to the PodGroup according to Event.
180181
// One of "Restart", "None".
@@ -202,6 +203,46 @@ type LifecyclePolicy struct {
202203
// Default to nil (take action immediately).
203204
// +optional
204205
Timeout *metav1.Duration `json:"timeout,omitempty" protobuf:"bytes,5,opt,name=timeout"`
206+
207+
// WebHookConfig specifies webhook configuration when Action is WebHook
208+
// +optional
209+
WebHookConfig *WebHookConfig `json:"webhookConfig,omitempty" protobuf:"bytes,6,opt,name=webhookConfig"`
210+
}
211+
212+
// WebHookConfig defines webhook configuration
213+
type WebHookConfig struct {
214+
// URL is the webhook endpoint URL
215+
// +kubebuilder:validation:Required
216+
// +kubebuilder:validation:Pattern=`^https?://.*`
217+
URL string `json:"url" protobuf:"bytes,1,opt,name=url"`
218+
219+
// Method is the HTTP method, defaults to POST
220+
// +kubebuilder:validation:Enum=GET;POST;PUT;PATCH
221+
// +kubebuilder:default="POST"
222+
// +optional
223+
Method string `json:"method,omitempty" protobuf:"bytes,2,opt,name=method"`
224+
225+
// Headers are custom HTTP headers to send
226+
// +optional
227+
Headers map[string]string `json:"headers,omitempty" protobuf:"bytes,3,opt,name=headers"`
228+
229+
// Body is the JSON request body template, supports variable substitution
230+
// +optional
231+
Body string `json:"body,omitempty" protobuf:"bytes,4,opt,name=body"`
232+
233+
// TimeoutSeconds is request timeout in seconds, defaults to 30
234+
// +kubebuilder:validation:Minimum=1
235+
// +kubebuilder:validation:Maximum=300
236+
// +kubebuilder:default=30
237+
// +optional
238+
TimeoutSeconds *int32 `json:"timeoutSeconds,omitempty" protobuf:"bytes,5,opt,name=timeoutSeconds"`
239+
240+
// Retries is the number of retry attempts for failed webhook requests, defaults to 3
241+
// +kubebuilder:validation:Minimum=0
242+
// +kubebuilder:validation:Maximum=10
243+
// +kubebuilder:default=3
244+
// +optional
245+
Retries *int32 `json:"retries,omitempty" protobuf:"bytes,6,opt,name=retries"`
205246
}
206247

207248
// +kubebuilder:validation:Enum=none;best-effort;restricted;single-numa-node

pkg/apis/batch/v1alpha1/zz_generated.deepcopy.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/bus/v1alpha1/actions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const (
4848
// ResumeJobAction is the action to resume an aborted job.
4949
ResumeJobAction Action = "ResumeJob"
5050

51+
// WebHookAction triggers a webhook call when event occurs.
52+
WebHookAction Action = "WebHook"
53+
5154
// Note: actions below are only used internally, should not be used by users.
5255

5356
// SyncJobAction is the action to sync Job/Pod status.

pkg/apis/bus/v1alpha1/events.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package v1alpha1
1818

1919
// Event represent the phase of Job, e.g. pod-failed.
20-
// +kubebuilder:validation:Enum=*;PodPending;PodRunning;PodFailed;PodEvicted;Unknown;TaskCompleted;OutOfSync;CommandIssued;JobUpdated;TaskFailed
20+
// +kubebuilder:validation:Enum=*;PodPending;PodRunning;PodFailed;PodEvicted;Unknown;TaskCompleted;OutOfSync;CommandIssued;JobUpdated;TaskFailed;JobTerminated;JobCompleted;JobAborted;JobFailed
2121
type Event string
2222

2323
const (
@@ -45,6 +45,18 @@ const (
4545
// TaskCompletedEvent is triggered if the 'Replicas' amount of pods in one task are succeed
4646
TaskCompletedEvent Event = "TaskCompleted"
4747

48+
// JobTerminatedEvent is triggered when job is terminated
49+
JobTerminatedEvent Event = "JobTerminated"
50+
51+
// JobCompletedEvent is triggered when job is completed successfully
52+
JobCompletedEvent Event = "JobCompleted"
53+
54+
// JobAbortedEvent is triggered when job is aborted
55+
JobAbortedEvent Event = "JobAborted"
56+
57+
// JobFailedEvent is triggered when job failed permanently
58+
JobFailedEvent Event = "JobFailed"
59+
4860
// Note: events below are used internally, should not be used by users.
4961

5062
// OutOfSyncEvent is triggered if Pod/Job is updated(add/update/delete)

pkg/client/applyconfiguration/batch/v1alpha1/lifecyclepolicy.go

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/client/applyconfiguration/batch/v1alpha1/networktopologyspec.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/client/applyconfiguration/batch/v1alpha1/webhookconfig.go

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/client/applyconfiguration/scheduling/v1beta1/networktopologyspec.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)