...

Source file src/helm.sh/helm/v3/pkg/action/push.go

Documentation: helm.sh/helm/v3/pkg/action

     1  /*
     2  Copyright The Helm 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 action
    18  
    19  import (
    20  	"io"
    21  	"strings"
    22  
    23  	"helm.sh/helm/v3/pkg/cli"
    24  	"helm.sh/helm/v3/pkg/pusher"
    25  	"helm.sh/helm/v3/pkg/registry"
    26  	"helm.sh/helm/v3/pkg/uploader"
    27  )
    28  
    29  // Push is the action for uploading a chart.
    30  //
    31  // It provides the implementation of 'helm push'.
    32  type Push struct {
    33  	Settings              *cli.EnvSettings
    34  	cfg                   *Configuration
    35  	certFile              string
    36  	keyFile               string
    37  	caFile                string
    38  	insecureSkipTLSverify bool
    39  	plainHTTP             bool
    40  	out                   io.Writer
    41  }
    42  
    43  // PushOpt is a type of function that sets options for a push action.
    44  type PushOpt func(*Push)
    45  
    46  // WithPushConfig sets the cfg field on the push configuration object.
    47  func WithPushConfig(cfg *Configuration) PushOpt {
    48  	return func(p *Push) {
    49  		p.cfg = cfg
    50  	}
    51  }
    52  
    53  // WithTLSClientConfig sets the certFile, keyFile, and caFile fields on the push configuration object.
    54  func WithTLSClientConfig(certFile, keyFile, caFile string) PushOpt {
    55  	return func(p *Push) {
    56  		p.certFile = certFile
    57  		p.keyFile = keyFile
    58  		p.caFile = caFile
    59  	}
    60  }
    61  
    62  // WithInsecureSkipTLSVerify determines if a TLS Certificate will be checked
    63  func WithInsecureSkipTLSVerify(insecureSkipTLSVerify bool) PushOpt {
    64  	return func(p *Push) {
    65  		p.insecureSkipTLSverify = insecureSkipTLSVerify
    66  	}
    67  }
    68  
    69  // WithPlainHTTP configures the use of plain HTTP connections.
    70  func WithPlainHTTP(plainHTTP bool) PushOpt {
    71  	return func(p *Push) {
    72  		p.plainHTTP = plainHTTP
    73  	}
    74  }
    75  
    76  // WithOptWriter sets the registryOut field on the push configuration object.
    77  func WithPushOptWriter(out io.Writer) PushOpt {
    78  	return func(p *Push) {
    79  		p.out = out
    80  	}
    81  }
    82  
    83  // NewPushWithOpts creates a new push, with configuration options.
    84  func NewPushWithOpts(opts ...PushOpt) *Push {
    85  	p := &Push{}
    86  	for _, fn := range opts {
    87  		fn(p)
    88  	}
    89  	return p
    90  }
    91  
    92  // Run executes 'helm push' against the given chart archive.
    93  func (p *Push) Run(chartRef string, remote string) (string, error) {
    94  	var out strings.Builder
    95  
    96  	c := uploader.ChartUploader{
    97  		Out:     &out,
    98  		Pushers: pusher.All(p.Settings),
    99  		Options: []pusher.Option{
   100  			pusher.WithTLSClientConfig(p.certFile, p.keyFile, p.caFile),
   101  			pusher.WithInsecureSkipTLSVerify(p.insecureSkipTLSverify),
   102  			pusher.WithPlainHTTP(p.plainHTTP),
   103  		},
   104  	}
   105  
   106  	if registry.IsOCI(remote) {
   107  		// Don't use the default registry client if tls options are set.
   108  		c.Options = append(c.Options, pusher.WithRegistryClient(p.cfg.RegistryClient))
   109  	}
   110  
   111  	return out.String(), c.UploadTo(chartRef, remote)
   112  }
   113  

View as plain text