-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgradeimpl.go
156 lines (136 loc) · 4.49 KB
/
upgradeimpl.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
154
155
156
package upgrade
import (
"context"
"fmt"
"strings"
"github.com/rancher/rancher/pkg/controllers/user/pipeline/controller/pipelineexecution"
"github.com/rancher/rancher/pkg/controllers/user/systemimage"
"github.com/rancher/rancher/pkg/pipeline/utils"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rancher/pkg/systemaccount"
rv1beta2 "github.com/rancher/types/apis/apps/v1beta2"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/config"
"k8s.io/api/apps/v1beta2"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
)
var (
serviceName = "pipeline"
)
type pipelineService struct {
deployments rv1beta2.DeploymentInterface
namespaceLister v1.NamespaceLister
secrets v1.SecretInterface
systemAccountManager *systemaccount.Manager
}
func init() {
systemimage.RegisterSystemService(serviceName, &pipelineService{})
}
func (l *pipelineService) Init(ctx context.Context, cluster *config.UserContext) {
l.deployments = cluster.Apps.Deployments("")
l.namespaceLister = cluster.Core.Namespaces("").Controller().Lister()
l.secrets = cluster.Core.Secrets("")
l.systemAccountManager = systemaccount.NewManager(cluster.Management)
}
func (l *pipelineService) Version() (string, error) {
d1, d2, d3 := getDeployments()
newJekinsVersion, err := systemimage.DefaultGetVersion(d1)
if err != nil {
return "", err
}
newRegistryVersion, err := systemimage.DefaultGetVersion(d2)
if err != nil {
return "", err
}
newMinioVersion, err := systemimage.DefaultGetVersion(d3)
if err != nil {
return "", err
}
return fmt.Sprintf("%s-%s-%s", newJekinsVersion, newRegistryVersion, newMinioVersion), nil
}
func (l *pipelineService) Upgrade(currentVersion string) (newVersion string, err error) {
var jekinsVersion, registryVersion, minioVersion, newJekinsVersion, newRegistryVersion, newMinioVersion string
if currentVersion != "" {
versions := strings.Split(currentVersion, "-")
if len(versions) < 3 {
return "", fmt.Errorf("invalid system service %s version %s", serviceName, currentVersion)
}
jekinsVersion = versions[0]
registryVersion = versions[1]
minioVersion = versions[2]
}
d1, d2, d3 := getDeployments()
newJekinsVersion, err = systemimage.DefaultGetVersion(d1)
if err != nil {
return "", err
}
newRegistryVersion, err = systemimage.DefaultGetVersion(d2)
if err != nil {
return "", err
}
newMinioVersion, err = systemimage.DefaultGetVersion(d3)
if err != nil {
return "", err
}
set := labels.Set(map[string]string{utils.PipelineNamespaceLabel: "true"})
pipelineNamespaces, err := l.namespaceLister.List("", set.AsSelector())
if err != nil {
return "", fmt.Errorf("list namespaces failed, %v", err)
}
for _, v := range pipelineNamespaces {
ns := v.Name
deployment := pipelineexecution.GetJenkinsDeployment(ns)
if jekinsVersion != newJekinsVersion {
if err = l.upgradeDeployment(deployment); err != nil {
return "", err
}
}
deployment = pipelineexecution.GetRegistryDeployment(ns)
if registryVersion != newRegistryVersion {
if err = l.upgradeDeployment(deployment); err != nil {
return "", err
}
}
deployment = pipelineexecution.GetMinioDeployment(ns)
if minioVersion != newMinioVersion {
if err = l.upgradeDeployment(deployment); err != nil {
return "", err
}
}
if err := l.ensureSecrets(v); err != nil {
return "", err
}
}
return fmt.Sprintf("%s-%s-%s", newJekinsVersion, newRegistryVersion, newMinioVersion), nil
}
func (l *pipelineService) ensureSecrets(namespace *corev1.Namespace) error {
projectName := namespace.Annotations["field.cattle.io/projectId"]
_, projectID := ref.Parse(projectName)
ns := namespace.Name
apikey, err := l.systemAccountManager.GetOrCreateProjectSystemToken(projectID)
if err != nil {
return err
}
secret := pipelineexecution.GetAPIKeySecret(ns, apikey)
if _, err := l.secrets.Create(secret); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
func (l *pipelineService) upgradeDeployment(deployment *v1beta2.Deployment) error {
if _, err := l.deployments.Update(deployment); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return fmt.Errorf("upgrade system service %s:%s failed, %v", deployment.Namespace, deployment.Name, err)
}
return nil
}
func getDeployments() (d1, d2, d3 *v1beta2.Deployment) {
d1 = pipelineexecution.GetJenkinsDeployment("")
d2 = pipelineexecution.GetRegistryDeployment("")
d3 = pipelineexecution.GetMinioDeployment("")
return d1, d2, d3
}