1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-16 01:06:27 +00:00
prometheus-operator/pkg/client/monitoring/v1/client.go

162 lines
5 KiB
Go
Raw Normal View History

2017-08-22 12:53:35 +02:00
// Copyright 2016 The prometheus-operator Authors
//
// 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 v1
import (
2017-11-07 17:28:21 +01:00
"fmt"
2018-06-05 13:09:13 +02:00
"strings"
2017-08-22 12:53:35 +02:00
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
dynamic "k8s.io/client-go/deprecated-dynamic"
2017-11-24 16:19:04 +01:00
"k8s.io/client-go/kubernetes/scheme"
2017-08-22 12:53:35 +02:00
"k8s.io/client-go/rest"
)
const (
2017-11-07 17:28:21 +01:00
Group = "monitoring.coreos.com"
PrometheusKindKey = "prometheus"
2017-11-16 09:23:22 +01:00
AlertManagerKindKey = "alertmanager"
2017-11-07 17:28:21 +01:00
ServiceMonitorKindKey = "servicemonitor"
2018-06-05 13:09:13 +02:00
PrometheusRuleKindKey = "prometheusrule"
2017-08-22 12:53:35 +02:00
)
2017-11-07 17:28:21 +01:00
type CrdKind struct {
2018-01-30 14:51:34 +01:00
Kind string
Plural string
SpecName string
2017-11-07 17:28:21 +01:00
}
type CrdKinds struct {
KindsString string
Prometheus CrdKind
Alertmanager CrdKind
ServiceMonitor CrdKind
2018-06-05 13:09:13 +02:00
PrometheusRule CrdKind
2017-11-07 17:28:21 +01:00
}
2017-11-16 09:23:22 +01:00
var DefaultCrdKinds CrdKinds = CrdKinds{
KindsString: "",
2018-01-30 14:51:34 +01:00
Prometheus: CrdKind{Plural: PrometheusName, Kind: PrometheusesKind, SpecName: "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1.Prometheus"},
ServiceMonitor: CrdKind{Plural: ServiceMonitorName, Kind: ServiceMonitorsKind, SpecName: "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1.ServiceMonitor"},
Alertmanager: CrdKind{Plural: AlertmanagerName, Kind: AlertmanagersKind, SpecName: "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1.Alertmanager"},
2018-06-05 13:09:13 +02:00
PrometheusRule: CrdKind{Plural: PrometheusRuleName, Kind: PrometheusRuleKind, SpecName: "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1.PrometheusRule"},
2017-11-16 09:23:22 +01:00
}
2017-11-07 17:28:21 +01:00
// Implement the flag.Value interface
func (crdkinds *CrdKinds) String() string {
return crdkinds.KindsString
}
// Set Implement the flag.Set interface
func (crdkinds *CrdKinds) Set(value string) error {
2017-11-16 09:23:22 +01:00
*crdkinds = DefaultCrdKinds
2017-11-07 17:28:21 +01:00
if value == "" {
prometheus: Introduce RuleFile Custom Resource Definition This patch introduces a new Custom Resource Definition to the Prometheus Operator - the Rule CRD. It addresses two main needs: 1. Prometheus (alerting and recording) Rule validation during creation time via Kubernetes Custom Resource Definition validation. 2. Life-cycle management of Prometheus application Rules alongside the application itself, inside the applications Kubernetes namespace, not necessarily the namespace of the scraping Prometheus instance. A user defines Prometheus alerting and recording Rules via a Kubernetes Custom Resource Definition. These Custom Resource Definitions can be fully validated by the Kubernetes API server during creation time via automatically generated OpenAPI specifications. Instead of the restriction of a Prometheus instance to only select Rule definitions inside its own namespace, the Prometheus specification is extended to also specify namespaces to look for Rule Custom Resource Definitions outside its own namespace. --- Dependent technical changes: - prometheus: Use github.com/jimmidyson/configmap-reload to reload rules - prometheus: Remove Prometheus Statefulset deletion function. Starting with K8s >=1.8 this is handled via OwnerReferences. - prometheus: Do not add rule files checksum to Prometheus configuration secret - prometheus: Update StatefulSet only on relevant changes. Instead of updating the Prometheus StatefulSet on every `sync()` run, only update it if the input parameters to `makeStatefulSet` change. Enforce this via a checksum of the parameters which is saved inside the annotations of the statefulset. - e2e/prometheus: Check how often resources (Secret, ConfigMap, Prometheus CRD, Service) are updated to enforce that Prometheus Operator only updated created resources if necessary. - contrib/prometheus-config-reloader: Remove logic to retriev K8s ConfigMaps. These are mounted into the pod right away now.
2018-05-08 09:43:45 +02:00
value = fmt.Sprintf("%s=%s:%s,%s=%s:%s,%s=%s:%s,%s=%s:%s",
2017-11-07 17:28:21 +01:00
PrometheusKindKey, PrometheusesKind, PrometheusName,
AlertManagerKindKey, AlertmanagersKind, AlertmanagerName,
prometheus: Introduce RuleFile Custom Resource Definition This patch introduces a new Custom Resource Definition to the Prometheus Operator - the Rule CRD. It addresses two main needs: 1. Prometheus (alerting and recording) Rule validation during creation time via Kubernetes Custom Resource Definition validation. 2. Life-cycle management of Prometheus application Rules alongside the application itself, inside the applications Kubernetes namespace, not necessarily the namespace of the scraping Prometheus instance. A user defines Prometheus alerting and recording Rules via a Kubernetes Custom Resource Definition. These Custom Resource Definitions can be fully validated by the Kubernetes API server during creation time via automatically generated OpenAPI specifications. Instead of the restriction of a Prometheus instance to only select Rule definitions inside its own namespace, the Prometheus specification is extended to also specify namespaces to look for Rule Custom Resource Definitions outside its own namespace. --- Dependent technical changes: - prometheus: Use github.com/jimmidyson/configmap-reload to reload rules - prometheus: Remove Prometheus Statefulset deletion function. Starting with K8s >=1.8 this is handled via OwnerReferences. - prometheus: Do not add rule files checksum to Prometheus configuration secret - prometheus: Update StatefulSet only on relevant changes. Instead of updating the Prometheus StatefulSet on every `sync()` run, only update it if the input parameters to `makeStatefulSet` change. Enforce this via a checksum of the parameters which is saved inside the annotations of the statefulset. - e2e/prometheus: Check how often resources (Secret, ConfigMap, Prometheus CRD, Service) are updated to enforce that Prometheus Operator only updated created resources if necessary. - contrib/prometheus-config-reloader: Remove logic to retriev K8s ConfigMaps. These are mounted into the pod right away now.
2018-05-08 09:43:45 +02:00
ServiceMonitorKindKey, ServiceMonitorsKind, ServiceMonitorName,
2018-06-05 13:09:13 +02:00
PrometheusRuleKindKey, PrometheusRuleKind, PrometheusRuleName,
prometheus: Introduce RuleFile Custom Resource Definition This patch introduces a new Custom Resource Definition to the Prometheus Operator - the Rule CRD. It addresses two main needs: 1. Prometheus (alerting and recording) Rule validation during creation time via Kubernetes Custom Resource Definition validation. 2. Life-cycle management of Prometheus application Rules alongside the application itself, inside the applications Kubernetes namespace, not necessarily the namespace of the scraping Prometheus instance. A user defines Prometheus alerting and recording Rules via a Kubernetes Custom Resource Definition. These Custom Resource Definitions can be fully validated by the Kubernetes API server during creation time via automatically generated OpenAPI specifications. Instead of the restriction of a Prometheus instance to only select Rule definitions inside its own namespace, the Prometheus specification is extended to also specify namespaces to look for Rule Custom Resource Definitions outside its own namespace. --- Dependent technical changes: - prometheus: Use github.com/jimmidyson/configmap-reload to reload rules - prometheus: Remove Prometheus Statefulset deletion function. Starting with K8s >=1.8 this is handled via OwnerReferences. - prometheus: Do not add rule files checksum to Prometheus configuration secret - prometheus: Update StatefulSet only on relevant changes. Instead of updating the Prometheus StatefulSet on every `sync()` run, only update it if the input parameters to `makeStatefulSet` change. Enforce this via a checksum of the parameters which is saved inside the annotations of the statefulset. - e2e/prometheus: Check how often resources (Secret, ConfigMap, Prometheus CRD, Service) are updated to enforce that Prometheus Operator only updated created resources if necessary. - contrib/prometheus-config-reloader: Remove logic to retriev K8s ConfigMaps. These are mounted into the pod right away now.
2018-05-08 09:43:45 +02:00
)
2017-11-07 17:28:21 +01:00
}
splited := strings.Split(value, ",")
for _, pair := range splited {
sp := strings.Split(pair, "=")
kind := strings.Split(sp[1], ":")
crdKind := CrdKind{Plural: kind[1], Kind: kind[0]}
switch kindKey := sp[0]; kindKey {
case PrometheusKindKey:
(*crdkinds).Prometheus = crdKind
case ServiceMonitorKindKey:
(*crdkinds).ServiceMonitor = crdKind
case AlertManagerKindKey:
(*crdkinds).Alertmanager = crdKind
2018-06-05 13:09:13 +02:00
case PrometheusRuleKindKey:
(*crdkinds).PrometheusRule = crdKind
2017-11-07 17:28:21 +01:00
default:
fmt.Printf("Warning: unknown kind: %s... ignoring", kindKey)
}
}
(*crdkinds).KindsString = value
return nil
}
2017-08-22 12:53:35 +02:00
var Version = "v1"
type MonitoringV1Interface interface {
RESTClient() rest.Interface
PrometheusesGetter
AlertmanagersGetter
ServiceMonitorsGetter
2018-06-05 13:09:13 +02:00
PrometheusRulesGetter
2017-08-22 12:53:35 +02:00
}
2017-12-22 15:01:54 +01:00
// +k8s:deepcopy-gen=false
2017-08-22 12:53:35 +02:00
type MonitoringV1Client struct {
restClient rest.Interface
dynamicClient *dynamic.Client
2017-11-07 17:28:21 +01:00
crdKinds *CrdKinds
2017-08-22 12:53:35 +02:00
}
func (c *MonitoringV1Client) Prometheuses(namespace string) PrometheusInterface {
2017-11-07 17:28:21 +01:00
return newPrometheuses(c.restClient, c.dynamicClient, c.crdKinds.Prometheus, namespace)
2017-08-22 12:53:35 +02:00
}
func (c *MonitoringV1Client) Alertmanagers(namespace string) AlertmanagerInterface {
2017-11-07 17:28:21 +01:00
return newAlertmanagers(c.restClient, c.dynamicClient, c.crdKinds.Alertmanager, namespace)
2017-08-22 12:53:35 +02:00
}
func (c *MonitoringV1Client) ServiceMonitors(namespace string) ServiceMonitorInterface {
2017-11-07 17:28:21 +01:00
return newServiceMonitors(c.restClient, c.dynamicClient, c.crdKinds.ServiceMonitor, namespace)
2017-08-22 12:53:35 +02:00
}
2018-06-05 13:09:13 +02:00
func (c *MonitoringV1Client) PrometheusRules(namespace string) PrometheusRuleInterface {
return newPrometheusRules(c.restClient, c.dynamicClient, c.crdKinds.PrometheusRule, namespace)
prometheus: Introduce RuleFile Custom Resource Definition This patch introduces a new Custom Resource Definition to the Prometheus Operator - the Rule CRD. It addresses two main needs: 1. Prometheus (alerting and recording) Rule validation during creation time via Kubernetes Custom Resource Definition validation. 2. Life-cycle management of Prometheus application Rules alongside the application itself, inside the applications Kubernetes namespace, not necessarily the namespace of the scraping Prometheus instance. A user defines Prometheus alerting and recording Rules via a Kubernetes Custom Resource Definition. These Custom Resource Definitions can be fully validated by the Kubernetes API server during creation time via automatically generated OpenAPI specifications. Instead of the restriction of a Prometheus instance to only select Rule definitions inside its own namespace, the Prometheus specification is extended to also specify namespaces to look for Rule Custom Resource Definitions outside its own namespace. --- Dependent technical changes: - prometheus: Use github.com/jimmidyson/configmap-reload to reload rules - prometheus: Remove Prometheus Statefulset deletion function. Starting with K8s >=1.8 this is handled via OwnerReferences. - prometheus: Do not add rule files checksum to Prometheus configuration secret - prometheus: Update StatefulSet only on relevant changes. Instead of updating the Prometheus StatefulSet on every `sync()` run, only update it if the input parameters to `makeStatefulSet` change. Enforce this via a checksum of the parameters which is saved inside the annotations of the statefulset. - e2e/prometheus: Check how often resources (Secret, ConfigMap, Prometheus CRD, Service) are updated to enforce that Prometheus Operator only updated created resources if necessary. - contrib/prometheus-config-reloader: Remove logic to retriev K8s ConfigMaps. These are mounted into the pod right away now.
2018-05-08 09:43:45 +02:00
}
2017-08-22 12:53:35 +02:00
func (c *MonitoringV1Client) RESTClient() rest.Interface {
return c.restClient
}
2017-11-07 17:28:21 +01:00
func NewForConfig(crdKinds *CrdKinds, apiGroup string, c *rest.Config) (*MonitoringV1Client, error) {
2017-08-22 12:53:35 +02:00
config := *c
SetConfigDefaults(apiGroup, &config)
2017-08-22 12:53:35 +02:00
client, err := rest.RESTClientFor(&config)
if err != nil {
return nil, err
}
dynamicClient, err := dynamic.NewClient(&config, schema.GroupVersion{
Group: apiGroup,
Version: Version,
})
2017-08-22 12:53:35 +02:00
if err != nil {
return nil, err
}
2017-11-07 17:28:21 +01:00
return &MonitoringV1Client{client, dynamicClient, crdKinds}, nil
2017-08-22 12:53:35 +02:00
}
func SetConfigDefaults(apiGroup string, config *rest.Config) {
2017-08-22 12:53:35 +02:00
config.GroupVersion = &schema.GroupVersion{
Group: apiGroup,
2017-08-22 12:53:35 +02:00
Version: Version,
}
config.APIPath = "/apis"
2017-11-24 16:19:04 +01:00
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
2017-08-22 12:53:35 +02:00
return
}