github.com/koderover/helm@v2.17.0+incompatible/pkg/helm/option.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 helm
    18  
    19  import (
    20  	"crypto/tls"
    21  	"time"
    22  
    23  	"github.com/golang/protobuf/proto"
    24  	"golang.org/x/net/context"
    25  	"google.golang.org/grpc/metadata"
    26  
    27  	cpb "k8s.io/helm/pkg/proto/hapi/chart"
    28  	"k8s.io/helm/pkg/proto/hapi/release"
    29  	rls "k8s.io/helm/pkg/proto/hapi/services"
    30  	"k8s.io/helm/pkg/version"
    31  )
    32  
    33  // Option allows specifying various settings configurable by
    34  // the helm client user for overriding the defaults used when
    35  // issuing rpc's to the Tiller release server.
    36  type Option func(*options)
    37  
    38  // options specify optional settings used by the helm client.
    39  type options struct {
    40  	// value of helm home override
    41  	host string
    42  	// if set dry-run helm client calls
    43  	dryRun bool
    44  	// if set enable TLS on helm client calls
    45  	useTLS bool
    46  	// if set, re-use an existing name
    47  	reuseName bool
    48  	// if set, performs pod restart during upgrade/rollback
    49  	recreate bool
    50  	// if set, force resource update through delete/recreate if needed
    51  	force bool
    52  	// if set, skip running hooks
    53  	disableHooks bool
    54  	// if set, skip CRD hook only
    55  	disableCRDHook bool
    56  	// name of release
    57  	releaseName string
    58  	// tls.Config to use for rpc if tls enabled
    59  	tlsConfig *tls.Config
    60  	// release list options are applied directly to the list releases request
    61  	listReq rls.ListReleasesRequest
    62  	// release install options are applied directly to the install release request
    63  	instReq rls.InstallReleaseRequest
    64  	// release update options are applied directly to the update release request
    65  	updateReq rls.UpdateReleaseRequest
    66  	// release uninstall options are applied directly to the uninstall release request
    67  	uninstallReq rls.UninstallReleaseRequest
    68  	// release get status options are applied directly to the get release status request
    69  	statusReq rls.GetReleaseStatusRequest
    70  	// release get content options are applied directly to the get release content request
    71  	contentReq rls.GetReleaseContentRequest
    72  	// release rollback options are applied directly to the rollback release request
    73  	rollbackReq rls.RollbackReleaseRequest
    74  	// before intercepts client calls before sending
    75  	before func(context.Context, proto.Message) error
    76  	// release history options are applied directly to the get release history request
    77  	histReq rls.GetHistoryRequest
    78  	// resetValues instructs Tiller to reset values to their defaults.
    79  	resetValues bool
    80  	// reuseValues instructs Tiller to reuse the values from the last release.
    81  	reuseValues bool
    82  	// release test options are applied directly to the test release history request
    83  	testReq rls.TestReleaseRequest
    84  	// connectTimeout specifies the time duration Helm will wait to establish a connection to tiller
    85  	connectTimeout time.Duration
    86  }
    87  
    88  // Host specifies the host address of the Tiller release server, (default = ":44134").
    89  func Host(host string) Option {
    90  	return func(opts *options) {
    91  		opts.host = host
    92  	}
    93  }
    94  
    95  // WithTLS specifies the tls configuration if the helm client is enabled to use TLS.
    96  func WithTLS(cfg *tls.Config) Option {
    97  	return func(opts *options) {
    98  		opts.useTLS = true
    99  		opts.tlsConfig = cfg
   100  	}
   101  }
   102  
   103  // BeforeCall returns an option that allows intercepting a helm client rpc
   104  // before being sent OTA to tiller. The intercepting function should return
   105  // an error to indicate that the call should not proceed or nil otherwise.
   106  func BeforeCall(fn func(context.Context, proto.Message) error) Option {
   107  	return func(opts *options) {
   108  		opts.before = fn
   109  	}
   110  }
   111  
   112  // ReleaseListOption allows specifying various settings
   113  // configurable by the helm client user for overriding
   114  // the defaults used when running the `helm list` command.
   115  type ReleaseListOption func(*options)
   116  
   117  // ReleaseListOffset specifies the offset into a list of releases.
   118  func ReleaseListOffset(offset string) ReleaseListOption {
   119  	return func(opts *options) {
   120  		opts.listReq.Offset = offset
   121  	}
   122  }
   123  
   124  // ReleaseListFilter specifies a filter to apply a list of releases.
   125  func ReleaseListFilter(filter string) ReleaseListOption {
   126  	return func(opts *options) {
   127  		opts.listReq.Filter = filter
   128  	}
   129  }
   130  
   131  // ReleaseListLimit set an upper bound on the number of releases returned.
   132  func ReleaseListLimit(limit int) ReleaseListOption {
   133  	return func(opts *options) {
   134  		opts.listReq.Limit = int64(limit)
   135  	}
   136  }
   137  
   138  // ReleaseListOrder specifies how to order a list of releases.
   139  func ReleaseListOrder(order int32) ReleaseListOption {
   140  	return func(opts *options) {
   141  		opts.listReq.SortOrder = rls.ListSort_SortOrder(order)
   142  	}
   143  }
   144  
   145  // ReleaseListSort specifies how to sort a release list.
   146  func ReleaseListSort(sort int32) ReleaseListOption {
   147  	return func(opts *options) {
   148  		opts.listReq.SortBy = rls.ListSort_SortBy(sort)
   149  	}
   150  }
   151  
   152  // ReleaseListStatuses specifies which status codes should be returned.
   153  func ReleaseListStatuses(statuses []release.Status_Code) ReleaseListOption {
   154  	return func(opts *options) {
   155  		if len(statuses) == 0 {
   156  			statuses = []release.Status_Code{release.Status_DEPLOYED}
   157  		}
   158  		opts.listReq.StatusCodes = statuses
   159  	}
   160  }
   161  
   162  // ReleaseListNamespace specifies the namespace to list releases from
   163  func ReleaseListNamespace(namespace string) ReleaseListOption {
   164  	return func(opts *options) {
   165  		opts.listReq.Namespace = namespace
   166  	}
   167  }
   168  
   169  // InstallOption allows specifying various settings
   170  // configurable by the helm client user for overriding
   171  // the defaults used when running the `helm install` command.
   172  type InstallOption func(*options)
   173  
   174  // ValueOverrides specifies a list of values to include when installing.
   175  func ValueOverrides(raw []byte) InstallOption {
   176  	return func(opts *options) {
   177  		opts.instReq.Values = &cpb.Config{Raw: string(raw)}
   178  	}
   179  }
   180  
   181  // ReleaseName specifies the name of the release when installing.
   182  func ReleaseName(name string) InstallOption {
   183  	return func(opts *options) {
   184  		opts.instReq.Name = name
   185  	}
   186  }
   187  
   188  // ConnectTimeout specifies the duration (in seconds) Helm will wait to establish a connection to tiller
   189  func ConnectTimeout(timeout int64) Option {
   190  	return func(opts *options) {
   191  		opts.connectTimeout = time.Duration(timeout) * time.Second
   192  	}
   193  }
   194  
   195  // InstallTimeout specifies the number of seconds before kubernetes calls timeout
   196  func InstallTimeout(timeout int64) InstallOption {
   197  	return func(opts *options) {
   198  		opts.instReq.Timeout = timeout
   199  	}
   200  }
   201  
   202  // UpgradeTimeout specifies the number of seconds before kubernetes calls timeout
   203  func UpgradeTimeout(timeout int64) UpdateOption {
   204  	return func(opts *options) {
   205  		opts.updateReq.Timeout = timeout
   206  	}
   207  }
   208  
   209  // DeleteTimeout specifies the number of seconds before kubernetes calls timeout
   210  func DeleteTimeout(timeout int64) DeleteOption {
   211  	return func(opts *options) {
   212  		opts.uninstallReq.Timeout = timeout
   213  	}
   214  }
   215  
   216  // ReleaseTestTimeout specifies the number of seconds before kubernetes calls timeout
   217  func ReleaseTestTimeout(timeout int64) ReleaseTestOption {
   218  	return func(opts *options) {
   219  		opts.testReq.Timeout = timeout
   220  	}
   221  }
   222  
   223  // ReleaseTestCleanup is a boolean value representing whether to cleanup test pods
   224  func ReleaseTestCleanup(cleanup bool) ReleaseTestOption {
   225  	return func(opts *options) {
   226  		opts.testReq.Cleanup = cleanup
   227  	}
   228  }
   229  
   230  // ReleaseTestParallel is a boolean value representing whether to run test pods in parallel
   231  func ReleaseTestParallel(parallel bool) ReleaseTestOption {
   232  	return func(opts *options) {
   233  		opts.testReq.Parallel = parallel
   234  	}
   235  }
   236  
   237  // ReleaseTestMaxParallel specifies the maximum number of test pods to run in parallel
   238  func ReleaseTestMaxParallel(max uint32) ReleaseTestOption {
   239  	return func(opts *options) {
   240  		opts.testReq.MaxParallel = max
   241  	}
   242  }
   243  
   244  // ReleaseTestLogs is a boolean value representing whether to dump the logs from test pods
   245  func ReleaseTestLogs(logs bool) ReleaseTestOption {
   246  	return func(opts *options) {
   247  		opts.testReq.Logs = logs
   248  	}
   249  }
   250  
   251  // RollbackTimeout specifies the number of seconds before kubernetes calls timeout
   252  func RollbackTimeout(timeout int64) RollbackOption {
   253  	return func(opts *options) {
   254  		opts.rollbackReq.Timeout = timeout
   255  	}
   256  }
   257  
   258  // InstallWait specifies whether or not to wait for all resources to be ready
   259  func InstallWait(wait bool) InstallOption {
   260  	return func(opts *options) {
   261  		opts.instReq.Wait = wait
   262  	}
   263  }
   264  
   265  // UpgradeWait specifies whether or not to wait for all resources to be ready
   266  func UpgradeWait(wait bool) UpdateOption {
   267  	return func(opts *options) {
   268  		opts.updateReq.Wait = wait
   269  	}
   270  }
   271  
   272  // RollbackWait specifies whether or not to wait for all resources to be ready
   273  func RollbackWait(wait bool) RollbackOption {
   274  	return func(opts *options) {
   275  		opts.rollbackReq.Wait = wait
   276  	}
   277  }
   278  
   279  // UpdateValueOverrides specifies a list of values to include when upgrading
   280  func UpdateValueOverrides(raw []byte) UpdateOption {
   281  	return func(opts *options) {
   282  		opts.updateReq.Values = &cpb.Config{Raw: string(raw)}
   283  	}
   284  }
   285  
   286  // InstallDescription specifies the description for the release
   287  func InstallDescription(description string) InstallOption {
   288  	return func(opts *options) {
   289  		opts.instReq.Description = description
   290  	}
   291  }
   292  
   293  // UpgradeDescription specifies the description for the update
   294  func UpgradeDescription(description string) UpdateOption {
   295  	return func(opts *options) {
   296  		opts.updateReq.Description = description
   297  	}
   298  }
   299  
   300  // RollbackDescription specifies the description for the release
   301  func RollbackDescription(description string) RollbackOption {
   302  	return func(opts *options) {
   303  		opts.rollbackReq.Description = description
   304  	}
   305  }
   306  
   307  // DeleteDescription specifies the description for the release
   308  func DeleteDescription(description string) DeleteOption {
   309  	return func(opts *options) {
   310  		opts.uninstallReq.Description = description
   311  	}
   312  }
   313  
   314  // UpgradeCleanupOnFail allows deletion of new resources created in this upgrade when upgrade failed
   315  func UpgradeCleanupOnFail(cleanupOnFail bool) UpdateOption {
   316  	return func(opts *options) {
   317  		opts.updateReq.CleanupOnFail = cleanupOnFail
   318  	}
   319  }
   320  
   321  // RollbackCleanupOnFail allows deletion of new resources created in this rollback when rollback failed
   322  func RollbackCleanupOnFail(cleanupOnFail bool) RollbackOption {
   323  	return func(opts *options) {
   324  		opts.rollbackReq.CleanupOnFail = cleanupOnFail
   325  	}
   326  }
   327  
   328  // DeleteDisableHooks will disable hooks for a deletion operation.
   329  func DeleteDisableHooks(disable bool) DeleteOption {
   330  	return func(opts *options) {
   331  		opts.disableHooks = disable
   332  	}
   333  }
   334  
   335  // DeleteDryRun will (if true) execute a deletion as a dry run.
   336  func DeleteDryRun(dry bool) DeleteOption {
   337  	return func(opts *options) {
   338  		opts.dryRun = dry
   339  	}
   340  }
   341  
   342  // DeletePurge removes the release from the store and make its name free for later use.
   343  func DeletePurge(purge bool) DeleteOption {
   344  	return func(opts *options) {
   345  		opts.uninstallReq.Purge = purge
   346  	}
   347  }
   348  
   349  // InstallDryRun will (if true) execute an installation as a dry run.
   350  func InstallDryRun(dry bool) InstallOption {
   351  	return func(opts *options) {
   352  		opts.dryRun = dry
   353  	}
   354  }
   355  
   356  // InstallDisableHooks disables hooks during installation.
   357  func InstallDisableHooks(disable bool) InstallOption {
   358  	return func(opts *options) {
   359  		opts.disableHooks = disable
   360  	}
   361  }
   362  
   363  // InstallDisableCRDHook disables CRD hook during installation.
   364  func InstallDisableCRDHook(disable bool) InstallOption {
   365  	return func(opts *options) {
   366  		opts.disableCRDHook = disable
   367  	}
   368  }
   369  
   370  // InstallReuseName will (if true) instruct Tiller to re-use an existing name.
   371  func InstallReuseName(reuse bool) InstallOption {
   372  	return func(opts *options) {
   373  		opts.reuseName = reuse
   374  	}
   375  }
   376  
   377  // InstallSubNotes will (if true) instruct Tiller to render SubChart Notes
   378  func InstallSubNotes(enable bool) InstallOption {
   379  	return func(opts *options) {
   380  		opts.instReq.SubNotes = enable
   381  	}
   382  }
   383  
   384  // UpgradeSubNotes will (if true) instruct Tiller to render SubChart Notes
   385  func UpgradeSubNotes(enable bool) UpdateOption {
   386  	return func(opts *options) {
   387  		opts.updateReq.SubNotes = enable
   388  	}
   389  }
   390  
   391  // RollbackDisableHooks will disable hooks for a rollback operation
   392  func RollbackDisableHooks(disable bool) RollbackOption {
   393  	return func(opts *options) {
   394  		opts.disableHooks = disable
   395  	}
   396  }
   397  
   398  // RollbackDryRun will (if true) execute a rollback as a dry run.
   399  func RollbackDryRun(dry bool) RollbackOption {
   400  	return func(opts *options) {
   401  		opts.dryRun = dry
   402  	}
   403  }
   404  
   405  // RollbackRecreate will (if true) recreate pods after rollback.
   406  func RollbackRecreate(recreate bool) RollbackOption {
   407  	return func(opts *options) {
   408  		opts.recreate = recreate
   409  	}
   410  }
   411  
   412  // RollbackForce will (if true) force resource update through delete/recreate if needed
   413  func RollbackForce(force bool) RollbackOption {
   414  	return func(opts *options) {
   415  		opts.force = force
   416  	}
   417  }
   418  
   419  // RollbackVersion sets the version of the release to deploy.
   420  func RollbackVersion(ver int32) RollbackOption {
   421  	return func(opts *options) {
   422  		opts.rollbackReq.Version = ver
   423  	}
   424  }
   425  
   426  // UpgradeDisableHooks will disable hooks for an upgrade operation.
   427  func UpgradeDisableHooks(disable bool) UpdateOption {
   428  	return func(opts *options) {
   429  		opts.disableHooks = disable
   430  	}
   431  }
   432  
   433  // UpgradeDryRun will (if true) execute an upgrade as a dry run.
   434  func UpgradeDryRun(dry bool) UpdateOption {
   435  	return func(opts *options) {
   436  		opts.dryRun = dry
   437  	}
   438  }
   439  
   440  // ResetValues will (if true) trigger resetting the values to their original state.
   441  func ResetValues(reset bool) UpdateOption {
   442  	return func(opts *options) {
   443  		opts.resetValues = reset
   444  	}
   445  }
   446  
   447  // ReuseValues will cause Tiller to reuse the values from the last release.
   448  // This is ignored if ResetValues is true.
   449  func ReuseValues(reuse bool) UpdateOption {
   450  	return func(opts *options) {
   451  		opts.reuseValues = reuse
   452  	}
   453  }
   454  
   455  // UpgradeRecreate will (if true) recreate pods after upgrade.
   456  func UpgradeRecreate(recreate bool) UpdateOption {
   457  	return func(opts *options) {
   458  		opts.recreate = recreate
   459  	}
   460  }
   461  
   462  // UpgradeForce will (if true) force resource update through delete/recreate if needed
   463  func UpgradeForce(force bool) UpdateOption {
   464  	return func(opts *options) {
   465  		opts.force = force
   466  	}
   467  }
   468  
   469  // ContentOption allows setting optional attributes when
   470  // performing a GetReleaseContent tiller rpc.
   471  type ContentOption func(*options)
   472  
   473  // ContentReleaseVersion will instruct Tiller to retrieve the content
   474  // of a particular version of a release.
   475  func ContentReleaseVersion(version int32) ContentOption {
   476  	return func(opts *options) {
   477  		opts.contentReq.Version = version
   478  	}
   479  }
   480  
   481  // StatusOption allows setting optional attributes when
   482  // performing a GetReleaseStatus tiller rpc.
   483  type StatusOption func(*options)
   484  
   485  // StatusReleaseVersion will instruct Tiller to retrieve the status
   486  // of a particular version of a release.
   487  func StatusReleaseVersion(version int32) StatusOption {
   488  	return func(opts *options) {
   489  		opts.statusReq.Version = version
   490  	}
   491  }
   492  
   493  // DeleteOption allows setting optional attributes when
   494  // performing a UninstallRelease tiller rpc.
   495  type DeleteOption func(*options)
   496  
   497  // VersionOption -- TODO
   498  type VersionOption func(*options)
   499  
   500  // UpdateOption allows specifying various settings
   501  // configurable by the helm client user for overriding
   502  // the defaults used when running the `helm upgrade` command.
   503  type UpdateOption func(*options)
   504  
   505  // RollbackOption allows specifying various settings configurable
   506  // by the helm client user for overriding the defaults used when
   507  // running the `helm rollback` command.
   508  type RollbackOption func(*options)
   509  
   510  // HistoryOption allows configuring optional request data for
   511  // issuing a GetHistory rpc.
   512  type HistoryOption func(*options)
   513  
   514  // WithMaxHistory sets the max number of releases to return
   515  // in a release history query.
   516  func WithMaxHistory(max int32) HistoryOption {
   517  	return func(opts *options) {
   518  		opts.histReq.Max = max
   519  	}
   520  }
   521  
   522  // NewContext creates a versioned context.
   523  func NewContext() context.Context {
   524  	return FromContext(context.TODO())
   525  }
   526  
   527  // FromContext returns a versioned context from a parent context
   528  func FromContext(ctx context.Context) context.Context {
   529  	md := metadata.Pairs("x-helm-api-client", version.GetVersion())
   530  	return metadata.NewOutgoingContext(ctx, md)
   531  }
   532  
   533  // ReleaseTestOption allows configuring optional request data for
   534  // issuing a TestRelease rpc.
   535  type ReleaseTestOption func(*options)