-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
145 lines (124 loc) · 4.02 KB
/
server.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package main
import (
"context"
"io/fs"
"strings"
corev1 "k8s.io/client-go/applyconfigurations/core/v1"
rbacv1 "k8s.io/client-go/applyconfigurations/rbac/v1"
"namespacelabs.dev/foundation/framework/kubernetes/kubeblueprint"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/kubernetes/kubetool"
"namespacelabs.dev/foundation/framework/provisioning"
"namespacelabs.dev/foundation/internal/runtime"
)
const (
id = "prometheus.foundation.namespacelabs.dev"
clusterRoleName = "fn:prometheus"
clusterRoleBindingName = "fn:prometheus"
configMapName = id
promYaml = "prometheus.yml"
)
var (
volumeName = strings.Replace(id, ".", "-", -1)
)
type configureServer struct{}
func (configureServer) Apply(ctx context.Context, r provisioning.StackRequest, out *provisioning.ApplyOutput) error {
kr, err := kubetool.MustNamespace(r)
if err != nil {
return err
}
promYamlData, err := fs.ReadFile(embeddedData, promYaml)
if err != nil {
return err
}
g := kubeblueprint.GrantKubeACLs{
DescriptionBase: "Prometheus",
Rules: []*rbacv1.PolicyRuleApplyConfiguration{
rbacv1.PolicyRule().
WithAPIGroups("").
WithResources("nodes", "nodes/proxy", "services", "endpoints", "pods").
WithVerbs("get", "list", "watch"),
rbacv1.PolicyRule().
WithAPIGroups("extensions").
WithResources("ingresses").
WithVerbs("get", "list", "watch"),
rbacv1.PolicyRule().
WithNonResourceURLs("/metrics").
WithVerbs("get"),
},
}
if err := g.Compile(r, kubeblueprint.ClusterScope, out); err != nil {
return err
}
configs := map[string]string{
promYaml: string(promYamlData),
}
out.Invocations = append(out.Invocations, kubedef.Apply{
Description: "Prometheus ConfigMap",
SetNamespace: kr.CanSetNamespace,
Resource: corev1.ConfigMap(configMapName, kr.Namespace).WithData(configs),
})
out.Extensions = append(out.Extensions, kubedef.ExtendSpec{
With: &kubedef.SpecExtension{
Volume: []*kubedef.SpecExtension_Volume{{
Name: volumeName, // XXX generate unique names.
VolumeType: &kubedef.SpecExtension_Volume_ConfigMap_{
ConfigMap: &kubedef.SpecExtension_Volume_ConfigMap{
Name: configMapName,
Item: []*kubedef.SpecExtension_Volume_ConfigMap_Item{{
Key: promYaml,
Path: promYaml,
}},
},
},
}},
},
})
out.Extensions = append(out.Extensions, kubedef.ExtendContainer{
With: &kubedef.ContainerExtension{
VolumeMount: []*kubedef.ContainerExtension_VolumeMount{{
Name: volumeName,
ReadOnly: true,
MountPath: "/etc/prometheus/",
}},
Args: []string{
"--config.file=/etc/prometheus/prometheus.yml",
"--storage.tsdb.path=/prometheus",
"--web.console.libraries=/usr/share/prometheus/console_libraries",
"--web.console.templates=/usr/share/prometheus/consoles",
"--web.enable-remote-write-receiver",
},
},
})
return nil
}
func (configureServer) Delete(ctx context.Context, r provisioning.StackRequest, out *provisioning.DeleteOutput) error {
kr, err := kubetool.MustNamespace(r)
if err != nil {
return err
}
out.Invocations = append(out.Invocations, kubedef.Delete{
Description: "Prometheus ClusterRoleBinding",
Resource: "clusterrolebindings",
Name: clusterRoleBindingName,
})
out.Invocations = append(out.Invocations, kubedef.Delete{
Description: "Prometheus ClusterRole",
Resource: "clusterroles",
Name: clusterRoleName,
})
out.Invocations = append(out.Invocations, kubedef.Delete{
Description: "Prometheus Service Account",
Resource: "serviceaccounts",
Name: makeServiceAccount(r.Focus.Server),
SetNamespace: kr.CanSetNamespace,
Namespace: kr.Namespace,
})
return nil
}
func makeServiceAccount(srv runtime.Deployable) string {
return kubedef.MakeDeploymentId(srv)
}