apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: docker-build-push
  labels:
    dagweave.io/catalog-item: docker-build-push
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "2.0.0"
    dagweave.io/catalog-category: "CI/CD"
spec:
  entrypoint: build
  # Registry credentials are a Docker config.json mounted at $DOCKER_CONFIG, which is
  # where buildctl looks for auth. The Secret is a standard kubernetes.io/dockerconfigjson
  # (or any Secret whose key holds a Docker config).
  volumes:
    - name: docker-config
      secret:
        secretName: "{{inputs.parameters.docker-config-secret-name}}"
        items:
          - key: "{{inputs.parameters.docker-config-secret-key}}"
            path: config.json
  templates:
    - name: build
      inputs:
        parameters:
          # A directory in the container, or a git/HTTP URL. A git URL carries its branch
          # and subdirectory as a `#ref:subdir` fragment rather than in separate flags.
          - name: context
          # Path to the Dockerfile, relative to the context root.
          - name: dockerfile
            default: Dockerfile
          # Fully-qualified destination image reference to push (registry/repo:tag).
          - name: destination
          # "false" builds the image and keeps it in the builder, pushing nothing.
          - name: push
            default: "true"
          # Name of the Kubernetes Secret holding the registry Docker config.json.
          # Defaults let a user leave them blank in the Inspector.
          - name: docker-config-secret-name
            default: docker-config
          - name: docker-config-secret-key
            default: .dockerconfigjson
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # moby/buildkit:v0.34.1-rootless
        image: moby/buildkit@sha256:f68752a5a94acfcdad2d07867b7075601a421638e17fc5f28efc1c814d0c4f89
        command: [sh, -c]
        # Values arrive as env vars (below) and are never interpolated from Argo into the
        # shell, so a context or tag holding quotes or metacharacters is safe. The if is
        # because buildkit names a local context with --local and a remote one with --opt.
        args:
          - |
            set -eu
            if [ -d "$CONTEXT" ]; then
              set -- --local "context=$CONTEXT" --local "dockerfile=$CONTEXT"
            else
              set -- --opt "context=$CONTEXT"
            fi
            buildctl-daemonless.sh build \
              --frontend dockerfile.v0 \
              --opt "filename=$DOCKERFILE" \
              "$@" \
              --output "type=image,name=$DESTINATION,push=$PUSH"
        env:
          - name: CONTEXT
            value: "{{inputs.parameters.context}}"
          - name: DOCKERFILE
            value: "{{inputs.parameters.dockerfile}}"
          - name: DESTINATION
            value: "{{inputs.parameters.destination}}"
          - name: PUSH
            value: "{{inputs.parameters.push}}"
          # buildctl reads registry auth from $DOCKER_CONFIG/config.json.
          - name: DOCKER_CONFIG
            value: /docker-config
          # Rootless buildkitd cannot nest a second sandbox inside an unprivileged pod.
          - name: BUILDKITD_FLAGS
            value: --oci-worker-no-process-sandbox
          # buildctl-daemonless.sh starts buildkitd in this pod and polls for it,
          # sleeping (100 + try*20)ms and giving up at try > max with exit 1. The
          # image defaults max to 10, which is eleven sleeps and 2.2 SECONDS in
          # total. A quiet cluster beats that; a busy one does not, which is why
          # this item passed 6/6 alone and failed intermittently inside full runs.
          # 60 is 61 sleeps and about 43 seconds. A daemon that has genuinely not
          # started still fails, 40 seconds later than it used to.
          - name: BUILDCTL_CONNECT_RETRIES_MAX
            value: "60"
        # Rootless buildkit unshares a user namespace, which the default seccomp and
        # AppArmor profiles block. Nothing privileged and no added capability, but
        # Unconfined is more than kaniko needed and PodSecurity `restricted` forbids it.
        securityContext:
          runAsUser: 1000
          runAsGroup: 1000
          seccompProfile:
            type: Unconfined
          appArmorProfile:
            type: Unconfined
        volumeMounts:
          - name: docker-config
            mountPath: /docker-config
