Skip to content

Commit

Permalink
feat: add support for overriding termination grace period in test (#603)
Browse files Browse the repository at this point in the history
* feat: add support for overriding termination grace period in test

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* release notes

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

---------

Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Dec 5, 2023
1 parent ff2362e commit 876f282
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ spec:
description:
description: Description contains a description of the test.
type: string
forceTerminationGracePeriod:
description: ForceTerminationGracePeriod forces the termination grace
period on pods, statefulsets, daemonsets and deployments.
type: string
namespace:
description: Namespace determines whether the test should run in a
random ephemeral namespace or not.
Expand Down
1 change: 1 addition & 0 deletions .release-notes/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Release notes for `TODO`.
## 💫 New features 💫

- Added a cleanup delay option to add a delay between the time a test ends and the time cleanup starts
- Added support for overriding termination grace period on a per test basis

## 🔧 Fixes 🔧

Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/v1alpha1/test_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type TestSpec struct {
// Steps defining the test.
Steps []TestSpecStep `json:"steps"`

// ForceTerminationGracePeriod forces the termination grace period on pods, statefulsets, daemonsets and deployments.
// +optional
ForceTerminationGracePeriod *metav1.Duration `json:"forceTerminationGracePeriod,omitempty"`

// DelayBeforeCleanup adds a delay between the time a test ends and the time cleanup starts.
// +optional
DelayBeforeCleanup *metav1.Duration `json:"delayBeforeCleanup,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 4 additions & 0 deletions pkg/data/crds/chainsaw.kyverno.io_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ spec:
description:
description: Description contains a description of the test.
type: string
forceTerminationGracePeriod:
description: ForceTerminationGracePeriod forces the termination grace
period on pods, statefulsets, daemonsets and deployments.
type: string
namespace:
description: Namespace determines whether the test should run in a
random ephemeral namespace or not.
Expand Down
34 changes: 20 additions & 14 deletions pkg/runner/processors/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,26 @@ func (p *stepProcessor) fileRefOrResource(ref v1alpha1.FileRefOrResource) ([]uns
}

func (p *stepProcessor) prepareResource(resource unstructured.Unstructured) error {
if p.config.ForceTerminationGracePeriod != nil {
seconds := int64(p.config.ForceTerminationGracePeriod.Duration.Seconds())
switch resource.GetKind() {
case "Pod":
if err := unstructured.SetNestedField(resource.UnstructuredContent(), seconds, "spec", "terminationGracePeriodSeconds"); err != nil {
return err
}
case "Deployment", "StatefulSet", "DaemonSet", "Job":
if err := unstructured.SetNestedField(resource.UnstructuredContent(), seconds, "spec", "template", "spec", "terminationGracePeriodSeconds"); err != nil {
return err
}
case "CronJob":
if err := unstructured.SetNestedField(resource.UnstructuredContent(), seconds, "spec", "jobTemplate", "spec", "template", "spec", "terminationGracePeriodSeconds"); err != nil {
return err
terminationGracePeriod := p.config.ForceTerminationGracePeriod
if p.test.Spec.ForceTerminationGracePeriod != nil {
terminationGracePeriod = p.test.Spec.ForceTerminationGracePeriod
}
if terminationGracePeriod != nil {
seconds := int64(terminationGracePeriod.Seconds())
if seconds != 0 {
switch resource.GetKind() {
case "Pod":
if err := unstructured.SetNestedField(resource.UnstructuredContent(), seconds, "spec", "terminationGracePeriodSeconds"); err != nil {
return err
}
case "Deployment", "StatefulSet", "DaemonSet", "Job":
if err := unstructured.SetNestedField(resource.UnstructuredContent(), seconds, "spec", "template", "spec", "terminationGracePeriodSeconds"); err != nil {
return err
}
case "CronJob":
if err := unstructured.SetNestedField(resource.UnstructuredContent(), seconds, "spec", "jobTemplate", "spec", "template", "spec", "terminationGracePeriodSeconds"); err != nil {
return err
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/apis/chainsaw.v1alpha1.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ For multiple objects use labels.</p>
| `skipDelete` | `bool` | | | <p>SkipDelete determines whether the resources created by the test should be deleted after the test is executed.</p> |
| `namespace` | `string` | | | <p>Namespace determines whether the test should run in a random ephemeral namespace or not.</p> |
| `steps` | [`[]TestSpecStep`](#chainsaw-kyverno-io-v1alpha1-TestSpecStep) | :white_check_mark: | | <p>Steps defining the test.</p> |
| `forceTerminationGracePeriod` | [`meta/v1.Duration`](https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration) | | | <p>ForceTerminationGracePeriod forces the termination grace period on pods, statefulsets, daemonsets and deployments.</p> |
| `delayBeforeCleanup` | [`meta/v1.Duration`](https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration) | | | <p>DelayBeforeCleanup adds a delay between the time a test ends and the time cleanup starts.</p> |

## `TestSpecStep` {#chainsaw-kyverno-io-v1alpha1-TestSpecStep}
Expand Down

0 comments on commit 876f282

Please sign in to comment.