Skip to content

Commit

Permalink
Merge pull request #79 from lindsayismith/master
Browse files Browse the repository at this point in the history
Add --include-artifact-registry flag to configure-docker
  • Loading branch information
lindsayismith authored Jul 17, 2020
2 parents be7633a + 03956f8 commit 77fe8fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
41 changes: 33 additions & 8 deletions cli/configure-docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type dockerConfigCmd struct {
overwrite bool
// the registries to configure the cred helper for
registries string
// whether to include all AR Registries
includeArtifactRegistry bool
}

// see https://github.com/docker/docker/blob/master/cliconfig/credentials/native_store.go
Expand All @@ -51,13 +53,14 @@ func NewDockerConfigSubcommand() subcommands.Command {
},
false,
"unused",
false,
}
}

func (c *dockerConfigCmd) SetFlags(fs *flag.FlagSet) {
defaultReg := strings.Join(config.DefaultGCRRegistries[:], ", ")
fs.BoolVar(&c.overwrite, "overwrite", false, "overwrite any previously configured credential store and/or credentials")
fs.StringVar(&c.registries, "registries", defaultReg, "the comma-separated list of registries to configure the cred helper for")
fs.BoolVar(&c.includeArtifactRegistry, "include-artifact-registry", false, "include all Artifact Registry registries as well as GCR registries ")
fs.StringVar(&c.registries, "registries", "", "the comma-separated list of registries to configure the cred helper for")
}

func (c *dockerConfigCmd) Execute(context.Context, *flag.FlagSet, ...interface{}) subcommands.ExitStatus {
Expand Down Expand Up @@ -96,12 +99,30 @@ func (c *dockerConfigCmd) setConfig(dockerConfig *configfile.ConfigFile, helperS
dockerConfig.CredentialHelpers = map[string]string{}
}

strReader := strings.NewReader(c.registries)
registries, err := csv.NewReader(strReader).Read()
if err != nil {
printErrorln("Unable to parse `--registries` value %q: %v", c.registries, err)
return subcommands.ExitFailure
var registries []string
if c.registries == "" {
fmt.Println("Configuring default registries....")
fmt.Println("WARNING: A long list of credential helpers may cause delays running 'docker build'.")
fmt.Println("We recommend passing the registry names via the --registries flag for the specific registries you are using")
if c.includeArtifactRegistry {
fmt.Println("Adding config for all GCR and AR registries.")
registries = append(config.DefaultGCRRegistries[:], config.DefaultARRegistries[:]...)
} else {
fmt.Println("Adding config for all GCR registries.")
registries = config.DefaultGCRRegistries[:]
}
} else {
fmt.Println("Configuring supplied registries....")
strReader := strings.NewReader(c.registries)
var err error
registries, err = csv.NewReader(strReader).Read()
if err != nil {
printErrorln("Unable to parse `--registries` value %q: %v", c.registries, err)
return subcommands.ExitFailure
}
fmt.Printf("Adding config for registries: %s\n", strings.Join(registries, ","))
}

for _, registry := range registries {
dockerConfig.CredentialHelpers[strings.TrimSpace(registry)] = helperSuffix
}
Expand All @@ -111,7 +132,11 @@ func (c *dockerConfigCmd) setConfig(dockerConfig *configfile.ConfigFile, helperS
return subcommands.ExitFailure
}

fmt.Printf("%s configured to use this credential helper for GCR registries\n", dockerConfig.Filename)
if c.includeArtifactRegistry {
fmt.Printf("%s configured to use this credential helper for GCR and AR registries\n", dockerConfig.Filename)
} else {
fmt.Printf("%s configured to use this credential helper for GCR registries\n", dockerConfig.Filename)
}
return subcommands.ExitSuccess
}

Expand Down
17 changes: 17 additions & 0 deletions config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ var DefaultGCRRegistries = [...]string{
"marketplace.gcr.io",
}

// DefaultARRegistries contains the list of default registries for Artifact
// Registry. If the --include-artifact-registry flag is supplied then these
// are added in addition to the GCR Registries.
var DefaultARRegistries = [...]string{
"northamerica-northeast1-docker.pkg.dev", "us-central1-docker.pkg.dev",
"us-east1-docker.pkg.dev", "us-east4-docker.pkg.dev",
"us-west2-docker.pkg.dev", "us-west1-docker.pkg.dev",
"southamerica-east1-docker.pkg.dev", "europe-north1-docker.pkg.dev",
"europe-west1-docker.pkg.dev", "europe-west2-docker.pkg.dev",
"europe-west3-docker.pkg.dev", "europe-west4-docker.pkg.dev",
"europe-west6-docker.pkg.dev", "asia-east1-docker.pkg.dev",
"asia-east2-docker.pkg.dev", "asia-northeast1-docker.pkg.dev",
"asia-northeast2-docker.pkg.dev", "asia-south1-docker.pkg.dev",
"asia-southeast1-docker.pkg.dev", "australia-southeast1-docker.pkg.dev",
"asia-docker.pkg.dev", "europe-docker.pkg.dev", "us-docker.pkg.dev",
}

// SupportedGCRTokenSources maps config keys to plain english explanations for
// where the helper should search for a GCR access token.
var SupportedGCRTokenSources = map[string]string{
Expand Down

0 comments on commit 77fe8fa

Please sign in to comment.