Skip to content

Commit 14a6796

Browse files
Add RancherOS upgrade
1 parent 44af057 commit 14a6796

File tree

15 files changed

+238
-29
lines changed

15 files changed

+238
-29
lines changed

cmd/rancherd/upgrade/upgrade.go

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func NewUpgrade() *cobra.Command {
1414

1515
type Upgrade struct {
1616
RancherVersion string `usage:"Target Rancher version" short:"r" default:"stable"`
17+
RancherOSVersion string `usage:"Target RancherOS version" short:"o" default:"latest" name:"rancher-os-version"`
1718
KubernetesVersion string `usage:"Target Kubernetes version" short:"k" default:"stable"`
1819
Force bool `usage:"Run without prompting for confirmation" short:"f"`
1920
}
@@ -27,5 +28,6 @@ func (b *Upgrade) Run(cmd *cobra.Command, args []string) error {
2728
return r.Upgrade(cmd.Context(), rancherd.UpgradeConfig{
2829
RancherVersion: b.RancherVersion,
2930
KubernetesVersion: b.KubernetesVersion,
31+
RancherOSVersion: b.RancherOSVersion,
3032
})
3133
}

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ replace (
3333
require (
3434
github.com/hashicorp/go-discover v0.0.0-20201029210230-738cb3105cd0
3535
github.com/pkg/errors v0.9.1
36-
github.com/rancher/fleet/pkg/apis v0.0.0-20210920165712-667022c1d661
3736
github.com/rancher/rancher/pkg/apis v0.0.0-20210920193801-79027c456224
3837
github.com/rancher/system-agent v0.0.1-alpha30
3938
github.com/rancher/wharfie v0.3.2

go.sum

-3
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,6 @@ github.com/rancher/client-go v0.21.0-rancher.1 h1:WVy7jpVmdjIRVkgXpBRlNx4jnGkLCF
828828
github.com/rancher/client-go v0.21.0-rancher.1/go.mod h1:nNBytTF9qPFDEhoqgEPaarobC8QPae13bElIVHzIglA=
829829
github.com/rancher/eks-operator v1.1.1/go.mod h1:MIgvBMHO0MqPuvehwAGxpg7Zcg20GhYO1dXjh9BAHqs=
830830
github.com/rancher/fleet/pkg/apis v0.0.0-20210918015053-5a141a6b18f0/go.mod h1:fiKjX1CsAZhe2u1h/GWNDSc5Ol4+5Q3roWxq9cMbgHk=
831-
github.com/rancher/fleet/pkg/apis v0.0.0-20210920165712-667022c1d661 h1:5z1ey2n0NvONqwjnRfb3+qg52P4zCqqn0VqFZ5oCxqo=
832-
github.com/rancher/fleet/pkg/apis v0.0.0-20210920165712-667022c1d661/go.mod h1:fiKjX1CsAZhe2u1h/GWNDSc5Ol4+5Q3roWxq9cMbgHk=
833831
github.com/rancher/gke-operator v1.1.1/go.mod h1:o2EC1q3xO3gq0vRid1CbaEvvXnWdptT7wiGPRPwUJqY=
834832
github.com/rancher/lasso v0.0.0-20200427171700-e0509f89f319/go.mod h1:6Dw19z1lDIpL887eelVjyqH/mna1hfR61ddCFOG78lw=
835833
github.com/rancher/lasso v0.0.0-20200515155337-a34e1e26ad91/go.mod h1:G6Vv2aj6xB2YjTVagmu4NkhBvbE8nBcGykHRENH6arI=
@@ -1565,7 +1563,6 @@ rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
15651563
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
15661564
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
15671565
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
1568-
sigs.k8s.io/cli-utils v0.16.0 h1:Wr32m1oxjIqc9G9l+igr13PeIM9LCyq8jQ8KjXKelvg=
15691566
sigs.k8s.io/cli-utils v0.16.0/go.mod h1:9Jqm9K2W6ShhCxsEuaz6HSRKKOXigPUx3ZfypGgxBLY=
15701567
sigs.k8s.io/cluster-api v0.3.11-0.20210430180359-45b6080c2764 h1:KR5002Y1x1g+BYmsoaODC/Z43xdKSY0B6bOcwfBa9Bk=
15711568
sigs.k8s.io/cluster-api v0.3.11-0.20210430180359-45b6080c2764/go.mod h1:iDmIO2c5/RSiSciQnI7Xocfe8hTu+KafcgEuc/hnhS4=

pkg/cacerts/cacerts.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ var insecureClient = &http.Client{
2828
}
2929

3030
func Get(server, token, path string) ([]byte, string, error) {
31+
return get(server, token, path, true)
32+
}
33+
34+
func MachineGet(server, token, path string) ([]byte, string, error) {
35+
return get(server, token, path, false)
36+
}
37+
38+
func get(server, token, path string, clusterToken bool) ([]byte, string, error) {
3139
u, err := url2.Parse(server)
3240
if err != nil {
3341
return nil, "", err
@@ -38,8 +46,11 @@ func Get(server, token, path string) ([]byte, string, error) {
3846
if err != nil {
3947
return nil, "", err
4048
}
49+
if !clusterToken {
50+
req.Header.Set("Authorization", "Bearer "+base64.StdEncoding.EncodeToString([]byte(token)))
51+
}
4152

42-
cacert, caChecksum, err := CACerts(server, token)
53+
cacert, caChecksum, err := CACerts(server, token, clusterToken)
4354
if err != nil {
4455
return nil, "", err
4556
}
@@ -71,10 +82,13 @@ func Get(server, token, path string) ([]byte, string, error) {
7182
}
7283

7384
data, err := ioutil.ReadAll(resp.Body)
85+
if resp.StatusCode != http.StatusOK {
86+
return nil, "", fmt.Errorf("%s: %s", resp.Status, data)
87+
}
7488
return data, caChecksum, err
7589
}
7690

77-
func CACerts(server, token string) ([]byte, string, error) {
91+
func CACerts(server, token string, clusterToken bool) ([]byte, string, error) {
7892
nonce, err := randomtoken.Generate()
7993
if err != nil {
8094
return nil, "", err
@@ -86,6 +100,9 @@ func CACerts(server, token string) ([]byte, string, error) {
86100
}
87101

88102
requestURL := fmt.Sprintf("https://%s/cacerts", url.Host)
103+
if !clusterToken {
104+
requestURL = fmt.Sprintf("https://%s/v1-rancheros/cacerts", url.Host)
105+
}
89106
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
90107
if err != nil {
91108
return nil, "", err

pkg/config/remote.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package config
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/rancher/rancherd/pkg/cacerts"
8+
"github.com/rancher/wrangler/pkg/data"
9+
"github.com/rancher/wrangler/pkg/data/convert"
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
func processRemote(cfg Config) (Config, error) {
14+
if cfg.Role != "" || cfg.Server == "" || cfg.Token == "" {
15+
return cfg, nil
16+
}
17+
18+
logrus.Infof("server and token set but required role is not set. Trying to bootstrapping config from machine inventory")
19+
resp, _, err := cacerts.MachineGet(cfg.Server, cfg.Token, "/v1-rancheros/inventory")
20+
if err != nil {
21+
return cfg, fmt.Errorf("bootstrapping config from machine inventory: %w", err)
22+
}
23+
24+
config := map[string]interface{}{}
25+
if err := json.Unmarshal(resp, &config); err != nil {
26+
return cfg, fmt.Errorf("decoding inventory response: %w", err)
27+
}
28+
29+
currentConfig, err := convert.EncodeToMap(cfg)
30+
if err != nil {
31+
return cfg, err
32+
}
33+
34+
var (
35+
newConfig = data.MergeMapsConcatSlice(currentConfig, config)
36+
result Config
37+
)
38+
39+
if err := convert.ToObj(newConfig, &result); err != nil {
40+
return result, err
41+
}
42+
43+
copyConfig := result
44+
copyConfig.Token = "--redacted--"
45+
downloadedConfig, err := json.Marshal(copyConfig)
46+
if err == nil {
47+
logrus.Infof("Downloaded config: %s", downloadedConfig)
48+
}
49+
50+
return result, nil
51+
}

pkg/config/types.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99

1010
v1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1"
11-
"github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1/plan"
11+
"github.com/rancher/system-agent/pkg/applyinator"
1212
"github.com/rancher/wharfie/pkg/registries"
1313
"github.com/rancher/wrangler/pkg/data"
1414
"github.com/rancher/wrangler/pkg/data/convert"
@@ -46,9 +46,9 @@ type Config struct {
4646
Server string `json:"server,omitempty"`
4747
Discovery *DiscoveryConfig `json:"discovery,omitempty"`
4848

49-
RancherValues map[string]interface{} `json:"rancherValues,omitempty"`
50-
PreInstructions []plan.Instruction `json:"preInstructions,omitempty"`
51-
PostInstructions []plan.Instruction `json:"postInstructions,omitempty"`
49+
RancherValues map[string]interface{} `json:"rancherValues,omitempty"`
50+
PreInstructions []applyinator.Instruction `json:"preInstructions,omitempty"`
51+
PostInstructions []applyinator.Instruction `json:"postInstructions,omitempty"`
5252
// Deprecated, use Resources instead
5353
BootstrapResources []v1.GenericMap `json:"bootstrapResources,omitempty"`
5454
Resources []v1.GenericMap `json:"resources,omitempty"`
@@ -111,7 +111,11 @@ func Load(path string) (result Config, err error) {
111111
}
112112

113113
err = convert.ToObj(values, &result)
114-
return
114+
if err != nil {
115+
return
116+
}
117+
118+
return processRemote(result)
115119
}
116120

117121
func populatedSystemResources(config *Config) error {

pkg/discovery/discovery.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ var (
4141

4242
func DiscoverServerAndRole(ctx context.Context, cfg *config.Config) error {
4343
if cfg.Discovery == nil {
44-
if cfg.Server == "" && cfg.Role == "server" && cfg.Token == "" {
44+
if cfg.Server == "" && cfg.Role == "server" {
4545
cfg.Role = "cluster-init"
4646
}
4747
return nil
4848
}
4949

5050
if cfg.Token == "" {
51-
return fmt.Errorf("token is required to be set")
51+
return fmt.Errorf("token is required to be set when discovery is set")
5252
}
5353

5454
server, clusterInit, err := discoverServerAndRole(ctx, cfg)

pkg/join/join.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ToInstruction(config *config.Config, dataDir string) (*applyinator.Instruct
4242
return nil, fmt.Errorf("invalid role (%s) defined", config.Role)
4343
}
4444

45-
_, caChecksum, err := cacerts.CACerts(config.Server, config.Token)
45+
_, caChecksum, err := cacerts.CACerts(config.Server, config.Token, true)
4646
if err != nil {
4747
return nil, err
4848
}

pkg/os/os.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package os
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/rancher/rancherd/pkg/kubectl"
9+
"github.com/rancher/rancherd/pkg/self"
10+
"github.com/rancher/system-agent/pkg/applyinator"
11+
)
12+
13+
func ToUpgradeInstruction(k8sVersion, rancherOSVersion string) (*applyinator.Instruction, error) {
14+
cmd, err := self.Self()
15+
if err != nil {
16+
return nil, fmt.Errorf("resolving location of %s: %w", os.Args[0], err)
17+
}
18+
patch, err := json.Marshal(map[string]interface{}{
19+
"spec": map[string]interface{}{
20+
"osImage": rancherOSVersion,
21+
},
22+
})
23+
if err != nil {
24+
return nil, err
25+
}
26+
return &applyinator.Instruction{
27+
Name: "patch-rancher-os-version",
28+
SaveOutput: true,
29+
Args: []string{"retry", kubectl.Command(k8sVersion), "--type=merge", "-n", "fleet-local", "patch", "managedosimages.rancheros.cattle.io", "default-os-image", "-p", string(patch)},
30+
Env: kubectl.Env(k8sVersion),
31+
Command: cmd,
32+
}, nil
33+
}

pkg/plan/bootstrap.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/rancher/rancherd/pkg/config"
88
"github.com/rancher/rancherd/pkg/discovery"
99
"github.com/rancher/rancherd/pkg/join"
10+
"github.com/rancher/rancherd/pkg/kubectl"
1011
"github.com/rancher/rancherd/pkg/probe"
1112
"github.com/rancher/rancherd/pkg/rancher"
1213
"github.com/rancher/rancherd/pkg/registry"
@@ -68,6 +69,8 @@ func toJoinPlan(cfg *config.Config, dataDir string) (*applyinator.Plan, error) {
6869
if err := plan.addProbesForRoles(cfg); err != nil {
6970
return nil, err
7071
}
72+
73+
plan.addPrePostInstructions(cfg, "")
7174
return (*applyinator.Plan)(&plan), nil
7275
}
7376

@@ -124,7 +127,33 @@ func (p *plan) addInstructions(cfg *config.Config, dataDir string) error {
124127
return err
125128
}
126129

127-
return p.addInstruction(runtime.ToWaitKubernetesInstruction(cfg.RuntimeInstallerImage, cfg.SystemDefaultRegistry, k8sVersion))
130+
if err := p.addInstruction(runtime.ToWaitKubernetesInstruction(cfg.RuntimeInstallerImage, cfg.SystemDefaultRegistry, k8sVersion)); err != nil {
131+
return err
132+
}
133+
134+
p.addPrePostInstructions(cfg, k8sVersion)
135+
return nil
136+
}
137+
138+
func (p *plan) addPrePostInstructions(cfg *config.Config, k8sVersion string) {
139+
var instructions []applyinator.Instruction
140+
141+
for _, inst := range cfg.PreInstructions {
142+
if k8sVersion != "" {
143+
inst.Env = append(inst.Env, kubectl.Env(k8sVersion)...)
144+
}
145+
instructions = append(instructions, inst)
146+
}
147+
148+
instructions = append(instructions, p.Instructions...)
149+
150+
for _, inst := range cfg.PostInstructions {
151+
inst.Env = append(inst.Env, kubectl.Env(k8sVersion)...)
152+
instructions = append(instructions, inst)
153+
}
154+
155+
p.Instructions = instructions
156+
return
128157
}
129158

130159
func (p *plan) addInstruction(instruction *applyinator.Instruction, err error) error {

pkg/plan/upgrade.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package plan
22

33
import (
44
"github.com/rancher/rancherd/pkg/config"
5+
"github.com/rancher/rancherd/pkg/os"
56
"github.com/rancher/rancherd/pkg/rancher"
67
"github.com/rancher/rancherd/pkg/runtime"
78
"github.com/rancher/system-agent/pkg/applyinator"
89
)
910

10-
func Upgrade(cfg *config.Config, k8sVersion, rancherVersion, dataDir string) (*applyinator.Plan, error) {
11+
func Upgrade(cfg *config.Config, k8sVersion, rancherVersion, rancherOSVersion, dataDir string) (*applyinator.Plan, error) {
1112
p := plan{}
1213

1314
if rancherVersion != "" {
@@ -28,5 +29,11 @@ func Upgrade(cfg *config.Config, k8sVersion, rancherVersion, dataDir string) (*a
2829
}
2930
}
3031

32+
if rancherOSVersion != "" {
33+
if err := p.addInstruction(os.ToUpgradeInstruction(k8sVersion, rancherOSVersion)); err != nil {
34+
return nil, err
35+
}
36+
}
37+
3138
return (*applyinator.Plan)(&p), nil
3239
}

pkg/rancherd/rancher.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Config struct {
3131
type UpgradeConfig struct {
3232
RancherVersion string
3333
KubernetesVersion string
34+
RancherOSVersion string
3435
Force bool
3536
}
3637

@@ -45,10 +46,13 @@ func New(cfg Config) *Rancherd {
4546
}
4647

4748
func (r *Rancherd) Info(ctx context.Context) error {
48-
rancherVersion, k8sVersion := r.getExistingVersions(ctx)
49+
rancherVersion, k8sVersion, rancherOSVersion := r.getExistingVersions(ctx)
4950

5051
fmt.Printf(" Rancher: %s\n", rancherVersion)
5152
fmt.Printf(" Kubernetes: %s\n", k8sVersion)
53+
if rancherOSVersion != "" {
54+
fmt.Printf(" RancherOS: %s\n", rancherOSVersion)
55+
}
5256
fmt.Printf(" Rancherd: %s\n\n", version.FriendlyVersion())
5357
return nil
5458
}
@@ -69,10 +73,20 @@ func (r *Rancherd) Upgrade(ctx context.Context, upgradeConfig UpgradeConfig) err
6973
return err
7074
}
7175

72-
existingRancherVersion, existingK8sVersion := r.getExistingVersions(ctx)
73-
if existingRancherVersion == rancherVersion && existingK8sVersion == k8sVersion {
76+
rancherOSVersion, err := versions.RancherOSVersion(upgradeConfig.RancherOSVersion)
77+
if err != nil {
78+
return err
79+
}
80+
81+
existingRancherVersion, existingK8sVersion, existingRancherOSVersion := r.getExistingVersions(ctx)
82+
if existingRancherVersion == rancherVersion &&
83+
existingK8sVersion == k8sVersion &&
84+
(existingRancherOSVersion == "" || existingRancherOSVersion == rancherOSVersion) {
7485
fmt.Printf("\nNothing to upgrade:\n\n")
7586
fmt.Printf(" Rancher: %s\n", rancherVersion)
87+
if existingRancherOSVersion != "" {
88+
fmt.Printf(" RancherOS: %s\n", rancherOSVersion)
89+
}
7690
fmt.Printf(" Kubernetes: %s\n\n", k8sVersion)
7791
return nil
7892
}
@@ -83,6 +97,9 @@ func (r *Rancherd) Upgrade(ctx context.Context, upgradeConfig UpgradeConfig) err
8397
if existingK8sVersion == k8sVersion {
8498
k8sVersion = ""
8599
}
100+
if existingRancherOSVersion == "" || existingRancherOSVersion == rancherOSVersion {
101+
rancherOSVersion = ""
102+
}
86103

87104
if k8sVersion != "" && existingK8sVersion != "" {
88105
existingRuntime := config.GetRuntime(existingK8sVersion)
@@ -100,6 +117,9 @@ func (r *Rancherd) Upgrade(ctx context.Context, upgradeConfig UpgradeConfig) err
100117
if k8sVersion != "" {
101118
fmt.Printf(" Kubernetes: %s => %s\n", existingK8sVersion, k8sVersion)
102119
}
120+
if rancherOSVersion != "" {
121+
fmt.Printf(" RancherOS: %s => %s\n", existingRancherOSVersion, rancherOSVersion)
122+
}
103123

104124
if !r.cfg.Force {
105125
go func() {
@@ -114,7 +134,7 @@ func (r *Rancherd) Upgrade(ctx context.Context, upgradeConfig UpgradeConfig) err
114134
}
115135
}
116136

117-
nodePlan, err := plan.Upgrade(&cfg, k8sVersion, rancherVersion, DefaultDataDir)
137+
nodePlan, err := plan.Upgrade(&cfg, k8sVersion, rancherVersion, rancherOSVersion, DefaultDataDir)
118138
if err != nil {
119139
return err
120140
}

0 commit comments

Comments
 (0)