github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/processors/namespace_based_enricher.go (about) 1 // Copyright 2015 Google Inc. All Rights Reserved. 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 processors 16 17 import ( 18 "net/url" 19 "time" 20 21 "github.com/golang/glog" 22 23 kube_api "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/fields" 25 "k8s.io/apimachinery/pkg/util/wait" 26 kube_client "k8s.io/client-go/kubernetes" 27 "k8s.io/client-go/tools/cache" 28 kube_config "k8s.io/heapster/common/kubernetes" 29 "k8s.io/heapster/metrics/core" 30 ) 31 32 type NamespaceBasedEnricher struct { 33 store cache.Store 34 reflector *cache.Reflector 35 } 36 37 func (this *NamespaceBasedEnricher) Name() string { 38 return "namespace_based_enricher" 39 } 40 41 func (this *NamespaceBasedEnricher) Process(batch *core.DataBatch) (*core.DataBatch, error) { 42 for _, ms := range batch.MetricSets { 43 this.addNamespaceInfo(ms) 44 } 45 return batch, nil 46 } 47 48 // Adds UID to all namespaced elements. 49 func (this *NamespaceBasedEnricher) addNamespaceInfo(metricSet *core.MetricSet) { 50 metricSetType, found := metricSet.Labels[core.LabelMetricSetType.Key] 51 if !found { 52 return 53 } 54 if metricSetType != core.MetricSetTypePodContainer && 55 metricSetType != core.MetricSetTypePod && 56 metricSetType != core.MetricSetTypeNamespace { 57 return 58 } 59 60 namespaceName, found := metricSet.Labels[core.LabelNamespaceName.Key] 61 if !found { 62 return 63 } 64 65 nsObj, exists, err := this.store.GetByKey(namespaceName) 66 if exists && err == nil { 67 namespace, ok := nsObj.(*kube_api.Namespace) 68 if ok { 69 metricSet.Labels[core.LabelPodNamespaceUID.Key] = string(namespace.UID) 70 } else { 71 glog.Errorf("Wrong namespace store content") 72 } 73 } else if err != nil { 74 glog.Warningf("Failed to get namespace %s: %v", namespaceName, err) 75 } else if !exists { 76 glog.Warningf("Namespace doesn't exist: %s", namespaceName) 77 } 78 } 79 80 func NewNamespaceBasedEnricher(url *url.URL) (*NamespaceBasedEnricher, error) { 81 kubeConfig, err := kube_config.GetKubeClientConfig(url) 82 if err != nil { 83 return nil, err 84 } 85 kubeClient := kube_client.NewForConfigOrDie(kubeConfig) 86 87 // watch nodes 88 lw := cache.NewListWatchFromClient(kubeClient.CoreV1().RESTClient(), "namespaces", kube_api.NamespaceAll, fields.Everything()) 89 store := cache.NewStore(cache.MetaNamespaceKeyFunc) 90 reflector := cache.NewReflector(lw, &kube_api.Namespace{}, store, time.Hour) 91 go reflector.Run(wait.NeverStop) 92 93 return &NamespaceBasedEnricher{ 94 store: store, 95 reflector: reflector, 96 }, nil 97 }