This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
/
main.go
153 lines (128 loc) · 3.74 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2019 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"os"
"time"
"github.com/go-logr/logr"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/vmware/cluster-api-upgrade-tool/pkg/logging"
"github.com/vmware/cluster-api-upgrade-tool/pkg/upgrade"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
)
func newLogger() logr.Logger {
log := logrus.New()
log.Out = os.Stdout
return logging.NewLogrusLoggerAdapter(log)
}
func main() {
configFile := ""
configFromFlags := upgrade.Config{
MachineTimeout: metav1.Duration{Duration: 15 * time.Minute},
}
root := &cobra.Command{
Use: os.Args[0],
Short: "Upgrades Kubernetes clusters created by Cluster API.",
RunE: func(_ *cobra.Command, _ []string) error {
// Default the final config to coming from flags
finalConfig := configFromFlags
// Process the config file, if applicable
if configFile != "" {
reader, err := os.Open(configFile)
if err != nil {
return errors.Wrap(err, "error opening config file")
}
defer reader.Close()
var configFromFile upgrade.Config
const bufferSize = 512
decoder := yaml.NewYAMLOrJSONDecoder(reader, bufferSize)
if err := decoder.Decode(&configFromFile); err != nil {
return errors.Wrap(err, "error parsing config file")
}
// Merge command line flags on top
if err := mergo.Merge(&configFromFile, configFromFlags, mergo.WithOverride); err != nil {
return errors.Wrap(err, "error merging flags with config file")
}
// Update our final config to be the merged copy
finalConfig = configFromFile
}
upgrader, err := upgrade.NewControlPlaneUpgrader(newLogger(), finalConfig)
if err != nil {
return err
}
return upgrader.Upgrade()
},
SilenceUsage: true,
}
root.Flags().StringVar(
&configFile,
"config",
"",
"Path to a config file in yaml or json format",
)
root.Flags().StringVar(
&configFromFlags.ManagementCluster.Kubeconfig,
"kubeconfig",
"",
"The kubeconfig path for the management cluster",
)
root.Flags().StringVar(
&configFromFlags.TargetCluster.Namespace,
"cluster-namespace",
"",
"The namespace of target cluster (required)",
)
root.Flags().StringVar(
&configFromFlags.TargetCluster.Name,
"cluster-name",
"",
"The name of target cluster (required)",
)
root.Flags().StringVar(
&configFromFlags.KubernetesVersion,
"kubernetes-version",
"",
"Desired kubernetes version to upgrade to (required)",
)
root.Flags().StringVar(
&configFromFlags.UpgradeID,
"upgrade-id",
"",
"Unique identifier used to resume a partial upgrade (required)",
)
root.Flags().StringVar(
&configFromFlags.Patches.Infrastructure,
"infrastructure-patches",
"",
"JSON patch expression of patches to apply to the machine's infrastructure resource (optional)",
)
root.Flags().StringVar(
&configFromFlags.Patches.Bootstrap,
"bootstrap-patches",
"",
"JSON patch expression of patches to apply to the machine's bootstrap resource (optional)",
)
root.Flags().StringVar(
&configFromFlags.Patches.KubeadmConfigMap,
"kubeadm-configmap-patches",
"",
"JSON patch expression of patches to apply to the kubeadm-config ConfigMap (optional)",
)
root.Flags().DurationVar(
&configFromFlags.MachineTimeout.Duration,
"machine-timeout",
configFromFlags.MachineTimeout.Duration,
"How long to wait for machine operations (create, delete) to complete",
)
if err := root.Execute(); err != nil {
// Print a stack trace, if possible. We may end up with the error message printed twice,
// but the stack trace can be invaluable, so we'll accept this for the time being.
fmt.Printf("%+v\n", err)
os.Exit(1)
}
}