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