github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+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  	"golang.org/x/net/context"
    21  	cpb "k8s.io/helm/pkg/proto/hapi/chart"
    22  	"k8s.io/helm/pkg/proto/hapi/release"
    23  	rls "k8s.io/helm/pkg/proto/hapi/services"
    24  )
    25  
    26  // Option allows specifying various settings configurable by
    27  // the helm client user for overriding the defaults used when
    28  // issuing rpc's to the Tiller release server.
    29  type Option func(*options)
    30  
    31  // options specify optional settings used by the helm client.
    32  type options struct {
    33  	// value of helm host override
    34  	home string
    35  	// value of helm home override
    36  	host string
    37  	// name of chart
    38  	chart string
    39  	// if set dry-run helm client calls
    40  	dryRun bool
    41  	// if set, re-use an existing name
    42  	reuseName bool
    43  	// if set, skip running hooks
    44  	disableHooks bool
    45  	// release list options are applied directly to the list releases request
    46  	listReq rls.ListReleasesRequest
    47  	// release install options are applied directly to the install release request
    48  	instReq rls.InstallReleaseRequest
    49  	// release update options are applied directly to the update release request
    50  	updateReq rls.UpdateReleaseRequest
    51  	// release uninstall options are applied directly to the uninstall release request
    52  	uninstallReq rls.UninstallReleaseRequest
    53  }
    54  
    55  // Home specifies the location of helm home, (default = "$HOME/.helm").
    56  func Home(home string) Option {
    57  	return func(opts *options) {
    58  		opts.home = home
    59  	}
    60  }
    61  
    62  // Host specifies the host address of the Tiller release server, (default = ":44134").
    63  func Host(host string) Option {
    64  	return func(opts *options) {
    65  		opts.host = host
    66  	}
    67  }
    68  
    69  // ReleaseListOption allows specifying various settings
    70  // configurable by the helm client user for overriding
    71  // the defaults used when running the `helm list` command.
    72  type ReleaseListOption func(*options)
    73  
    74  // ReleaseListOffset specifies the offset into a list of releases.
    75  func ReleaseListOffset(offset string) ReleaseListOption {
    76  	return func(opts *options) {
    77  		opts.listReq.Offset = offset
    78  	}
    79  }
    80  
    81  // ReleaseListFilter specifies a filter to apply a list of releases.
    82  func ReleaseListFilter(filter string) ReleaseListOption {
    83  	return func(opts *options) {
    84  		opts.listReq.Filter = filter
    85  	}
    86  }
    87  
    88  // ReleaseListLimit set an upper bound on the number of releases returned.
    89  func ReleaseListLimit(limit int) ReleaseListOption {
    90  	return func(opts *options) {
    91  		opts.listReq.Limit = int64(limit)
    92  	}
    93  }
    94  
    95  // ReleaseListOrder specifies how to order a list of releases.
    96  func ReleaseListOrder(order int32) ReleaseListOption {
    97  	return func(opts *options) {
    98  		opts.listReq.SortOrder = rls.ListSort_SortOrder(order)
    99  	}
   100  }
   101  
   102  // ReleaseListSort specifies how to sort a release list.
   103  func ReleaseListSort(sort int32) ReleaseListOption {
   104  	return func(opts *options) {
   105  		opts.listReq.SortBy = rls.ListSort_SortBy(sort)
   106  	}
   107  }
   108  
   109  // ReleaseListStatuses specifies which status codes should be returned.
   110  func ReleaseListStatuses(statuses []release.Status_Code) ReleaseListOption {
   111  	return func(opts *options) {
   112  		if len(statuses) == 0 {
   113  			statuses = []release.Status_Code{release.Status_DEPLOYED}
   114  		}
   115  		opts.listReq.StatusCodes = statuses
   116  	}
   117  }
   118  
   119  // InstallOption allows specifying various settings
   120  // configurable by the helm client user for overriding
   121  // the defaults used when running the `helm install` command.
   122  type InstallOption func(*options)
   123  
   124  // ValueOverrides specifies a list of values to include when installing.
   125  func ValueOverrides(raw []byte) InstallOption {
   126  	return func(opts *options) {
   127  		opts.instReq.Values = &cpb.Config{Raw: string(raw)}
   128  	}
   129  }
   130  
   131  // UpdateValueOverrides specifies a list of values to include when upgrading
   132  func UpdateValueOverrides(raw []byte) UpdateOption {
   133  	return func(opts *options) {
   134  		opts.updateReq.Values = &cpb.Config{Raw: string(raw)}
   135  	}
   136  }
   137  
   138  // ReleaseName specifies the name of the release when installing.
   139  func ReleaseName(name string) InstallOption {
   140  	return func(opts *options) {
   141  		opts.instReq.Name = name
   142  	}
   143  }
   144  
   145  // DeleteDisableHooks will disable hooks for a deletion operation.
   146  func DeleteDisableHooks(disable bool) DeleteOption {
   147  	return func(opts *options) {
   148  		opts.disableHooks = disable
   149  	}
   150  }
   151  
   152  // DeleteDryRun will (if true) execute a deletion as a dry run.
   153  func DeleteDryRun(dry bool) DeleteOption {
   154  	return func(opts *options) {
   155  		opts.dryRun = dry
   156  	}
   157  }
   158  
   159  // DeletePurge removes the release from the store and make its name free for later use.
   160  func DeletePurge(purge bool) DeleteOption {
   161  	return func(opts *options) {
   162  		opts.uninstallReq.Purge = purge
   163  	}
   164  }
   165  
   166  // UpgradeDisableHooks will disable hooks for an upgrade operation.
   167  func UpgradeDisableHooks(disable bool) UpdateOption {
   168  	return func(opts *options) {
   169  		opts.disableHooks = disable
   170  	}
   171  }
   172  
   173  // UpgradeDryRun will (if true) execute an upgrade as a dry run.
   174  func UpgradeDryRun(dry bool) UpdateOption {
   175  	return func(opts *options) {
   176  		opts.dryRun = dry
   177  	}
   178  }
   179  
   180  // InstallDisableHooks disables hooks during installation.
   181  func InstallDisableHooks(disable bool) InstallOption {
   182  	return func(opts *options) {
   183  		opts.disableHooks = disable
   184  	}
   185  }
   186  
   187  // InstallDryRun will (if true) execute an installation as a dry run.
   188  func InstallDryRun(dry bool) InstallOption {
   189  	return func(opts *options) {
   190  		opts.dryRun = dry
   191  	}
   192  }
   193  
   194  // InstallReuseName will (if true) instruct Tiller to re-use an existing name.
   195  func InstallReuseName(reuse bool) InstallOption {
   196  	return func(opts *options) {
   197  		opts.reuseName = reuse
   198  	}
   199  }
   200  
   201  // ContentOption -- TODO
   202  type ContentOption func(*options)
   203  
   204  // StatusOption -- TODO
   205  type StatusOption func(*options)
   206  
   207  // DeleteOption -- TODO
   208  type DeleteOption func(*options)
   209  
   210  // UpdateOption allows specifying various settings
   211  // configurable by the helm client user for overriding
   212  // the defaults used when running the `helm upgrade` command.
   213  type UpdateOption func(*options)
   214  
   215  // RPC helpers defined on `options` type. Note: These actually execute the
   216  // the corresponding tiller RPC. There is no particular reason why these
   217  // are APIs are hung off `options`, they are internal to pkg/helm to remain
   218  // malleable.
   219  
   220  // Executes tiller.ListReleases RPC.
   221  func (o *options) rpcListReleases(rlc rls.ReleaseServiceClient, opts ...ReleaseListOption) (*rls.ListReleasesResponse, error) {
   222  	// apply release list options
   223  	for _, opt := range opts {
   224  		opt(o)
   225  	}
   226  	s, err := rlc.ListReleases(context.TODO(), &o.listReq)
   227  	if err != nil {
   228  		return nil, err
   229  	}
   230  
   231  	return s.Recv()
   232  }
   233  
   234  // Executes tiller.InstallRelease RPC.
   235  func (o *options) rpcInstallRelease(chr *cpb.Chart, rlc rls.ReleaseServiceClient, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) {
   236  	// apply the install options
   237  	for _, opt := range opts {
   238  		opt(o)
   239  	}
   240  	o.instReq.Chart = chr
   241  	o.instReq.Namespace = ns
   242  	o.instReq.DryRun = o.dryRun
   243  	o.instReq.DisableHooks = o.disableHooks
   244  	o.instReq.ReuseName = o.reuseName
   245  
   246  	return rlc.InstallRelease(context.TODO(), &o.instReq)
   247  }
   248  
   249  // Executes tiller.UninstallRelease RPC.
   250  func (o *options) rpcDeleteRelease(rlsName string, rlc rls.ReleaseServiceClient, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) {
   251  	for _, opt := range opts {
   252  		opt(o)
   253  	}
   254  	if o.dryRun {
   255  		// In the dry run case, just see if the release exists
   256  		r, err := o.rpcGetReleaseContent(rlsName, rlc)
   257  		if err != nil {
   258  			return &rls.UninstallReleaseResponse{}, err
   259  		}
   260  		return &rls.UninstallReleaseResponse{Release: r.Release}, nil
   261  	}
   262  
   263  	o.uninstallReq.Name = rlsName
   264  	o.uninstallReq.DisableHooks = o.disableHooks
   265  
   266  	return rlc.UninstallRelease(context.TODO(), &o.uninstallReq)
   267  }
   268  
   269  // Executes tiller.UpdateRelease RPC.
   270  func (o *options) rpcUpdateRelease(rlsName string, chr *cpb.Chart, rlc rls.ReleaseServiceClient, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) {
   271  	for _, opt := range opts {
   272  		opt(o)
   273  	}
   274  
   275  	o.updateReq.Chart = chr
   276  	o.updateReq.DryRun = o.dryRun
   277  	o.updateReq.Name = rlsName
   278  
   279  	return rlc.UpdateRelease(context.TODO(), &o.updateReq)
   280  }
   281  
   282  // Executes tiller.GetReleaseStatus RPC.
   283  func (o *options) rpcGetReleaseStatus(rlsName string, rlc rls.ReleaseServiceClient, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error) {
   284  	req := &rls.GetReleaseStatusRequest{Name: rlsName}
   285  	return rlc.GetReleaseStatus(context.TODO(), req)
   286  }
   287  
   288  // Executes tiller.GetReleaseContent.
   289  func (o *options) rpcGetReleaseContent(rlsName string, rlc rls.ReleaseServiceClient, opts ...ContentOption) (*rls.GetReleaseContentResponse, error) {
   290  	req := &rls.GetReleaseContentRequest{Name: rlsName}
   291  	return rlc.GetReleaseContent(context.TODO(), req)
   292  }