Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkg/templates/partials/manifests.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -737,10 +737,16 @@ spec:
volumeMounts:
- name: toe-volume
mountPath: /etc/teknoir
# The TOE image is pinned by digest: a moving tag with imagePullPolicy: Always can turn a
# changed or unpullable image into a watchdog reboot loop. Bump it together with the robots
# (ansible playbooks/toe-image.yml). The digest is the multi-arch (amd64 + arm64) index
# copied from ghcr.io/teknoir/toe-app:sha-4c366dae.
# Keep `command: - /manager`, `image:` and `imagePullPolicy:` on consecutive lines: the playbook
# finds the toe container by exactly that sequence (ansible playbooks/vars/toe-manifest.yml toe_re).
command:
- /manager
image: docker.io/tekn0ir/toe:toe_v4
imagePullPolicy: Always
image: docker.io/tekn0ir/toe:toe_v4.2-4c366dae@sha256:c83116d0829561afc639eec90a75b82ee10e565b08e65dcb36e122a830935207
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /healthz
Expand Down
10 changes: 8 additions & 2 deletions pkg/templates/testdata/docker_server.golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -885,10 +885,16 @@ spec:
volumeMounts:
- name: toe-volume
mountPath: /etc/teknoir
# The TOE image is pinned by digest: a moving tag with imagePullPolicy: Always can turn a
# changed or unpullable image into a watchdog reboot loop. Bump it together with the robots
# (ansible playbooks/toe-image.yml). The digest is the multi-arch (amd64 + arm64) index
# copied from ghcr.io/teknoir/toe-app:sha-4c366dae.
# Keep `command: - /manager`, `image:` and `imagePullPolicy:` on consecutive lines: the playbook
# finds the toe container by exactly that sequence (ansible playbooks/vars/toe-manifest.yml toe_re).
command:
- /manager
image: docker.io/tekn0ir/toe:toe_v4
imagePullPolicy: Always
image: docker.io/tekn0ir/toe:toe_v4.2-4c366dae@sha256:c83116d0829561afc639eec90a75b82ee10e565b08e65dcb36e122a830935207
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /healthz
Expand Down
10 changes: 8 additions & 2 deletions pkg/templates/testdata/server.golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2837,10 +2837,16 @@ spec:
volumeMounts:
- name: toe-volume
mountPath: /etc/teknoir
# The TOE image is pinned by digest: a moving tag with imagePullPolicy: Always can turn a
# changed or unpullable image into a watchdog reboot loop. Bump it together with the robots
# (ansible playbooks/toe-image.yml). The digest is the multi-arch (amd64 + arm64) index
# copied from ghcr.io/teknoir/toe-app:sha-4c366dae.
# Keep `command: - /manager`, `image:` and `imagePullPolicy:` on consecutive lines: the playbook
# finds the toe container by exactly that sequence (ansible playbooks/vars/toe-manifest.yml toe_re).
command:
- /manager
image: docker.io/tekn0ir/toe:toe_v4
imagePullPolicy: Always
image: docker.io/tekn0ir/toe:toe_v4.2-4c366dae@sha256:c83116d0829561afc639eec90a75b82ee10e565b08e65dcb36e122a830935207
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /healthz
Expand Down
10 changes: 8 additions & 2 deletions pkg/templates/testdata/server.roles.golden.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2990,10 +2990,16 @@ spec:
volumeMounts:
- name: toe-volume
mountPath: /etc/teknoir
# The TOE image is pinned by digest: a moving tag with imagePullPolicy: Always can turn a
# changed or unpullable image into a watchdog reboot loop. Bump it together with the robots
# (ansible playbooks/toe-image.yml). The digest is the multi-arch (amd64 + arm64) index
# copied from ghcr.io/teknoir/toe-app:sha-4c366dae.
# Keep `command: - /manager`, `image:` and `imagePullPolicy:` on consecutive lines: the playbook
# finds the toe container by exactly that sequence (ansible playbooks/vars/toe-manifest.yml toe_re).
command:
- /manager
image: docker.io/tekn0ir/toe:toe_v4
imagePullPolicy: Always
image: docker.io/tekn0ir/toe:toe_v4.2-4c366dae@sha256:c83116d0829561afc639eec90a75b82ee10e565b08e65dcb36e122a830935207
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /healthz
Expand Down
124 changes: 124 additions & 0 deletions pkg/templates/toeimage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package templates

import (
"regexp"
"strings"
"testing"
"text/template"
)

// toeDigestRE is the pinned form of the TOE image: a tag for humans, then the digest that really
// decides what runs.
var toeDigestRE = regexp.MustCompile(`@sha256:[0-9a-f]{64}$`)

// toeDeployment returns the rendered Deployment named toe of a script, or "" when the script has
// none (the agent script only joins the cluster).
func toeDeployment(out string) string {
const marker = "\n name: toe\n"
i := strings.Index(out, marker)
if i < 0 {
return ""
}
rest := out[i+len(marker):]
if j := strings.Index(rest, "\n---"); j >= 0 {
rest = rest[:j]
}
return rest
}

// toeContainer returns the image and imagePullPolicy of the container that runs /manager in the
// toe Deployment of a rendered script.
func toeContainer(t *testing.T, out string) (image, policy string) {
t.Helper()
block := toeDeployment(out)
if block == "" {
t.Fatal("no Deployment named toe in the rendered script")
}
lines := strings.Split(block, "\n")
for i, l := range lines {
if strings.TrimSpace(l) != "- /manager" {
continue
}
for _, c := range lines[i:] {
s := strings.TrimSpace(c)
if image == "" && strings.HasPrefix(s, "image:") {
image = strings.TrimSpace(strings.TrimPrefix(s, "image:"))
}
if policy == "" && strings.HasPrefix(s, "imagePullPolicy:") {
policy = strings.TrimSpace(strings.TrimPrefix(s, "imagePullPolicy:"))
}
}
return image, policy
}
t.Fatal("no command line - /manager in the toe Deployment")
return "", ""
}

// Every variant that renders the TOE Deployment pins the image by digest: a moving tag with
// imagePullPolicy: Always can turn a changed or unpullable image into a watchdog reboot loop.
// The digest is the one ansible playbooks/toe-image.yml writes on the robots; bump both together.
func TestTOEImagePinnedByDigest(t *testing.T) {
variants := map[string]*template.Template{
"server": ServerTemplate,
"docker server": DockerServerTemplate,
}
for name, tpl := range variants {
t.Run(name, func(t *testing.T) {
image, policy := toeContainer(t, renderK3S(t, tpl, ""))
if !toeDigestRE.MatchString(image) {
t.Errorf("toe image %q is not pinned by @sha256: and a 64-character digest", image)
}
if !strings.Contains(image, "tekn0ir/toe:") {
t.Errorf("toe image %q is not the TOE image", image)
}
if policy != "IfNotPresent" {
t.Errorf("toe imagePullPolicy is %q, want IfNotPresent", policy)
}
})
}
t.Run("server roles", func(t *testing.T) {
var b strings.Builder
if err := ServerTemplate.Execute(&b, serverRolesData()); err != nil {
t.Fatal(err)
}
out := b.String()
image, policy := toeContainer(t, out)
if !toeDigestRE.MatchString(image) {
t.Errorf("toe image %q is not pinned by digest", image)
}
if policy != "IfNotPresent" {
t.Errorf("toe imagePullPolicy is %q, want IfNotPresent", policy)
}
if strings.Contains(toeDeployment(out), "toe:toe_v4\n") {
t.Error("the toe Deployment still uses the moving tag toe_v4")
}
})
}

// toeAnsibleRE is toe_re of the ansible repo (playbooks/vars/toe-manifest.yml): toe-image.yml and the
// rebootstrap TOE keep find the toe container by exactly this sequence and refuse a manifest where it
// is not found once. A comment or another field between the lines breaks every later TOE rollout.
var toeAnsibleRE = regexp.MustCompile(`(?m)([ ]+command:\n[ ]+- /manager\n[ ]+image: )(\S+)(\n[ ]+imagePullPolicy: )(\S+)`)

func TestTOEContainerMatchesAnsiblePlaybook(t *testing.T) {
var roles strings.Builder
if err := ServerTemplate.Execute(&roles, serverRolesData()); err != nil {
t.Fatal(err)
}
outputs := map[string]string{
"server": renderK3S(t, ServerTemplate, ""),
"docker server": renderK3S(t, DockerServerTemplate, ""),
"server roles": roles.String(),
}
for name, out := range outputs {
t.Run(name, func(t *testing.T) {
m := toeAnsibleRE.FindAllStringSubmatch(out, -1)
if len(m) != 1 {
t.Fatalf("the playbook's toe_re matches %d times, want exactly 1", len(m))
}
if !toeDigestRE.MatchString(m[0][2]) || m[0][4] != "IfNotPresent" {
t.Errorf("toe_re found image %q policy %q", m[0][2], m[0][4])
}
})
}
}
Loading