lib/promscrape/discovery/kubernetes: localize Bookmark parsing code

This is a follow-up for e772d1c920
This commit is contained in:
Aliaksandr Valialkin 2021-03-11 13:06:40 +02:00
parent ccfe944ce7
commit 41f641b132
9 changed files with 43 additions and 32 deletions

View file

@ -31,7 +31,6 @@ type WatchEvent struct {
type object interface { type object interface {
key() string key() string
getTargetLabels(aw *apiWatcher) []map[string]string getTargetLabels(aw *apiWatcher) []map[string]string
resourceVersion() string
} }
// parseObjectFunc must parse object from the given data. // parseObjectFunc must parse object from the given data.
@ -397,13 +396,13 @@ func (uw *urlWatcher) readObjectUpdateStream(r io.Reader) error {
if err := d.Decode(&we); err != nil { if err := d.Decode(&we); err != nil {
return err return err
} }
switch we.Type {
case "ADDED", "MODIFIED":
o, err := uw.parseObject(we.Object) o, err := uw.parseObject(we.Object)
if err != nil { if err != nil {
return err return err
} }
key := o.key() key := o.key()
switch we.Type {
case "ADDED", "MODIFIED":
uw.objectsByKey.update(key, o) uw.objectsByKey.update(key, o)
labels := o.getTargetLabels(aw) labels := o.getTargetLabels(aw)
swos := getScrapeWorkObjectsForLabels(aw.swcFunc, labels) swos := getScrapeWorkObjectsForLabels(aw.swcFunc, labels)
@ -415,19 +414,44 @@ func (uw *urlWatcher) readObjectUpdateStream(r io.Reader) error {
} }
uw.mu.Unlock() uw.mu.Unlock()
case "DELETED": case "DELETED":
o, err := uw.parseObject(we.Object)
if err != nil {
return err
}
key := o.key()
uw.objectsByKey.remove(key) uw.objectsByKey.remove(key)
uw.mu.Lock() uw.mu.Lock()
delete(uw.swosByKey, key) delete(uw.swosByKey, key)
uw.mu.Unlock() uw.mu.Unlock()
case "BOOKMARK": case "BOOKMARK":
// See https://kubernetes.io/docs/reference/using-api/api-concepts/#watch-bookmarks // See https://kubernetes.io/docs/reference/using-api/api-concepts/#watch-bookmarks
uw.setResourceVersion(o.resourceVersion()) bm, err := parseBookmark(we.Object)
if err != nil {
return fmt.Errorf("cannot parse bookmark from %q: %w", we.Object, err)
}
uw.setResourceVersion(bm.Metadata.ResourceVersion)
default: default:
return fmt.Errorf("unexpected WatchEvent type %q for role %q", we.Type, uw.role) return fmt.Errorf("unexpected WatchEvent type %q for role %q", we.Type, uw.role)
} }
} }
} }
// Bookmark is a bookmark from Kubernetes Watch API.
// See https://kubernetes.io/docs/reference/using-api/api-concepts/#watch-bookmarks
type Bookmark struct {
Metadata struct {
ResourceVersion string
}
}
func parseBookmark(data []byte) (*Bookmark, error) {
var bm Bookmark
if err := json.Unmarshal(data, &bm); err != nil {
return nil, err
}
return &bm, nil
}
func getAPIPaths(role string, namespaces []string, selectors []Selector) []string { func getAPIPaths(role string, namespaces []string, selectors []Selector) []string {
objectName := getObjectNameByRole(role) objectName := getObjectNameByRole(role)
if objectName == "nodes" || len(namespaces) == 0 { if objectName == "nodes" || len(namespaces) == 0 {

View file

@ -160,3 +160,15 @@ func TestGetAPIPaths(t *testing.T) {
"/apis/networking.k8s.io/v1beta1/namespaces/y/ingresses?labelSelector=cde%2Cbaaa&fieldSelector=abc", "/apis/networking.k8s.io/v1beta1/namespaces/y/ingresses?labelSelector=cde%2Cbaaa&fieldSelector=abc",
}) })
} }
func TestParseBookmark(t *testing.T) {
data := `{"kind": "Pod", "apiVersion": "v1", "metadata": {"resourceVersion": "12746"} }`
bm, err := parseBookmark([]byte(data))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
expectedResourceVersion := "12746"
if bm.Metadata.ResourceVersion != expectedResourceVersion {
t.Fatalf("unexpected resourceVersion; got %q; want %q", bm.Metadata.ResourceVersion, expectedResourceVersion)
}
}

View file

@ -8,7 +8,6 @@ import (
// //
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#objectmeta-v1-meta // See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#objectmeta-v1-meta
type ObjectMeta struct { type ObjectMeta struct {
ResourceVersion string
Name string Name string
Namespace string Namespace string
UID string UID string

View file

@ -47,10 +47,6 @@ type Endpoints struct {
Subsets []EndpointSubset Subsets []EndpointSubset
} }
func (eps *Endpoints) resourceVersion() string {
return eps.Metadata.ResourceVersion
}
// EndpointSubset implements k8s endpoint subset. // EndpointSubset implements k8s endpoint subset.
// //
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpointsubset-v1-core // See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpointsubset-v1-core

View file

@ -161,10 +161,6 @@ type EndpointSlice struct {
Ports []EndpointPort Ports []EndpointPort
} }
func (eps *EndpointSlice) resourceVersion() string {
return eps.Metadata.ResourceVersion
}
// Endpoint implements kubernetes object endpoint for endpoint slice. // Endpoint implements kubernetes object endpoint for endpoint slice.
// https://v1-17.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpoint-v1beta1-discovery-k8s-io // https://v1-17.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#endpoint-v1beta1-discovery-k8s-io
type Endpoint struct { type Endpoint struct {

View file

@ -45,10 +45,6 @@ type Ingress struct {
Spec IngressSpec Spec IngressSpec
} }
func (ig *Ingress) resourceVersion() string {
return ig.Metadata.ResourceVersion
}
// IngressSpec represents ingress spec in k8s. // IngressSpec represents ingress spec in k8s.
// //
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#ingressspec-v1beta1-extensions // See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#ingressspec-v1beta1-extensions

View file

@ -48,10 +48,6 @@ type Node struct {
Status NodeStatus Status NodeStatus
} }
func (n *Node) resourceVersion() string {
return n.Metadata.ResourceVersion
}
// NodeStatus represents NodeStatus from k8s API. // NodeStatus represents NodeStatus from k8s API.
// //
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#nodestatus-v1-core // See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#nodestatus-v1-core

View file

@ -50,10 +50,6 @@ type Pod struct {
Status PodStatus Status PodStatus
} }
func (p *Pod) resourceVersion() string {
return p.Metadata.ResourceVersion
}
// PodSpec implements k8s pod spec. // PodSpec implements k8s pod spec.
// //
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#podspec-v1-core // See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#podspec-v1-core

View file

@ -47,10 +47,6 @@ type Service struct {
Spec ServiceSpec Spec ServiceSpec
} }
func (s *Service) resourceVersion() string {
return s.Metadata.ResourceVersion
}
// ServiceSpec is k8s service spec. // ServiceSpec is k8s service spec.
// //
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#servicespec-v1-core // See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#servicespec-v1-core