-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
76 lines (64 loc) · 2.46 KB
/
prometheus.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
package prometheus_grafana
import (
"fmt"
"io/ioutil"
"os/exec"
"strings"
"log"
)
type PrometheusSpec struct {
NamespaceName string `json:"namespaceName"` //namespace 명 (defult: monitoring)
ImgVersion string `json:"imgVersion"` //prometheus image version (defalt: latest)
ScrapeInterv string `json:"scrapeInterv"` //prometheus가 스크랩을 요청하는 시간 간격(default: 15s)
NodePort string `json:"nodePort"` //(default: 30000)
}
func promApplyYamlFileCmd(gitPath string, fileName string, spec *PrometheusSpec, option string) {
replacerArg := strings.NewReplacer(
"{{namespaceName}}", spec.NamespaceName,
"{{nodePort}}", spec.NodePort,
"{{scrapeInterv}}", spec.ScrapeInterv,
"{{imgVersion}}", spec.ImgVersion,
)
if yamlStr, err := getYaml(gitPath + fileName); err != nil {
log.Printf("Failed to get yaml file: %v", err)
} else {
log.Println("Received YAML:")
log.Println(yamlStr)
// Replace all pairs.
newFormatYmlString := replacerArg.Replace(yamlStr)
//fmt.Println(newFormatYmlString)
if err = ioutil.WriteFile("/tmp/custom_"+fileName, []byte(newFormatYmlString), 0666); err != nil {
fmt.Println(err)
} else{
applyYamlFileCmd("/tmp/custom_"+fileName, option, spec.NamespaceName)
//deleteFile("/tmp/custom_"+fileName)
}
}
}
func CreatePrometheus(prometheusSpec PrometheusSpec, gitPath string) {
// /////////test config
// var Prometheus_spec = PrometheusSpec{
// scrapeInterv: "15s",
// nodePort: "30000",
// namespaceName: "monitoring",
// imgVersion: "v2.12.0",
// }
// ///////////////////
//connectToClusterCmd()
createNamespaceCmd(prometheusSpec.NamespaceName)
promApplyYamlFileCmd(gitPath, "prom_clusterRole.yaml", &prometheusSpec, "")
promApplyYamlFileCmd(gitPath, "prom_config_map.yaml", &prometheusSpec, "")
promApplyYamlFileCmd(gitPath, "prom_deployment.yaml", &prometheusSpec, "")
//////////////////Check deployment file
cmd := exec.Command("kubectl", "get", "deployments", "--namespace="+prometheusSpec.NamespaceName)
printCommand(cmd)
output, err := cmd.CombinedOutput()
printError(err)
printOutput(output)
//////////////////////////////////////////
promApplyYamlFileCmd(gitPath, "prom_service.yaml", &prometheusSpec, "--namespace=")
promApplyYamlFileCmd(gitPath, "prom_vpa.yaml", &prometheusSpec, "")
/////////////////dns and ingress
promApplyYamlFileCmd(gitPath, "prom_dnsendpoint.yaml", &prometheusSpec, "")
promApplyYamlFileCmd(gitPath, "prom_ingressroute.yaml", &prometheusSpec, "")
}