Skip to content
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,15 @@ by a private CA, on top of the system ones. It is checked on every run, and
never read for an archive. `--insecure-skip-tls-verify` accepts any
certificate instead, for a test cluster only, and prints a warning.
`--plain-http` reaches a registry that serves plain HTTP, and prints a warning
too. It applies to the registry of the source only, and excludes the two
certificate flags. Without it, the registry is reached over HTTPS only, even
on a private or loopback IP address. The agent takes a list instead,
too. It applies to the registry of the source only, or to the mirror when
`--mirror` is given, and excludes the two certificate flags. With `--mirror`,
it goes with `--ca-file`, which then covers the registry the source names.
Without it, the registry is reached over HTTPS only, even on a private or
loopback IP address. The agent takes a list instead,
`--plain-http-registry`, see [agent/README.md](agent/README.md).
`--mirror=<host[:port]>` pulls the source from that registry first, and falls
back on the registry the source names, see "Image pulling" in
[agent/DESIGN.md](agent/DESIGN.md).

The import refuses to replace a directory without a sentinel. The command runs
as root, so a wrong name or cache path that lands on a directory the store did
Expand Down
4 changes: 3 additions & 1 deletion agent/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ converges that node for real.
of the calls, key-value pairs balanced, not the wording, so follow
Kubernetes' own convention for messages: a capital first letter, no trailing
period, the object type named. The reconciler logs outcomes, the packages it
calls return errors.
calls return errors. One exception: the puller logs a mirror it passed over
when the registry served the image, with the logr logger of the context.
The pull worked, so there is no error to return.
- **Node labels**: patched, never updated wholesale. Several agents write the
same Node object at once, and everything they did not touch has to survive.
- **Vendor neutrality**: this code knows about Kubernetes and containerd, never
Expand Down
44 changes: 41 additions & 3 deletions agent/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,43 @@ Pulling and extraction use
goes with `--ca-file`, which covers the registries that serve TLS, and
excludes `--insecure-skip-tls-verify`, meant for test clusters only.
`imagecachectl import` keeps a `--plain-http` switch: it pulls one source
per run, so the switch already covers that source's registry alone.
per run, so the switch already covers that source's registry alone, or
the mirror when `--mirror` is given.
- The library has no mirror support. `--mirror <host[:port]>` adds one global
mirror, tried before the registry each source names, as containerd does with
`hosts.toml`. The source keeps its real name (`ghcr.io/scality/...`), and
the puller builds `<mirror>/<repository>:<tag>` (or `@<digest>`) from the
mirror's registry name, never by parsing a string: a short host name would
read as a Docker Hub repository. Each registry API request to the mirror,
under `/v2/`, carries `ns=<registry>`, the query parameter containerd sends;
a registry that honours it prepends it to the repository. Docker Hub goes as
`ns=docker.io`. The mirror is a registry like any other: `--ca-file` covers
it, and it is reached over plain HTTP only if `--plain-http-registry` lists
it. Rewriting the reference rather than the host of each request lets the
library pick the scheme and name the mirror in its errors.
The choice is made before any layer streams. A pull uses the mirror once it
has resolved the manifest and the configuration and answered a HEAD for
every layer: without the HEAD, a mirror missing one layer would fail the
pull on every pass and the registry would never be tried. A resolve (the
adoption check) reads no layer, so it asks no HEAD. Otherwise the registry
is tried, and when both fail the error carries both causes. When the
registry answers, the mirror's failure is logged rather than returned, since
nothing failed: the agent logs it, and `imagecachectl` prints it as a
warning. The layers then stream from whichever answered: a mirror lost
during the stream fails the pull, and the next pass resolves again.
The mirror is trusted the way containerd trusts one. It is expected to hold
the images: a pull-through proxy that fetches a blob only on a GET would
answer the HEAD with a 404 and be passed over. For a source by tag, the
mirror's tag wins, even if the registry has moved it since; a source by
digest does not have that gap. The sentinel records the source as the
resource gives it, never the mirror reference. A source that already names
the mirror is pulled once, with no `ns`. One mirror per registry is out of
scope.
`imagecachectl import --mirror` does the same. Its `--plain-http` then goes
to the mirror, not to the source's registry, so it goes with `--ca-file`,
which covers that registry. The command runs on a host that may resolve no
cluster name, so the mirror is given in a form the host can reach, such as
an IP.

The cache images are regular container images (they must remain importable
and mountable with `ctr` by provisioning tooling), so an artifact-oriented
Expand Down Expand Up @@ -471,7 +507,9 @@ The agent never writes ImageCache resources (no status, no finalizers).
nobody listens on leaves the node `pending`. Two registries run in the
cluster: a pull over plain HTTP lands on the selected node only, and a
registry signed by a private CA is refused until the shipped CA patch is
applied. An archive rewritten on the node with other content of the same
size is extracted again. `imagecachectl` seeds two directories on the
applied. With `--mirror`, a source the registry does not hold is pulled
from the mirror, then from the registry once the mirror is stopped. An
archive rewritten on the node with other content of the same size is
extracted again. `imagecachectl` seeds two directories on the
worker: the agent adopts the one holding the image its resource names, with
no extraction, and replaces the other.
8 changes: 8 additions & 0 deletions agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ warning at startup. It goes with `--ca-file`, which covers the registries that
serve TLS, but not with `--insecure-skip-tls-verify`. No manifest here sets
it.

`--mirror=<host[:port]>` names a registry every source is pulled from first,
as a containerd mirror is, so the `ImageCache` sources keep the real image
name, such as `ghcr.io/scality/x:1.0`. When the mirror fails or misses a
layer, the agent pulls from the registry the source names and logs the
mirror's failure. The mirror takes `--ca-file` like any registry, and needs
`--plain-http-registry` if it serves plain HTTP. How it works and what it
trusts is in "Image pulling" in [DESIGN.md](DESIGN.md).

Leader election is deliberately absent. Every agent converges the node it runs
on, so there is nothing to elect a leader for.

Expand Down
13 changes: 11 additions & 2 deletions agent/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func main() {
var caFile string
var skipVerify bool
var plainHTTPRegistries stringList
var mirror string
var tlsOpts []func(*tls.Config)
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
Expand Down Expand Up @@ -117,6 +118,10 @@ func main() {
flag.Var(&plainHTTPRegistries, "plain-http-registry",
"A registry, as host[:port] exactly as the sources name it, the agent may reach over plain HTTP. "+
"Repeatable. Excludes --insecure-skip-tls-verify.")
flag.StringVar(&mirror, "mirror", "",
"A registry, as host[:port], every source is pulled from first. The registry the source "+
"names is tried next, when the mirror fails. Reached over plain HTTP only if it is "+
"listed with --plain-http-registry.")
opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -225,10 +230,10 @@ func main() {
// Checked before anything starts: an unusable CA would otherwise surface
// as a failed pull on every node and every resource.
remote, err := puller.NewRemote(puller.TLS{
CAFile: caFile, SkipVerify: skipVerify, PlainHTTPRegistries: plainHTTPRegistries,
CAFile: caFile, SkipVerify: skipVerify, PlainHTTPRegistries: plainHTTPRegistries, Mirror: mirror,
})
if err != nil {
setupLog.Error(err, "unable to set up registry TLS")
setupLog.Error(err, "unable to set up the registry access")
os.Exit(1)
}
if skipVerify {
Expand All @@ -241,6 +246,10 @@ func main() {
"registries", []string(plainHTTPRegistries))
}

if mirror != "" {
setupLog.Info("sources are pulled from the mirror first", "mirror", mirror)
}

fw, err := controller.NewFSWatcher(nodeName)
if err != nil {
setupLog.Error(err, "unable to create filesystem watcher")
Expand Down
56 changes: 46 additions & 10 deletions agent/internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"path/filepath"
"strings"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
imgname "github.com/google/go-containerregistry/pkg/name"
"github.com/scality/go-errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -43,7 +45,11 @@ const importLong = `Fills the image cache with the archives a boot cache image c
<source> is either the path of a docker archive or the reference of an image
in a registry. It is a path when it starts with a separator or a dot, or ends
in .tar, and a reference otherwise: the shape of what you pass decides, not
what is on disk.`
what is on disk.

--plain-http excludes --insecure-skip-tls-verify. It also excludes --ca-file,
unless --mirror is given: the plain HTTP then goes to the mirror, and the CA
covers the registry the source names.`

const importExample = ` imagecachectl import --name worker-1-0-0 registry.example.com/boot-cache-worker:1.0.0
imagecachectl import --name worker-1-0-0 /mnt/iso/images/boot-cache-worker.tar
Expand All @@ -52,10 +58,10 @@ const importExample = ` imagecachectl import --name worker-1-0-0 registry.examp
// Run executes the command line and returns the process exit code: 0 on
// success, ExitInterrupted when the context was cancelled, 1 otherwise.
func Run(ctx context.Context, args []string, out, errOut io.Writer) int {
var name, cachePath, caFile string
var name, cachePath, caFile, mirror string
var skipVerify, plainHTTP bool
importCmd := &cobra.Command{
Use: "import --name <resource> [--cache-path <dir>] [--ca-file <file> | --insecure-skip-tls-verify | --plain-http] <source>",
Use: "import --name <resource> [--cache-path <dir>] [--mirror <host[:port]>] [--ca-file <file> | --insecure-skip-tls-verify] [--plain-http] <source>",
Short: "Fill the image cache from a registry or a docker archive",
Long: importLong,
Example: importExample,
Expand All @@ -74,14 +80,16 @@ func Run(ctx context.Context, args []string, out, errOut io.Writer) int {
if caFile != "" && skipVerify {
return errors.New("--ca-file and --insecure-skip-tls-verify exclude each other")
}
if plainHTTP && caFile != "" {
// With a mirror, --plain-http goes to the mirror and --ca-file
// covers the source's registry, so the two go together.
if plainHTTP && caFile != "" && mirror == "" {
return errors.New("--ca-file and --plain-http exclude each other")
}
if plainHTTP && skipVerify {
return errors.New("--insecure-skip-tls-verify and --plain-http exclude each other")
}
err := do(cmd.Context(), cachePath, name, args[0],
puller.TLS{CAFile: caFile, SkipVerify: skipVerify}, plainHTTP, out, errOut)
puller.TLS{CAFile: caFile, SkipVerify: skipVerify, Mirror: mirror}, plainHTTP, out, errOut)
Comment thread
ezekiel-alexrod marked this conversation as resolved.
Resolved
// An interrupted run is not a failure to diagnose: the extraction
// publishes by rename, so nothing half written is left behind.
if errors.Is(err, context.Canceled) {
Expand All @@ -98,7 +106,9 @@ func Run(ctx context.Context, args []string, out, errOut io.Writer) int {
importCmd.Flags().BoolVar(&skipVerify, "insecure-skip-tls-verify", false,
"accept any registry certificate, for a test cluster set up by hand only")
importCmd.Flags().BoolVar(&plainHTTP, "plain-http", false,
"allow plain HTTP to the registry, for a registry without TLS")
"allow plain HTTP to the registry, for a registry without TLS: to the mirror when --mirror is given")
importCmd.Flags().StringVar(&mirror, "mirror", "",
"registry, as `host[:port]`, to pull the source from first, before the registry it names")
_ = importCmd.MarkFlagRequired("name")

root := &cobra.Command{
Expand Down Expand Up @@ -149,6 +159,10 @@ func validate(name, cachePath string) error {
func do(ctx context.Context, cachePath, name, source string, tls puller.TLS, plainHTTP bool,
out, errOut io.Writer) error {
store := cache.Store{}
// The puller logs a mirror it passed over. On the host, that is a warning.
ctx = logr.NewContext(ctx, funcr.New(func(_, args string) {
printf(errOut, "imagecachectl: warning: %s\n", args)
}, funcr.Options{RenderBuiltinsHook: dropLevel}))

// Before the cache state, so that a CA that cannot be used is reported
// on every run, including one that would have found the resource
Expand All @@ -162,7 +176,11 @@ func do(ctx context.Context, cachePath, name, source string, tls puller.TLS, pla
printf(errOut, "imagecachectl: warning: the registry certificate is not verified\n")
}
if plainHTTP {
printf(errOut, "imagecachectl: warning: the registry may be reached over plain HTTP\n")
target := "the registry"
if tls.Mirror != "" {
target = "the mirror"
}
printf(errOut, "imagecachectl: warning: %s may be reached over plain HTTP\n", target)
}
}

Expand Down Expand Up @@ -219,8 +237,9 @@ func do(ctx context.Context, cachePath, name, source string, tls puller.TLS, pla
//
// The TLS and plain HTTP settings only matter to a registry, so an archive
// never reads them. The command pulls one source per run, so plain HTTP is
// allowed to that source's registry: the puller takes a list of registries,
// and this one is the only entry.
// allowed to one registry: the puller takes a list, and it is the only entry.
// With a mirror, that registry is the mirror, and the registry the source
// names keeps HTTPS, see "Image pulling" in agent/DESIGN.md.
func pullerFor(source string, tls puller.TLS, plainHTTP bool) (puller.Puller, error) {
switch {
case strings.HasPrefix(source, string(os.PathSeparator)),
Expand All @@ -230,9 +249,26 @@ func pullerFor(source string, tls puller.TLS, plainHTTP bool) (puller.Puller, er
default:
// A reference that does not parse is reported by the pull, which
// names it as one.
if ref, err := imgname.ParseReference(source); plainHTTP && err == nil {
switch ref, err := imgname.ParseReference(source); {
case !plainHTTP:
// Every registry stays HTTPS only.
case tls.Mirror != "":
tls.PlainHTTPRegistries = []string{tls.Mirror}
case err == nil:
tls.PlainHTTPRegistries = []string{ref.Context().RegistryStr()}
}
return puller.NewRemote(tls)
}
}

// dropLevel leaves the log level out of a warning: the command prints every
// line at the same level, so it says nothing to the reader.
func dropLevel(kv []any) []any {
out := make([]any, 0, len(kv))
for i := 0; i+1 < len(kv); i += 2 {
if kv[i] != "level" {
out = append(out, kv[i], kv[i+1])
}
}
return out
}
Loading
Loading