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