1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 03:38:43 +00:00

pkg/client: add versioned monitoring client

This commit is contained in:
Frederic Branczyk 2017-01-06 17:59:22 +01:00
parent ff9bae2755
commit 18a5ba18e4
No known key found for this signature in database
GPG key ID: CA14788B1E48B256
5 changed files with 651 additions and 1 deletions

View file

@ -0,0 +1,189 @@
// 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 v1alpha1
import (
"encoding/json"
"fmt"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/pkg/api/v1"
metav1 "k8s.io/client-go/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/pkg/runtime"
"k8s.io/client-go/pkg/watch"
"k8s.io/client-go/rest"
)
const (
TPRAlertmanagersKind = "Alertmanager"
TPRAlertmanagerName = "alertmanagers"
)
type AlertmanagersGetter interface {
Alertmanagers(namespace string) AlertmanagerInterface
}
type AlertmanagerInterface interface {
Create(*Alertmanager) (*Alertmanager, error)
Get(name string) (*Alertmanager, error)
Update(*Alertmanager) (*Alertmanager, error)
Delete(name string, options *v1.DeleteOptions) error
List(opts v1.ListOptions) (runtime.Object, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
}
type alertmanagers struct {
restClient rest.Interface
client *dynamic.ResourceClient
ns string
}
func newAlertmanagers(r rest.Interface, c *dynamic.Client, namespace string) *alertmanagers {
return &alertmanagers{
r,
c.Resource(
&metav1.APIResource{
Kind: TPRAlertmanagersKind,
Name: TPRAlertmanagerName,
Namespaced: true,
},
namespace,
),
namespace,
}
}
func (a *alertmanagers) Create(o *Alertmanager) (*Alertmanager, error) {
ua, err := UnstructuredFromAlertmanager(o)
if err != nil {
return nil, fmt.Errorf("unstructuring alertmanager failed: %s", err)
}
ua, err = a.client.Create(ua)
if err != nil {
return nil, fmt.Errorf("creating alertmanager failed: %s", err)
}
return AlertmanagerFromUnstructured(ua)
}
func (a *alertmanagers) Get(name string) (*Alertmanager, error) {
obj, err := a.client.Get(name)
if err != nil {
return nil, fmt.Errorf("retrieving alertmanager failed: %s", err)
}
return AlertmanagerFromUnstructured(obj)
}
func (a *alertmanagers) Update(o *Alertmanager) (*Alertmanager, error) {
ua, err := UnstructuredFromAlertmanager(o)
if err != nil {
return nil, fmt.Errorf("unstructuring alertmanager failed: %s", err)
}
ua, err = a.client.Update(ua)
if err != nil {
return nil, fmt.Errorf("updating alertmanager failed: %s", err)
}
return AlertmanagerFromUnstructured(ua)
}
func (a *alertmanagers) Delete(name string, options *v1.DeleteOptions) error {
return a.client.Delete(name, options)
}
func (a *alertmanagers) List(opts v1.ListOptions) (runtime.Object, error) {
req := a.restClient.Get().
Namespace(a.ns).
Resource("alertmanagers").
// VersionedParams(&options, api.ParameterCodec)
FieldsSelectorParam(nil)
b, err := req.DoRaw()
if err != nil {
return nil, err
}
var p AlertmanagerList
return &p, json.Unmarshal(b, &p)
}
func (a *alertmanagers) Watch(opts v1.ListOptions) (watch.Interface, error) {
r, err := a.restClient.Get().
Prefix("watch").
Namespace(a.ns).
Resource("alertmanagers").
// VersionedParams(&options, api.ParameterCodec).
FieldsSelectorParam(nil).
Stream()
if err != nil {
return nil, err
}
return watch.NewStreamWatcher(&alertmanagerDecoder{
dec: json.NewDecoder(r),
close: r.Close,
}), nil
}
// AlertmanagerFromUnstructured unmarshals an Alertmanager object from dynamic client's unstructured
func AlertmanagerFromUnstructured(r *unstructured.Unstructured) (*Alertmanager, error) {
b, err := json.Marshal(r.Object)
if err != nil {
return nil, err
}
var a Alertmanager
if err := json.Unmarshal(b, &a); err != nil {
return nil, err
}
a.TypeMeta.Kind = TPRAlertmanagersKind
a.TypeMeta.APIVersion = TPRGroup + "/" + TPRVersion
return &a, nil
}
// UnstructuredFromAlertmanager marshals an Alertmanager object into dynamic client's unstructured
func UnstructuredFromAlertmanager(a *Alertmanager) (*unstructured.Unstructured, error) {
a.TypeMeta.Kind = TPRAlertmanagersKind
a.TypeMeta.APIVersion = TPRGroup + "/" + TPRVersion
b, err := json.Marshal(a)
if err != nil {
return nil, err
}
var r unstructured.Unstructured
if err := json.Unmarshal(b, &r.Object); err != nil {
return nil, err
}
return &r, nil
}
type alertmanagerDecoder struct {
dec *json.Decoder
close func() error
}
func (d *alertmanagerDecoder) Close() {
d.close()
}
func (d *alertmanagerDecoder) Decode() (action watch.EventType, object runtime.Object, err error) {
var e struct {
Type watch.EventType
Object Alertmanager
}
if err := d.dec.Decode(&e); err != nil {
return watch.Error, nil, err
}
return e.Type, &e.Object, nil
}

View file

@ -0,0 +1,82 @@
// 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 v1alpha1
import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/runtime/schema"
"k8s.io/client-go/pkg/runtime/serializer"
"k8s.io/client-go/rest"
)
const (
TPRGroup = "monitoring.coreos.com"
TPRVersion = "v1alpha1"
)
type MonitoringV1alpha1Interface interface {
RESTClient() rest.Interface
PrometheusesGetter
AlertmanagersGetter
ServiceMonitorsGetter
}
type MonitoringV1alpha1Client struct {
restClient rest.Interface
dynamicClient *dynamic.Client
}
func (c *MonitoringV1alpha1Client) Prometheuses(namespace string) PrometheusInterface {
return newPrometheuses(c.restClient, c.dynamicClient, namespace)
}
func (c *MonitoringV1alpha1Client) Alertmanagers(namespace string) AlertmanagerInterface {
return newAlertmanagers(c.restClient, c.dynamicClient, namespace)
}
func (c *MonitoringV1alpha1Client) ServiceMonitors(namespace string) ServiceMonitorInterface {
return newServiceMonitors(c.restClient, c.dynamicClient, namespace)
}
func (c *MonitoringV1alpha1Client) RESTClient() rest.Interface {
return c.restClient
}
func NewForConfig(c *rest.Config) (*MonitoringV1alpha1Client, error) {
config := *c
setConfigDefaults(&config)
client, err := rest.RESTClientFor(&config)
if err != nil {
return nil, err
}
dynamicClient, err := dynamic.NewClient(&config)
if err != nil {
return nil, err
}
return &MonitoringV1alpha1Client{client, dynamicClient}, nil
}
func setConfigDefaults(config *rest.Config) {
config.GroupVersion = &schema.GroupVersion{
Group: TPRGroup,
Version: TPRVersion,
}
config.APIPath = "/apis"
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs}
return
}

View file

@ -0,0 +1,188 @@
// 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 v1alpha1
import (
"encoding/json"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/pkg/api/v1"
metav1 "k8s.io/client-go/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/pkg/runtime"
"k8s.io/client-go/pkg/watch"
"k8s.io/client-go/rest"
)
const (
TPRPrometheusesKind = "Prometheus"
TPRPrometheusName = "prometheuses"
)
type PrometheusesGetter interface {
Prometheuses(namespace string) PrometheusInterface
}
type PrometheusInterface interface {
Create(*Prometheus) (*Prometheus, error)
Get(name string) (*Prometheus, error)
Update(*Prometheus) (*Prometheus, error)
Delete(name string, options *v1.DeleteOptions) error
List(opts v1.ListOptions) (runtime.Object, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
}
type prometheuses struct {
restClient rest.Interface
client *dynamic.ResourceClient
ns string
}
func newPrometheuses(r rest.Interface, c *dynamic.Client, namespace string) *prometheuses {
return &prometheuses{
r,
c.Resource(
&metav1.APIResource{
Kind: TPRPrometheusesKind,
Name: TPRPrometheusName,
Namespaced: true,
},
namespace,
),
namespace,
}
}
func (p *prometheuses) Create(o *Prometheus) (*Prometheus, error) {
up, err := UnstructuredFromPrometheus(o)
if err != nil {
return nil, err
}
up, err = p.client.Create(up)
if err != nil {
return nil, err
}
return PrometheusFromUnstructured(up)
}
func (p *prometheuses) Get(name string) (*Prometheus, error) {
obj, err := p.client.Get(name)
if err != nil {
return nil, err
}
return PrometheusFromUnstructured(obj)
}
func (p *prometheuses) Update(o *Prometheus) (*Prometheus, error) {
up, err := UnstructuredFromPrometheus(o)
if err != nil {
return nil, err
}
up, err = p.client.Update(up)
if err != nil {
return nil, err
}
return PrometheusFromUnstructured(up)
}
func (p *prometheuses) Delete(name string, options *v1.DeleteOptions) error {
return p.client.Delete(name, options)
}
func (p *prometheuses) List(opts v1.ListOptions) (runtime.Object, error) {
req := p.restClient.Get().
Namespace(p.ns).
Resource("prometheuses").
// VersionedParams(&options, v1.ParameterCodec)
FieldsSelectorParam(nil)
b, err := req.DoRaw()
if err != nil {
return nil, err
}
var prom PrometheusList
return &prom, json.Unmarshal(b, &prom)
}
func (p *prometheuses) Watch(opts v1.ListOptions) (watch.Interface, error) {
r, err := p.restClient.Get().
Prefix("watch").
Namespace(p.ns).
Resource("prometheuses").
// VersionedParams(&options, v1.ParameterCodec).
FieldsSelectorParam(nil).
Stream()
if err != nil {
return nil, err
}
return watch.NewStreamWatcher(&prometheusDecoder{
dec: json.NewDecoder(r),
close: r.Close,
}), nil
}
// PrometheusFromUnstructured unmarshals a Prometheus object from dynamic client's unstructured
func PrometheusFromUnstructured(r *unstructured.Unstructured) (*Prometheus, error) {
b, err := json.Marshal(r.Object)
if err != nil {
return nil, err
}
var p Prometheus
if err := json.Unmarshal(b, &p); err != nil {
return nil, err
}
p.TypeMeta.Kind = TPRPrometheusesKind
p.TypeMeta.APIVersion = TPRGroup + "/" + TPRVersion
return &p, nil
}
// UnstructuredFromPrometheus marshals a Prometheus object into dynamic client's unstructured
func UnstructuredFromPrometheus(p *Prometheus) (*unstructured.Unstructured, error) {
p.TypeMeta.Kind = TPRPrometheusesKind
p.TypeMeta.APIVersion = TPRGroup + "/" + TPRVersion
b, err := json.Marshal(p)
if err != nil {
return nil, err
}
var r unstructured.Unstructured
if err := json.Unmarshal(b, &r.Object); err != nil {
return nil, err
}
return &r, nil
}
type prometheusDecoder struct {
dec *json.Decoder
close func() error
}
func (d *prometheusDecoder) Close() {
d.close()
}
func (d *prometheusDecoder) Decode() (action watch.EventType, object runtime.Object, err error) {
var e struct {
Type watch.EventType
Object Prometheus
}
if err := d.dec.Decode(&e); err != nil {
return watch.Error, nil, err
}
return e.Type, &e.Object, nil
}

View file

@ -0,0 +1,188 @@
// 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 v1alpha1
import (
"encoding/json"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/pkg/api/v1"
metav1 "k8s.io/client-go/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/pkg/runtime"
"k8s.io/client-go/pkg/watch"
"k8s.io/client-go/rest"
)
const (
TPRServiceMonitorsKind = "ServiceMonitor"
TPRServiceMonitorName = "servicemonitors"
)
type ServiceMonitorsGetter interface {
ServiceMonitors(namespace string) *dynamic.ResourceClient
}
type ServiceMonitorInterface interface {
Create(*ServiceMonitor) (*ServiceMonitor, error)
Get(name string) (*ServiceMonitor, error)
Update(*ServiceMonitor) (*ServiceMonitor, error)
Delete(name string, options *v1.DeleteOptions) error
List(opts v1.ListOptions) (runtime.Object, error)
Watch(opts v1.ListOptions) (watch.Interface, error)
}
type servicemonitors struct {
restClient rest.Interface
client *dynamic.ResourceClient
ns string
}
func newServiceMonitors(r rest.Interface, c *dynamic.Client, namespace string) *servicemonitors {
return &servicemonitors{
r,
c.Resource(
&metav1.APIResource{
Kind: TPRServiceMonitorsKind,
Name: TPRServiceMonitorName,
Namespaced: true,
},
namespace,
),
namespace,
}
}
func (s *servicemonitors) Create(o *ServiceMonitor) (*ServiceMonitor, error) {
us, err := UnstructuredFromServiceMonitor(o)
if err != nil {
return nil, err
}
us, err = s.client.Create(us)
if err != nil {
return nil, err
}
return ServiceMonitorFromUnstructured(us)
}
func (s *servicemonitors) Get(name string) (*ServiceMonitor, error) {
obj, err := s.client.Get(name)
if err != nil {
return nil, err
}
return ServiceMonitorFromUnstructured(obj)
}
func (s *servicemonitors) Update(o *ServiceMonitor) (*ServiceMonitor, error) {
us, err := UnstructuredFromServiceMonitor(o)
if err != nil {
return nil, err
}
us, err = s.client.Update(us)
if err != nil {
return nil, err
}
return ServiceMonitorFromUnstructured(us)
}
func (s *servicemonitors) Delete(name string, options *v1.DeleteOptions) error {
return s.client.Delete(name, options)
}
func (s *servicemonitors) List(opts v1.ListOptions) (runtime.Object, error) {
req := s.restClient.Get().
Namespace(s.ns).
Resource("servicemonitors").
// VersionedParams(&options, v1.ParameterCodec)
FieldsSelectorParam(nil)
b, err := req.DoRaw()
if err != nil {
return nil, err
}
var sm ServiceMonitorList
return &sm, json.Unmarshal(b, &sm)
}
func (s *servicemonitors) Watch(opts v1.ListOptions) (watch.Interface, error) {
r, err := s.restClient.Get().
Prefix("watch").
Namespace(s.ns).
Resource("servicemonitors").
// VersionedParams(&options, v1.ParameterCodec).
FieldsSelectorParam(nil).
Stream()
if err != nil {
return nil, err
}
return watch.NewStreamWatcher(&serviceMonitorDecoder{
dec: json.NewDecoder(r),
close: r.Close,
}), nil
}
// ServiceMonitorFromUnstructured unmarshals a ServiceMonitor object from dynamic client's unstructured
func ServiceMonitorFromUnstructured(r *unstructured.Unstructured) (*ServiceMonitor, error) {
b, err := json.Marshal(r.Object)
if err != nil {
return nil, err
}
var s ServiceMonitor
if err := json.Unmarshal(b, &s); err != nil {
return nil, err
}
s.TypeMeta.Kind = TPRServiceMonitorsKind
s.TypeMeta.APIVersion = TPRGroup + "/" + TPRVersion
return &s, nil
}
// UnstructuredFromServiceMonitor marshals a ServiceMonitor object into dynamic client's unstructured
func UnstructuredFromServiceMonitor(s *ServiceMonitor) (*unstructured.Unstructured, error) {
s.TypeMeta.Kind = TPRServiceMonitorsKind
s.TypeMeta.APIVersion = TPRGroup + "/" + TPRVersion
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
var r unstructured.Unstructured
if err := json.Unmarshal(b, &r.Object); err != nil {
return nil, err
}
return &r, nil
}
type serviceMonitorDecoder struct {
dec *json.Decoder
close func() error
}
func (d *serviceMonitorDecoder) Close() {
d.close()
}
func (d *serviceMonitorDecoder) Decode() (action watch.EventType, object runtime.Object, err error) {
var e struct {
Type watch.EventType
Object ServiceMonitor
}
if err := d.dec.Decode(&e); err != nil {
return watch.Error, nil, err
}
return e.Type, &e.Object, nil
}

View file

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package spec
package v1alpha1
import (
"k8s.io/client-go/pkg/api/v1"
@ -170,3 +170,6 @@ type Selector struct {
// Currently the selector is only used for namespaces which require more complex
// implementation to support label selections.
}
type ListOptions v1.ListOptions
type DeleteOptions v1.DeleteOptions