forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
159 lines (137 loc) · 6.48 KB
/
logging.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
package openshift
import (
"bytes"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kapi "k8s.io/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/oc/bootstrap/docker/errors"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
)
const (
loggingNamespace = "logging"
svcKibana = "kibana-logging"
loggingDeployerAccountTemplate = "logging-deployer-account-template"
loggingDeployerTemplate = "logging-deployer-template"
loggingPlaybook = "playbooks/byo/openshift-cluster/openshift-logging.yml"
)
// InstallLoggingViaAnsible checks whether logging is installed and installs it if not already installed
func (h *Helper) InstallLoggingViaAnsible(f *clientcmd.Factory, serverIP, publicHostname, loggerHost, imagePrefix, imageVersion, hostConfigDir, imageStreams string) error {
kubeClient, err := f.ClientSet()
if err != nil {
return errors.NewError("cannot obtain API clients").WithCause(err).WithDetails(h.OriginLog())
}
securityClient, err := f.OpenshiftInternalSecurityClient()
if err != nil {
return errors.NewError("cannot obtain API clients").WithCause(err).WithDetails(h.OriginLog())
}
_, err = kubeClient.Core().Namespaces().Get(loggingNamespace, metav1.GetOptions{})
if err == nil {
// If there's no error, the logging namespace already exists and we won't initialize it
return nil
}
// Create logging namespace
out := &bytes.Buffer{}
err = CreateProject(f, loggingNamespace, "", "", "oc", out)
if err != nil {
return errors.NewError("cannot create logging project").WithCause(err).WithDetails(out.String())
}
params := newAnsibleInventoryParams()
params.Template = defaultLoggingInventory
params.MasterIP = serverIP
params.MasterPublicURL = fmt.Sprintf("https://%s:8443", publicHostname)
params.OSERelease = imageVersion
params.LoggingImagePrefix = fmt.Sprintf("%s-", imagePrefix)
params.LoggingImageVersion = imageVersion
params.LoggingNamespace = loggingNamespace
params.KibanaHostName = loggerHost
runner := newAnsibleRunner(h, kubeClient, securityClient, loggingNamespace, imageStreams, "logging")
//run logging playbook
return runner.RunPlaybook(params, loggingPlaybook, hostConfigDir, imagePrefix, imageVersion)
}
// InstallLogging checks whether logging is installed and installs it if not already installed
func (h *Helper) InstallLogging(f *clientcmd.Factory, publicHostname, loggerHost, imagePrefix, imageVersion string) error {
kubeClient, err := f.ClientSet()
if err != nil {
return errors.NewError("cannot obtain API clients").WithCause(err).WithDetails(h.OriginLog())
}
authorizationClient, err := f.OpenshiftInternalAuthorizationClient()
if err != nil {
return errors.NewError("cannot obtain API clients").WithCause(err).WithDetails(h.OriginLog())
}
templateClient, err := f.OpenshiftInternalTemplateClient()
if err != nil {
return errors.NewError("cannot obtain API clients").WithCause(err).WithDetails(h.OriginLog())
}
securityClient, err := f.OpenshiftInternalSecurityClient()
if err != nil {
return errors.NewError("cannot obtain API clients").WithCause(err).WithDetails(h.OriginLog())
}
_, err = kubeClient.Core().Namespaces().Get(loggingNamespace, metav1.GetOptions{})
if err == nil {
// If there's no error, the logging namespace already exists and we won't initialize it
return nil
}
// Create logging namespace
out := &bytes.Buffer{}
err = CreateProject(f, loggingNamespace, "", "", "oc", out)
if err != nil {
return errors.NewError("cannot create logging project").WithCause(err).WithDetails(out.String())
}
// Instantiate logging deployer account template
err = instantiateTemplate(templateClient.Template(), clientcmd.ResourceMapper(f), nil, OpenshiftInfraNamespace, loggingDeployerAccountTemplate, loggingNamespace, nil, false)
if err != nil {
return errors.NewError("cannot instantiate logger accounts").WithCause(err)
}
// Add oauth-editor cluster role to logging-deployer sa
if err = AddClusterRole(authorizationClient.Authorization(), "oauth-editor", "system:serviceaccount:logging:logging-deployer"); err != nil {
return errors.NewError("cannot add oauth editor role to logging deployer service account").WithCause(err).WithDetails(h.OriginLog())
}
// Add cluster-reader cluster role to aggregated-logging-fluentd sa
if err = AddClusterRole(authorizationClient.Authorization(), "cluster-reader", "system:serviceaccount:logging:aggregated-logging-fluentd"); err != nil {
return errors.NewError("cannot cluster reader role to logging fluentd service account").WithCause(err).WithDetails(h.OriginLog())
}
// Add privileged SCC to aggregated-logging-fluentd sa
if err = AddSCCToServiceAccount(securityClient.Security(), "privileged", "aggregated-logging-fluentd", loggingNamespace, out); err != nil {
return errors.NewError("cannot add privileged security context constraint to logging fluentd service account").WithCause(err).WithDetails(h.OriginLog())
}
// Label all nodes with default fluentd label
nodeList, err := kubeClient.Core().Nodes().List(metav1.ListOptions{})
if err != nil {
return errors.NewError("cannot retrieve nodes").WithCause(err).WithDetails(h.OriginLog())
}
// Iterate through all nodes (there should only be one)
for _, node := range nodeList.Items {
node.Labels["logging-infra-fluentd"] = "true"
if _, err = kubeClient.Core().Nodes().Update(&node); err != nil {
return errors.NewError("cannot update labels on node %s", node.Name).WithCause(err)
}
}
// Create ConfigMap with deployment values
loggingConfig := &kapi.ConfigMap{}
loggingConfig.Name = "logging-deployer"
loggingConfig.Data = map[string]string{
"kibana-hostname": loggerHost,
"public-master-url": fmt.Sprintf("https://%s:8443", publicHostname),
"es-cluster-size": "1",
"es-instance-ram": "1024M",
"es-pvc-size": "100G",
}
kubeClient.Core().ConfigMaps(loggingNamespace).Create(loggingConfig)
// Instantiate logging deployer
deployerParams := map[string]string{
"IMAGE_VERSION": imageVersion,
"IMAGE_PREFIX": fmt.Sprintf("%s-", imagePrefix),
"MODE": "install",
}
err = instantiateTemplate(templateClient.Template(), clientcmd.ResourceMapper(f), nil, OpenshiftInfraNamespace, loggingDeployerTemplate, loggingNamespace, deployerParams, false)
if err != nil {
return errors.NewError("cannot instantiate logging deployer").WithCause(err)
}
return nil
}
func LoggingHost(routingSuffix, serverIP string) string {
if len(routingSuffix) > 0 {
return fmt.Sprintf("kibana-logging.%s", routingSuffix)
}
return fmt.Sprintf("kibana-logging.%s.nip.io", serverIP)
}