Skip to content

Commit

Permalink
Add connectivity test example
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Oct 18, 2023
1 parent c87bb6b commit fe24b7e
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 5 deletions.
24 changes: 24 additions & 0 deletions examples/minimal/cue.mod/pkg/timoni.sh/core/v1alpha1/action.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2023 Stefan Prodan
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

// Action holds a list of annotations for controlling
// Timoni's apply behaviour of Kubernetes resources.
action: {
// Force annotation for recreating immutable resources such as Kubernetes Jobs.
force: {
"action.timoni.sh/force": enabled
}
// One-off annotation for appling resources only if they don't exist on the cluster.
oneoff: {
"action.timoni.sh/one-off": enabled
}
// Keep annotation for preventing Timoni's garbage collector from deleting resources.
keep: {
"action.timoni.sh/prune": disabled
}
}

enabled: "enabled"
disabled: "disabled"
6 changes: 4 additions & 2 deletions examples/minimal/debug_tool.cue
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"text/tabwriter"
)

_resources: timoni.apply.app + timoni.apply.test

// The build command generates the Kubernetes manifests and prints the multi-docs YAML to stdout.
// Example 'cue cmd -t debug -t name=test -t namespace=test -t mv=1.0.0 -t kv=1.28.0 build'.
command: build: {
task: print: cli.Print & {
text: yaml.MarshalStream(timoni.apply.all)
text: yaml.MarshalStream(_resources)
}
}

Expand All @@ -20,7 +22,7 @@ command: ls: {
task: print: cli.Print & {
text: tabwriter.Write([
"RESOURCE \tAPI VERSION",
for r in timoni.apply.all {
for r in _resources {
if r.metadata.namespace == _|_ {
"\(r.kind)/\(r.metadata.name) \t\(r.apiVersion)"
}
Expand Down
8 changes: 8 additions & 0 deletions examples/minimal/debug_values.cue
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ values: {
tag: "1-alpine"
digest: ""
}
test: {
enabled: true
image: {
repository: "docker.io/curlimages/curl"
tag: "latest"
digest: ""
}
}
}
14 changes: 12 additions & 2 deletions examples/minimal/templates/config.cue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
selector: timoniv1.#Selector & {#Name: metadata.name}

// App settings
message: string
message!: string

// Deployment
replicas: *1 | int & >0
Expand All @@ -32,13 +32,19 @@ import (
topologySpreadConstraints?: [...corev1.#TopologySpreadConstraint]

// Container
image: timoniv1.#Image
image!: timoniv1.#Image
imagePullPolicy: *"IfNotPresent" | string
resources?: corev1.#ResourceRequirements
securityContext?: corev1.#SecurityContext

// Service
service: port: *80 | int & >0 & <=65535

// Test Job
test: {
enabled: *false | bool
image!: timoniv1.#Image
}
}

// Instance takes the config values and outputs the Kubernetes objects.
Expand All @@ -55,4 +61,8 @@ import (
_cmName: objects.cm.metadata.name
}
}

tests: {
"test-svc": #TestJob & {_config: config}
}
}
58 changes: 58 additions & 0 deletions examples/minimal/templates/job.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package templates

import (
"encoding/yaml"
"uuid"

corev1 "k8s.io/api/core/v1"
batchv1 "k8s.io/api/batch/v1"
timoniv1 "timoni.sh/core/v1alpha1"
)

#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
if _config.podSecurityContext != _|_ {
securityContext: _config.podSecurityContext
}
if _config.topologySpreadConstraints != _|_ {
topologySpreadConstraints: _config.topologySpreadConstraints
}
if _config.affinity != _|_ {
affinity: _config.affinity
}
if _config.tolerations != _|_ {
tolerations: _config.tolerations
}
if _config.imagePullSecrets != _|_ {
imagePullSecrets: _config.imagePullSecrets
}
}
}
7 changes: 6 additions & 1 deletion examples/minimal/timoni.cue
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ timoni: {

// Pass Kubernetes resources outputted by the instance
// to Timoni's multi-step apply.
apply: all: [ for obj in instance.objects {obj}]
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}]
}
}
5 changes: 5 additions & 0 deletions examples/minimal/values.cue
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ values: {
digest: "sha256:015b0c4d993d85846a15505e66c25ff717261f292d7168c70ab18ac5efbc4f00"
tag: "1.25.2"
}
test: image: {
repository: "cgr.dev/chainguard/curl"
digest: ""
tag: "latest"
}
}

0 comments on commit fe24b7e

Please sign in to comment.