# Slim OCI images of the swarm daemons. Imported from ../packages/default.nix # with the per-bin packages, and from ../checks.nix for `mkServiceImage` alone. { pkgs, lib, packages ? { }, }: let # The registry host every image name carries. `.invalid` is reserved by RFC 6761 # and never resolves, so a node that holds no copy of an image, and finds none # at a peer, fails to pull it instead of asking a public registry for a name # it does not own. ../host-modules/k8s/node.nix mirrors this host. registryHost = "registry.hyperhive.invalid"; # The conventional nonroot id. An OCI image has no `config.ids` to take it # from, so it is defined once here: the image's `User`, the smoke check and # a pod's `securityContext.runAsUser`/`runAsGroup` must all read this. serviceUser = { uid = 65532; gid = 65532; }; # One daemon per image: the binary, CA roots and the binary's own closure. # No shell and no init, so the image has no command to exec into and no # service manager to disagree with the kubelet about restarts. # # `buildLayeredImage` writes a `.tar.gz`, which `podman load`, `skopeo` and # k3s's image directory read. `streamLayeredImage` writes a script instead. # `tag` stays unset, so it is the content hash and a changed binary is a # changed reference; `imageName` and `imageTag` are on the result. mkServiceImage = { name, package, bin ? name, extraContents ? [ ], env ? { }, }: pkgs.dockerTools.buildLayeredImage { name = "${registryHost}/${name}"; contents = [ package pkgs.cacert ] ++ extraContents; config = { Entrypoint = [ "/bin/${bin}" ]; Env = [ "SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt" ] ++ lib.mapAttrsToList (k: v: "${k}=${v}") env; User = "${toString serviceUser.uid}:${toString serviceUser.gid}"; }; }; # `wget` and nothing else of busybox: the smallest HTTP client an image can # hold. A health listener binds to the pod's loopback, so the kubelet probes # it from inside the container with this. wgetOnly = pkgs.runCommand "wget-only" { } '' mkdir -p $out/bin ln -s ${pkgs.pkgsStatic.busybox}/bin/busybox $out/bin/wget ''; # An `exec` probe that GETs `path` on the loopback `port`. `wget` exits # non-zero on any non-2xx status, so a 503 fails the probe. loopbackProbe = port: path: { exec.command = [ "/bin/wget" "-q" "-O" "/dev/null" "http://127.0.0.1:${toString port}${path}" ]; periodSeconds = 10; timeoutSeconds = 5; }; daemon = extraContents: name: mkServiceImage { inherit name extraContents; package = packages.${name}; }; in { inherit mkServiceImage registryHost serviceUser wgetOnly loopbackProbe ; images = lib.genAttrs [ "swarm-controller" "swarm-nats-auth" "swarm-logs-auth" "swarm-metrics-auth" "swarm-notify-router" ] (daemon [ wgetOnly ]) // lib.genAttrs [ "swarm-ci-metrics" ] (daemon [ ]); }