github.com/amtisyAts/helm@v2.17.0+incompatible/cmd/helm/installer/options.go (about)

     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 installer // import "k8s.io/helm/cmd/helm/installer"
    18  
    19  import (
    20  	"fmt"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/helm/pkg/strvals"
    24  	"k8s.io/helm/pkg/version"
    25  )
    26  
    27  const (
    28  	defaultImage = "ghcr.io/helm/tiller"
    29  
    30  	fmtJSON OutputFormat = "json"
    31  	fmtYAML OutputFormat = "yaml"
    32  )
    33  
    34  // Options control how to install Tiller into a cluster, upgrade, and uninstall Tiller from a cluster.
    35  type Options struct {
    36  	// EnableTLS instructs Tiller to serve with TLS enabled.
    37  	//
    38  	// Implied by VerifyTLS. If set the TLSKey and TLSCert are required.
    39  	EnableTLS bool
    40  
    41  	// VerifyTLS instructs Tiller to serve with TLS enabled verify remote certificates.
    42  	//
    43  	// If set TLSKey, TLSCert, TLSCaCert are required.
    44  	VerifyTLS bool
    45  
    46  	// UseCanary indicates that Tiller should deploy using the latest Tiller image.
    47  	UseCanary bool
    48  
    49  	// Namespace is the Kubernetes namespace to use to deploy Tiller.
    50  	Namespace string
    51  
    52  	// ServiceAccount is the Kubernetes service account to add to Tiller.
    53  	ServiceAccount string
    54  
    55  	// AutoMountServiceAccountToken determines whether or not the service account should be added to Tiller.
    56  	AutoMountServiceAccountToken bool
    57  
    58  	// ForceUpgrade allows to force upgrading tiller if deployed version is greater than current version
    59  	ForceUpgrade bool
    60  
    61  	// ImageSpec identifies the image Tiller will use when deployed.
    62  	//
    63  	// Valid if and only if UseCanary is false.
    64  	ImageSpec string
    65  
    66  	// TLSKeyFile identifies the file containing the pem encoded TLS private
    67  	// key Tiller should use.
    68  	//
    69  	// Required and valid if and only if EnableTLS or VerifyTLS is set.
    70  	TLSKeyFile string
    71  
    72  	// TLSCertFile identifies the file containing the pem encoded TLS
    73  	// certificate Tiller should use.
    74  	//
    75  	// Required and valid if and only if EnableTLS or VerifyTLS is set.
    76  	TLSCertFile string
    77  
    78  	// TLSCaCertFile identifies the file containing the pem encoded TLS CA
    79  	// certificate Tiller should use to verify remotes certificates.
    80  	//
    81  	// Required and valid if and only if VerifyTLS is set.
    82  	TLSCaCertFile string
    83  
    84  	// EnableHostNetwork installs Tiller with net=host.
    85  	EnableHostNetwork bool
    86  
    87  	// MaxHistory sets the maximum number of release versions stored per release.
    88  	//
    89  	// Less than or equal to zero means no limit.
    90  	MaxHistory int
    91  
    92  	// Replicas sets the amount of Tiller replicas to start
    93  	//
    94  	// Less than or equals to 1 means 1.
    95  	Replicas int
    96  
    97  	// NodeSelectors determine which nodes Tiller can land on.
    98  	NodeSelectors string
    99  
   100  	// Output dumps the Tiller manifest in the specified format (e.g. JSON) but skips Helm/Tiller installation.
   101  	Output OutputFormat
   102  
   103  	// Set merges additional values into the Tiller Deployment manifest.
   104  	Values []string
   105  }
   106  
   107  // SelectImage returns the image according to whether UseCanary is true or not
   108  func (opts *Options) SelectImage() string {
   109  	switch {
   110  	case opts.UseCanary:
   111  		return defaultImage + ":canary"
   112  	case opts.ImageSpec == "":
   113  		if version.BuildMetadata == "unreleased" {
   114  			return defaultImage + ":canary"
   115  		}
   116  		return fmt.Sprintf("%s:%s", defaultImage, version.Version)
   117  	default:
   118  		return opts.ImageSpec
   119  	}
   120  }
   121  
   122  func (opts *Options) pullPolicy() v1.PullPolicy {
   123  	if opts.UseCanary {
   124  		return v1.PullAlways
   125  	}
   126  	return v1.PullIfNotPresent
   127  }
   128  
   129  func (opts *Options) getReplicas() *int32 {
   130  	replicas := int32(1)
   131  	if opts.Replicas > 1 {
   132  		replicas = int32(opts.Replicas)
   133  	}
   134  	return &replicas
   135  }
   136  
   137  func (opts *Options) tls() bool { return opts.EnableTLS || opts.VerifyTLS }
   138  
   139  // valuesMap returns user set values in map format
   140  func (opts *Options) valuesMap(m map[string]interface{}) (map[string]interface{}, error) {
   141  	for _, skv := range opts.Values {
   142  		if err := strvals.ParseInto(skv, m); err != nil {
   143  			return nil, err
   144  		}
   145  	}
   146  	return m, nil
   147  }
   148  
   149  // OutputFormat defines valid values for init output (json, yaml)
   150  type OutputFormat string
   151  
   152  // String returns the string value of the OutputFormat
   153  func (f *OutputFormat) String() string {
   154  	return string(*f)
   155  }
   156  
   157  // Type returns the string value of the OutputFormat
   158  func (f *OutputFormat) Type() string {
   159  	return "OutputFormat"
   160  }
   161  
   162  // Set validates and sets the value of the OutputFormat
   163  func (f *OutputFormat) Set(s string) error {
   164  	for _, of := range []OutputFormat{fmtJSON, fmtYAML} {
   165  		if s == string(of) {
   166  			*f = of
   167  			return nil
   168  		}
   169  	}
   170  	return fmt.Errorf("unknown output format %q", s)
   171  }