forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
upgradeimpl.go
149 lines (125 loc) · 5.29 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
package deployer
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/rancher/rancher/pkg/controllers/user/helm/common"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/rancher/pkg/controllers/user/systemimage"
"github.com/rancher/rancher/pkg/project"
"github.com/rancher/rancher/pkg/settings"
appsv1beta2 "github.com/rancher/types/apis/apps/v1beta2"
v1 "github.com/rancher/types/apis/core/v1"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/namespace"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
var (
serviceName = "logging"
)
type loggingService struct {
clusterName string
projectLister v3.ProjectLister
templateLister v3.CatalogTemplateLister
daemonsets appsv1beta2.DaemonSetInterface
secrets v1.SecretInterface
appDeployer *AppDeployer
}
func init() {
systemimage.RegisterSystemService(serviceName, &loggingService{})
}
func (l *loggingService) Init(ctx context.Context, cluster *config.UserContext) {
ad := &AppDeployer{
AppsGetter: cluster.Management.Project,
Namespaces: cluster.Core.Namespaces(metav1.NamespaceAll),
}
l.clusterName = cluster.ClusterName
l.projectLister = cluster.Management.Management.Projects(metav1.NamespaceAll).Controller().Lister()
l.templateLister = cluster.Management.Management.CatalogTemplates(metav1.NamespaceAll).Controller().Lister()
l.daemonsets = cluster.Apps.DaemonSets(loggingconfig.LoggingNamespace)
l.secrets = cluster.Core.Secrets(loggingconfig.LoggingNamespace)
l.appDeployer = ad
}
func (l *loggingService) Version() (string, error) {
catalogID := settings.SystemLoggingCatalogID.Get()
templateVersionID, _, err := common.ParseExternalID(catalogID)
if err != nil {
return "", fmt.Errorf("get system logging catalog version failed, %v", err)
}
return templateVersionID, nil
}
func (l *loggingService) Upgrade(currentVersion string) (string, error) {
appName := loggingconfig.AppName
templateID := loggingconfig.RancherLoggingTemplateID()
template, err := l.templateLister.Get(namespace.GlobalNamespace, templateID)
if err != nil {
return "", errors.Wrapf(err, "get template %s failed", templateID)
}
newFullVersion := fmt.Sprintf("%s-%s", templateID, template.Spec.DefaultVersion)
if currentVersion == newFullVersion {
return currentVersion, nil
}
//clean old version
if !strings.Contains(currentVersion, templateID) {
if err = l.removeLegacy(); err != nil {
return "", err
}
}
//upgrade old app
newVersion := template.Spec.DefaultVersion
newCatalogID := loggingconfig.RancherLoggingCatalogID(newVersion)
defaultSystemProjects, err := l.projectLister.List(metav1.NamespaceAll, labels.Set(project.SystemProjectLabel).AsSelector())
if err != nil {
return "", errors.Wrap(err, "list system project failed")
}
if len(defaultSystemProjects) == 0 {
return "", errors.New("get system project failed")
}
systemProject := defaultSystemProjects[0]
if systemProject == nil {
return "", errors.New("get system project failed")
}
app, err := l.appDeployer.AppsGetter.Apps(metav1.NamespaceAll).GetNamespaced(systemProject.Name, appName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return newFullVersion, nil
}
return "", errors.Wrapf(err, "get app %s:%s failed", systemProject.Name, appName)
}
newApp := app.DeepCopy()
newApp.Spec.ExternalID = newCatalogID
if !reflect.DeepEqual(newApp, app) {
if _, err = l.appDeployer.AppsGetter.Apps(metav1.NamespaceAll).Update(newApp); err != nil {
return "", errors.Wrapf(err, "update app %s:%s failed", app.Namespace, app.Name)
}
}
return newFullVersion, nil
}
func (l *loggingService) removeLegacy() error {
op := metav1.DeletePropagationBackground
errMsg := "failed to remove legacy logging %s %s:%s when upgrade"
if err := l.daemonsets.Delete(loggingconfig.FluentdName, &metav1.DeleteOptions{PropagationPolicy: &op}); err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, errMsg, loggingconfig.LoggingNamespace, "daemonset", loggingconfig.FluentdName)
}
if err := l.daemonsets.Delete(loggingconfig.LogAggregatorName, &metav1.DeleteOptions{PropagationPolicy: &op}); err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, errMsg, loggingconfig.LoggingNamespace, "daemonset", loggingconfig.LogAggregatorName)
}
legacySSlConfigName := "sslconfig"
legacyClusterConfigName := "cluster-logging"
legacyProjectConfigName := "project-logging"
if err := l.secrets.Delete(legacySSlConfigName, &metav1.DeleteOptions{PropagationPolicy: &op}); err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, errMsg, "serect", loggingconfig.LoggingNamespace, legacySSlConfigName)
}
if err := l.secrets.Delete(legacyClusterConfigName, &metav1.DeleteOptions{PropagationPolicy: &op}); err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, errMsg, "serect", loggingconfig.LoggingNamespace, legacyClusterConfigName)
}
if err := l.secrets.Delete(legacyProjectConfigName, &metav1.DeleteOptions{PropagationPolicy: &op}); err != nil && !apierrors.IsNotFound(err) {
return errors.Wrapf(err, errMsg, "serect", loggingconfig.LoggingNamespace, legacyProjectConfigName)
}
return nil
}