-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtuned.go
236 lines (214 loc) · 7.67 KB
/
tuned.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright 2022- IBM Inc. All rights reserved
* SPDX-License-Identifier: Apache2.0
*/
package controllers
///////////////////////////////////////////////////////////////////////////
//
// tuned.go
//
// ApplyProfile
// - add profile label to the node selected by selector (called before start job)
// DeleteLabel
// - delete profile label from the node (called after job done)
//
////////////////////////////////////////////////////////////////////////////
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)
const (
TUNED_RESOURCE = "tuneds.v1.tuned.openshift.io"
RENDERED_TUNED_NAME = "rendered"
TUNED_NAMESPACE = "openshift-cluster-node-tuning-operator"
PROFILE_RESOURCE = "profiles.v1.tuned.openshift.io"
TUNED_KIND = "Tuned"
RESERVED_PRIORITY_NUMBER = 0
RESERVED_AUTOTUNED_PROFILE_NAME = "auto-tuned"
BASE_PROFILE = "openshift-default"
)
type TunedHandler struct {
*kubernetes.Clientset
Log logr.Logger
DYN dynamic.Interface
}
func (t *TunedHandler) checkProfileExist(profileName string) bool {
gvr, _ := schema.ParseResourceArg(TUNED_RESOURCE)
rendered_item, err := t.DYN.Resource(*gvr).Namespace(TUNED_NAMESPACE).Get(context.TODO(), profileName, metav1.GetOptions{})
if err != nil {
t.Log.Info(fmt.Sprintf("Cannot get tuned %s/%s: %v", profileName, TUNED_NAMESPACE, err))
} else {
profiles := rendered_item.Object["spec"].(map[string]interface{})[NODESELECT_ITR_NAME].([]interface{})
for _, profile := range profiles {
if profileName == profile.(map[string]interface{})["name"].(string) {
return true
}
}
}
return false
}
func (t *TunedHandler) checkIfProfileApplied(nodes []string, profileName string) bool {
gvr, _ := schema.ParseResourceArg(PROFILE_RESOURCE)
for _, nodeName := range nodes {
nodeProfile, err := t.DYN.Resource(*gvr).Namespace(TUNED_NAMESPACE).Get(context.TODO(), nodeName, metav1.GetOptions{})
if err != nil {
t.Log.Info(fmt.Sprintf("Cannot get profile %s/%s: %v", nodeName, TUNED_NAMESPACE, err))
continue // to avoid loop
} else {
tunedProfile := nodeProfile.Object["spec"].(map[string]interface{})["config"].(map[string]interface{})["tunedProfile"].(string)
if tunedProfile != profileName {
t.Log.Info(fmt.Sprintf("Profile for %s hasn't update yet actual: %s expect: %s", nodeName, tunedProfile, profileName))
return false
}
}
}
return true // all applied
}
type patchStringValue struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}
func (t *TunedHandler) getNodeList(nodeSelector *metav1.LabelSelector) *corev1.NodeList {
var selectOptions metav1.ListOptions
if nodeSelector != nil {
labelMap, _ := metav1.LabelSelectorAsMap(nodeSelector)
selectOptions = metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labelMap).String(),
}
} else {
selectOptions = metav1.ListOptions{}
}
nodes, _ := t.Clientset.CoreV1().Nodes().List(context.TODO(), selectOptions)
return nodes
}
func (t *TunedHandler) ApplyProfile(nodeSelector *metav1.LabelSelector, profileName string) {
nodes := t.getNodeList(nodeSelector)
var labeledNodes []string
for _, node := range nodes.Items {
nodeName := node.ObjectMeta.Name
payload := []patchStringValue{{
Op: "replace",
Path: fmt.Sprintf("/metadata/labels/%s", NODESELECT_ITR_NAME),
Value: profileName,
}}
payloadBytes, _ := json.Marshal(payload)
_, err := t.Clientset.CoreV1().Nodes().Patch(context.TODO(), nodeName, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
if err != nil {
t.Log.Info(fmt.Sprintf("Cannot patch label to node %s: %v", nodeName, err))
} else {
t.Log.Info(fmt.Sprintf("Label node %s: %s=%s", nodeName, NODESELECT_ITR_NAME, profileName))
labeledNodes = append(labeledNodes, nodeName)
}
}
if t.checkProfileExist(profileName) {
// Running a loop until profile updated > risk for infinite loop
// for {
// time.Sleep(1 * time.Second)
// if t.checkIfProfileApplied(labeledNodes, profileName) {
// t.Log.Info(fmt.Sprintf("Node %v set %s=%s", labeledNodes, NODESELECT_ITR_NAME, profileName))
// break
// }
// t.Log.Info(fmt.Sprintf("Wait for %v to apply %s ...", labeledNodes, profileName))
// }
// Running only one check
time.Sleep(10 * time.Second)
if t.checkIfProfileApplied(labeledNodes, profileName) {
t.Log.Info(fmt.Sprintf("Node %v set %s=%s", labeledNodes, NODESELECT_ITR_NAME, profileName))
} else {
t.Log.Info(fmt.Sprintf("Node %v: %s is not applied yet", labeledNodes, profileName))
}
} else {
t.Log.Info(fmt.Sprintf("Profile %s not exists", profileName))
}
}
func (t *TunedHandler) DeleteLabel(nodeSelector *metav1.LabelSelector) {
nodes := t.getNodeList(nodeSelector)
for _, node := range nodes.Items {
nodeName := node.ObjectMeta.Name
payload := []patchStringValue{{
Op: "remove",
Path: fmt.Sprintf("/metadata/labels/%s", NODESELECT_ITR_NAME),
}}
payloadBytes, _ := json.Marshal(payload)
_, err := t.Clientset.CoreV1().Nodes().Patch(context.TODO(), nodeName, types.JSONPatchType, payloadBytes, metav1.PatchOptions{})
if err != nil {
t.Log.Info(fmt.Sprintf("Cannot remove label of node %s: %v", nodeName, err))
}
}
}
func GetDataProfile(tunedProfile map[TuneType]map[string]string) string {
data := "[main]\n"
data += "summary=auto-generated profile\n"
data += fmt.Sprintf("include=%s\n", BASE_PROFILE)
for tuneType, configKV := range tunedProfile {
data += fmt.Sprintf("[%s]\n", tuneType)
for key, value := range configKV {
data += fmt.Sprintf("%s=%s\n", key, value)
}
}
return data
}
func GetAutoTunedProfile(tunedProfile map[TuneType]map[string]string) *unstructured.Unstructured {
object := make(map[string]interface{})
gvr, _ := schema.ParseResourceArg(TUNED_RESOURCE)
object["apiVersion"] = gvr.Group + "/" + gvr.Version
object["kind"] = TUNED_KIND
object["metadata"] = map[string]interface{}{
"name": RESERVED_AUTOTUNED_PROFILE_NAME,
"namespace": TUNED_NAMESPACE,
}
spec := make(map[string]interface{})
spec["profile"] = []interface{}{
map[string]interface{}{
"data": GetDataProfile(tunedProfile),
"name": RESERVED_AUTOTUNED_PROFILE_NAME,
},
}
spec["recommend"] = []interface{}{
map[string]interface{}{
"match": []interface{}{
map[string]interface{}{
"label": NODESELECT_ITR_NAME,
"value": RESERVED_AUTOTUNED_PROFILE_NAME,
},
},
"priority": RESERVED_PRIORITY_NUMBER,
"profile": RESERVED_AUTOTUNED_PROFILE_NAME,
"operand": map[string]interface{}{
"debug": false,
},
},
}
object["spec"] = spec
profile := &unstructured.Unstructured{
Object: object,
}
return profile
}
func (t *TunedHandler) CreateAutoTunedProfile(tunedProfile map[TuneType]map[string]string) error {
gvr, _ := schema.ParseResourceArg(TUNED_RESOURCE)
profile := GetAutoTunedProfile(tunedProfile)
t.Log.Info(fmt.Sprintf("Tuned Profile: %v", tunedProfile))
_, err := t.DYN.Resource(*gvr).Namespace(TUNED_NAMESPACE).Create(context.TODO(), profile, metav1.CreateOptions{})
return err
}
func (t *TunedHandler) DeleteAutoTunedProfile() error {
gvr, _ := schema.ParseResourceArg(TUNED_RESOURCE)
return t.DYN.Resource(*gvr).Namespace(TUNED_NAMESPACE).Delete(context.TODO(), RESERVED_AUTOTUNED_PROFILE_NAME, metav1.DeleteOptions{})
}
func (t *TunedHandler) IsSameProfile(tunedProfile map[TuneType]map[string]string, cmpTunedProfile map[TuneType]map[string]string) bool {
return reflect.DeepEqual(tunedProfile, cmpTunedProfile)
}