-
Notifications
You must be signed in to change notification settings - Fork 492
/
controller_upgrade.go
123 lines (102 loc) · 3.07 KB
/
controller_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
116
117
118
119
120
121
122
123
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"context"
"github.com/juju/errors"
"github.com/juju/version/v2"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/client-go/kubernetes"
"github.com/juju/juju/caas/kubernetes/provider/resources"
providerutils "github.com/juju/juju/caas/kubernetes/provider/utils"
"github.com/juju/juju/environs/bootstrap"
)
type upgradeCAASControllerBridge struct {
clientFn func() kubernetes.Interface
isLegacyFn func() bool
namespaceFn func() string
}
// UpgradeCAASControllerBroker describes the interface needed for upgrading
// Juju Kubernetes controllers
type UpgradeCAASControllerBroker interface {
// Client returns a Kubernetes client associated with the current broker's
// cluster
Client() kubernetes.Interface
// IsLegacyLabels indicates if this provider is operating on a legacy label schema
IsLegacyLabels() bool
// Namespace returns the targeted Kubernetes namespace for this broker
Namespace() string
}
func (u *upgradeCAASControllerBridge) Client() kubernetes.Interface {
return u.clientFn()
}
func (u *upgradeCAASControllerBridge) IsLegacyLabels() bool {
return u.isLegacyFn()
}
func (u *upgradeCAASControllerBridge) Namespace() string {
return u.namespaceFn()
}
func controllerUpgrade(appName string, vers version.Number, broker UpgradeCAASControllerBroker) error {
return upgradeOperatorOrControllerStatefulSet(
appName,
appName,
false,
"",
"",
vers,
broker.IsLegacyLabels(),
broker.Client().AppsV1().StatefulSets(broker.Namespace()))
}
func (k *kubernetesClient) upgradeController(vers version.Number) error {
broker := &upgradeCAASControllerBridge{
clientFn: k.client,
namespaceFn: k.GetCurrentNamespace,
isLegacyFn: k.IsLegacyLabels,
}
return controllerUpgrade(bootstrap.ControllerModelName, vers, broker)
}
// InClusterCredentialUpgrade implements upgrades.upgradeKubernetesClusterCredential
// used in the Juju 2.9.6 upgrade step
func (k *kubernetesClient) InClusterCredentialUpgrade() error {
return inClusterCredentialUpgrade(
k.client(),
k.IsLegacyLabels(),
k.GetCurrentNamespace(),
)
}
func inClusterCredentialUpgrade(
client kubernetes.Interface,
legacyLabels bool,
namespace string,
) error {
ctx := context.TODO()
labels := providerutils.LabelsForApp("controller", legacyLabels)
saName, cleanUps, err := ensureControllerServiceAccount(
ctx,
client,
namespace,
labels,
map[string]string{},
)
runCleanups := func() {
for _, v := range cleanUps {
v()
}
}
if err != nil {
runCleanups()
return errors.Trace(err)
}
ss := resources.NewStatefulSet("controller", namespace, &appsv1.StatefulSet{})
if err := ss.Get(ctx, client); err != nil {
runCleanups()
return errors.Annotate(err, "updating controller for in cluster credentials")
}
ss.Spec.Template.Spec.ServiceAccountName = saName
ss.Spec.Template.Spec.AutomountServiceAccountToken = boolPtr(true)
if err := ss.Apply(ctx, client); err != nil {
runCleanups()
return errors.Annotate(err, "updating controller for in cluster credentials")
}
return nil
}