Skip to content

Commit 963b525

Browse files
authored
Add ServiceMonitor to pkg (#7)
add ServiceMonitor to pkg
1 parent 69d2c16 commit 963b525

File tree

3 files changed

+444
-0
lines changed

3 files changed

+444
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ endif
1212
generate: controller-gen
1313
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./pkg/secret/...
1414
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./pkg/volume/...
15+
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./pkg/prometheus/...
1516

1617
# find or download controller-gen
1718
# download controller-gen if necessary

pkg/prometheus/servicemonitor.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright © 2020 Banzai Cloud
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package prometheus
16+
17+
import (
18+
v1 "k8s.io/api/core/v1"
19+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
"k8s.io/apimachinery/pkg/runtime/schema"
21+
"k8s.io/apimachinery/pkg/util/intstr"
22+
"sigs.k8s.io/controller-runtime/pkg/scheme"
23+
)
24+
25+
const (
26+
ServiceMonitorsKind = "ServiceMonitor"
27+
ServiceMonitorName = "servicemonitors"
28+
ServiceMonitorKindKey = "servicemonitor"
29+
)
30+
31+
var (
32+
// GroupVersion is group version used to register these objects
33+
GroupVersion = schema.GroupVersion{Group: "monitoring.coreos.com", Version: "v1"}
34+
35+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
36+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
37+
38+
// AddToScheme adds the types in this group-version to the given scheme.
39+
AddToScheme = SchemeBuilder.AddToScheme
40+
)
41+
42+
func init() {
43+
SchemeBuilder.Register(&ServiceMonitor{}, &ServiceMonitorList{})
44+
}
45+
46+
// +kubebuilder:object:root=true
47+
48+
type ServiceMonitor struct {
49+
metav1.TypeMeta `json:",inline"`
50+
// Standard object’s metadata. More info:
51+
// https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#metadata
52+
// +k8s:openapi-gen=false
53+
metav1.ObjectMeta `json:"metadata,omitempty"`
54+
// Specification of desired Service selection for target discrovery by
55+
// Prometheus.
56+
Spec ServiceMonitorSpec `json:"spec"`
57+
}
58+
59+
// +kubebuilder:object:root=true
60+
61+
type ServiceMonitorList struct {
62+
metav1.TypeMeta `json:",inline"`
63+
// Standard list metadata
64+
// More info: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#metadata
65+
metav1.ListMeta `json:"metadata,omitempty"`
66+
// List of ServiceMonitors
67+
Items []*ServiceMonitor `json:"items"`
68+
}
69+
70+
// +kubebuilder:object:generate=true
71+
72+
type ServiceMonitorSpec struct {
73+
// The label to use to retrieve the job name from.
74+
JobLabel string `json:"jobLabel,omitempty"`
75+
// TargetLabels transfers labels on the Kubernetes Service onto the target.
76+
TargetLabels []string `json:"targetLabels,omitempty"`
77+
// PodTargetLabels transfers labels on the Kubernetes Pod onto the target.
78+
PodTargetLabels []string `json:"podTargetLabels,omitempty"`
79+
// A list of endpoints allowed as part of this ServiceMonitor.
80+
Endpoints []Endpoint `json:"endpoints"`
81+
// Selector to select Endpoints objects.
82+
Selector metav1.LabelSelector `json:"selector"`
83+
// Selector to select which namespaces the Endpoints objects are discovered from.
84+
NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"`
85+
// SampleLimit defines per-scrape limit on number of scraped samples that will be accepted.
86+
SampleLimit uint64 `json:"sampleLimit,omitempty"`
87+
}
88+
89+
// +kubebuilder:object:generate=true
90+
91+
type Endpoint struct {
92+
// Name of the service port this endpoint refers to. Mutually exclusive with targetPort.
93+
Port string `json:"port,omitempty"`
94+
// Name or number of the target port of the endpoint. Mutually exclusive with port.
95+
TargetPort *intstr.IntOrString `json:"targetPort,omitempty"`
96+
// HTTP path to scrape for metrics.
97+
Path string `json:"path,omitempty"`
98+
// HTTP scheme to use for scraping.
99+
Scheme string `json:"scheme,omitempty"`
100+
// Optional HTTP URL parameters
101+
Params map[string][]string `json:"params,omitempty"`
102+
// Interval at which metrics should be scraped
103+
Interval string `json:"interval,omitempty"`
104+
// Timeout after which the scrape is ended
105+
ScrapeTimeout string `json:"scrapeTimeout,omitempty"`
106+
// Certificate configuration to use when scraping the endpoint
107+
TLSConfig *TLSConfig `json:"tlsConfig,omitempty"`
108+
// File to read bearer token for scraping targets.
109+
BearerTokenFile string `json:"bearerTokenFile,omitempty"`
110+
// HonorLabels chooses the metric's labels on collisions with target labels.
111+
HonorLabels bool `json:"honorLabels,omitempty"`
112+
// BasicAuth allow an endpoint to authenticate over basic authentication
113+
// More info: https://prometheus.io/docs/operating/configuration/#endpoints
114+
BasicAuth *BasicAuth `json:"basicAuth,omitempty"`
115+
// MetricRelabelConfigs to apply to samples before ingestion.
116+
MetricRelabelConfigs []*RelabelConfig `json:"metricRelabelings,omitempty"`
117+
// RelabelConfigs to apply to samples before ingestion.
118+
// More info: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#<relabel_config>
119+
RelabelConfigs []*RelabelConfig `json:"relabelings,omitempty"`
120+
// ProxyURL eg http://proxyserver:2195 Directs scrapes to proxy through this endpoint.
121+
ProxyURL *string `json:"proxyUrl,omitempty"`
122+
}
123+
124+
// +kubebuilder:object:generate=true
125+
126+
type TLSConfig struct {
127+
// The CA cert to use for the targets.
128+
CAFile string `json:"caFile,omitempty"`
129+
// The client cert file for the targets.
130+
CertFile string `json:"certFile,omitempty"`
131+
// The client key file for the targets.
132+
KeyFile string `json:"keyFile,omitempty"`
133+
// Used to verify the hostname for the targets.
134+
ServerName string `json:"serverName,omitempty"`
135+
// Disable target certificate validation.
136+
InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"`
137+
}
138+
139+
// +kubebuilder:object:generate=true
140+
141+
type BasicAuth struct {
142+
// The secret that contains the username for authenticate
143+
Username v1.SecretKeySelector `json:"username,omitempty"`
144+
// The secret that contains the password for authenticate
145+
Password v1.SecretKeySelector `json:"password,omitempty"`
146+
}
147+
148+
// +kubebuilder:object:generate=true
149+
150+
type RelabelConfig struct {
151+
//The source labels select values from existing labels. Their content is concatenated
152+
//using the configured separator and matched against the configured regular expression
153+
//for the replace, keep, and drop actions.
154+
SourceLabels []string `json:"sourceLabels,omitempty"`
155+
//Separator placed between concatenated source label values. default is ';'.
156+
Separator string `json:"separator,omitempty"`
157+
//Label to which the resulting value is written in a replace action.
158+
//It is mandatory for replace actions. Regex capture groups are available.
159+
TargetLabel string `json:"targetLabel,omitempty"`
160+
//Regular expression against which the extracted value is matched. defailt is '(.*)'
161+
Regex string `json:"regex,omitempty"`
162+
// Modulus to take of the hash of the source label values.
163+
Modulus uint64 `json:"modulus,omitempty"`
164+
//Replacement value against which a regex replace is performed if the
165+
//regular expression matches. Regex capture groups are available. Default is '$1'
166+
Replacement string `json:"replacement,omitempty"`
167+
// Action to perform based on regex matching. Default is 'replace'
168+
Action string `json:"action,omitempty"`
169+
}
170+
171+
// +kubebuilder:object:generate=true
172+
173+
type NamespaceSelector struct {
174+
// Boolean describing whether all namespaces are selected in contrast to a
175+
// list restricting them.
176+
Any bool `json:"any,omitempty"`
177+
// List of namespace names.
178+
MatchNames []string `json:"matchNames,omitempty"`
179+
}

0 commit comments

Comments
 (0)