-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstate_helper.go
134 lines (124 loc) · 4.67 KB
/
state_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package controller
import (
"context"
"strconv"
"strings"
"time"
"github.com/intel/intent-driven-orchestration/pkg/common"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
)
// resourceDelimiter defines the delimiter used to constructor the keys for the resource map.
const resourceDelimiter = "_"
// getPods returns information about the PODs & their containers which are part of the pod set. Will return information on pod states, annotations and resources.
func getPods(clientSet kubernetes.Interface,
informer v1.PodInformer,
targetKey string,
targetKind string,
podErrors map[string][]common.PodError) (map[string]common.PodState, map[string]string, map[string]int64, []string) {
podStates := map[string]common.PodState{}
var hosts []string
tmp := strings.Split(targetKey, "/")
if len(tmp) <= 1 {
// TODO: for future release check if we can use a struct(ns, name) instead of string - check K8s.
klog.Errorf("invalid target key")
return nil, nil, nil, nil
}
var labels *metaV1.LabelSelector
if targetKind == "Deployment" {
deployment, err := clientSet.AppsV1().Deployments(tmp[0]).Get(context.TODO(), tmp[1], metaV1.GetOptions{})
if err != nil {
klog.Errorf("Deployment '%s' could not be found: %s.", targetKey, err)
return nil, nil, nil, nil
}
labels = deployment.Spec.Selector
} else if targetKind == "ReplicaSet" {
rs, err := clientSet.AppsV1().ReplicaSets(tmp[0]).Get(context.TODO(), tmp[1], metaV1.GetOptions{})
if err != nil {
klog.Errorf("ReplicaSet '%s' could not be found: %s.", targetKey, err)
return nil, nil, nil, nil
}
labels = rs.Spec.Selector
}
// Will ignore errors as pod list will be empty anyhow.
selector, _ := metaV1.LabelSelectorAsSelector(labels)
pods, _ := informer.Lister().Pods(tmp[0]).List(selector)
containerResources := map[string]int64{}
var annotations map[string]string
for _, pod := range pods {
if len(annotations) != len(pod.ObjectMeta.Annotations) {
annotations = pod.ObjectMeta.Annotations
}
// for each container an entry is created in the map; the key holds a container index, resource name and identifier for requests and limits.
if len(containerResources) < 1 {
for i, container := range pod.Spec.Containers {
for name, requests := range container.Resources.Requests {
containerResources[strings.Join([]string{strconv.Itoa(i), name.String(), "requests"}, resourceDelimiter)] = requests.MilliValue()
}
for name, limits := range container.Resources.Limits {
containerResources[strings.Join([]string{strconv.Itoa(i), name.String(), "limits"}, resourceDelimiter)] = limits.MilliValue()
}
}
}
podAvailability := podAvailability(podErrors[pod.Name], time.Now())
podStates[pod.Name] = common.PodState{
Availability: podAvailability,
NodeName: pod.Spec.NodeName,
State: string(pod.Status.Phase),
QoSClass: string(pod.Status.QOSClass),
}
hosts = append(hosts, pod.Spec.NodeName)
}
return podStates, annotations, containerResources, hosts
}
// getCurrentState returns the current state for an objective.
func getCurrentState(
cfg common.ControllerConfig,
clientSet kubernetes.Interface,
informer v1.PodInformer,
objective common.Intent,
podErrors map[string][]common.PodError,
profiles map[string]common.Profile) common.State {
currentObjectives := map[string]float64{}
// get current measurements
pods, annotations, resources, hosts := getPods(clientSet, informer, objective.TargetKey, objective.TargetKind, podErrors)
for item := range objective.Objectives {
profile := profiles[item]
if profile == (common.Profile{}) {
klog.Warningf("Non-existence profile references for objective target: %s.", item)
continue
}
if profile.ProfileType == common.ProfileTypeFromText("availability") {
currentObjectives[item] = PodSetAvailability(pods)
} else {
currentObjectives[item] = doQuery(profile, objective)
}
}
// bring in telemetry info
data := map[string]map[string]float64{}
for _, metric := range cfg.Metrics {
tmp := getHostTelemetry(cfg.TelemetryEndpoint, metric.Query, hosts, cfg.HostField)
data[metric.Name] = tmp
}
// and finally come up with the state.
state := common.State{
Intent: common.Intent{
Key: objective.Key,
Priority: objective.Priority,
TargetKey: objective.TargetKey,
TargetKind: objective.TargetKind,
Objectives: currentObjectives,
},
CurrentPods: pods,
CurrentData: data,
Resources: resources,
Annotations: annotations,
}
return state
}
// getDesiredState returns the desired state for an objective.
func getDesiredState(objective common.Intent) common.State {
return common.State{Intent: objective}
}