| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package kubecost
- import (
- "fmt"
- "sort"
- "strings"
- )
- type AllocationProperty string
- const (
- AllocationNilProp AllocationProperty = ""
- AllocationClusterProp AllocationProperty = "cluster"
- AllocationNodeProp AllocationProperty = "node"
- AllocationContainerProp AllocationProperty = "container"
- AllocationControllerProp AllocationProperty = "controller"
- AllocationControllerKindProp AllocationProperty = "controllerKind"
- AllocationNamespaceProp AllocationProperty = "namespace"
- AllocationPodProp AllocationProperty = "pod"
- AllocationProviderIDProp AllocationProperty = "providerID"
- AllocationServiceProp AllocationProperty = "service"
- AllocationLabelProp AllocationProperty = "label"
- AllocationAnnotationProp AllocationProperty = "annotation"
- )
- func ParseProperty(text string) (AllocationProperty, error) {
- switch strings.TrimSpace(strings.ToLower(text)) {
- case "cluster":
- return AllocationClusterProp, nil
- case "node":
- return AllocationNodeProp, nil
- case "container":
- return AllocationContainerProp, nil
- case "controller":
- return AllocationControllerProp, nil
- case "controllerKind":
- return AllocationControllerKindProp, nil
- case "namespace":
- return AllocationNamespaceProp, nil
- case "pod":
- return AllocationPodProp, nil
- case "providerid":
- return AllocationProviderIDProp, nil
- case "service":
- return AllocationServiceProp, nil
- case "label":
- return AllocationLabelProp, nil
- case "annotation":
- return AllocationAnnotationProp, nil
- }
- return AllocationNilProp, fmt.Errorf("invalid allocation property: %s", text)
- }
- func (p AllocationProperty) String() string {
- return string(p)
- }
- // AllocationProperties describes a set of Kubernetes objects.
- type AllocationProperties struct {
- Cluster string `json:"cluster,omitempty"`
- Node string `json:"node,omitempty"`
- Container string `json:"container,omitempty"`
- Controller string `json:"controller,omitempty"`
- ControllerKind string `json:"controllerKind,omitempty"`
- Namespace string `json:"namespace,omitempty"`
- Pod string `json:"pod,omitempty"`
- Services []string `json:"services,omitempty"`
- ProviderID string `json:"providerID,omitempty"`
- Labels AllocationLabels `json:"allocationLabels,omitempty"`
- Annotations AllocationAnnotations `json:"allocationAnnotations,omitempty"`
- }
- // AllocationLabels is a schema-free mapping of key/value pairs that can be
- // attributed to an Allocation
- type AllocationLabels map[string]string
- // AllocationAnnotations is a schema-free mapping of key/value pairs that can be
- // attributed to an Allocation
- type AllocationAnnotations map[string]string
- // TODO niko/etl make sure Services deep copy works correctly
- func (p *AllocationProperties) Clone() *AllocationProperties {
- if p == nil {
- return nil
- }
- clone := &AllocationProperties{}
- clone.Cluster = p.Cluster
- clone.Node = p.Node
- clone.Container = p.Container
- clone.Controller = p.Controller
- clone.ControllerKind = p.ControllerKind
- clone.Namespace = p.Namespace
- clone.Pod = p.Pod
- clone.ProviderID = p.ProviderID
- clone.Services = p.Services
- clone.Labels = p.Labels
- clone.Annotations = p.Annotations
- return clone
- }
- func (p *AllocationProperties) Equal(that *AllocationProperties) bool {
- if p == nil || that == nil {
- return false
- }
- if p.Cluster != that.Cluster {
- return false
- }
- if p.Node != that.Node {
- return false
- }
- if p.Container != that.Container {
- return false
- }
- if p.Controller != that.Controller {
- return false
- }
- if p.ControllerKind != that.ControllerKind {
- return false
- }
- if p.Namespace != that.Namespace {
- return false
- }
- if p.Pod != that.Pod {
- return false
- }
- if p.ProviderID != that.ProviderID {
- return false
- }
- pLabels := p.Labels
- thatLabels := that.Labels
- if len(pLabels) != len(thatLabels) {
- for k, pv := range pLabels {
- tv, ok := thatLabels[k]
- if !ok || tv != pv {
- return false
- }
- }
- return false
- }
- pAnnotations := p.Annotations
- thatAnnotations := that.Annotations
- if len(pAnnotations) != len(thatAnnotations) {
- for k, pv := range pAnnotations {
- tv, ok := thatAnnotations[k]
- if !ok || tv != pv {
- return false
- }
- }
- return false
- }
- pServices := p.Services
- thatServices := that.Services
- if len(pServices) != len(thatServices) {
- sort.Strings(pServices)
- sort.Strings(thatServices)
- for i, pv := range pServices {
- tv := thatServices[i]
- if tv != pv {
- return false
- }
- }
- return false
- }
- return true
- }
- func (p *AllocationProperties) String() string {
- if p == nil {
- return "<nil>"
- }
- strs := []string{}
- if p.Cluster != "" {
- strs = append(strs, "Cluster:"+p.Cluster)
- }
- if p.Node != "" {
- strs = append(strs, "Node:"+p.Node)
- }
- if p.Container != "" {
- strs = append(strs, "Container:"+p.Container)
- }
- if p.Controller != "" {
- strs = append(strs, "Controller:"+p.Controller)
- }
- if p.ControllerKind != "" {
- strs = append(strs, "ControllerKind:"+p.ControllerKind)
- }
- if p.Namespace != "" {
- strs = append(strs, "Namespace:"+p.Namespace)
- }
- if p.Pod != "" {
- strs = append(strs, "Pod:"+p.Pod)
- }
- if p.ProviderID != "" {
- strs = append(strs, "ProviderID:"+p.ProviderID)
- }
- if len(p.Services) > 0 {
- strs = append(strs, "Services:"+strings.Join(p.Services, ";"))
- }
- var labelStrs []string
- for k, prop := range p.Labels {
- labelStrs = append(labelStrs, fmt.Sprintf("%s:%s", k, prop))
- }
- strs = append(strs, fmt.Sprintf("Labels:{%s}", strings.Join(strs, ",")))
- var AnnotationStrs []string
- for k, prop := range p.Annotations {
- AnnotationStrs = append(AnnotationStrs, fmt.Sprintf("%s:%s", k, prop))
- }
- strs = append(strs, fmt.Sprintf("Annotations:{%s}", strings.Join(strs, ",")))
- return strings.Join(strs, ",")
- }
- // AggregationStrings converts a AllocationProperties object into a slice of strings
- // representing a request to aggregate by certain properties.
- // NOTE: today, the ordering of the properties *has to match the ordering
- // of the allocaiton function generateKey*
- func (p *AllocationProperties) AggregationStrings() []string {
- if p == nil {
- return []string{}
- }
- aggStrs := []string{}
- if p.Cluster != "" {
- aggStrs = append(aggStrs, AllocationClusterProp.String())
- }
- if p.Node != "" {
- aggStrs = append(aggStrs, AllocationNodeProp.String())
- }
- if p.Container != "" {
- aggStrs = append(aggStrs, AllocationContainerProp.String())
- }
- if p.Controller != "" {
- aggStrs = append(aggStrs, AllocationControllerProp.String())
- }
- if p.ControllerKind != "" {
- aggStrs = append(aggStrs, AllocationControllerKindProp.String())
- }
- if p.Namespace != "" {
- aggStrs = append(aggStrs, AllocationNamespaceProp.String())
- }
- if p.Pod != "" {
- aggStrs = append(aggStrs, AllocationPodProp.String())
- }
- if p.ProviderID != "" {
- aggStrs = append(aggStrs, AllocationProviderIDProp.String())
- }
- if len(p.Services) > 0 {
- aggStrs = append(aggStrs, AllocationServiceProp.String())
- }
- if len(p.Services) > 0 {
- aggStrs = append(aggStrs, AllocationServiceProp.String())
- }
- if len(p.Labels) > 0 {
- // e.g. expect format map[string]string{
- // "env":""
- // "app":"",
- // }
- // for aggregating by "label:app,label:env"
- labels := p.Labels
- labelAggStrs := []string{}
- for labelName := range labels {
- labelAggStrs = append(labelAggStrs, fmt.Sprintf("label:%s", labelName))
- }
- if len(labelAggStrs) > 0 {
- // Enforce alphabetical ordering, then append to aggStrs
- sort.Strings(labelAggStrs)
- for _, labelName := range labelAggStrs {
- aggStrs = append(aggStrs, labelName)
- }
- }
- }
- return aggStrs
- }
- func (p *AllocationProperties) isEmpty() bool {
- if p == nil {
- return true
- }
- return p.Equal(&AllocationProperties{})
- }
|