-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpodmonitors.go
121 lines (110 loc) · 5.33 KB
/
podmonitors.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package master
import (
"context"
"fmt"
"github.com/awslabs/kubernetes-iteration-toolkit/operator/pkg/apis/controlplane/v1alpha1"
"github.com/awslabs/kubernetes-iteration-toolkit/operator/pkg/components/iamauthenticator"
"github.com/awslabs/kubernetes-iteration-toolkit/operator/pkg/utils/object"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// reconcilePodmonitors patches the required cert volumes by Prometheus to scrape guest cluster metrics
func (c *Controller) reconcilePodmonitors(ctx context.Context, controlPlane *v1alpha1.ControlPlane) error {
// create pod monitor for API server and etcd pods
for _, spec := range []monitoringv1.PodMonitorSpec{
apiServerPodMonitorFor(controlPlane),
etcdPodMonitorFor(controlPlane),
kcmPodMonitorFor(controlPlane),
schedulerPodMonitorFor(controlPlane),
authenticatorPodMonitorFor(controlPlane),
} {
if err := c.kubeClient.EnsureCreate(ctx, object.WithOwner(controlPlane, &monitoringv1.PodMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: spec.JobLabel,
Namespace: controlPlane.Namespace,
Labels: map[string]string{"release": "kube-prometheus-stack"},
},
Spec: spec,
})); err != nil {
return fmt.Errorf("ensuring podmonitor for %s, %w", spec.JobLabel, err)
}
}
return nil
}
func apiServerPodMonitorFor(controlPlane *v1alpha1.ControlPlane) monitoringv1.PodMonitorSpec {
return monitoringv1.PodMonitorSpec{
JobLabel: fmt.Sprintf("%s-apiserver", controlPlane.ClusterName()),
NamespaceSelector: monitoringv1.NamespaceSelector{MatchNames: []string{controlPlane.Namespace}},
Selector: metav1.LabelSelector{MatchLabels: APIServerLabels(controlPlane.ClusterName())},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{{
Port: "https", Scheme: "https",
TLSConfig: &monitoringv1.PodMetricsEndpointTLSConfig{
SafeTLSConfig: monitoringv1.SafeTLSConfig{
ServerName: "kubernetes",
CA: monitoringv1.SecretOrConfigMap{Secret: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{Name: RootCASecretNameFor(controlPlane.ClusterName())},
Key: "public",
}},
Cert: monitoringv1.SecretOrConfigMap{Secret: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{Name: PrometheusClientCertsFor(controlPlane.ClusterName())},
Key: "public",
}},
KeySecret: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{Name: PrometheusClientCertsFor(controlPlane.ClusterName())},
Key: "private",
},
},
},
}},
}
}
func etcdPodMonitorFor(controlPlane *v1alpha1.ControlPlane) monitoringv1.PodMonitorSpec {
return monitoringv1.PodMonitorSpec{
JobLabel: fmt.Sprintf("%s-etcd", controlPlane.ClusterName()),
NamespaceSelector: monitoringv1.NamespaceSelector{MatchNames: []string{controlPlane.Namespace}},
Selector: metav1.LabelSelector{MatchLabels: map[string]string{
object.AppNameLabelKey: "etcd", object.ControlPlaneLabelKey: controlPlane.ClusterName()}},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{{Port: "metrics"}},
}
}
func kcmPodMonitorFor(controlPlane *v1alpha1.ControlPlane) monitoringv1.PodMonitorSpec {
return monitoringv1.PodMonitorSpec{
JobLabel: fmt.Sprintf("%s-controller-manager", controlPlane.ClusterName()),
NamespaceSelector: monitoringv1.NamespaceSelector{MatchNames: []string{controlPlane.Namespace}},
Selector: metav1.LabelSelector{MatchLabels: kcmLabels(controlPlane.ClusterName())},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{{Port: "metrics"}},
}
}
func schedulerPodMonitorFor(controlPlane *v1alpha1.ControlPlane) monitoringv1.PodMonitorSpec {
return monitoringv1.PodMonitorSpec{
JobLabel: fmt.Sprintf("%s-scheduler", controlPlane.ClusterName()),
NamespaceSelector: monitoringv1.NamespaceSelector{MatchNames: []string{controlPlane.Namespace}},
Selector: metav1.LabelSelector{MatchLabels: schedulerLabels(controlPlane.ClusterName())},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{{Port: "metrics"}},
}
}
func authenticatorPodMonitorFor(controlPlane *v1alpha1.ControlPlane) monitoringv1.PodMonitorSpec {
return monitoringv1.PodMonitorSpec{
JobLabel: fmt.Sprintf("%s-authenticator", controlPlane.ClusterName()),
NamespaceSelector: monitoringv1.NamespaceSelector{MatchNames: []string{controlPlane.Namespace}},
Selector: metav1.LabelSelector{MatchLabels: iamauthenticator.Labels(controlPlane.ClusterName())},
PodMetricsEndpoints: []monitoringv1.PodMetricsEndpoint{{
Port: "metrics", Scheme: "https",
TLSConfig: &monitoringv1.PodMetricsEndpointTLSConfig{
SafeTLSConfig: monitoringv1.SafeTLSConfig{InsecureSkipVerify: true},
},
}},
}
}