github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/pkg/helm/option.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  
    22  	"github.com/golang/protobuf/proto"
    23  	"golang.org/x/net/context"
    24  	"google.golang.org/grpc/metadata"
    25  
    26  	cpb "k8s.io/helm/pkg/proto/hapi/chart"
    27  	"k8s.io/helm/pkg/proto/hapi/release"
    28  	rls "k8s.io/helm/pkg/proto/hapi/services"
    29  	"k8s.io/helm/pkg/version"
    30  )
    31  
    32  // Option allows specifying various settings configurable by
    33  // the helm client user for overriding the defaults used when
    34  // issuing rpc's to the Tiller release server.
    35  type Option func(*options)
    36  
    37  // options specify optional settings used by the helm client.
    38  type options struct {
    39  	// value of helm home override
    40  	host string
    41  	// if set dry-run helm client calls
    42  	dryRun bool
    43  	// if set enable TLS on helm client calls
    44  	useTLS bool
    45  	// if set, re-use an existing name
    46  	reuseName bool
    47  	// if set, performs pod restart during upgrade/rollback
    48  	recreate bool
    49  	// if set, skip running hooks
    50  	disableHooks bool
    51  	// name of release
    52  	releaseName string
    53  	// tls.Config to use for rpc if tls enabled
    54  	tlsConfig *tls.Config
    55  	// release list options are applied directly to the list releases request
    56  	listReq rls.ListReleasesRequest
    57  	// release install options are applied directly to the install release request
    58  	instReq rls.InstallReleaseRequest
    59  	// release update options are applied directly to the update release request
    60  	updateReq rls.UpdateReleaseRequest
    61  	// release uninstall options are applied directly to the uninstall release request
    62  	uninstallReq rls.UninstallReleaseRequest
    63  	// release get status options are applied directly to the get release status request
    64  	statusReq rls.GetReleaseStatusRequest
    65  	// release get content options are applied directly to the get release content request
    66  	contentReq rls.GetReleaseContentRequest
    67  	// release rollback options are applied directly to the rollback release request
    68  	rollbackReq rls.RollbackReleaseRequest
    69  	// before intercepts client calls before sending
    70  	before func(context.Context, proto.Message) error
    71  	// release history options are applied directly to the get release history request
    72  	histReq rls.GetHistoryRequest
    73  	// resetValues instructs Tiller to reset values to their defaults.
    74  	resetValues bool
    75  	// reuseValues instructs Tiller to reuse the values from the last release.
    76  	reuseValues bool
    77  	// release test options are applied directly to the test release history request
    78  	testReq rls.TestReleaseRequest
    79  }
    80  
    81  // Host specifies the host address of the Tiller release server, (default = ":44134").
    82  func Host(host string) Option {
    83  	return func(opts *options) {
    84  		opts.host = host
    85  	}
    86  }
    87  
    88  // WithTLS specifies the tls configuration if the helm client is enabled to use TLS.
    89  func WithTLS(cfg *tls.Config) Option {
    90  	return func(opts *options) {
    91  		opts.useTLS = true
    92  		opts.tlsConfig = cfg
    93  	}
    94  }
    95  
    96  // BeforeCall returns an option that allows intercepting a helm client rpc
    97  // before being sent OTA to tiller. The intercepting function should return
    98  // an error to indicate that the call should not proceed or nil otherwise.
    99  func BeforeCall(fn func(context.Context, proto.Message) error) Option {
   100  	return func(opts *options) {
   101  		opts.before = fn
   102  	}
   103  }
   104  
   105  // ReleaseListOption allows specifying various settings
   106  // configurable by the helm client user for overriding
   107  // the defaults used when running the `helm list` command.
   108  type ReleaseListOption func(*options)
   109  
   110  // ReleaseListOffset specifies the offset into a list of releases.
   111  func ReleaseListOffset(offset string) ReleaseListOption {
   112  	return func(opts *options) {
   113  		opts.listReq.Offset = offset
   114  	}
   115  }
   116  
   117  // ReleaseListFilter specifies a filter to apply a list of releases.
   118  func ReleaseListFilter(filter string) ReleaseListOption {
   119  	return func(opts *options) {
   120  		opts.listReq.Filter = filter
   121  	}
   122  }
   123  
   124  // ReleaseListLimit set an upper bound on the number of releases returned.
   125  func ReleaseListLimit(limit int) ReleaseListOption {
   126  	return func(opts *options) {
   127  		opts.listReq.Limit = int64(limit)
   128  	}
   129  }
   130  
   131  // ReleaseListOrder specifies how to order a list of releases.
   132  func ReleaseListOrder(order int32) ReleaseListOption {
   133  	return func(opts *options) {
   134  		opts.listReq.SortOrder = rls.ListSort_SortOrder(order)
   135  	}
   136  }
   137  
   138  // ReleaseListSort specifies how to sort a release list.
   139  func ReleaseListSort(sort int32) ReleaseListOption {
   140  	return func(opts *options) {
   141  		opts.listReq.SortBy = rls.ListSort_SortBy(sort)
   142  	}
   143  }
   144  
   145  // ReleaseListStatuses specifies which status codes should be returned.
   146  func ReleaseListStatuses(statuses []release.Status_Code) ReleaseListOption {
   147  	return func(opts *options) {
   148  		if len(statuses) == 0 {
   149  			statuses = []release.Status_Code{release.Status_DEPLOYED}
   150  		}
   151  		opts.listReq.StatusCodes = statuses
   152  	}
   153  }
   154  
   155  // ReleaseListNamespace specifies the namespace to list releases from
   156  func ReleaseListNamespace(namespace string) ReleaseListOption {
   157  	return func(opts *options) {
   158  		opts.listReq.Namespace = namespace
   159  	}
   160  }
   161  
   162  // InstallOption allows specifying various settings
   163  // configurable by the helm client user for overriding
   164  // the defaults used when running the `helm install` command.
   165  type InstallOption func(*options)
   166  
   167  // ValueOverrides specifies a list of values to include when installing.
   168  func ValueOverrides(raw []byte) InstallOption {
   169  	return func(opts *options) {
   170  		opts.instReq.Values = &cpb.Config{Raw: string(raw)}
   171  	}
   172  }
   173  
   174  // ReleaseName specifies the name of the release when installing.
   175  func ReleaseName(name string) InstallOption {
   176  	return func(opts *options) {
   177  		opts.instReq.Name = name
   178  	}
   179  }
   180  
   181  // InstallTimeout specifies the number of seconds before kubernetes calls timeout
   182  func InstallTimeout(timeout int64) InstallOption {
   183  	return func(opts *options) {
   184  		opts.instReq.Timeout = timeout
   185  	}
   186  }
   187  
   188  // UpgradeTimeout specifies the number of seconds before kubernetes calls timeout
   189  func UpgradeTimeout(timeout int64) UpdateOption {
   190  	return func(opts *options) {
   191  		opts.updateReq.Timeout = timeout
   192  	}
   193  }
   194  
   195  // DeleteTimeout specifies the number of seconds before kubernetes calls timeout
   196  func DeleteTimeout(timeout int64) DeleteOption {
   197  	return func(opts *options) {
   198  		opts.uninstallReq.Timeout = timeout
   199  	}
   200  }
   201  
   202  // ReleaseTestTimeout specifies the number of seconds before kubernetes calls timeout
   203  func ReleaseTestTimeout(timeout int64) ReleaseTestOption {
   204  	return func(opts *options) {
   205  		opts.testReq.Timeout = timeout
   206  	}
   207  }
   208  
   209  // ReleaseTestCleanup is a boolean value representing whether to cleanup test pods
   210  func ReleaseTestCleanup(cleanup bool) ReleaseTestOption {
   211  	return func(opts *options) {
   212  		opts.testReq.Cleanup = cleanup
   213  	}
   214  }
   215  
   216  // RollbackTimeout specifies the number of seconds before kubernetes calls timeout
   217  func RollbackTimeout(timeout int64) RollbackOption {
   218  	return func(opts *options) {
   219  		opts.rollbackReq.Timeout = timeout
   220  	}
   221  }
   222  
   223  // InstallWait specifies whether or not to wait for all resources to be ready
   224  func InstallWait(wait bool) InstallOption {
   225  	return func(opts *options) {
   226  		opts.instReq.Wait = wait
   227  	}
   228  }
   229  
   230  // UpgradeWait specifies whether or not to wait for all resources to be ready
   231  func UpgradeWait(wait bool) UpdateOption {
   232  	return func(opts *options) {
   233  		opts.updateReq.Wait = wait
   234  	}
   235  }
   236  
   237  // RollbackWait specifies whether or not to wait for all resources to be ready
   238  func RollbackWait(wait bool) RollbackOption {
   239  	return func(opts *options) {
   240  		opts.rollbackReq.Wait = wait
   241  	}
   242  }
   243  
   244  // UpdateValueOverrides specifies a list of values to include when upgrading
   245  func UpdateValueOverrides(raw []byte) UpdateOption {
   246  	return func(opts *options) {
   247  		opts.updateReq.Values = &cpb.Config{Raw: string(raw)}
   248  	}
   249  }
   250  
   251  // DeleteDisableHooks will disable hooks for a deletion operation.
   252  func DeleteDisableHooks(disable bool) DeleteOption {
   253  	return func(opts *options) {
   254  		opts.disableHooks = disable
   255  	}
   256  }
   257  
   258  // DeleteDryRun will (if true) execute a deletion as a dry run.
   259  func DeleteDryRun(dry bool) DeleteOption {
   260  	return func(opts *options) {
   261  		opts.dryRun = dry
   262  	}
   263  }
   264  
   265  // DeletePurge removes the release from the store and make its name free for later use.
   266  func DeletePurge(purge bool) DeleteOption {
   267  	return func(opts *options) {
   268  		opts.uninstallReq.Purge = purge
   269  	}
   270  }
   271  
   272  // InstallDryRun will (if true) execute an installation as a dry run.
   273  func InstallDryRun(dry bool) InstallOption {
   274  	return func(opts *options) {
   275  		opts.dryRun = dry
   276  	}
   277  }
   278  
   279  // InstallDisableHooks disables hooks during installation.
   280  func InstallDisableHooks(disable bool) InstallOption {
   281  	return func(opts *options) {
   282  		opts.disableHooks = disable
   283  	}
   284  }
   285  
   286  // InstallReuseName will (if true) instruct Tiller to re-use an existing name.
   287  func InstallReuseName(reuse bool) InstallOption {
   288  	return func(opts *options) {
   289  		opts.reuseName = reuse
   290  	}
   291  }
   292  
   293  // RollbackDisableHooks will disable hooks for a rollback operation
   294  func RollbackDisableHooks(disable bool) RollbackOption {
   295  	return func(opts *options) {
   296  		opts.disableHooks = disable
   297  	}
   298  }
   299  
   300  // RollbackDryRun will (if true) execute a rollback as a dry run.
   301  func RollbackDryRun(dry bool) RollbackOption {
   302  	return func(opts *options) {
   303  		opts.dryRun = dry
   304  	}
   305  }
   306  
   307  // RollbackRecreate will (if true) recreate pods after rollback.
   308  func RollbackRecreate(recreate bool) RollbackOption {
   309  	return func(opts *options) {
   310  		opts.recreate = recreate
   311  	}
   312  }
   313  
   314  // RollbackVersion sets the version of the release to deploy.
   315  func RollbackVersion(ver int32) RollbackOption {
   316  	return func(opts *options) {
   317  		opts.rollbackReq.Version = ver
   318  	}
   319  }
   320  
   321  // UpgradeDisableHooks will disable hooks for an upgrade operation.
   322  func UpgradeDisableHooks(disable bool) UpdateOption {
   323  	return func(opts *options) {
   324  		opts.disableHooks = disable
   325  	}
   326  }
   327  
   328  // UpgradeDryRun will (if true) execute an upgrade as a dry run.
   329  func UpgradeDryRun(dry bool) UpdateOption {
   330  	return func(opts *options) {
   331  		opts.dryRun = dry
   332  	}
   333  }
   334  
   335  // ResetValues will (if true) trigger resetting the values to their original state.
   336  func ResetValues(reset bool) UpdateOption {
   337  	return func(opts *options) {
   338  		opts.resetValues = reset
   339  	}
   340  }
   341  
   342  // ReuseValues will (if true) trigger resetting the values to their original state.
   343  func ReuseValues(reuse bool) UpdateOption {
   344  	return func(opts *options) {
   345  		opts.reuseValues = reuse
   346  	}
   347  }
   348  
   349  // UpgradeRecreate will (if true) recreate pods after upgrade.
   350  func UpgradeRecreate(recreate bool) UpdateOption {
   351  	return func(opts *options) {
   352  		opts.recreate = recreate
   353  	}
   354  }
   355  
   356  // ContentOption allows setting optional attributes when
   357  // performing a GetReleaseContent tiller rpc.
   358  type ContentOption func(*options)
   359  
   360  // ContentReleaseVersion will instruct Tiller to retrieve the content
   361  // of a paritcular version of a release.
   362  func ContentReleaseVersion(version int32) ContentOption {
   363  	return func(opts *options) {
   364  		opts.contentReq.Version = version
   365  	}
   366  }
   367  
   368  // StatusOption allows setting optional attributes when
   369  // performing a GetReleaseStatus tiller rpc.
   370  type StatusOption func(*options)
   371  
   372  // StatusReleaseVersion will instruct Tiller to retrieve the status
   373  // of a particular version of a release.
   374  func StatusReleaseVersion(version int32) StatusOption {
   375  	return func(opts *options) {
   376  		opts.statusReq.Version = version
   377  	}
   378  }
   379  
   380  // DeleteOption allows setting optional attributes when
   381  // performing a UninstallRelease tiller rpc.
   382  type DeleteOption func(*options)
   383  
   384  // VersionOption -- TODO
   385  type VersionOption func(*options)
   386  
   387  // UpdateOption allows specifying various settings
   388  // configurable by the helm client user for overriding
   389  // the defaults used when running the `helm upgrade` command.
   390  type UpdateOption func(*options)
   391  
   392  // RollbackOption allows specififying various settings configurable
   393  // by the helm client user for overriding the defaults used when
   394  // running the `helm rollback` command.
   395  type RollbackOption func(*options)
   396  
   397  // HistoryOption allows configuring optional request data for
   398  // issuing a GetHistory rpc.
   399  type HistoryOption func(*options)
   400  
   401  // WithMaxHistory sets the max number of releases to return
   402  // in a release history query.
   403  func WithMaxHistory(max int32) HistoryOption {
   404  	return func(opts *options) {
   405  		opts.histReq.Max = max
   406  	}
   407  }
   408  
   409  // NewContext creates a versioned context.
   410  func NewContext() context.Context {
   411  	md := metadata.Pairs("x-helm-api-client", version.Version)
   412  	return metadata.NewContext(context.TODO(), md)
   413  }
   414  
   415  // ReleaseTestOption allows configuring optional request data for
   416  // issuing a TestRelease rpc.
   417  type ReleaseTestOption func(*options)