Skip to content

PROPOSAL: Make Env in #ImageConfig a struct #1437

Closed

Description

@helderco

Overview

The OCI spec sets Env as a list of strings, naturally we want a 1:1 map in CUE:

// Container image config. See [OCI](https://www.opencontainers.org/).
// Spec left open on purpose to account for additional fields.
// [Image Spec](https://github.com/opencontainers/image-spec/blob/main/specs-go/v1/config.go)
// [Docker Superset](https://github.com/moby/buildkit/blob/master/frontend/dockerfile/dockerfile2llb/image.go)
#ImageConfig: {
	Env?: [...string]
	User?: string
	Cmd?: [...string]
	...
}

For example, with alpine:

config: {
    Env: ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]
    Cmd: ["/bin/sh"]
}

I'm working on #1339 to provide a build step that allows overriding #ImageConfig. You'd use it like this:

docker.#Build & {
    steps: [
        docker.#Pull & {
            source: "alpine"
        },
        docker.#Copy & {
            contents: inputs.directories.rootfs.contents
        },
        docker.#Set & {
            config: {
                Entrypoint: ["/entrypoint.sh"]
            }
        },
    ]
}

Problem

With CUE, it's much easier to work with structs, rather than lists, especially in the case of Env where we'll frequently need to reference keys individually.

Here's some pain points I've found.

Exhibit A: Env's special case

There's a special case for Env when creating a new #ImageConfig on #Set because it's actually a map disguised as a list (Labels is an actual map). So to reconcile, it's much easier to first convert the list to a struct, merge that, and convert back to a list to set the new Env.

Not too bad if this was it.

Exhibit B: Building uppon a current value (i.e., variable substitution)

I suppose this is why it's defined as a list, take the example of updating PATH:

// Dockerfile
...
ENV COMPOSER_HOME=/home/app/.composer
ENV PATH=${COMPOSER_HOME}:${PATH}

The variable substitution doesn't work with #ImageConfig though. It must come from the dockerfile2llb project. To do it in CUE, you need the previous value:

actions: {
    // outside #Build because I need to reference it's image next
    pull: docker.#Pull & { ... }

    // OMG, do I need to do this everytime?
    _current_env: (util.#ListToMap & { input: pull.image.config.Env }).output

    build: docker.#Build & {
        steps: [
            docker.#Copy & {
                input: pull.image  // now this is also needed
                contents: inputs.directories.rootfs.contents
            },
            docker.#Set & {
                // more duplication to reuse with this solution
                let COMPOSER_HOME = "/home/app/.composer"
                config: {
                    Env: [
                        "COMPOSER_HOME=\(COMPOSER_HOME)",
                        "PATH=\(COMPOSER_HOME):\(_current_env.PATH)",
                    ]
                    Entrypoint: ["/entrypoint.sh"]
                }
            }
        ]
    }
}

A bit ugly, that's why I experimented with a specialized #SetEnv abstraction:

actions: {
    build: docker.#Build & {
        steps: [
            docker.#Pull & { ... },
            docker.#Copy & { ... },
            docker.#SetEnv & {
                current: _
                env: {
                    COMPOSER_HOME: "/home/app/.composer"
                    PATH: "\(COMPOSER_HOME):\(current.PATH)"
                }
            },
            docker.#Set & {
                config: {
                    Entrypoint: ["/entrypoint.sh"]
                }
            },
        ]
    }
}

That's much better, but I wish #Set would work like this and be enough 🤔

We could support variable substitution in Go land as well, which would make this simpler but read on...

Exhibit C: Setting env in docker.#Run

We want to fill env with defaults from #ImageConfig in docker.#Run:

#Run: {
	...
	// Environment variables
	// Example: {"DEBUG": "1"}
	env: [string]: string

	// we need this conversion every time we need to reference a key
	_image_env: (util.#ListToMap & { input: image.config.Env }).output
        env: {
		for k, v in _image_env {
			"\(k)": string | *v
		}
	}
	...
}

Again, you could just iterate the list and do the split inside the for (which is actually what #ListToMap does), but what about all the other places where you may need an env var from #ImageConfig.Env?

Proposal

It would be much easier if in Go we read the list into a map when pulling or building with a Dockerfile, and back into a list when pushing.

In CUE then, make this:

#ImageConfig: {
	Env?: [string]: string
	User?: string
	Cmd?: [...string]
	...
}

Which would make #Set usage like so:

actions: {
    build: docker.#Build & {
        steps: [
            docker.#Pull & { ... },
            docker.#Copy & { ... },
            docker.#Set & {
                input: _
                config: {
                    Env: {
                        COMPOSER_HOME: "/home/app/.composer"
                        PATH: "\(COMPOSER_HOME):\(input.config.Env.PATH)"
                    }
                    Entrypoint: ["/entrypoint.sh"]
                }
            },
        ]
    }
}

And #Run:

#Run: {
	...
	// Environment variables
	// Example: {"DEBUG": "1"}
	env: {
		[string]: string
		if image.config.Env != _|_ {
			for k, v in image.config.Env {
				"\(k)": string | *v
			}
		}
	}
	...
}

Aren't these much better? It makes this much cleaner/neater.

Pros

  • No special case for merging Envs in docker.#Set;
  • No need for #SetEnv specialization;
  • No need to convert list to struct for better handling in CUE;
  • More intuitive to use;

Cons

  • No longer a 1:1 mapping with the spec (at least in CUE);
  • Are there implications to the final image if the order of the env vars is changed?

Seems to me these are non issues, unless someone has more to say.

/cc @shykes @aluzzardi @samalba @jlongtine

Activity

  1. changed the title [-]PROPOSAL: The case for making `Env` in `#ImageConfig` a struct[/-] [+]PROPOSAL: Make `Env` in `#ImageConfig` a struct[/+] on Jan 15, 2022
  2. jlongtine commented on Jan 18, 2022

    @jlongtine
    Contributor

    @helderco I generally like using maps in CUE as well. It's often much cleaner and more intuitive.

    This proposal generally feels good to me, and you've covered my concerns well in your Cons section. There is some uncertainty for me around the ordering bit, in particular. I'm not sure how that might impact Buildkit (and it may not matter).

  3. helderco commented on Jan 18, 2022

    @helderco
    ContributorAuthor

    If the order does matter somehow then this must always be taken into consideration. For example, in #Set, I’m converting that list into a struct to merge more easily and convert back into a list. That alone changes the order.

  4. aluzzardi commented on Jan 18, 2022

    @aluzzardi
    Contributor

    I'm open to the idea, however I want to pause for a second and look at the overall architecture.

    The original design goal of ImageConfig was to not process image configs in the runtime but instead push them down to CUE. As it turns out, processing the format with CUE is not straightforward.

    Implementing this proposal would require the runtime to understand image configs (e.g. they're not opaque blobs anymore), in addition to CUE understanding them as well.

    If we have to process them, does it still make sense to attempt to pass them verbatim rather than a "simplified" version? For instance it's called config.Env because that's the name of the upstream field. But if the runtime needs to translate a map to an array, why not call it config.env and follow the same casing conventions of dagger packages? It's going to require custom code anyway to translate.

    Also, do we want to pass ALL fields like we're doing now or just a simplified version (env, workdir, ...)? For instance healthchecks are also an array rather than a map, we'll run into the same problem as env.

  5. helderco commented on Jan 18, 2022

    @helderco
    ContributorAuthor

    Yeah, I like that approach @aluzzardi, it’s also more explicit.

    I looked at the Go code and this is the most straightforward, I just wasn’t sure how you would take it because I remember the decision of leaving it open and passing the config through to CUE so that changes from upstream wouldn’t have to be managed. But also the opposite have been decided, for example to explicitly set defaults in engine instead of delegating to the defaults of buildkit.

    A little copying is better than a little dependency.

    In fact, by being explicit it helps to avoid surprises from upstream.

  6. shykes commented on Jan 19, 2022

    @shykes
    Contributor

    Just checking where this leaves us. @helderco @aluzzardi is there a consensus on what changes you two recommend, or does this need a bit more discussion?

  7. helderco commented on Jan 19, 2022

    @helderco
    ContributorAuthor

    We'll need to discuss which fields we want to keep and which we want to translate. I can submit a PR that implements this and see how it goes.

  8. helderco commented on Jan 19, 2022

    @helderco
    ContributorAuthor

    Probably best to keep everything. If we pull an image, change some config, and push the new image... if we leave something out it won't get preserved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions