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

Add capability to run tests with Kubernetes Jobs #220

Merged
merged 6 commits into from
Oct 19, 2023
Merged

Conversation

stefanprodan
Copy link
Owner

@stefanprodan stefanprodan commented Oct 18, 2023

This PR adds testing capabilities to Timoni. Module authors can add Kubernetes Jobs templates that get applied after all other resources. After the app workloads are applied and become ready, Timoni will apply the Kubernetes Jobs and wait for the created pods to run to completion. On upgrades, Timoni will delete the previous test pods and will recreate the Jobs for the current module values and version. Test runs are idempotent, if the values or the module version doesn't change, Timoni will not create new test pods, tests are run only when a drift is detected in desired state vs cluster state.

Example

Define a Job that performs a connectivity test:

#TestJob: batchv1.#Job & {
	_config:    #Config
	apiVersion: "batch/v1"
	kind:       "Job"
	metadata: name:        "\(_config.metadata.name)-test"
	metadata: namespace:   _config.metadata.namespace
	metadata: labels:      _config.metadata.labels
	metadata: annotations: timoniv1.action.force
	spec: batchv1.#JobSpec & {
		template: corev1.#PodTemplateSpec & {
			metadata: labels: _config.metadata.labels
			let _checksum = uuid.SHA1(uuid.ns.DNS, yaml.Marshal(_config))
			metadata: annotations: "timoni.sh/checksum": "\(_checksum)"
			spec: {
				containers: [{
					name:            "curl"
					image:           _config.test.image.reference
					imagePullPolicy: _config.imagePullPolicy
					command: [
						"curl",
						"-v",
						"-m",
						"5",
						"\(_config.metadata.name):\(_config.service.port)",
					]
				}]
				restartPolicy: "Never"
			}
		}
		backoffLimit: 1
	}
}

Allow end-users to enable the tests:

// timoni.cue

apply: app: [ for obj in instance.objects {obj}]

// Conditionally run tests after an install or upgrade.
if instance.config.test.enabled {
	apply: test: [ for obj in instance.tests {obj}]
}

@stefanprodan stefanprodan added area/api API related issues and pull requests area/runtime Kubernetes operations related issues and pull requests labels Oct 18, 2023
@stefanprodan stefanprodan marked this pull request as ready for review October 19, 2023 19:23
@stefanprodan stefanprodan merged commit 4489fe8 into main Oct 19, 2023
4 checks passed
@stefanprodan stefanprodan deleted the testing-feature branch October 19, 2023 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/api API related issues and pull requests area/runtime Kubernetes operations related issues and pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant