-
Notifications
You must be signed in to change notification settings - Fork 104
/
upgrade.go
115 lines (96 loc) · 4.39 KB
/
upgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cmd
import (
"errors"
"fmt"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/kudobuilder/kudo/pkg/kudoctl/cmd/install"
"github.com/kudobuilder/kudo/pkg/kudoctl/cmd/params"
"github.com/kudobuilder/kudo/pkg/kudoctl/env"
pkgresolver "github.com/kudobuilder/kudo/pkg/kudoctl/packages/resolver"
"github.com/kudobuilder/kudo/pkg/kudoctl/resources/upgrade"
"github.com/kudobuilder/kudo/pkg/kudoctl/util/repo"
)
var (
upgradeDesc = `Upgrade KUDO package from current version to new version. The upgrade argument must be a name of the
package in the repository, a path to package in *.tgz format, or a path to an unpacked package directory.`
upgradeExample = ` # Upgrade flink instance dev-flink to the latest version
kubectl kudo upgrade flink --instance dev-flink
*Note*: should you have a local "flink" folder in the current directory it will take precedence over the remote repository.
# Upgrade flink to the version 1.1.1
kubectl kudo upgrade flink --instance dev-flink --operator-version 1.1.1
# By default arguments are all reused from the previous installation, if you need to modify, use -p
kubectl kudo upgrade flink --instance dev-flink -p param=xxx`
)
type options struct {
install.RepositoryOptions
InstanceName string
AppVersion string
OperatorVersion string
Parameters map[string]string
}
// defaultOptions initializes the install command options to its defaults
var defaultOptions = &options{}
// newUpgradeCmd creates the install command for the CLI
func newUpgradeCmd(fs afero.Fs) *cobra.Command {
options := defaultOptions
var parameters []string
var parameterFiles []string
upgradeCmd := &cobra.Command{
Use: "upgrade <name>",
Short: "Upgrade KUDO package.",
Long: upgradeDesc,
Example: upgradeExample,
RunE: func(cmd *cobra.Command, args []string) error {
// Prior to command execution we parse and validate passed arguments
var err error
options.Parameters, err = params.GetParameterMap(fs, parameters, parameterFiles)
if err != nil {
return fmt.Errorf("could not parse parameters: %v", err)
}
return runUpgrade(args, options, fs, &Settings)
},
}
upgradeCmd.Flags().StringVar(&options.InstanceName, "instance", "", "The instance name.")
upgradeCmd.Flags().StringArrayVarP(¶meters, "parameter", "p", nil, "The parameter name and value separated by '='")
upgradeCmd.Flags().StringArrayVarP(¶meterFiles, "parameter-file", "P", nil, "YAML file with parameters")
upgradeCmd.Flags().StringVar(&options.RepoName, "repo", "", "Name of repository configuration to use. (default defined by context)")
upgradeCmd.Flags().StringVar(&options.AppVersion, "app-version", "",
"A specific app version in the official repository. When installing from other sources than an official repository, a version from inside operator.yaml will be used. (default to the most recent)")
upgradeCmd.Flags().StringVar(&options.OperatorVersion, "operator-version", "",
"A specific operator version in the official repository. When installing from other sources than an official repository, a version from inside operator.yaml will be used. (default to the most recent)")
return upgradeCmd
}
func validateCmd(args []string, options *options) error {
if len(args) != 1 {
return errors.New("expecting exactly one argument - name of the package or path to upgrade")
}
if options.InstanceName == "" {
return errors.New("please use --instance and specify instance name. It cannot be empty")
}
return nil
}
func runUpgrade(args []string, options *options, fs afero.Fs, settings *env.Settings) error {
err := validateCmd(args, options)
if err != nil {
return err
}
packageToUpgrade := args[0]
kc, err := env.GetClient(settings)
if err != nil {
return fmt.Errorf("creating kudo client: %w", err)
}
// Resolve the package to upgrade to
repository, err := repo.ClientFromSettings(fs, settings.Home, options.RepoName)
if err != nil {
return fmt.Errorf("could not build operator repository: %w", err)
}
resolver := pkgresolver.New(repository)
pkg, err := resolver.Resolve(packageToUpgrade, options.AppVersion, options.OperatorVersion)
if err != nil {
return fmt.Errorf("failed to resolve operator package for: %s: %w", packageToUpgrade, err)
}
resources := pkg.Resources
resources.OperatorVersion.SetNamespace(settings.Namespace)
return upgrade.OperatorVersion(kc, resources.OperatorVersion, options.InstanceName, options.Parameters, resolver)
}