Skip to content
This repository has been archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
Add JobGeneration
Browse files Browse the repository at this point in the history
For tracking when jobs are recreated

Signed-off-by: Fabricio Aguiar <[email protected]>
  • Loading branch information
fao89 committed Apr 2, 2024
1 parent 1003b1a commit 6e8cdc0
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/bases/ansibleee.openstack.org_openstackansibleees.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,10 @@ spec:
type: object
status:
properties:
JobGeneration:
default: 1
format: int64
type: integer
JobStatus:
default: Pending
enum:
Expand Down
4 changes: 4 additions & 0 deletions api/v1beta1/openstack_ansibleee_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ type OpenStackAnsibleEEStatus struct {
// +kubebuilder:default:=Pending
// JobStatus status of the executed job (Pending/Running/Succeeded/Failed)
JobStatus string `json:"JobStatus,omitempty" optional:"true"`

// +kubebuilder:default:=1
// JobGeneration track the amount of job generations
JobGeneration int64 `json:"JobGeneration,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,10 @@ spec:
type: object
status:
properties:
JobGeneration:
default: 1
format: int64
type: integer
JobStatus:
default: Pending
enum:
Expand Down
4 changes: 4 additions & 0 deletions controllers/openstack_ansibleee_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func (r *OpenStackAnsibleEEReconciler) Reconcile(ctx context.Context, req ctrl.R
currentJobHash,
)

if len(currentJobHash) > 0 && ansibleeeJob.HasChanged() {
instance.Status.JobGeneration++
}

ctrlResult, err := ansibleeeJob.DoJob(
ctx,
helper,
Expand Down
5 changes: 5 additions & 0 deletions docs/assemblies/openstack_ansibleee.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ OpenStackAnsibleEEStatus defines the observed state of OpenStackAnsibleEE
| JobStatus status of the executed job (Pending/Running/Succeeded/Failed)
| string
| false
| JobGeneration
| JobGeneration track the amount of job generations
| int64
| false
|===
<<custom-resources,Back to Custom Resources>>
19 changes: 19 additions & 0 deletions tests/functional/ansibleee_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -72,6 +73,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Succeeded"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).To(HaveKey("ansibleee"))
})
Expand All @@ -93,6 +95,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -115,6 +118,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Failed"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))
})
Expand Down Expand Up @@ -145,6 +149,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Succeeded"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
originalInputHash := ansibleee.Status.Hash["input"]
Expect(ansibleee.Status.Hash).To(HaveKey("ansibleee"))
Expand All @@ -170,6 +175,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))

// simulate that the second job succeeds
th.SimulateJobSuccess(ansibleeeName)
Expand All @@ -182,6 +188,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Succeeded"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash["input"]).NotTo(Equal(originalInputHash))
Expect(ansibleee.Status.Hash["ansibleee"]).NotTo(Equal(originalJobHash))
})
Expand Down Expand Up @@ -227,6 +234,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -246,6 +254,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Succeeded"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).To(HaveKey("ansibleee"))
})
Expand All @@ -267,6 +276,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -289,6 +299,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Failed"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))
})
Expand Down Expand Up @@ -358,6 +369,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -377,6 +389,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Succeeded"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).To(HaveKey("ansibleee"))
})
Expand All @@ -398,6 +411,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -420,6 +434,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Failed"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))
})
Expand Down Expand Up @@ -506,6 +521,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -525,6 +541,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Succeeded"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).To(HaveKey("ansibleee"))
})
Expand All @@ -546,6 +563,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee := GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Running"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))

Expand All @@ -568,6 +586,7 @@ var _ = Describe("Ansibleee controller", func() {
)
ansibleee = GetAnsibleee(ansibleeeName)
Expect(ansibleee.Status.JobStatus).To(Equal("Failed"))
Expect(ansibleee.Status.JobGeneration).To(Equal(int64(1)))
Expect(ansibleee.Status.Hash).To(HaveKey("input"))
Expect(ansibleee.Status.Hash).NotTo(HaveKey("ansibleee"))
})
Expand Down
1 change: 1 addition & 0 deletions tests/kuttl/tests/run_failed_playbook/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ spec:
preserveJobs: true
status:
JobStatus: Failed
JobGeneration: 1
conditions:
- message: 'AnsibleExecutionJob error occured Internal error occurred: Job Failed.
Check job logs'
Expand Down
117 changes: 117 additions & 0 deletions tests/kuttl/tests/run_failed_playbook/02-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#
# Check for:
#
# - 1 OpenStackAnsibleEE CR
# - 1 failed-play pod
# - 1 failed-play job
# - Correct output from ansible play
#
apiVersion: ansibleee.openstack.org/v1beta1
kind: OpenStackAnsibleEE
metadata:
name: failed-play
spec:
name: openstackansibleee
play: |
- name: Execution failure
hosts: localhost
tasks:
- name: Copy absent file
ansible.builtin.shell: |
set -euxo pipefail
cp absent failed_op
preserveJobs: true
status:
JobStatus: Failed
JobGeneration: 2
conditions:
- message: 'AnsibleExecutionJob error occured Internal error occurred: Job Failed.
Check job logs'
reason: Error
severity: Warning
status: "False"
type: Ready
- message: 'AnsibleExecutionJob error occured Internal error occurred: Job Failed.
Check job logs'
reason: Error
severity: Warning
status: "False"
type: AnsibleExecutionJobReady
---
apiVersion: v1
kind: Pod
metadata:
generateName: failed-play-
labels:
app: openstackansibleee
job-name: failed-play
status:
phase: Failed
---
apiVersion: batch/v1
kind: Job
metadata:
labels:
app: openstackansibleee
job-name: failed-play
openstackansibleee_cr: failed-play
osaee: "true"
name: failed-play
spec:
backoffLimit: 1
completionMode: NonIndexed
completions: 1
parallelism: 1
template:
metadata:
labels:
app: openstackansibleee
batch.kubernetes.io/job-name: failed-play
job-name: failed-play
openstackansibleee_cr: failed-play
osaee: "true"
spec:
containers:
- args:
- ansible-runner
- run
- /runner
- -p
- playbook.yaml
- -i
- failed-play
env:
- name: RUNNER_PLAYBOOK
value: |2+
- name: Execution failure
hosts: localhost
tasks:
- name: Copy absent file
ansible.builtin.shell: |
set -euxo pipefail
cp absent failed_op
- name: RUNNER_EXTRA_VARS
value: |2+
aaa: %!s(int=1)
bbb: %!s(int=2)
ccc: %!s(int=3)
ddd: %!s(int=4)
imagePullPolicy: Always
name: openstackansibleee
dnsPolicy: ClusterFirst
restartPolicy: Never
schedulerName: default-scheduler
terminationGracePeriodSeconds: 30
status:
conditions:
- message: Job has reached the specified backoff limit
reason: BackoffLimitExceeded
status: "True"
type: Failed
failed: 2
19 changes: 19 additions & 0 deletions tests/kuttl/tests/run_failed_playbook/02-patch_aee.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: ansibleee.openstack.org/v1beta1
kind: OpenStackAnsibleEE
metadata:
name: failed-play
spec:
play: |
- name: Execution failure
hosts: localhost
tasks:
- name: Copy absent file
ansible.builtin.shell: |
set -euxo pipefail
cp absent failed_op
extraVars:
ddd: 4
aaa: 1
ccc: 3
bbb: 2
backoffLimit: 1
1 change: 1 addition & 0 deletions tests/kuttl/tests/run_simple_playbook/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spec:
msg: "Hello, world this is ansibleee-play.yaml"
status:
JobStatus: Succeeded
JobGeneration: 1
---
apiVersion: v1
kind: Pod
Expand Down
1 change: 1 addition & 0 deletions tests/kuttl/tests/run_simple_playbook_debug/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spec:
msg: "Hello, world this is ansibleee-play-debug.yaml"
status:
JobStatus: Running
JobGeneration: 1
---
apiVersion: v1
kind: Pod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spec:
msg: "Hello, world this is ansibleee-play-debug.yaml"
status:
JobStatus: Running
JobGeneration: 1
---
apiVersion: v1
kind: Pod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spec:
why: "because"
status:
JobStatus: Succeeded
JobGeneration: 1
---
apiVersion: v1
kind: Pod
Expand Down

0 comments on commit 6e8cdc0

Please sign in to comment.