Skip to content

Commit 63b4ba7

Browse files
committed
Merge pull request #11 from Random-Liu/use-patch-instead-of-update-status
Use patch instead of update status
2 parents 891d858 + 03b28c9 commit 63b4ba7

File tree

331 files changed

+48149
-14411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

331 files changed

+48149
-14411
lines changed

Godeps/Godeps.json

Lines changed: 325 additions & 239 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
all: push
22

33
# See pod.yaml for the version currently running-- bump this ahead before rebuilding!
4-
TAG = 0.1
4+
TAG = v0.1
55

66
PROJ = google_containers
77

config/kernel-monitor.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"logPath": "/log/kern.log",
3-
"bufferSize": 50,
3+
"bufferSize": 10,
44
"rules": [
55
{
66
"type": "temporary",

node-problem-detector.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ spec:
1414
command:
1515
- /node-problem-detector
1616
- --kernel-monitor=/config/kernel-monitor.json
17-
image: gcr.io/google_containers/node-problem-detector:0.1
17+
image: gcr.io/google_containers/node-problem-detector:v0.1
1818
imagePullPolicy: Always
1919
env:
2020
# Config the host ip and port of apiserver.

pkg/condition/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (c *conditionManager) sync() {
125125
for i := range c.conditions {
126126
conditions = append(conditions, problemutil.ConvertToAPICondition(c.conditions[i]))
127127
}
128-
if err := c.client.SetConditions(conditions, updateTimeout); err != nil {
128+
if err := c.client.SetConditions(conditions); err != nil {
129129
// The conditions will be updated again in future sync
130130
glog.Errorf("failed to update node conditions: %v", err)
131131
return

pkg/kernelmonitor/kernel_monitor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ func defaultCondition() types.Condition {
195195
Type: KernelDeadlockCondition,
196196
Status: false,
197197
Transition: time.Now(),
198+
Reason: "KernelHasNoDeadlock",
199+
Message: "kernel has no deadlock",
198200
}
199201
}
200202

pkg/problemclient/problem_client.go

Lines changed: 20 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,32 @@ limitations under the License.
1717
package problemclient
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"os"
22-
"time"
2323

2424
"k8s.io/kubernetes/pkg/api"
25-
"k8s.io/kubernetes/pkg/api/errors"
2625
"k8s.io/kubernetes/pkg/api/unversioned"
27-
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
28-
unversionedcore "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/unversioned"
2926
"k8s.io/kubernetes/pkg/client/record"
3027
"k8s.io/kubernetes/pkg/client/restclient"
28+
client "k8s.io/kubernetes/pkg/client/unversioned"
3129
"k8s.io/kubernetes/pkg/types"
3230
"k8s.io/kubernetes/pkg/util"
33-
34-
"github.com/golang/glog"
3531
)
3632

3733
// Client is the interface of problem client
3834
type Client interface {
3935
// GetConditions get all specifiec conditions of current node.
4036
GetConditions(conditionTypes []api.NodeConditionType) ([]*api.NodeCondition, error)
4137
// SetConditions set or update conditions of current node.
42-
// Notice that conditions with status api.ConditionFalse will be removed from the condition list, so that
43-
// we'll only have useful conditions in the condition list.
44-
SetConditions(conditions []api.NodeCondition, timeout time.Duration) error
38+
SetConditions(conditions []api.NodeCondition) error
4539
// Eventf reports the event.
4640
Eventf(eventType string, source, reason, messageFmt string, args ...interface{})
4741
}
4842

4943
type nodeProblemClient struct {
5044
nodeName string
51-
client clientset.Interface
45+
client *client.Client
5246
clock util.Clock
5347
recorders map[string]record.EventRecorder
5448
nodeRef *api.ObjectReference
@@ -62,10 +56,7 @@ func NewClientOrDie() Client {
6256
panic(err)
6357
}
6458
// TODO(random-liu): Set QPS Limit
65-
c.client, err = clientset.NewForConfig(cfg)
66-
if err != nil {
67-
panic(err)
68-
}
59+
c.client = client.NewOrDie(cfg)
6960
// TODO(random-liu): Get node name from cloud provider
7061
c.nodeName, err = os.Hostname()
7162
if err != nil {
@@ -77,7 +68,7 @@ func NewClientOrDie() Client {
7768
}
7869

7970
func (c *nodeProblemClient) GetConditions(conditionTypes []api.NodeConditionType) ([]*api.NodeCondition, error) {
80-
node, err := c.client.Core().Nodes().Get(c.nodeName)
71+
node, err := c.client.Nodes().Get(c.nodeName)
8172
if err != nil {
8273
return nil, err
8374
}
@@ -92,21 +83,16 @@ func (c *nodeProblemClient) GetConditions(conditionTypes []api.NodeConditionType
9283
return conditions, nil
9384
}
9485

95-
func (c *nodeProblemClient) SetConditions(newConditions []api.NodeCondition, timeout time.Duration) error {
86+
func (c *nodeProblemClient) SetConditions(newConditions []api.NodeCondition) error {
9687
for i := range newConditions {
9788
// Each time we update the conditions, we update the heart beat time
9889
newConditions[i].LastHeartbeatTime = unversioned.NewTime(c.clock.Now())
9990
}
100-
return c.updateNodeCondition(func(conditions []api.NodeCondition) []api.NodeCondition {
101-
for _, condition := range newConditions {
102-
if condition.Status == api.ConditionFalse {
103-
conditions = unsetCondition(condition.Type, conditions)
104-
} else {
105-
conditions = setCondition(condition, conditions)
106-
}
107-
}
108-
return conditions
109-
}, timeout)
91+
patch, err := generatePatch(newConditions)
92+
if err != nil {
93+
return nil
94+
}
95+
return c.client.Patch(api.StrategicMergePatchType).Resource("nodes").Name(c.nodeName).SubResource("status").Body(patch).Do().Error()
11096
}
11197

11298
func (c *nodeProblemClient) Eventf(eventType, source, reason, messageFmt string, args ...interface{}) {
@@ -119,60 +105,20 @@ func (c *nodeProblemClient) Eventf(eventType, source, reason, messageFmt string,
119105
recorder.Eventf(c.nodeRef, eventType, reason, messageFmt, args...)
120106
}
121107

122-
func unsetCondition(conditionType api.NodeConditionType, conditions []api.NodeCondition) []api.NodeCondition {
123-
result := []api.NodeCondition{}
124-
for _, condition := range conditions {
125-
if condition.Type != conditionType {
126-
result = append(result, condition)
127-
}
128-
}
129-
return result
130-
}
131-
132-
func setCondition(condition api.NodeCondition, conditions []api.NodeCondition) []api.NodeCondition {
133-
found := false
134-
for i := range conditions {
135-
if conditions[i].Type == condition.Type {
136-
target := &conditions[i]
137-
*target = condition
138-
found = true
139-
break
140-
}
141-
}
142-
if !found {
143-
conditions = append(conditions, condition)
144-
}
145-
return conditions
146-
}
147-
148-
func (c *nodeProblemClient) updateNodeCondition(updateFunc func([]api.NodeCondition) []api.NodeCondition, timeout time.Duration) error {
149-
updateTime := c.clock.Now()
150-
for {
151-
node, err := c.client.Core().Nodes().Get(c.nodeName)
152-
if err != nil {
153-
return err
154-
}
155-
node.Status.Conditions = updateFunc(node.Status.Conditions)
156-
_, err = c.client.Core().Nodes().UpdateStatus(node)
157-
if err != nil {
158-
if errors.IsConflict(err) {
159-
glog.Warningf("Conflicting update node status for node %q, will retry soon: %v", c.nodeName, err)
160-
if c.clock.Now().Sub(updateTime) >= timeout {
161-
return timeoutError{node: c.nodeName, timeout: timeout}
162-
}
163-
continue
164-
}
165-
return err
166-
}
167-
return nil
108+
// generatePatch generates condition patch
109+
func generatePatch(conditions []api.NodeCondition) ([]byte, error) {
110+
raw, err := json.Marshal(&conditions)
111+
if err != nil {
112+
return nil, err
168113
}
114+
return []byte(fmt.Sprintf(`{"status":{"conditions":%s}}`, raw)), nil
169115
}
170116

171117
// getEventRecorder generates a recorder for specific node name and source.
172-
func getEventRecorder(c clientset.Interface, nodeName, source string) record.EventRecorder {
118+
func getEventRecorder(c *client.Client, nodeName, source string) record.EventRecorder {
173119
eventBroadcaster := record.NewBroadcaster()
174120
recorder := eventBroadcaster.NewRecorder(api.EventSource{Component: source, Host: nodeName})
175-
eventBroadcaster.StartRecordingToSink(&unversionedcore.EventSinkImpl{Interface: c.Core().Events("")})
121+
eventBroadcaster.StartRecordingToSink(c.Events(""))
176122
return recorder
177123
}
178124

@@ -184,19 +130,3 @@ func getNodeRef(nodeName string) *api.ObjectReference {
184130
Namespace: "",
185131
}
186132
}
187-
188-
// timeoutError is the error returned by problem client when condition update timeout.
189-
type timeoutError struct {
190-
node string
191-
timeout time.Duration
192-
}
193-
194-
func (e timeoutError) Error() string {
195-
return fmt.Sprintf("update condition for node %q timeout %s", e.node, e.timeout)
196-
}
197-
198-
// IsErrTimeout checks whether a given error is timeout error.
199-
func IsErrTimeout(err error) bool {
200-
_, ok := err.(timeoutError)
201-
return ok
202-
}

0 commit comments

Comments
 (0)