forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.go
183 lines (148 loc) · 5.46 KB
/
deployer.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package deployer
import (
"github.com/rancher/norman/controller"
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/rancher/pkg/controllers/user/logging/configsyncer"
"github.com/rancher/rancher/pkg/controllers/user/logging/utils"
"github.com/rancher/rancher/pkg/project"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rancher/pkg/systemaccount"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"golang.org/x/sync/errgroup"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/namespace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
type Deployer struct {
clusterName string
clusterLister mgmtv3.ClusterLister
clusterLoggingLister mgmtv3.ClusterLoggingLister
projectLoggingLister mgmtv3.ProjectLoggingLister
projectLister mgmtv3.ProjectLister
templateLister mgmtv3.CatalogTemplateLister
appDeployer *AppDeployer
systemAccountManager *systemaccount.Manager
}
func NewDeployer(cluster *config.UserContext, secretSyncer *configsyncer.SecretManager) *Deployer {
clusterName := cluster.ClusterName
appsgetter := cluster.Management.Project
appDeployer := &AppDeployer{
AppsGetter: appsgetter,
Namespaces: cluster.Core.Namespaces(metav1.NamespaceAll),
Pods: cluster.Core.Pods(metav1.NamespaceAll),
}
return &Deployer{
clusterName: clusterName,
clusterLister: cluster.Management.Management.Clusters(metav1.NamespaceAll).Controller().Lister(),
clusterLoggingLister: cluster.Management.Management.ClusterLoggings(clusterName).Controller().Lister(),
projectLoggingLister: cluster.Management.Management.ProjectLoggings(metav1.NamespaceAll).Controller().Lister(),
projectLister: cluster.Management.Management.Projects(metav1.NamespaceAll).Controller().Lister(),
templateLister: cluster.Management.Management.CatalogTemplates(metav1.NamespaceAll).Controller().Lister(),
appDeployer: appDeployer,
systemAccountManager: systemaccount.NewManager(cluster.Management),
}
}
func (d *Deployer) ClusterLoggingSync(key string, obj *mgmtv3.ClusterLogging) (runtime.Object, error) {
return obj, d.sync()
}
func (d *Deployer) ProjectLoggingSync(key string, obj *mgmtv3.ProjectLogging) (runtime.Object, error) {
return obj, d.sync()
}
func (d *Deployer) sync() error {
appName := loggingconfig.AppName
namepspace := loggingconfig.LoggingNamespace
systemProject, err := project.GetSystemProject(d.clusterName, d.projectLister)
if err != nil {
return err
}
systemProjectID := ref.Ref(systemProject)
allDisabled, err := d.isAllLoggingDisable()
if err != nil {
return err
}
if allDisabled {
return d.appDeployer.cleanup(appName, namepspace, systemProjectID)
}
creator, err := d.systemAccountManager.GetProjectSystemUser(systemProject.Name)
if err != nil {
return err
}
return d.deploy(systemProjectID, creator.Name)
}
func (d *Deployer) deploy(systemProjectID, appCreator string) error {
if err := d.deployRancherLogging(systemProjectID, appCreator); err != nil {
return err
}
return d.isRancherLoggingDeploySuccess()
}
func (d *Deployer) deployRancherLogging(systemProjectID, appCreator string) error {
cluster, err := d.clusterLister.Get("", d.clusterName)
if err != nil {
return errors.Wrapf(err, "get dockerRootDir from cluster %s failed", d.clusterName)
}
driverDir := getDriverDir(cluster.Status.Driver)
templateVersionID := loggingconfig.RancherLoggingTemplateID()
template, err := d.templateLister.Get(namespace.GlobalNamespace, templateVersionID)
if err != nil {
return errors.Wrapf(err, "failed to find template by ID %s", templateVersionID)
}
catalogID := loggingconfig.RancherLoggingCatalogID(template.Spec.DefaultVersion)
app := rancherLoggingApp(appCreator, systemProjectID, catalogID, driverDir)
return d.appDeployer.deploy(app)
}
func (d *Deployer) isRancherLoggingDeploySuccess() error {
namespace := loggingconfig.LoggingNamespace
var errgrp errgroup.Group
errgrp.Go(func() error {
return d.appDeployer.isDeploySuccess(namespace, loggingconfig.FluentdSelector)
})
errgrp.Go(func() error {
return d.appDeployer.isDeploySuccess(namespace, loggingconfig.LogAggregatorSelector)
})
return errgrp.Wait()
}
func (d *Deployer) isAllLoggingDisable() (bool, error) {
clusterLoggings, err := d.clusterLoggingLister.List("", labels.NewSelector())
if err != nil {
return false, err
}
allClusterProjectLoggings, err := d.projectLoggingLister.List("", labels.NewSelector())
if err != nil {
return false, err
}
var projectLoggings []*mgmtv3.ProjectLogging
for _, v := range allClusterProjectLoggings {
if controller.ObjectInCluster(d.clusterName, v) {
projectLoggings = append(projectLoggings, v)
}
}
if len(clusterLoggings) == 0 && len(projectLoggings) == 0 {
return true, nil
}
for _, v := range clusterLoggings {
wl := utils.NewLoggingTargetTestWrap(v.Spec.LoggingTargets)
if wl != nil {
return false, nil
}
}
for _, v := range projectLoggings {
wpl := utils.NewLoggingTargetTestWrap(v.Spec.LoggingTargets)
if wpl != nil {
return false, nil
}
}
return true, nil
}
func getDriverDir(driverName string) string {
switch driverName {
case mgmtv3.ClusterDriverRKE:
return "/var/lib/kubelet/volumeplugins"
case loggingconfig.GoogleKubernetesEngine:
return "/home/kubernetes/flexvolume"
default:
return "/usr/libexec/kubernetes/kubelet-plugins/volume/exec"
}
}