github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/metrics/processors/node_autoscaling_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  	kube_config "k8s.io/heapster/common/kubernetes"
    22  	"k8s.io/heapster/metrics/core"
    23  	kube_api "k8s.io/kubernetes/pkg/api"
    24  	"k8s.io/kubernetes/pkg/client/cache"
    25  	kube_client "k8s.io/kubernetes/pkg/client/unversioned"
    26  	"k8s.io/kubernetes/pkg/fields"
    27  )
    28  
    29  type NodeAutoscalingEnricher struct {
    30  	nodeLister *cache.StoreToNodeLister
    31  	reflector  *cache.Reflector
    32  }
    33  
    34  func (this *NodeAutoscalingEnricher) Name() string {
    35  	return "node_autoscaling_enricher"
    36  }
    37  
    38  func (this *NodeAutoscalingEnricher) Process(batch *core.DataBatch) (*core.DataBatch, error) {
    39  	nodes, err := this.nodeLister.List()
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	for _, node := range nodes.Items {
    44  		if metricSet, found := batch.MetricSets[core.NodeKey(node.Name)]; found {
    45  			availableCpu, _ := node.Status.Capacity[kube_api.ResourceCPU]
    46  			availableMem, _ := node.Status.Capacity[kube_api.ResourceMemory]
    47  
    48  			cpuRequested := getInt(metricSet, &core.MetricCpuRequest)
    49  			cpuUsed := getInt(metricSet, &core.MetricCpuUsageRate)
    50  			memRequested := getInt(metricSet, &core.MetricMemoryRequest)
    51  			memUsed := getInt(metricSet, &core.MetricMemoryUsage)
    52  
    53  			if availableCpu.MilliValue() != 0 {
    54  				setFloat(metricSet, &core.MetricNodeCpuUtilization, float32(cpuUsed)/float32(availableCpu.MilliValue()))
    55  				setFloat(metricSet, &core.MetricNodeCpuReservation, float32(cpuRequested)/float32(availableCpu.MilliValue()))
    56  			}
    57  
    58  			if availableMem.Value() != 0 {
    59  				setFloat(metricSet, &core.MetricNodeMemoryUtilization, float32(memUsed)/float32(availableMem.Value()))
    60  				setFloat(metricSet, &core.MetricNodeMemoryReservation, float32(memRequested)/float32(availableMem.Value()))
    61  			}
    62  		}
    63  	}
    64  	return batch, nil
    65  }
    66  
    67  func getInt(metricSet *core.MetricSet, metric *core.Metric) int64 {
    68  	if value, found := metricSet.MetricValues[metric.MetricDescriptor.Name]; found {
    69  		return value.IntValue
    70  	}
    71  	return 0
    72  }
    73  
    74  func setFloat(metricSet *core.MetricSet, metric *core.Metric, value float32) {
    75  	metricSet.MetricValues[metric.MetricDescriptor.Name] = core.MetricValue{
    76  		MetricType: core.MetricGauge,
    77  		ValueType:  core.ValueFloat,
    78  		FloatValue: value,
    79  	}
    80  }
    81  
    82  func NewNodeAutoscalingEnricher(url *url.URL) (*NodeAutoscalingEnricher, error) {
    83  	kubeConfig, err := kube_config.GetKubeClientConfig(url)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	kubeClient := kube_client.NewOrDie(kubeConfig)
    88  
    89  	// watch nodes
    90  	lw := cache.NewListWatchFromClient(kubeClient, "nodes", kube_api.NamespaceAll, fields.Everything())
    91  	nodeLister := &cache.StoreToNodeLister{Store: cache.NewStore(cache.MetaNamespaceKeyFunc)}
    92  	reflector := cache.NewReflector(lw, &kube_api.Node{}, nodeLister.Store, time.Hour)
    93  	reflector.Run()
    94  
    95  	return &NodeAutoscalingEnricher{
    96  		nodeLister: nodeLister,
    97  		reflector:  reflector,
    98  	}, nil
    99  }