forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testerdeployer.go
121 lines (99 loc) · 3.92 KB
/
testerdeployer.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
package deployer
import (
loggingconfig "github.com/rancher/rancher/pkg/controllers/user/logging/config"
"github.com/rancher/rancher/pkg/controllers/user/logging/generator"
"github.com/rancher/rancher/pkg/project"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rancher/pkg/systemaccount"
"github.com/rancher/types/apis/core/v1"
mgmtv3 "github.com/rancher/types/apis/management.cattle.io/v3"
projectv3 "github.com/rancher/types/apis/project.cattle.io/v3"
"github.com/pkg/errors"
"github.com/rancher/rancher/pkg/namespace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
type TesterDeployer struct {
projectLister mgmtv3.ProjectLister
templateLister mgmtv3.CatalogTemplateLister
projectLoggingLister mgmtv3.ProjectLoggingLister
namespacesLister v1.NamespaceLister
appDeployer *AppDeployer
systemAccountManager *systemaccount.Manager
}
func NewTesterDeployer(appsGetter projectv3.AppsGetter, projectLister mgmtv3.ProjectLister, pods v1.PodInterface, projectLoggingLister mgmtv3.ProjectLoggingLister, namespaces v1.NamespaceInterface, templateLister mgmtv3.CatalogTemplateLister) *TesterDeployer {
appDeployer := &AppDeployer{
AppsGetter: appsGetter,
Namespaces: namespaces,
Pods: pods,
}
return &TesterDeployer{
projectLister: projectLister,
templateLister: templateLister,
projectLoggingLister: projectLoggingLister,
namespacesLister: namespaces.Controller().Lister(),
appDeployer: appDeployer,
}
}
func (d *TesterDeployer) Deploy(level, clusterName, projectID string, loggingTarget mgmtv3.LoggingTargets) error {
systemProject, err := project.GetSystemProject(clusterName, d.projectLister)
if err != nil {
return err
}
systemProjectID := ref.Ref(systemProject)
creator, err := d.systemAccountManager.GetProjectSystemUser(systemProject.Name)
if err != nil {
return err
}
if err = d.deployLoggingTester(systemProjectID, creator.Name, level, clusterName, projectID, loggingTarget); err != nil {
return err
}
return d.isLoggingTesterDeploySuccess()
}
func (d *TesterDeployer) deployLoggingTester(systemProjectID, appCreator, level, clusterName, projectID string, loggingTarget mgmtv3.LoggingTargets) error {
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)
var clusterSecret, projectSecret string
if level == loggingconfig.ClusterLevel {
spec := mgmtv3.ClusterLoggingSpec{
LoggingTargets: loggingTarget,
ClusterName: clusterName,
}
buf, err := generator.GenerateClusterConfig(spec, "", loggingconfig.DefaultCertDir)
if err != nil {
return err
}
clusterSecret = string(buf)
} else if level == loggingconfig.ProjectLevel {
cur, err := d.projectLoggingLister.List(metav1.NamespaceAll, labels.NewSelector())
if err != nil {
return errors.Wrap(err, "list project logging failed")
}
namespaces, err := d.namespacesLister.List(metav1.NamespaceAll, labels.NewSelector())
if err != nil {
return errors.New("list namespace failed")
}
new := &mgmtv3.ProjectLogging{
Spec: mgmtv3.ProjectLoggingSpec{
LoggingTargets: loggingTarget,
ProjectName: projectID,
},
}
loggings := append(cur, new)
buf, err := generator.GenerateProjectConfig(loggings, namespaces, systemProjectID, loggingconfig.DefaultCertDir)
if err != nil {
return err
}
projectSecret = string(buf)
}
app := loggingTesterApp(appCreator, systemProjectID, catalogID, clusterSecret, projectSecret)
return d.appDeployer.deploy(app)
}
func (d *TesterDeployer) isLoggingTesterDeploySuccess() error {
namespace := loggingconfig.LoggingNamespace
return d.appDeployer.isDeploySuccess(namespace, loggingconfig.FluentdTesterSelector)
}