github.com/Beeketing/helm@v2.12.1+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  // RollbackTimeout specifies the number of seconds before kubernetes calls timeout
   231  func RollbackTimeout(timeout int64) RollbackOption {
   232  	return func(opts *options) {
   233  		opts.rollbackReq.Timeout = timeout
   234  	}
   235  }
   236  
   237  // InstallWait specifies whether or not to wait for all resources to be ready
   238  func InstallWait(wait bool) InstallOption {
   239  	return func(opts *options) {
   240  		opts.instReq.Wait = wait
   241  	}
   242  }
   243  
   244  // UpgradeWait specifies whether or not to wait for all resources to be ready
   245  func UpgradeWait(wait bool) UpdateOption {
   246  	return func(opts *options) {
   247  		opts.updateReq.Wait = wait
   248  	}
   249  }
   250  
   251  // RollbackWait specifies whether or not to wait for all resources to be ready
   252  func RollbackWait(wait bool) RollbackOption {
   253  	return func(opts *options) {
   254  		opts.rollbackReq.Wait = wait
   255  	}
   256  }
   257  
   258  // UpdateValueOverrides specifies a list of values to include when upgrading
   259  func UpdateValueOverrides(raw []byte) UpdateOption {
   260  	return func(opts *options) {
   261  		opts.updateReq.Values = &cpb.Config{Raw: string(raw)}
   262  	}
   263  }
   264  
   265  // InstallDescription specifies the description for the release
   266  func InstallDescription(description string) InstallOption {
   267  	return func(opts *options) {
   268  		opts.instReq.Description = description
   269  	}
   270  }
   271  
   272  // UpgradeDescription specifies the description for the update
   273  func UpgradeDescription(description string) UpdateOption {
   274  	return func(opts *options) {
   275  		opts.updateReq.Description = description
   276  	}
   277  }
   278  
   279  // RollbackDescription specifies the description for the release
   280  func RollbackDescription(description string) RollbackOption {
   281  	return func(opts *options) {
   282  		opts.rollbackReq.Description = description
   283  	}
   284  }
   285  
   286  // DeleteDescription specifies the description for the release
   287  func DeleteDescription(description string) DeleteOption {
   288  	return func(opts *options) {
   289  		opts.uninstallReq.Description = description
   290  	}
   291  }
   292  
   293  // DeleteDisableHooks will disable hooks for a deletion operation.
   294  func DeleteDisableHooks(disable bool) DeleteOption {
   295  	return func(opts *options) {
   296  		opts.disableHooks = disable
   297  	}
   298  }
   299  
   300  // DeleteDryRun will (if true) execute a deletion as a dry run.
   301  func DeleteDryRun(dry bool) DeleteOption {
   302  	return func(opts *options) {
   303  		opts.dryRun = dry
   304  	}
   305  }
   306  
   307  // DeletePurge removes the release from the store and make its name free for later use.
   308  func DeletePurge(purge bool) DeleteOption {
   309  	return func(opts *options) {
   310  		opts.uninstallReq.Purge = purge
   311  	}
   312  }
   313  
   314  // InstallDryRun will (if true) execute an installation as a dry run.
   315  func InstallDryRun(dry bool) InstallOption {
   316  	return func(opts *options) {
   317  		opts.dryRun = dry
   318  	}
   319  }
   320  
   321  // InstallDisableHooks disables hooks during installation.
   322  func InstallDisableHooks(disable bool) InstallOption {
   323  	return func(opts *options) {
   324  		opts.disableHooks = disable
   325  	}
   326  }
   327  
   328  // InstallDisableCRDHook disables CRD hook during installation.
   329  func InstallDisableCRDHook(disable bool) InstallOption {
   330  	return func(opts *options) {
   331  		opts.disableCRDHook = disable
   332  	}
   333  }
   334  
   335  // InstallReuseName will (if true) instruct Tiller to re-use an existing name.
   336  func InstallReuseName(reuse bool) InstallOption {
   337  	return func(opts *options) {
   338  		opts.reuseName = reuse
   339  	}
   340  }
   341  
   342  // RollbackDisableHooks will disable hooks for a rollback operation
   343  func RollbackDisableHooks(disable bool) RollbackOption {
   344  	return func(opts *options) {
   345  		opts.disableHooks = disable
   346  	}
   347  }
   348  
   349  // RollbackDryRun will (if true) execute a rollback as a dry run.
   350  func RollbackDryRun(dry bool) RollbackOption {
   351  	return func(opts *options) {
   352  		opts.dryRun = dry
   353  	}
   354  }
   355  
   356  // RollbackRecreate will (if true) recreate pods after rollback.
   357  func RollbackRecreate(recreate bool) RollbackOption {
   358  	return func(opts *options) {
   359  		opts.recreate = recreate
   360  	}
   361  }
   362  
   363  // RollbackForce will (if true) force resource update through delete/recreate if needed
   364  func RollbackForce(force bool) RollbackOption {
   365  	return func(opts *options) {
   366  		opts.force = force
   367  	}
   368  }
   369  
   370  // RollbackVersion sets the version of the release to deploy.
   371  func RollbackVersion(ver int32) RollbackOption {
   372  	return func(opts *options) {
   373  		opts.rollbackReq.Version = ver
   374  	}
   375  }
   376  
   377  // UpgradeDisableHooks will disable hooks for an upgrade operation.
   378  func UpgradeDisableHooks(disable bool) UpdateOption {
   379  	return func(opts *options) {
   380  		opts.disableHooks = disable
   381  	}
   382  }
   383  
   384  // UpgradeDryRun will (if true) execute an upgrade as a dry run.
   385  func UpgradeDryRun(dry bool) UpdateOption {
   386  	return func(opts *options) {
   387  		opts.dryRun = dry
   388  	}
   389  }
   390  
   391  // ResetValues will (if true) trigger resetting the values to their original state.
   392  func ResetValues(reset bool) UpdateOption {
   393  	return func(opts *options) {
   394  		opts.resetValues = reset
   395  	}
   396  }
   397  
   398  // ReuseValues will cause Tiller to reuse the values from the last release.
   399  // This is ignored if ResetValues is true.
   400  func ReuseValues(reuse bool) UpdateOption {
   401  	return func(opts *options) {
   402  		opts.reuseValues = reuse
   403  	}
   404  }
   405  
   406  // UpgradeRecreate will (if true) recreate pods after upgrade.
   407  func UpgradeRecreate(recreate bool) UpdateOption {
   408  	return func(opts *options) {
   409  		opts.recreate = recreate
   410  	}
   411  }
   412  
   413  // UpgradeForce will (if true) force resource update through delete/recreate if needed
   414  func UpgradeForce(force bool) UpdateOption {
   415  	return func(opts *options) {
   416  		opts.force = force
   417  	}
   418  }
   419  
   420  // ContentOption allows setting optional attributes when
   421  // performing a GetReleaseContent tiller rpc.
   422  type ContentOption func(*options)
   423  
   424  // ContentReleaseVersion will instruct Tiller to retrieve the content
   425  // of a particular version of a release.
   426  func ContentReleaseVersion(version int32) ContentOption {
   427  	return func(opts *options) {
   428  		opts.contentReq.Version = version
   429  	}
   430  }
   431  
   432  // StatusOption allows setting optional attributes when
   433  // performing a GetReleaseStatus tiller rpc.
   434  type StatusOption func(*options)
   435  
   436  // StatusReleaseVersion will instruct Tiller to retrieve the status
   437  // of a particular version of a release.
   438  func StatusReleaseVersion(version int32) StatusOption {
   439  	return func(opts *options) {
   440  		opts.statusReq.Version = version
   441  	}
   442  }
   443  
   444  // DeleteOption allows setting optional attributes when
   445  // performing a UninstallRelease tiller rpc.
   446  type DeleteOption func(*options)
   447  
   448  // VersionOption -- TODO
   449  type VersionOption func(*options)
   450  
   451  // UpdateOption allows specifying various settings
   452  // configurable by the helm client user for overriding
   453  // the defaults used when running the `helm upgrade` command.
   454  type UpdateOption func(*options)
   455  
   456  // RollbackOption allows specififying various settings configurable
   457  // by the helm client user for overriding the defaults used when
   458  // running the `helm rollback` command.
   459  type RollbackOption func(*options)
   460  
   461  // HistoryOption allows configuring optional request data for
   462  // issuing a GetHistory rpc.
   463  type HistoryOption func(*options)
   464  
   465  // WithMaxHistory sets the max number of releases to return
   466  // in a release history query.
   467  func WithMaxHistory(max int32) HistoryOption {
   468  	return func(opts *options) {
   469  		opts.histReq.Max = max
   470  	}
   471  }
   472  
   473  // NewContext creates a versioned context.
   474  func NewContext() context.Context {
   475  	md := metadata.Pairs("x-helm-api-client", version.GetVersion())
   476  	return metadata.NewOutgoingContext(context.TODO(), md)
   477  }
   478  
   479  // ReleaseTestOption allows configuring optional request data for
   480  // issuing a TestRelease rpc.
   481  type ReleaseTestOption func(*options)