-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathproblem_client.go
215 lines (191 loc) · 7.07 KB
/
problem_client.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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package problemclient
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"path/filepath"
"slices"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientset "k8s.io/client-go/kubernetes"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
"k8s.io/utils/clock"
"k8s.io/node-problem-detector/cmd/options"
"k8s.io/node-problem-detector/pkg/version"
)
// Client is the interface of problem client
type Client interface {
// DeleteDeprecatedConditions deletes the deprecated conditions of the node.
DeleteDeprecatedConditions(ctx context.Context, conditionTypeStrings []string) error
// GetConditions get all specific conditions of current node.
GetConditions(ctx context.Context, conditionTypes []v1.NodeConditionType) ([]*v1.NodeCondition, error)
// SetConditions set or update conditions of current node.
SetConditions(ctx context.Context, conditionTypes []v1.NodeCondition) error
// Eventf reports the event.
Eventf(eventType string, source, reason, messageFmt string, args ...interface{})
// GetNode returns the Node object of the node on which the
// node-problem-detector runs.
GetNode(ctx context.Context) (*v1.Node, error)
}
type nodeProblemClient struct {
nodeName string
client typedcorev1.CoreV1Interface
clock clock.Clock
recorders map[string]record.EventRecorder
nodeRef *v1.ObjectReference
eventNamespace string
}
// NewClientOrDie creates a new problem client, panics if error occurs.
func NewClientOrDie(npdo *options.NodeProblemDetectorOptions) Client {
c := &nodeProblemClient{clock: clock.RealClock{}}
// we have checked it is a valid URI after command line argument is parsed.:)
uri, _ := url.Parse(npdo.ApiServerOverride)
cfg, err := getKubeClientConfig(uri)
if err != nil {
panic(err)
}
cfg.UserAgent = fmt.Sprintf("%s/%s", filepath.Base(os.Args[0]), version.Version())
cfg.QPS = npdo.QPS
cfg.Burst = npdo.Burst
c.client = clientset.NewForConfigOrDie(cfg).CoreV1()
c.nodeName = npdo.NodeName
c.eventNamespace = npdo.EventNamespace
c.nodeRef = getNodeRef(c.eventNamespace, c.nodeName)
c.recorders = make(map[string]record.EventRecorder)
return c
}
func (c *nodeProblemClient) GetConditions(ctx context.Context, conditionTypes []v1.NodeConditionType) ([]*v1.NodeCondition, error) {
node, err := c.GetNode(ctx)
if err != nil {
return nil, err
}
conditions := []*v1.NodeCondition{}
for _, conditionType := range conditionTypes {
for _, condition := range node.Status.Conditions {
if condition.Type == conditionType {
conditions = append(conditions, &condition)
}
}
}
return conditions, nil
}
func (c *nodeProblemClient) SetConditions(ctx context.Context, newConditions []v1.NodeCondition) error {
for i := range newConditions {
// Each time we update the conditions, we update the heart beat time
newConditions[i].LastHeartbeatTime = metav1.NewTime(c.clock.Now())
}
patch, err := generatePatch(newConditions)
if err != nil {
return err
}
return retry.OnError(retry.DefaultRetry,
func(error) bool {
return true
},
func() error {
_, err := c.client.Nodes().PatchStatus(ctx, c.nodeName, patch)
return err
},
)
}
func (c *nodeProblemClient) DeleteDeprecatedConditions(ctx context.Context, conditionTypeStrings []string) error {
// get node object
klog.Infof("Deleting deprecated conditions %v (if present)...", conditionTypeStrings)
node, err := c.GetNode(ctx)
if err != nil {
return err
}
conditionTypes := generateConditionTypes(conditionTypeStrings)
// create a slice of the conditions we want to keep
newConditions := []v1.NodeCondition{}
for _, condition := range node.Status.Conditions {
if !slices.Contains(conditionTypes, condition.Type) {
newConditions = append(newConditions, condition)
} else {
klog.Infof("Deleting deprecated condition %s", condition.Type)
}
}
// update the node status if we need to,
// we only need to if the number of conditions has changed
if len(newConditions) < len(node.Status.Conditions) {
node.Status.Conditions = newConditions
return retry.OnError(retry.DefaultRetry,
func(error) bool {
return true
},
func() error {
_, err = c.client.Nodes().UpdateStatus(ctx, node, metav1.UpdateOptions{})
return err
},
)
} else {
klog.Infof("No deprecated conditions to delete")
}
return nil
}
func (c *nodeProblemClient) Eventf(eventType, source, reason, messageFmt string, args ...interface{}) {
recorder, found := c.recorders[source]
if !found {
// TODO(random-liu): If needed use separate client and QPS limit for event.
recorder = getEventRecorder(c.client, c.eventNamespace, c.nodeName, source)
c.recorders[source] = recorder
}
recorder.Eventf(c.nodeRef, eventType, reason, messageFmt, args...)
}
func (c *nodeProblemClient) GetNode(ctx context.Context) (*v1.Node, error) {
// To reduce the load on APIServer & etcd, we are serving GET operations from
// apiserver cache (the data might be slightly delayed).
return c.client.Nodes().Get(ctx, c.nodeName, metav1.GetOptions{ResourceVersion: "0"})
}
// generatePatch generates condition patch
func generatePatch(conditions []v1.NodeCondition) ([]byte, error) {
raw, err := json.Marshal(&conditions)
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf(`{"status":{"conditions":%s}}`, raw)), nil
}
// getEventRecorder generates a recorder for specific node name and source.
func getEventRecorder(c typedcorev1.CoreV1Interface, namespace, nodeName, source string) record.EventRecorder {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(klog.V(4).Infof)
recorder := eventBroadcaster.NewRecorder(runtime.NewScheme(), v1.EventSource{Component: source, Host: nodeName})
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: c.Events(namespace)})
return recorder
}
func getNodeRef(namespace, nodeName string) *v1.ObjectReference {
// TODO(random-liu): Get node to initialize the node reference
return &v1.ObjectReference{
Kind: "Node",
Name: nodeName,
UID: types.UID(nodeName),
Namespace: namespace,
}
}
func generateConditionTypes(conditionTypeStrings []string) []v1.NodeConditionType {
// convert the condition type strings to NodeConditionType
conditionTypes := []v1.NodeConditionType{}
for _, conditionTypeString := range conditionTypeStrings {
conditionTypes = append(conditionTypes, v1.NodeConditionType(conditionTypeString))
}
return conditionTypes
}