Skip to content

Commit

Permalink
node updater support patch node status
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng authored and waynepeking348 committed Nov 28, 2023
1 parent c9cd33b commit 01c79af
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/client/control/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// NodeUpdater is used to update Node
type NodeUpdater interface {
PatchNode(ctx context.Context, oldNode, newNode *core.Node) error
PatchNodeStatus(ctx context.Context, oldNode, newNode *core.Node) error
UpdateNode(ctx context.Context, node *core.Node, opts metav1.UpdateOptions) (*core.Node, error)
}

Expand All @@ -46,6 +47,10 @@ func (d *DummyNodeUpdater) PatchNode(_ context.Context, _, _ *core.Node) error {
return nil
}

func (d *DummyNodeUpdater) PatchNodeStatus(_ context.Context, _, _ *core.Node) error {
return nil
}

type RealNodeUpdater struct {
client kubernetes.Interface
}
Expand Down Expand Up @@ -90,3 +95,30 @@ func (r *RealNodeUpdater) PatchNode(ctx context.Context, oldNode, newNode *core.
_, err = r.client.CoreV1().Nodes().Patch(ctx, oldNode.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
return err
}

func (r *RealNodeUpdater) PatchNodeStatus(ctx context.Context, oldNode, newNode *core.Node) error {
if oldNode == nil || newNode == nil {
return fmt.Errorf("can't update a nil node")
}

oldData, err := json.Marshal(oldNode)
if err != nil {
return err
}

newData, err := json.Marshal(newNode)
if err != nil {
return err
}

patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, &core.Node{})
if err != nil {
return fmt.Errorf("failed to create merge patch for node%q: %v", oldNode.Name, err)
}
if general.JsonPathEmpty(patchBytes) {
return nil
}

_, err = r.client.CoreV1().Nodes().Patch(ctx, oldNode.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}, "status")
return err
}
238 changes: 238 additions & 0 deletions pkg/client/control/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
Copyright 2022 The Katalyst Authors.
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 control

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

katalystbase "github.com/kubewharf/katalyst-core/cmd/base"
)

func TestRealNodeUpdater_PatchNodeStatus(t *testing.T) {
t.Parallel()

type fields struct {
oldNode *v1.Node
}
type args struct {
newNode *v1.Node
}
tests := []struct {
name string
fields fields
args args
wantNode *v1.Node
wantErr assert.ErrorAssertionFunc
}{
{
name: "patch resources",
fields: fields{
oldNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
},
},
args: args{
newNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Status: v1.NodeStatus{
Capacity: v1.ResourceList{
"resource-1": resource.MustParse("10"),
},
},
},
},
wantNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Status: v1.NodeStatus{
Capacity: v1.ResourceList{
"resource-1": resource.MustParse("10"),
},
},
},
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseCtx, err := katalystbase.GenerateFakeGenericContext([]runtime.Object{tt.fields.oldNode})
assert.NoError(t, err)
r := NewRealNodeUpdater(baseCtx.Client.KubeClient)
tt.wantErr(t, r.PatchNodeStatus(context.Background(), tt.fields.oldNode, tt.args.newNode), fmt.Sprintf("PatchNodeStatus(%v, %v)", tt.fields.oldNode, tt.args.newNode))

getNode, err := baseCtx.Client.KubeClient.CoreV1().Nodes().Get(context.Background(), tt.fields.oldNode.Name, metav1.GetOptions{ResourceVersion: "0"})
assert.NoError(t, err)
assert.Equal(t, tt.wantNode, getNode)
})
}
}

func TestRealNodeUpdater_PatchNode(t *testing.T) {
t.Parallel()

type fields struct {
oldNode *v1.Node
}
type args struct {
newNode *v1.Node
}
tests := []struct {
name string
fields fields
args args
wantNode *v1.Node
wantErr assert.ErrorAssertionFunc
}{
{
name: "patch taint",
fields: fields{
oldNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
},
},
args: args{
newNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: "key-1",
Value: "value-1",
Effect: v1.TaintEffectNoSchedule,
},
},
},
},
},
wantNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: "key-1",
Value: "value-1",
Effect: v1.TaintEffectNoSchedule,
},
},
},
},
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseCtx, err := katalystbase.GenerateFakeGenericContext([]runtime.Object{tt.fields.oldNode})
assert.NoError(t, err)
r := NewRealNodeUpdater(baseCtx.Client.KubeClient)
tt.wantErr(t, r.PatchNode(context.Background(), tt.fields.oldNode, tt.args.newNode), fmt.Sprintf("PatchNodeStatus(%v, %v)", tt.fields.oldNode, tt.args.newNode))

getNode, err := baseCtx.Client.KubeClient.CoreV1().Nodes().Get(context.Background(), tt.fields.oldNode.Name, metav1.GetOptions{ResourceVersion: "0"})
assert.NoError(t, err)
assert.Equal(t, tt.wantNode, getNode)
})
}
}

func TestRealNodeUpdater_UpdateNode(t *testing.T) {
t.Parallel()

type fields struct {
oldNode *v1.Node
}
type args struct {
newNode *v1.Node
opt metav1.UpdateOptions
}
tests := []struct {
name string
fields fields
args args
wantNode *v1.Node
wantErr assert.ErrorAssertionFunc
}{
{
name: "update taint",
fields: fields{
oldNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
},
},
args: args{
newNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: "key-1",
Value: "value-1",
Effect: v1.TaintEffectNoSchedule,
},
},
},
},
},
wantNode: &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node-1",
},
Spec: v1.NodeSpec{
Taints: []v1.Taint{
{
Key: "key-1",
Value: "value-1",
Effect: v1.TaintEffectNoSchedule,
},
},
},
},
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseCtx, err := katalystbase.GenerateFakeGenericContext([]runtime.Object{tt.fields.oldNode})
assert.NoError(t, err)
r := NewRealNodeUpdater(baseCtx.Client.KubeClient)
getNode, err := r.UpdateNode(context.Background(), tt.args.newNode, tt.args.opt)
assert.NoError(t, err)
assert.Equal(t, tt.wantNode, getNode)
})
}
}

0 comments on commit 01c79af

Please sign in to comment.