forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
systemimage.go
97 lines (81 loc) · 2.36 KB
/
systemimage.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
package systemimage
import (
"encoding/json"
"fmt"
"github.com/rancher/rancher/pkg/project"
"github.com/rancher/types/apis/apps/v1beta2"
"github.com/rancher/types/apis/management.cattle.io/v3"
"k8s.io/apimachinery/pkg/labels"
)
var systemProjectLabels = labels.Set(map[string]string{"authz.management.cattle.io/system-project": "true"})
type Syncer struct {
clusterName string
daemonsets v1beta2.DaemonSetInterface
daemonsetLister v1beta2.DaemonSetLister
deployments v1beta2.DeploymentInterface
deploymentLister v1beta2.DeploymentLister
projectLister v3.ProjectLister
projects v3.ProjectInterface
}
func (s *Syncer) Sync(key string, obj *v3.Project) error {
if obj == nil || obj.DeletionTimestamp != nil {
return nil
}
projects, err := s.projectLister.List(s.clusterName, systemProjectLabels.AsSelector())
if err != nil {
return fmt.Errorf("get projects failed when try to upgrade system tools, %v", err)
}
var systemProject *v3.Project
for _, v := range projects {
if v.Spec.DisplayName == project.System {
systemProject = v.DeepCopy()
}
}
if systemProject == nil {
return nil
}
versionMap := make(map[string]string)
curSysImageVersion := systemProject.Annotations[project.SystemImageVersionAnn]
if curSysImageVersion != "" {
if err = json.Unmarshal([]byte(curSysImageVersion), &versionMap); err != nil {
return fmt.Errorf("unmashal current system service version failed, %v", err)
}
}
changed := false
for k, v := range systemServices {
oldVersion := versionMap[k]
newVersion, err := v.Upgrade(oldVersion)
if err != nil {
return err
}
if oldVersion != newVersion {
changed = true
versionMap[k] = newVersion
}
}
if !changed {
return nil
}
newVersion, err := json.Marshal(versionMap)
if err != nil {
return fmt.Errorf("marshal new system service version %v failed, %v", versionMap, err)
}
systemProject.Annotations[project.SystemImageVersionAnn] = string(newVersion)
_, err = s.projects.Update(systemProject)
return err
}
func GetSystemImageVersion() (string, error) {
versionMap := make(map[string]string)
for k, v := range systemServices {
version, err := v.Version()
if err != nil {
return "", err
}
versionMap[k] = version
}
b, err := json.Marshal(versionMap)
if err != nil {
return "", fmt.Errorf("marshal toolsSystemImages failed: %v", err)
}
return string(b), nil
}