github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/ko/publisher.go (about)

     1  /*
     2  Copyright 2021 The Skaffold Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ko
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/google/go-containerregistry/pkg/name"
    23  	"github.com/google/go-containerregistry/pkg/v1/daemon"
    24  	"github.com/google/ko/pkg/commands"
    25  	"github.com/google/ko/pkg/commands/options"
    26  	"github.com/google/ko/pkg/publish"
    27  
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
    29  )
    30  
    31  func (b *Builder) newKoPublisher(ref string) (publish.Interface, error) {
    32  	var dockerClient daemon.Client
    33  	if b.localDocker != nil {
    34  		dockerClient = b.localDocker.RawClient()
    35  	}
    36  	po, err := publishOptions(ref, b.pushImages, dockerClient, b.insecureRegistries)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return commands.NewPublisher(po)
    41  }
    42  
    43  func publishOptions(ref string, pushImages bool, dockerClient daemon.Client, insecureRegistries map[string]bool) (*options.PublishOptions, error) {
    44  	imageRef, err := name.ParseReference(ref)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	imageNameWithoutTag := imageRef.Context().Name()
    49  
    50  	return &options.PublishOptions{
    51  		Bare:             true,
    52  		DockerClient:     dockerClient,
    53  		DockerRepo:       imageNameWithoutTag,
    54  		InsecureRegistry: useInsecureRegistry(imageNameWithoutTag, insecureRegistries),
    55  		Local:            !pushImages,
    56  		LocalDomain:      imageNameWithoutTag,
    57  		Push:             pushImages,
    58  		Tags:             []string{imageRef.Identifier()},
    59  		UserAgent:        version.UserAgentWithClient(),
    60  	}, nil
    61  }
    62  
    63  func useInsecureRegistry(imageName string, insecureRegistries map[string]bool) bool {
    64  	for registry := range insecureRegistries {
    65  		if strings.HasPrefix(imageName, registry) {
    66  			return true
    67  		}
    68  	}
    69  	return false
    70  }