Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
8b562218
Unverified
Commit
8b562218
authored
Nov 13, 2018
by
k8s-ci-robot
Committed by
GitHub
Nov 13, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #70876 from chuckha/idempotency
kubeadm: Adds tests to node patching
parents
7050a847
db3d636f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
108 additions
and
9 deletions
+108
-9
BUILD
cmd/kubeadm/app/util/apiclient/BUILD
+3
-0
idempotency.go
cmd/kubeadm/app/util/apiclient/idempotency.go
+22
-9
idempotency_test.go
cmd/kubeadm/app/util/apiclient/idempotency_test.go
+83
-0
No files found.
cmd/kubeadm/app/util/apiclient/BUILD
View file @
8b562218
...
@@ -62,14 +62,17 @@ go_test(
...
@@ -62,14 +62,17 @@ go_test(
name = "go_default_test",
name = "go_default_test",
srcs = [
srcs = [
"dryrunclient_test.go",
"dryrunclient_test.go",
"idempotency_test.go",
"init_dryrun_test.go",
"init_dryrun_test.go",
],
],
embed = [":go_default_library"],
embed = [":go_default_library"],
deps = [
deps = [
"//pkg/kubelet/apis:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/api/rbac/v1:go_default_library",
"//staging/src/k8s.io/api/rbac/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
"//staging/src/k8s.io/client-go/testing:go_default_library",
"//staging/src/k8s.io/client-go/testing:go_default_library",
],
],
)
)
cmd/kubeadm/app/util/apiclient/idempotency.go
View file @
8b562218
...
@@ -194,13 +194,16 @@ func CreateOrUpdateClusterRoleBinding(client clientset.Interface, clusterRoleBin
...
@@ -194,13 +194,16 @@ func CreateOrUpdateClusterRoleBinding(client clientset.Interface, clusterRoleBin
return
nil
return
nil
}
}
// PatchNode tries to patch a node using the following client, executing patchFn for the actual mutating logic
// PatchNodeOnce executes patchFn on the node object found by the node name.
func
PatchNode
(
client
clientset
.
Interface
,
nodeName
string
,
patchFn
func
(
*
v1
.
Node
))
error
{
// This is a condition function meant to be used with wait.Poll. false, nil
// Loop on every false return. Return with an error if raised. Exit successfully if true is returned.
// implies it is safe to try again, an error indicates no more tries should be
return
wait
.
Poll
(
constants
.
APICallRetryInterval
,
constants
.
PatchNodeTimeout
,
func
()
(
bool
,
error
)
{
// made and true indicates success.
func
PatchNodeOnce
(
client
clientset
.
Interface
,
nodeName
string
,
patchFn
func
(
*
v1
.
Node
))
func
()
(
bool
,
error
)
{
return
func
()
(
bool
,
error
)
{
// First get the node object
// First get the node object
n
,
err
:=
client
.
CoreV1
()
.
Nodes
()
.
Get
(
nodeName
,
metav1
.
GetOptions
{})
n
,
err
:=
client
.
CoreV1
()
.
Nodes
()
.
Get
(
nodeName
,
metav1
.
GetOptions
{})
if
err
!=
nil
{
if
err
!=
nil
{
// TODO this should only be for timeouts
return
false
,
nil
return
false
,
nil
}
}
...
@@ -212,7 +215,7 @@ func PatchNode(client clientset.Interface, nodeName string, patchFn func(*v1.Nod
...
@@ -212,7 +215,7 @@ func PatchNode(client clientset.Interface, nodeName string, patchFn func(*v1.Nod
oldData
,
err
:=
json
.
Marshal
(
n
)
oldData
,
err
:=
json
.
Marshal
(
n
)
if
err
!=
nil
{
if
err
!=
nil
{
return
false
,
err
return
false
,
err
ors
.
Wrapf
(
err
,
"failed to marshal unmodified node %q into JSON"
,
n
.
Name
)
}
}
// Execute the mutating function
// Execute the mutating function
...
@@ -220,22 +223,32 @@ func PatchNode(client clientset.Interface, nodeName string, patchFn func(*v1.Nod
...
@@ -220,22 +223,32 @@ func PatchNode(client clientset.Interface, nodeName string, patchFn func(*v1.Nod
newData
,
err
:=
json
.
Marshal
(
n
)
newData
,
err
:=
json
.
Marshal
(
n
)
if
err
!=
nil
{
if
err
!=
nil
{
return
false
,
err
return
false
,
err
ors
.
Wrapf
(
err
,
"failed to marshal modified node %q into JSON"
,
n
.
Name
)
}
}
patchBytes
,
err
:=
strategicpatch
.
CreateTwoWayMergePatch
(
oldData
,
newData
,
v1
.
Node
{})
patchBytes
,
err
:=
strategicpatch
.
CreateTwoWayMergePatch
(
oldData
,
newData
,
v1
.
Node
{})
if
err
!=
nil
{
if
err
!=
nil
{
return
false
,
err
return
false
,
err
ors
.
Wrap
(
err
,
"failed to create two way merge patch"
)
}
}
if
_
,
err
:=
client
.
CoreV1
()
.
Nodes
()
.
Patch
(
n
.
Name
,
types
.
StrategicMergePatchType
,
patchBytes
);
err
!=
nil
{
if
_
,
err
:=
client
.
CoreV1
()
.
Nodes
()
.
Patch
(
n
.
Name
,
types
.
StrategicMergePatchType
,
patchBytes
);
err
!=
nil
{
// TODO also check for timeouts
if
apierrors
.
IsConflict
(
err
)
{
if
apierrors
.
IsConflict
(
err
)
{
fmt
.
Println
(
"[patchnode] Temporarily unable to update node metadata due to conflict (will retry)"
)
fmt
.
Println
(
"[patchnode] Temporarily unable to update node metadata due to conflict (will retry)"
)
return
false
,
nil
return
false
,
nil
}
}
return
false
,
err
return
false
,
err
ors
.
Wrapf
(
err
,
"error patching node %q through apiserver"
,
n
.
Name
)
}
}
return
true
,
nil
return
true
,
nil
})
}
}
// PatchNode tries to patch a node using patchFn for the actual mutating logic.
// Retries are provided by the wait package.
func
PatchNode
(
client
clientset
.
Interface
,
nodeName
string
,
patchFn
func
(
*
v1
.
Node
))
error
{
// wait.Poll will rerun the condition function every interval function if
// the function returns false. If the condition function returns an error
// then the retries end and the error is returned.
return
wait
.
Poll
(
constants
.
APICallRetryInterval
,
constants
.
PatchNodeTimeout
,
PatchNodeOnce
(
client
,
nodeName
,
patchFn
))
}
}
cmd/kubeadm/app/util/apiclient/idempotency_test.go
0 → 100644
View file @
8b562218
/*
Copyright 2018 The Kubernetes 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
apiclient_test
import
(
"testing"
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
kubeletapis
"k8s.io/kubernetes/pkg/kubelet/apis"
)
func
TestPatchNodeNonErrorCases
(
t
*
testing
.
T
)
{
testcases
:=
[]
struct
{
name
string
lookupName
string
node
v1
.
Node
success
bool
}{
{
name
:
"simple update"
,
lookupName
:
"testnode"
,
node
:
v1
.
Node
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"testnode"
,
Labels
:
map
[
string
]
string
{
kubeletapis
.
LabelHostname
:
""
},
},
},
success
:
true
,
},
{
name
:
"node does not exist"
,
lookupName
:
"whale"
,
success
:
false
,
},
{
name
:
"node not labelled yet"
,
lookupName
:
"robin"
,
node
:
v1
.
Node
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"robin"
,
},
},
success
:
false
,
},
}
for
_
,
tc
:=
range
testcases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
client
:=
fake
.
NewSimpleClientset
()
_
,
err
:=
client
.
Core
()
.
Nodes
()
.
Create
(
&
tc
.
node
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed to create node to fake client: %v"
,
err
)
}
conditionFunction
:=
apiclient
.
PatchNodeOnce
(
client
,
tc
.
lookupName
,
func
(
node
*
v1
.
Node
)
{
node
.
Name
=
"testNewNode"
})
success
,
err
:=
conditionFunction
()
if
err
!=
nil
{
t
.
Fatalf
(
"did not expect error: %v"
,
err
)
}
if
success
!=
tc
.
success
{
t
.
Fatalf
(
"expected %v got %v"
,
tc
.
success
,
success
)
}
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment