-
Notifications
You must be signed in to change notification settings - Fork 21
/
kibanacatalog.go
162 lines (134 loc) · 4.7 KB
/
kibanacatalog.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
package kibanacatalog
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/cloudstax/firecamp/catalog"
"github.com/cloudstax/firecamp/common"
"github.com/cloudstax/firecamp/dns"
"github.com/cloudstax/firecamp/manage"
"github.com/cloudstax/firecamp/utils"
)
const (
defaultVersion = "5.6"
// ContainerImage is the main running container.
ContainerImage = common.ContainerNamePrefix + "kibana:" + defaultVersion
// DefaultReserveMemoryMB is the default reserved memory size for Kibana
DefaultReserveMemoryMB = 2048
// https://www.elastic.co/guide/en/kibana/5.6/settings.html
kbHTTPPort = 5601
kbsslConfFileName = "server.key"
kbcertConfFileName = "server.cert"
)
// ValidateRequest checks if the request is valid
func ValidateRequest(r *manage.CatalogCreateKibanaRequest) error {
if len(r.Options.ESServiceName) == 0 {
return errors.New("please specify the elasticsearch service name")
}
if len(r.Options.ProxyBasePath) != 0 && strings.HasSuffix(r.Options.ProxyBasePath, "/") {
return errors.New("the ProxyBasePath cannot end in a slash (/)")
}
if r.Options.EnableSSL && (len(r.Options.SSLKey) == 0 || len(r.Options.SSLCert) == 0) {
return errors.New("SSL enabled, but ssl key or certificate is empty")
}
return nil
}
// GenDefaultCreateServiceRequest returns the default service creation request.
// kibana simply connects to the first elasticsearch node. TODO create a local coordinating elasticsearch instance.
func GenDefaultCreateServiceRequest(platform string, region string, azs []string, cluster string,
service string, res *common.Resources, opts *manage.CatalogKibanaOptions, esNodeURL string) *manage.CreateServiceRequest {
// generate service configs
serviceCfgs := genServiceConfigs(platform, opts, esNodeURL)
// generate member ReplicaConfigs
replicaCfgs := genReplicaConfigs(platform, cluster, service, azs, opts)
portMappings := []common.PortMapping{
{ContainerPort: kbHTTPPort, HostPort: kbHTTPPort, IsServicePort: true},
}
req := &manage.CreateServiceRequest{
Service: &manage.ServiceCommonRequest{
Region: region,
Cluster: cluster,
ServiceName: service,
},
Resource: res,
ServiceType: common.ServiceTypeStateful,
CatalogServiceType: common.CatalogService_Kibana,
ContainerImage: ContainerImage,
Replicas: opts.Replicas,
PortMappings: portMappings,
RegisterDNS: true,
ServiceConfigs: serviceCfgs,
Volume: opts.Volume,
ContainerPath: common.DefaultContainerMountPath,
ReplicaConfigs: replicaCfgs,
}
return req
}
// genServiceConfigs generates the service configs.
func genServiceConfigs(platform string, opts *manage.CatalogKibanaOptions, esNodeURL string) []*manage.ConfigFileContent {
// create the service.conf file
content := fmt.Sprintf(servicefileContent, platform, opts.ESServiceName, esNodeURL, opts.ProxyBasePath, strconv.FormatBool(opts.EnableSSL))
serviceCfg := &manage.ConfigFileContent{
FileName: catalog.SERVICE_FILE_NAME,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
configs := []*manage.ConfigFileContent{serviceCfg}
// create ssl key and cert files if ssl is enabled
if opts.EnableSSL {
sslKeyCfg := &manage.ConfigFileContent{
FileName: kbsslConfFileName,
FileMode: common.DefaultConfigFileMode,
Content: opts.SSLKey,
}
sslCertCfg := &manage.ConfigFileContent{
FileName: kbcertConfFileName,
FileMode: common.DefaultConfigFileMode,
Content: opts.SSLCert,
}
configs = append(configs, sslKeyCfg, sslCertCfg)
}
return configs
}
// genReplicaConfigs generates the replica configs.
func genReplicaConfigs(platform string, cluster string, service string, azs []string, opts *manage.CatalogKibanaOptions) []*manage.ReplicaConfig {
domain := dns.GenDefaultDomainName(cluster)
replicaCfgs := make([]*manage.ReplicaConfig, opts.Replicas)
for i := int64(0); i < opts.Replicas; i++ {
azIndex := int(i) % len(azs)
az := azs[azIndex]
member := utils.GenServiceMemberName(service, i)
memberHost := dns.GenDNSName(member, domain)
bind := memberHost
if platform == common.ContainerPlatformSwarm {
bind = catalog.BindAllIP
}
// create the member.conf file
content := fmt.Sprintf(memberfileContent, az, member, memberHost, bind)
memberCfg := &manage.ConfigFileContent{
FileName: catalog.MEMBER_FILE_NAME,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
configs := []*manage.ConfigFileContent{memberCfg}
replicaCfg := &manage.ReplicaConfig{Zone: az, MemberName: member, Configs: configs}
replicaCfgs[i] = replicaCfg
}
return replicaCfgs
}
const (
servicefileContent = `
PLATFORM=%s
ES_SERVICE_NAME=%s
ES_URL=%s
PROXY_BASE_PATH=%s
ENABLE_SSL=%s
`
memberfileContent = `
AVAILABILITY_ZONE=%s
MEMBER_NAME=%s
SERVICE_MEMBER=%s
BIND_IP=%s
`
)