Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for private Docker registries #1666

Closed
wants to merge 10 commits into from

Conversation

RichhLi
Copy link
Contributor

@RichhLi RichhLi commented Jun 21, 2023

Usage:

envd bootstrap --registry-config path_to_json_file

Format:
Screenshot 2023-06-23 at 9 32 52 PM

Note that the "name" field is required. The other fields are all optional.

This will allow users to specify private Docker registries and the respective credentials. Under the hood, we parse these credentials and create a corresponding buildkit TOML file, as well as set up Docker bindings. This enables users to pull and push images from private Docker registries.

Richard Li added 5 commits June 21, 2023 22:55
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
@RichhLi RichhLi force-pushed the support_private_registries branch from f7b7425 to b173e8d Compare June 22, 2023 05:55
.DS_Store Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete this file.

for _, reg := range strings.Split(clicontext.String("registry"), ";") {
// Split into parts
parts := strings.Split(reg, ",")
if len(parts) != 4 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it mandatory to set CA for the private registry?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. From our findings, ca is required. Interestingly, cert and key weren't necessary for us, but we wanted to keep the existing structure as generic as possible. We assume the original writers added the cert and key values for a reason.

@@ -315,3 +331,29 @@ func buildkit(clicontext *cli.Context) error {

return nil
}

func updateConfigWithRegistry(clicontext *cli.Context, config *buildkitutil.BuildkitConfig) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a method of the buildkitutil.BuildkitConfig?

Comment on lines 344 to 348
config.Registry = append(config.Registry, strings.Split(parts[0], "=")[1])
caPath := strings.Split(parts[1], "=")[1]
config.Ca = append(config.Ca, caPath)
config.Key = append(config.Key, strings.Split(parts[2], "=")[1])
config.Cert = append(config.Cert, strings.Split(parts[3], "=")[1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has an assumption: the order has to be register,ca,key,cert but this has not been validated.

BTW, it seems these 3 files have to be in the same directory as the CA path. Otherwise the bind won't work.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah so i think passing a json string might be better

Copy link

@ByronHsu ByronHsu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General suggestions:

  1. I think naming as registry-credential makes more sense then registry because the main purpose is to pass credential to enable auth with the registry.
  2. I feel the string parsing logic is a bit overwhelming now. Users have to write bunch of stuff with ; and ,, which is kinda error-prone. I feel passing the whole thing as a json string would be easier to handle and validate. For example:
envd bootstrap --registry-credential='{
  "Registry": ["reg1", "reg2"],
  "Ca": ["ca1, "ca2"],
   ...etc
}'

or

envd bootstrap --registry-credentials='{
  "Credentials": [
     {
       "register": "reg1",
       "ca": "ca1",
     }
     {
       "register": "reg2",
       "ca": "ca2",
     }
  ]
}'

and you can use golang json to unmarshal the string like

type MyData struct {
	List []string `json:"list"`
}

func main() {
	app := &cli.App{
		Name:  "myapp",
		Usage: "An example CLI",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:  "json",
				Usage: "JSON string argument",
			},
		},
		Action: func(c *cli.Context) error {
			jsonString := c.String("json")
			if jsonString != "" {
				var data MyData
				err := json.Unmarshal([]byte(jsonString), &data)
				if err != nil {
					return err
				}
				// Process the JSON data here
				fmt.Printf("List items: %+v\n", data.List)
			}
			return nil
		},
	}

	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
}
  1. Should use-http and dockerhub-mirror also be included in registry-credential? i see they are both for per registry

}

// Generate file path
path, err := fileutil.ConfigFile(fmt.Sprintf("registry_%s.pem", kv[0]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious why do we have to read and write the pem files? can't we just use the original one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copied from the original implementation. The main purpose is to make sure these 3 files are in the same directory since we will mount this directory to the buildkitd container.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we basically copy 3 pems to the new dir? If that's the case, do we still need binding to allow the permission to /etc or other folders? I think buildkit will have permission to the new dir?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean the original impl? and Why not just use io.Copy instead of read and write?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use io.Copy instead of read and write?

That should be better.

So we basically copy 3 pems to the new dir? If that's the case, do we still need binding to allow the permission to /etc or other folders? I think buildkit will have permission to the new dir?

Need to mount a directory to the buildkitd container. This directory is used to store the envd config, which should be safe.

This PR supports multiple registries which means it needs different CAs for different registries. It might need to differentiate the file names.

// Split into parts
parts := strings.Split(reg, ",")
if len(parts) != 4 {
return fmt.Errorf("invalid registry string: %s", reg)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use errors.Newf everywhere for consistency?

@@ -315,3 +331,29 @@ func buildkit(clicontext *cli.Context) error {

return nil
}

func updateConfigWithRegistry(clicontext *cli.Context, config *buildkitutil.BuildkitConfig) error {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better to add a test to this func

{{- range $index, $value := .Registry }}
[registry."{{ if $value }}{{ $value }}{{ else }}docker.io{{ end }}"]
{{- if $.UseHTTP }}
http = true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if they want to specify different http/mirrors for different registries?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should make the mirror a string slice.

Copy link
Contributor Author

@RichhLi RichhLi Jun 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kemingy advised me to leave the dockerhub-mirror command as is, since Docker mirrors are unrelated to private registries. I can change it, but then the command will be removed (the mirror config will be in the JSON file instead).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @kemingy WDYT

hostConfig := &container.HostConfig{
Privileged: true,
AutoRemove: true,
}

if bc.SetCA {
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will removing this cause any side effect?

Richard Li added 5 commits June 23, 2023 16:42
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
Signed-off-by: Richard Li <ricli@linkedin.com>
Usage: "Specify the registry to pull the image from",
Aliases: []string{"r"},
Name: "registry-config",
Usage: "Path to a JSON file containing registry configuration",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to pass a JSON file, why not use the buildkit TOML file directly?

I think the main purpose of the CLI is to make it user-friendly. If the situation is complex, it's almost the same as setting up a buildkitd instance manually.

This decision will heavily affect the implementation. What do you think?

cc @gaocegege @RichhLi @ByronHsu

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about providing the buildkit TOML directly to envd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point. The problem with directly providing a TOML file is that the Docker mount won't be set up. I think this pr may not even be necessary because we might as well just set up buildkitd manually like you said. We will discuss this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kemingy @gaocegege Would it be possible to keep the current envd implementation, and the only change we'll make is adding a command that accepts JSON input? And we'll add a check where users can only use one of the commands, not both at the same time. So it will be completely backward compatible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kemingy @gaocegege Would it be possible to keep the current envd implementation, and the only change we'll make is adding a command that accepts JSON input? And we'll add a check where users can only use one of the commands, not both at the same time. So it will be completely backward compatible.

I'm okay with it.

Copy link
Member

@gaocegege gaocegege Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Will you implement it in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, or do you want it in different pr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I am just asking. Keeping in this PR LGTM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @RichhLi Do you still need this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I merged this one: #1680

@kemingy
Copy link
Member

kemingy commented Sep 3, 2023

close due to #1680

@kemingy kemingy closed this Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants