-
Notifications
You must be signed in to change notification settings - Fork 344
/
elasticsearch.go
180 lines (168 loc) · 6.24 KB
/
elasticsearch.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
package storage
import (
"fmt"
"strings"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/jaegertracing/jaeger-operator/pkg/apis/io/v1alpha1"
esv1alpha1 "github.com/jaegertracing/jaeger-operator/pkg/storage/elasticsearch/v1alpha1"
)
const (
// #nosec G101: Potential hardcoded credentials (Confidence: LOW, Severity: HIGH)
k8sTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
volumeName = "certs"
volumeMountPath = "/certs"
caPath = volumeMountPath + "/ca"
keyPath = volumeMountPath + "/key"
certPath = volumeMountPath + "/cert"
elasticsearchURL = "https://elasticsearch:9200"
)
// ShouldDeployElasticsearch determines whether a new instance of Elasticsearch should be deployed
func ShouldDeployElasticsearch(s v1alpha1.JaegerStorageSpec) bool {
if !strings.EqualFold(s.Type, "elasticsearch") {
return false
}
_, ok := s.Options.Map()["es.server-urls"]
return !ok
}
// ElasticsearchDeployment represents an ES deployment for Jaeger
type ElasticsearchDeployment struct {
Jaeger *v1alpha1.Jaeger
}
// InjectStorageConfiguration changes the given spec to include ES-related command line options
func (ed *ElasticsearchDeployment) InjectStorageConfiguration(p *v1.PodSpec) {
p.Volumes = append(p.Volumes, v1.Volume{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secretName(ed.Jaeger.Name, jaegerSecret.name),
},
},
})
// we assume jaeger containers are first
if len(p.Containers) > 0 {
// TODO add to archive storage if it is enabled?
p.Containers[0].Args = append(p.Containers[0].Args,
"--es.server-urls="+elasticsearchURL,
"--es.token-file="+k8sTokenFile,
"--es.tls.ca="+caPath)
if !containsPrefix("--es.num-shards", p.Containers[0].Args) {
// taken from https://github.com/openshift/cluster-logging-operator/blob/32b69e8bcf61a805e8f3c45c664a3c08d1ee62d5/vendor/github.com/openshift/elasticsearch-operator/pkg/k8shandler/configmaps.go#L38
// every ES node is a data node
p.Containers[0].Args = append(p.Containers[0].Args, fmt.Sprintf("--es.num-shards=%d", ed.Jaeger.Spec.Storage.Elasticsearch.NodeCount))
}
if !containsPrefix("--es.num-replicas", p.Containers[0].Args) {
p.Containers[0].Args = append(p.Containers[0].Args, fmt.Sprintf("--es.num-replicas=%d",
calculateReplicaShards(ed.Jaeger.Spec.Storage.Elasticsearch.RedundancyPolicy, int(ed.Jaeger.Spec.Storage.Elasticsearch.NodeCount))))
}
p.Containers[0].VolumeMounts = append(p.Containers[0].VolumeMounts, v1.VolumeMount{
Name: volumeName,
ReadOnly: true,
MountPath: volumeMountPath,
})
}
}
// InjectIndexCleanerConfiguration changes the given spec to include the options for the index cleaner
func (ed *ElasticsearchDeployment) InjectIndexCleanerConfiguration(p *v1.PodSpec) {
p.Volumes = append(p.Volumes, v1.Volume{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secretName(ed.Jaeger.Name, curatorSecret.name),
},
},
})
// we assume jaeger containers are first
if len(p.Containers) > 0 {
// the size of arguments array should be always 2
p.Containers[0].Args[1] = elasticsearchURL
p.Containers[0].Env = append(p.Containers[0].Env,
v1.EnvVar{Name: "ES_TLS", Value: "true"},
v1.EnvVar{Name: "ES_TLS_CA", Value: caPath},
v1.EnvVar{Name: "ES_TLS_KEY", Value: keyPath},
v1.EnvVar{Name: "ES_TLS_CERT", Value: certPath},
)
p.Containers[0].VolumeMounts = append(p.Containers[0].VolumeMounts, v1.VolumeMount{
Name: volumeName,
ReadOnly: true,
MountPath: volumeMountPath,
})
}
}
// Elasticsearch returns an ES CR for the deployment
func (ed *ElasticsearchDeployment) Elasticsearch() *esv1alpha1.Elasticsearch {
return &esv1alpha1.Elasticsearch{
ObjectMeta: metav1.ObjectMeta{
Namespace: ed.Jaeger.Namespace,
Name: esSecret.name,
Labels: map[string]string{
"app": "jaeger",
"app.kubernetes.io/name": esSecret.name,
"app.kubernetes.io/instance": ed.Jaeger.Name,
"app.kubernetes.io/component": "elasticsearch",
"app.kubernetes.io/part-of": "jaeger",
// we cannot use jaeger-operator because our component controllers removes all objects
// created by ES operator
//"app.kubernetes.io/managed-by": "jaeger-operator",
},
OwnerReferences: []metav1.OwnerReference{asOwner(ed.Jaeger)},
},
Spec: esv1alpha1.ElasticsearchSpec{
ManagementState: esv1alpha1.ManagementStateManaged,
RedundancyPolicy: ed.Jaeger.Spec.Storage.Elasticsearch.RedundancyPolicy,
Spec: esv1alpha1.ElasticsearchNodeSpec{
Resources: ed.Jaeger.Spec.Storage.Elasticsearch.Resources,
},
Nodes: getNodes(ed.Jaeger.Spec.Storage.Elasticsearch),
},
}
}
func getNodes(es v1alpha1.ElasticsearchSpec) []esv1alpha1.ElasticsearchNode {
if es.NodeCount <= 3 {
return []esv1alpha1.ElasticsearchNode{
{
NodeCount: es.NodeCount,
Storage: es.Storage,
NodeSelector: es.NodeSelector,
Roles: []esv1alpha1.ElasticsearchNodeRole{esv1alpha1.ElasticsearchRoleClient, esv1alpha1.ElasticsearchRoleData, esv1alpha1.ElasticsearchRoleMaster},
},
}
}
return []esv1alpha1.ElasticsearchNode{
{
NodeCount: 3,
Storage: es.Storage,
NodeSelector: es.NodeSelector,
Roles: []esv1alpha1.ElasticsearchNodeRole{esv1alpha1.ElasticsearchRoleMaster},
},
{
NodeCount: es.NodeCount - 3,
Storage: es.Storage,
NodeSelector: es.NodeSelector,
Roles: []esv1alpha1.ElasticsearchNodeRole{esv1alpha1.ElasticsearchRoleClient, esv1alpha1.ElasticsearchRoleData},
},
}
}
// taken from https://github.com/openshift/cluster-logging-operator/blob/1ead6701c7c7af9c0578aa66597261079b2781d5/vendor/github.com/openshift/elasticsearch-operator/pkg/k8shandler/defaults.go#L33
func calculateReplicaShards(policyType esv1alpha1.RedundancyPolicyType, dataNodes int) int {
switch policyType {
case esv1alpha1.FullRedundancy:
return dataNodes - 1
case esv1alpha1.MultipleRedundancy:
return (dataNodes - 1) / 2
case esv1alpha1.SingleRedundancy:
return 1
case esv1alpha1.ZeroRedundancy:
return 0
default:
return 1
}
}
func containsPrefix(prefix string, arr []string) bool {
for _, a := range arr {
if strings.HasPrefix(a, prefix) {
return true
}
}
return false
}