actions.go 10.6 KB
Newer Older
bryanl's avatar
bryanl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright 2018 The ksonnet authors
//
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

package actions

import (
19
	"bytes"
20
	"crypto/tls"
bryanl's avatar
bryanl committed
21
	"fmt"
22 23 24
	"net"
	"net/http"
	"time"
bryanl's avatar
bryanl committed
25

bryanl's avatar
bryanl committed
26
	"github.com/ksonnet/ksonnet/pkg/app"
bryanl's avatar
bryanl committed
27
	"github.com/ksonnet/ksonnet/pkg/client"
28 29
	"github.com/ksonnet/ksonnet/pkg/registry"
	"github.com/ksonnet/ksonnet/pkg/upgrade"
bryanl's avatar
bryanl committed
30 31
	"github.com/pkg/errors"
	"github.com/spf13/afero"
bryanl's avatar
bryanl committed
32 33 34
)

const (
bryanl's avatar
bryanl committed
35 36
	// OptionApp is app option.
	OptionApp = "app"
37 38
	// OptionAppRoot is the root directory of the application.
	OptionAppRoot = "app-root"
bryanl's avatar
bryanl committed
39 40
	// OptionArguments is arguments option. Used for passing arguments to prototypes.
	OptionArguments = "arguments"
41 42
	// OptionAsString is asString. Used for setting values as strings.
	OptionAsString = "as-string"
bryanl's avatar
bryanl committed
43 44 45 46 47
	// OptionClientConfig is clientConfig option.
	OptionClientConfig = "client-config"
	// OptionComponentName is a componentName option.
	OptionComponentName = "component-name"
	// OptionComponentNames is componentNames option.
bryanl's avatar
bryanl committed
48
	OptionComponentNames = "component-names"
bryanl's avatar
bryanl committed
49 50 51 52 53 54
	// OptionCreate is create option.
	OptionCreate = "create"
	// OptionDryRun is dryRun option.
	OptionDryRun = "dry-run"
	// OptionEnvName is envName option.
	OptionEnvName = "env-name"
55 56 57 58
	// OptionEnvName1 is envName1. Used for param diff.
	OptionEnvName1 = "env-name-1"
	// OptionEnvName2 is envName1. Used for param diff.
	OptionEnvName2 = "env-name-2"
bryanl's avatar
bryanl committed
59 60 61 62
	// OptionExtVarFiles is jsonnet ext var files.
	OptionExtVarFiles = "ext-vars-files"
	// OptionExtVars is jsonnet ext vars.
	OptionExtVars = "ext-vars"
63 64
	// OptionForce is force option.
	OptionForce = "force"
bryanl's avatar
bryanl committed
65 66
	// OptionFormat is format option.
	OptionFormat = "format"
bryanl's avatar
bryanl committed
67 68 69 70 71 72
	// OptionFs is fs option.
	OptionFs = "fs"
	// OptionGcTag is gcTag option.
	OptionGcTag = "gc-tag"
	// OptionGlobal is global option.
	OptionGlobal = "global"
bryanl's avatar
bryanl committed
73 74
	// OptionGracePeriod is gracePeriod option.
	OptionGracePeriod = "grace-period"
75 76
	// OptionHTTPClient is the http.Client for outbound network requests.
	OptionHTTPClient = "http-client"
bryanl's avatar
bryanl committed
77 78
	// OptionInstalled is for listing installed packages.
	OptionInstalled = "only-installed"
bryanl's avatar
bryanl committed
79 80
	// OptionJPaths is jsonnet paths.
	OptionJPaths = "jpaths"
Oren Shomron's avatar
Oren Shomron committed
81 82
	// OptionPkgName is (an optionally qualified) name of a package.
	OptionPkgName = "pkg-name"
bryanl's avatar
bryanl committed
83 84
	// OptionName is name option.
	OptionName = "name"
85 86 87 88
	// OptionModule is component module option.
	OptionModule = "module"
	// OptionNamespace is a cluster namespace option
	OptionNamespace = "namespace"
89 90
	// OptionNewRoot is init new root path option.
	OptionNewRoot = "root-path"
bryanl's avatar
bryanl committed
91 92 93 94 95 96 97 98 99 100 101 102
	// OptionNewEnvName is newEnvName option. Used for renaming environments.
	OptionNewEnvName = "new-env-name"
	// OptionOutput is output option.
	OptionOutput = "output"
	// OptionOverride is override option.
	OptionOverride = "override"
	// OptionPackageName is packageName option.
	OptionPackageName = "package-name"
	// OptionPath is path option.
	OptionPath = "path"
	// OptionQuery is query option.
	OptionQuery = "query"
103 104 105
	// OptionResolveImage is resolve image option. It is used to resolve docker image references
	// when setting parameters.
	OptionResolveImage = "resolve-image"
bryanl's avatar
bryanl committed
106 107 108 109
	// OptionServer is server option.
	OptionServer = "server"
	// OptionServerURI is serverURI option.
	OptionServerURI = "server-uri"
110 111
	// OptionSkipCheckUpgrade tells app not to emit upgrade warnings, probably because the user is already upgrading.
	OptionSkipCheckUpgrade = "skip-check-upgrade"
112 113
	// OptionSkipDefaultRegistries is skipDefaultRegistries option. Used by init.
	OptionSkipDefaultRegistries = "skip-default-registries"
bryanl's avatar
bryanl committed
114 115 116 117
	// OptionSkipGc is skipGc option.
	OptionSkipGc = "skip-gc"
	// OptionSpecFlag is specFlag option. Used for setting k8s spec.
	OptionSpecFlag = "spec-flag"
bryanl's avatar
bryanl committed
118 119 120 121
	// OptionSrc1 is src1 option.
	OptionSrc1 = "src-1"
	// OptionSrc2 is src2 option.
	OptionSrc2 = "src-2"
bryanl's avatar
bryanl committed
122 123 124 125
	// OptionTlaVarFiles is jsonnet tla var files.
	OptionTlaVarFiles = "tla-var-files"
	// OptionTlaVars is jsonnet tla vars.
	OptionTlaVars = "tla-vars"
126 127
	// OptionTLSSkipVerify specifies that tls server certifactes should not be verified.
	OptionTLSSkipVerify = "tls-skip-verify"
bryanl's avatar
bryanl committed
128 129
	// OptionUnset is unset option.
	OptionUnset = "unset"
bryanl's avatar
bryanl committed
130 131
	// OptionURI is uri option. Used for setting registry URI.
	OptionURI = "URI"
bryanl's avatar
bryanl committed
132 133
	// OptionWithoutModules is without modules option.
	OptionWithoutModules = "without-modules"
bryanl's avatar
bryanl committed
134 135 136 137 138 139
	// OptionValue is value option.
	OptionValue = "value"
	// OptionVersion is version option.
	OptionVersion = "version"
)

bryanl's avatar
bryanl committed
140 141 142
const (
	// OutputWide is wide output
	OutputWide = "wide"
bryanl's avatar
bryanl committed
143 144
	// OutputJSON is JSON output
	OutputJSON = "json"
bryanl's avatar
bryanl committed
145 146
)

bryanl's avatar
bryanl committed
147 148 149
var (
	// ErrNotInApp is an error stating the user is not in a ksonnet application directory
	// hierarchy.
150
	ErrNotInApp = errors.Errorf("this command has to be run within a ksonnet application")
bryanl's avatar
bryanl committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
)

type missingOptionError struct {
	name string
}

func newMissingOptionError(name string) *missingOptionError {
	return &missingOptionError{
		name: name,
	}
}

func (e *missingOptionError) Error() string {
	return fmt.Sprintf("missing required %s option", e.name)
}

type invalidOptionError struct {
	name string
}

func newInvalidOptionError(name string) *invalidOptionError {
	return &invalidOptionError{
		name: name,
	}
}

func (e *invalidOptionError) Error() string {
	return fmt.Sprintf("invalid type for option %s", e.name)
}

181 182 183
// optionLoader loads typed option from a configuration map. If an error
// occurs in any of the load processes, the loader will return default
// values for type.
bryanl's avatar
bryanl committed
184
type optionLoader struct {
185 186
	// err is the error state for the optionLoader. If this is not nil, all
	// subsequent calls to load will return nil.
bryanl's avatar
bryanl committed
187 188 189 190 191 192 193 194 195 196
	err error
	m   map[string]interface{}
}

func newOptionLoader(m map[string]interface{}) *optionLoader {
	return &optionLoader{
		m: m,
	}
}

197 198
func (o *optionLoader) LoadFs() afero.Fs {
	i := o.loadOptional(OptionFs)
bryanl's avatar
bryanl committed
199
	if i == nil {
200
		return afero.NewOsFs()
bryanl's avatar
bryanl committed
201 202 203 204
	}

	a, ok := i.(afero.Fs)
	if !ok {
205
		o.err = newInvalidOptionError(OptionFs)
bryanl's avatar
bryanl committed
206 207 208 209 210 211
		return nil
	}

	return a
}

bryanl's avatar
bryanl committed
212
func (o *optionLoader) LoadBool(name string) bool {
bryanl's avatar
bryanl committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226
	i := o.load(name)
	if i == nil {
		return false
	}

	a, ok := i.(bool)
	if !ok {
		o.err = newInvalidOptionError(name)
		return false
	}

	return a
}

bryanl's avatar
bryanl committed
227
func (o *optionLoader) LoadOptionalBool(name string) bool {
bryanl's avatar
bryanl committed
228 229 230 231 232 233 234 235 236 237 238 239 240
	i := o.loadOptional(name)
	if i == nil {
		return false
	}

	a, ok := i.(bool)
	if !ok {
		return false
	}

	return a
}

bryanl's avatar
bryanl committed
241
func (o *optionLoader) LoadInt(name string) int {
bryanl's avatar
bryanl committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255
	i := o.load(name)
	if i == nil {
		return 0
	}

	a, ok := i.(int)
	if !ok {
		o.err = newInvalidOptionError(name)
		return 0
	}

	return a
}

bryanl's avatar
bryanl committed
256
func (o *optionLoader) LoadInt64(name string) int64 {
bryanl's avatar
bryanl committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
	i := o.load(name)
	if i == nil {
		return 0
	}

	a, ok := i.(int64)
	if !ok {
		o.err = newInvalidOptionError(name)
		return 0
	}

	return a
}

bryanl's avatar
bryanl committed
271
func (o *optionLoader) LoadOptionalInt(name string) int {
bryanl's avatar
bryanl committed
272 273 274 275 276 277 278 279 280 281 282 283 284
	i := o.loadOptional(name)
	if i == nil {
		return 0
	}

	a, ok := i.(int)
	if !ok {
		return 0
	}

	return a
}

bryanl's avatar
bryanl committed
285
func (o *optionLoader) LoadString(name string) string {
bryanl's avatar
bryanl committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299
	i := o.load(name)
	if i == nil {
		return ""
	}

	a, ok := i.(string)
	if !ok {
		o.err = newInvalidOptionError(name)
		return ""
	}

	return a
}

bryanl's avatar
bryanl committed
300
func (o *optionLoader) LoadOptionalString(name string) string {
bryanl's avatar
bryanl committed
301
	i := o.loadOptional(name)
bryanl's avatar
bryanl committed
302
	if i == nil {
bryanl's avatar
bryanl committed
303
		return ""
bryanl's avatar
bryanl committed
304 305
	}

bryanl's avatar
bryanl committed
306
	a, ok := i.(string)
bryanl's avatar
bryanl committed
307
	if !ok {
bryanl's avatar
bryanl committed
308
		return ""
bryanl's avatar
bryanl committed
309 310 311 312 313
	}

	return a
}

bryanl's avatar
bryanl committed
314
func (o *optionLoader) LoadStringSlice(name string) []string {
bryanl's avatar
bryanl committed
315
	i := o.load(name)
bryanl's avatar
bryanl committed
316 317 318 319
	if i == nil {
		return nil
	}

bryanl's avatar
bryanl committed
320
	a, ok := i.([]string)
bryanl's avatar
bryanl committed
321
	if !ok {
bryanl's avatar
bryanl committed
322
		o.err = newInvalidOptionError(name)
bryanl's avatar
bryanl committed
323 324 325 326 327 328
		return nil
	}

	return a
}

bryanl's avatar
bryanl committed
329
func (o *optionLoader) LoadClientConfig() *client.Config {
bryanl's avatar
bryanl committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343
	i := o.load(OptionClientConfig)
	if i == nil {
		return nil
	}

	a, ok := i.(*client.Config)
	if !ok {
		o.err = newInvalidOptionError(OptionClientConfig)
		return nil
	}

	return a
}

344 345
// LoadApp returns an app.App reference - either as passed via OptionApp,
// or newly constructed.
bryanl's avatar
bryanl committed
346
func (o *optionLoader) LoadApp() app.App {
347 348 349 350 351
	i := o.loadOptional(OptionApp)
	a, ok := i.(app.App)
	if i != nil && !ok {
		// App was provided but was invalid type
		o.err = newInvalidOptionError(OptionApp)
bryanl's avatar
bryanl committed
352 353
		return nil
	}
354 355 356 357
	if a != nil {
		// Return app if a valid app.App was provided
		return a
	}
bryanl's avatar
bryanl committed
358

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
	var fs = o.LoadFs()
	if fs == nil {
		o.err = errors.New("missing required fs reference")
		return nil
	}
	var httpClient = o.LoadHTTPClient()
	if httpClient == nil {
		o.err = errors.New("initializing http client")
		return nil
	}
	var appRoot = o.LoadOptionalString(OptionAppRoot)

	appRoot, err := app.FindRoot(fs, appRoot)
	if err != nil {
		o.err = errors.Wrapf(err, "finding app root from starting path: %s", appRoot)
bryanl's avatar
bryanl committed
374 375 376
		return nil
	}

377 378 379 380 381 382 383
	a, err = app.Load(fs, httpClient, appRoot)
	if err != nil {
		o.err = errors.New("initializing app")
		return nil
	}

	if !o.LoadOptionalBool(OptionSkipCheckUpgrade) {
384 385
		pm := registry.NewPackageManager(a)
		if _, err := upgrade.CheckUpgrade(a, new(bytes.Buffer), pm, false); err != nil {
386 387 388 389 390
			o.err = errors.Wrap(err, "checking for app upgrades")
			return nil
		}
	}

bryanl's avatar
bryanl committed
391 392 393
	return a
}

394 395
// LoadHTTPClient loads an HTTP client based on common configuration for certificates, tls verification, timeouts, etc.
func (o *optionLoader) LoadHTTPClient() *http.Client {
396 397 398 399 400 401
	i := o.loadOptional(OptionHTTPClient)
	if c, ok := i.(*http.Client); ok {
		return c
	}

	// Construct a client if none was passed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	tlsSkipVerify := o.LoadOptionalBool(OptionTLSSkipVerify)

	tlsConfig := &tls.Config{
		InsecureSkipVerify: tlsSkipVerify,
	}

	timeoutSeconds := 10

	var defaultTransport http.RoundTripper = &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
			DualStack: true,
		}).DialContext,
		MaxIdleConns:          100,
		IdleConnTimeout:       90 * time.Second,
		TLSClientConfig:       tlsConfig,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	}

	c := &http.Client{
		Timeout:   time.Duration(timeoutSeconds) * time.Second,
		Transport: defaultTransport,
	}

	return c
}

bryanl's avatar
bryanl committed
432 433
func (o *optionLoader) load(key string) interface{} {
	if o.err != nil {
bryanl's avatar
bryanl committed
434 435 436
		return nil
	}

bryanl's avatar
bryanl committed
437
	i, ok := o.m[key]
bryanl's avatar
bryanl committed
438
	if !ok {
bryanl's avatar
bryanl committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452
		o.err = newMissingOptionError(key)
	}

	return i
}

func (o *optionLoader) loadOptional(key string) interface{} {
	if o.err != nil {
		return nil
	}

	i, ok := o.m[key]
	if !ok {
		return nil
bryanl's avatar
bryanl committed
453 454 455 456
	}

	return i
}