From 516cdeec7d37f8ac4697411beb8f04ec3968184a Mon Sep 17 00:00:00 2001 From: Halvard Skogsrud Date: Sat, 1 May 2021 03:53:00 +1000 Subject: [PATCH] Add flag and PublishOption for destination repo (#351) * Add flag and PublishOption for destination repo This enables programmatically setting the destination image repository when embedding ko's `publish` functionality in other tools. See https://github.com/google/ko/pull/348 * Set DockerRepo PublishOption from KO_DOCKER_REPO This enables programmatically setting the destination image repository and avoids exposing a flag. * Update comment on DockerRepo option * Fix readme and copyright headers --- pkg/commands/options/publish.go | 39 ++++++++++++++++++++++----------- pkg/commands/resolver.go | 30 +++++++++++++------------ 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/pkg/commands/options/publish.go b/pkg/commands/options/publish.go index 61ca0880c5..1888e7e63a 100644 --- a/pkg/commands/options/publish.go +++ b/pkg/commands/options/publish.go @@ -1,22 +1,25 @@ -// Copyright 2018 Google LLC All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* +Copyright 2021 Google LLC All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package options import ( "crypto/md5" //nolint: gosec // No strong cryptography needed. "encoding/hex" + "os" "path" "github.com/google/ko/pkg/publish" @@ -25,6 +28,10 @@ import ( // PublishOptions encapsulates options when publishing. type PublishOptions struct { + // DockerRepo configures the destination image repository. + // In normal ko usage, this is populated with the value of $KO_DOCKER_REPO. + DockerRepo string + Tags []string // Push publishes images to a registry. @@ -46,6 +53,12 @@ type PublishOptions struct { } func AddPublishArg(cmd *cobra.Command, po *PublishOptions) { + // Set DockerRepo from the KO_DOCKER_REPO envionment variable. + // See https://github.com/google/ko/pull/351 for flag discussion. + if dockerRepo, exists := os.LookupEnv("KO_DOCKER_REPO"); exists { + po.DockerRepo = dockerRepo + } + cmd.Flags().StringSliceVarP(&po.Tags, "tags", "t", []string{"latest"}, "Which tags to use for the produced image instead of the default 'latest' tag "+ "(may not work properly with --base-import-paths or --bare).") diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index 7e9acc1f8e..da30485a8f 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -1,16 +1,18 @@ -// Copyright 2018 Google LLC All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* +Copyright 2021 Google LLC All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package commands @@ -131,7 +133,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) { // Create the publish.Interface that we will use to publish image references // to either a docker daemon or a container image registry. innerPublisher, err := func() (publish.Interface, error) { - repoName := os.Getenv("KO_DOCKER_REPO") + repoName := po.DockerRepo namer := options.MakeNamer(po) if repoName == publish.LocalDomain || po.Local { // TODO(jonjohnsonjr): I'm assuming that nobody will