github.com/oam-dev/kubevela@v1.9.11/pkg/utils/registries/options.go (about)

     1  /*
     2  Copyright 2023 The KubeVela 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 registries
    18  
    19  import (
    20  	"context"
    21  	"net/http"
    22  
    23  	"github.com/google/go-containerregistry/pkg/authn"
    24  	"github.com/google/go-containerregistry/pkg/name"
    25  	v1 "github.com/google/go-containerregistry/pkg/v1"
    26  	"github.com/google/go-containerregistry/pkg/v1/remote"
    27  )
    28  
    29  const (
    30  	// DefaultRegistry is the registry name that will be used if no registry
    31  	// provided and the default is not overridden.
    32  	DefaultRegistry      = "index.docker.io"
    33  	defaultRegistryAlias = "docker.io"
    34  
    35  	// DefaultTag is the tag name that will be used if no tag provided and the
    36  	// default is not overridden.
    37  	DefaultTag = "latest"
    38  )
    39  
    40  type options struct {
    41  	name     []name.Option
    42  	remote   []remote.Option
    43  	platform *v1.Platform
    44  }
    45  
    46  // Option is a functional option
    47  type Option func(*options)
    48  
    49  // WithTransport is a functional option for overriding the default transport
    50  // for remote operations.
    51  func WithTransport(t http.RoundTripper) Option {
    52  	return func(o *options) {
    53  		o.remote = append(o.remote, remote.WithTransport(t))
    54  	}
    55  }
    56  
    57  // Insecure is an Option that allows image references to be fetched without TLS.
    58  func Insecure(o *options) {
    59  	o.name = append(o.name, name.Insecure)
    60  }
    61  
    62  // WithAuth is a functional option for overriding the default authenticator
    63  // for remote operations.
    64  func WithAuth(auth authn.Authenticator) Option {
    65  	return func(o *options) {
    66  		// Replace the default keychain at position 0.
    67  		o.remote[0] = remote.WithAuth(auth)
    68  	}
    69  }
    70  
    71  // WithContext is a functional option for setting the context.
    72  func WithContext(ctx context.Context) Option {
    73  	return func(o *options) {
    74  		o.remote = append(o.remote, remote.WithContext(ctx))
    75  	}
    76  }
    77  
    78  // WithPlatform is an Option to specify the platform.
    79  func WithPlatform(platform *v1.Platform) Option {
    80  	return func(o *options) {
    81  		if platform != nil {
    82  			o.remote = append(o.remote, remote.WithPlatform(*platform))
    83  		}
    84  		o.platform = platform
    85  	}
    86  }