-
Notifications
You must be signed in to change notification settings - Fork 38
/
scrape_config.go
113 lines (97 loc) · 3.74 KB
/
scrape_config.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
package scrapeconfig
import (
"encoding/base64"
"fmt"
"github.com/berops/claudie/internal/kubectl"
"github.com/berops/claudie/internal/templateUtils"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
"github.com/berops/claudie/services/kuber/templates"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
comm "github.com/berops/claudie/internal/command"
)
type ScrapeConfig struct {
Cluster *pb.K8Scluster
LBClusters []*pb.LBcluster
Directory string
}
type SCData struct {
LBClusters []*LBcluster
}
type LBcluster struct {
NodePools *NodePools
}
type NodePools struct {
Dynamic []*pb.NodePool
Static []*pb.NodePool
}
type ScManifestData struct {
ScrapeConfigB64 string
Namespace string
}
const (
scrapeConfigNamespace = "monitoring"
scManifestFile = "scrape-config-manifest.yaml"
scrapeConfigFile = "scrape-config.yaml"
)
// GenerateAndApplyScrapeConfig will template a secret with Prometheus configuration,
// for scraping Loadbalancers node-exporter endpoints
// it will create the secret in a applied namespace
// If there is no loadbalancers it will apply the config with no target endpoints
func (sc *ScrapeConfig) GenerateAndApplyScrapeConfig() error {
// Generate loadbalancers scrape config
template := templateUtils.Templates{Directory: sc.Directory}
// Generate prometheus scrape config to file
tpl, err := templateUtils.LoadTemplate(templates.ScrapeConfigTemplate)
if err != nil {
return fmt.Errorf("error while loading scrape config file for %s: %w", sc.Cluster.ClusterInfo.Name, err)
}
scrapeConfig, err := template.GenerateToString(tpl, sc.getData())
if err != nil {
return fmt.Errorf("error while generating %s on %s: %w", scrapeConfigFile, sc.Cluster.ClusterInfo.Name, err)
}
// Generate manifest for namespace and secret
tpl, err = templateUtils.LoadTemplate(templates.ScrapeConfigManifestTemplate)
if err != nil {
return fmt.Errorf("error while loading scrape config template for %s: %w", sc.Cluster.ClusterInfo.Name, err)
}
if err = template.Generate(tpl, scManifestFile, ScManifestData{Namespace: scrapeConfigNamespace,
ScrapeConfigB64: base64.StdEncoding.EncodeToString([]byte(scrapeConfig))}); err != nil {
return fmt.Errorf("error while generating %s on %s: %w", scManifestFile, sc.Cluster.ClusterInfo.Name, err)
}
// Apply namespace and secret to the cluster
k := kubectl.Kubectl{Kubeconfig: sc.Cluster.Kubeconfig, Directory: sc.Directory}
if log.Logger.GetLevel() == zerolog.DebugLevel {
prefix := utils.GetClusterID(sc.Cluster.ClusterInfo)
k.Stdout = comm.GetStdOut(prefix)
k.Stderr = comm.GetStdErr(prefix)
}
if err = k.KubectlApply(scManifestFile); err != nil {
return fmt.Errorf("error while applying %s on %s: %w", scManifestFile, sc.Cluster.ClusterInfo.Name, err)
}
return nil
}
// RemoveIfNoLBScrapeConfig will remove the LB scrape-config.yml
func (sc *ScrapeConfig) RemoveLBScrapeConfig() error {
k := kubectl.Kubectl{Kubeconfig: sc.Cluster.Kubeconfig, MaxKubectlRetries: 3}
if log.Logger.GetLevel() == zerolog.DebugLevel {
prefix := utils.GetClusterID(sc.Cluster.ClusterInfo)
k.Stdout = comm.GetStdOut(prefix)
k.Stderr = comm.GetStdErr(prefix)
}
if err := k.KubectlDeleteResource("secret", "loadbalancers-scrape-config", "-n", scrapeConfigNamespace); err != nil {
return fmt.Errorf("error while removing LB scrape-config on %s: %w", sc.Cluster.ClusterInfo.Name, err)
}
return nil
}
func (sc *ScrapeConfig) getData() SCData {
lbs := make([]*LBcluster, 0, len(sc.LBClusters))
for _, l := range sc.LBClusters {
lbs = append(lbs, &LBcluster{NodePools: &NodePools{
Dynamic: utils.GetCommonDynamicNodePools(l.ClusterInfo.NodePools),
Static: utils.GetCommonStaticNodePools(l.ClusterInfo.NodePools),
}})
}
return SCData{LBClusters: lbs}
}