Use coordination v1 API

This commit is contained in:
wojtekt 2019-06-27 14:31:33 +02:00
parent f20876908f
commit e8ca50c43c
6 changed files with 41 additions and 44 deletions

View File

@ -6,14 +6,14 @@ go_library(
importpath = "k8s.io/kubernetes/pkg/kubelet/nodelease",
visibility = ["//visibility:public"],
deps = [
"//staging/src/k8s.io/api/coordination/v1beta1:go_default_library",
"//staging/src/k8s.io/api/coordination/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/clock:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/coordination/v1beta1:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/coordination/v1:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
],
@ -24,7 +24,7 @@ go_test(
srcs = ["controller_test.go"],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/api/coordination/v1beta1:go_default_library",
"//staging/src/k8s.io/api/coordination/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",

View File

@ -20,14 +20,14 @@ import (
"fmt"
"time"
coordv1beta1 "k8s.io/api/coordination/v1beta1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
coordclientset "k8s.io/client-go/kubernetes/typed/coordination/v1beta1"
coordclientset "k8s.io/client-go/kubernetes/typed/coordination/v1"
"k8s.io/utils/pointer"
"k8s.io/klog"
@ -35,9 +35,6 @@ import (
const (
// renewInterval is the interval at which the lease is renewed
// TODO(mtaufen): 10s was the decision in the KEP, to keep the behavior as close to the
// current default behavior as possible. In the future, we should determine a reasonable
// fraction of the lease duration at which to renew, and use that instead.
renewInterval = 10 * time.Second
// maxUpdateRetries is the number of immediate, successive retries the Kubelet will attempt
// when renewing the lease before it waits for the renewal interval before trying again,
@ -66,7 +63,7 @@ type controller struct {
func NewController(clock clock.Clock, client clientset.Interface, holderIdentity string, leaseDurationSeconds int32, onRepeatedHeartbeatFailure func()) Controller {
var leaseClient coordclientset.LeaseInterface
if client != nil {
leaseClient = client.CoordinationV1beta1().Leases(corev1.NamespaceNodeLease)
leaseClient = client.CoordinationV1().Leases(corev1.NamespaceNodeLease)
}
return &controller{
client: client,
@ -102,9 +99,9 @@ func (c *controller) sync() {
// and uses exponentially increasing waits to prevent overloading the API server
// with retries. Returns the lease, and true if this call created the lease,
// false otherwise.
func (c *controller) backoffEnsureLease() (*coordv1beta1.Lease, bool) {
func (c *controller) backoffEnsureLease() (*coordinationv1.Lease, bool) {
var (
lease *coordv1beta1.Lease
lease *coordinationv1.Lease
created bool
err error
)
@ -124,7 +121,7 @@ func (c *controller) backoffEnsureLease() (*coordv1beta1.Lease, bool) {
// ensureLease creates the lease if it does not exist. Returns the lease and
// a bool (true if this call created the lease), or any error that occurs.
func (c *controller) ensureLease() (*coordv1beta1.Lease, bool, error) {
func (c *controller) ensureLease() (*coordinationv1.Lease, bool, error) {
lease, err := c.leaseClient.Get(c.holderIdentity, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
// lease does not exist, create it
@ -143,7 +140,7 @@ func (c *controller) ensureLease() (*coordv1beta1.Lease, bool, error) {
// retryUpdateLease attempts to update the lease for maxUpdateRetries,
// call this once you're sure the lease has been created
func (c *controller) retryUpdateLease(base *coordv1beta1.Lease) error {
func (c *controller) retryUpdateLease(base *coordinationv1.Lease) error {
for i := 0; i < maxUpdateRetries; i++ {
_, err := c.leaseClient.Update(c.newLease(base))
if err == nil {
@ -164,17 +161,17 @@ func (c *controller) retryUpdateLease(base *coordv1beta1.Lease) error {
// newLease constructs a new lease if base is nil, or returns a copy of base
// with desired state asserted on the copy.
func (c *controller) newLease(base *coordv1beta1.Lease) *coordv1beta1.Lease {
func (c *controller) newLease(base *coordinationv1.Lease) *coordinationv1.Lease {
// Use the bare minimum set of fields; other fields exist for debugging/legacy,
// but we don't need to make node heartbeats more complicated by using them.
var lease *coordv1beta1.Lease
var lease *coordinationv1.Lease
if base == nil {
lease = &coordv1beta1.Lease{
lease = &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: c.holderIdentity,
Namespace: corev1.NamespaceNodeLease,
},
Spec: coordv1beta1.LeaseSpec{
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr(c.holderIdentity),
LeaseDurationSeconds: pointer.Int32Ptr(c.leaseDurationSeconds),
},

View File

@ -21,7 +21,7 @@ import (
"testing"
"time"
coordv1beta1 "k8s.io/api/coordination/v1beta1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
@ -47,8 +47,8 @@ func TestNewLease(t *testing.T) {
cases := []struct {
desc string
controller *controller
base *coordv1beta1.Lease
expect *coordv1beta1.Lease
base *coordinationv1.Lease
expect *coordinationv1.Lease
}{
{
desc: "nil base without node",
@ -59,12 +59,12 @@ func TestNewLease(t *testing.T) {
clock: fakeClock,
},
base: nil,
expect: &coordv1beta1.Lease{
expect: &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: node.Name,
Namespace: corev1.NamespaceNodeLease,
},
Spec: coordv1beta1.LeaseSpec{
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr(node.Name),
LeaseDurationSeconds: pointer.Int32Ptr(10),
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
@ -80,7 +80,7 @@ func TestNewLease(t *testing.T) {
clock: fakeClock,
},
base: nil,
expect: &coordv1beta1.Lease{
expect: &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: node.Name,
Namespace: corev1.NamespaceNodeLease,
@ -93,7 +93,7 @@ func TestNewLease(t *testing.T) {
},
},
},
Spec: coordv1beta1.LeaseSpec{
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr(node.Name),
LeaseDurationSeconds: pointer.Int32Ptr(10),
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
@ -108,18 +108,18 @@ func TestNewLease(t *testing.T) {
leaseDurationSeconds: 10,
clock: fakeClock,
},
base: &coordv1beta1.Lease{
base: &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: node.Name,
Namespace: corev1.NamespaceNodeLease,
},
Spec: coordv1beta1.LeaseSpec{
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr(node.Name),
LeaseDurationSeconds: pointer.Int32Ptr(10),
RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-10 * time.Second)},
},
},
expect: &coordv1beta1.Lease{
expect: &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: node.Name,
Namespace: corev1.NamespaceNodeLease,
@ -132,7 +132,7 @@ func TestNewLease(t *testing.T) {
},
},
},
Spec: coordv1beta1.LeaseSpec{
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr(node.Name),
LeaseDurationSeconds: pointer.Int32Ptr(10),
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
@ -147,7 +147,7 @@ func TestNewLease(t *testing.T) {
leaseDurationSeconds: 10,
clock: fakeClock,
},
base: &coordv1beta1.Lease{
base: &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: node.Name,
Namespace: corev1.NamespaceNodeLease,
@ -160,13 +160,13 @@ func TestNewLease(t *testing.T) {
},
},
},
Spec: coordv1beta1.LeaseSpec{
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr(node.Name),
LeaseDurationSeconds: pointer.Int32Ptr(10),
RenewTime: &metav1.MicroTime{Time: fakeClock.Now().Add(-10 * time.Second)},
},
},
expect: &coordv1beta1.Lease{
expect: &coordinationv1.Lease{
ObjectMeta: metav1.ObjectMeta{
Name: node.Name,
Namespace: corev1.NamespaceNodeLease,
@ -179,7 +179,7 @@ func TestNewLease(t *testing.T) {
},
},
},
Spec: coordv1beta1.LeaseSpec{
Spec: coordinationv1.LeaseSpec{
HolderIdentity: pointer.StringPtr(node.Name),
LeaseDurationSeconds: pointer.Int32Ptr(10),
RenewTime: &metav1.MicroTime{Time: fakeClock.Now()},
@ -221,7 +221,7 @@ func TestRetryUpdateLease(t *testing.T) {
{
desc: "no errors",
updateReactor: func(action clienttesting.Action) (bool, runtime.Object, error) {
return true, &coordv1beta1.Lease{}, nil
return true, &coordinationv1.Lease{}, nil
},
getReactor: nil,
onRepeatedHeartbeatFailure: nil,
@ -248,12 +248,12 @@ func TestRetryUpdateLease(t *testing.T) {
case 2:
return true, nil, optimistcLockUpdateErr
default:
return true, &coordv1beta1.Lease{}, nil
return true, &coordinationv1.Lease{}, nil
}
}
}(),
getReactor: func(action clienttesting.Action) (bool, runtime.Object, error) {
return true, &coordv1beta1.Lease{}, nil
return true, &coordinationv1.Lease{}, nil
},
onRepeatedHeartbeatFailure: func() { t.Fatalf("onRepeatedHeartbeatFailure called") },
expectErr: false,
@ -271,7 +271,7 @@ func TestRetryUpdateLease(t *testing.T) {
c := &controller{
clock: clock.NewFakeClock(time.Now()),
client: cl,
leaseClient: cl.CoordinationV1beta1().Leases(corev1.NamespaceNodeLease),
leaseClient: cl.CoordinationV1().Leases(corev1.NamespaceNodeLease),
holderIdentity: node.Name,
leaseDurationSeconds: 10,
onRepeatedHeartbeatFailure: tc.onRepeatedHeartbeatFailure,

View File

@ -54,7 +54,7 @@ go_library(
"//pkg/kubelet/sysctl:go_default_library",
"//pkg/security/apparmor:go_default_library",
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
"//staging/src/k8s.io/api/coordination/v1beta1:go_default_library",
"//staging/src/k8s.io/api/coordination/v1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",

View File

@ -20,7 +20,7 @@ import (
"fmt"
"time"
coordinationv1beta1 "k8s.io/api/coordination/v1beta1"
coordinationv1 "k8s.io/api/coordination/v1"
v1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -48,10 +48,10 @@ var _ = framework.KubeDescribe("NodeLease", func() {
ginkgo.Context("when the NodeLease feature is enabled", func() {
ginkgo.It("the kubelet should create and update a lease in the kube-node-lease namespace", func() {
leaseClient := f.ClientSet.CoordinationV1beta1().Leases(v1.NamespaceNodeLease)
leaseClient := f.ClientSet.CoordinationV1().Leases(v1.NamespaceNodeLease)
var (
err error
lease *coordinationv1beta1.Lease
lease *coordinationv1.Lease
)
ginkgo.By("check that lease for this Kubelet exists in the kube-node-lease namespace")
gomega.Eventually(func() error {
@ -91,9 +91,9 @@ var _ = framework.KubeDescribe("NodeLease", func() {
ginkgo.By("wait until there is node lease")
var err error
var lease *coordinationv1beta1.Lease
var lease *coordinationv1.Lease
gomega.Eventually(func() error {
lease, err = f.ClientSet.CoordinationV1beta1().Leases(v1.NamespaceNodeLease).Get(nodeName, metav1.GetOptions{})
lease, err = f.ClientSet.CoordinationV1().Leases(v1.NamespaceNodeLease).Get(nodeName, metav1.GetOptions{})
if err != nil {
return err
}
@ -170,7 +170,7 @@ func getHeartbeatTimeAndStatus(clientSet clientset.Interface, nodeName string) (
return heartbeatTime, node.Status
}
func expectLease(lease *coordinationv1beta1.Lease, nodeName string) error {
func expectLease(lease *coordinationv1.Lease, nodeName string) error {
// expect values for HolderIdentity, LeaseDurationSeconds, and RenewTime
if lease.Spec.HolderIdentity == nil {
return fmt.Errorf("Spec.HolderIdentity should not be nil")

View File

@ -99,7 +99,7 @@ var _ = SIGDescribe("[Disruptive]NodeLease", func() {
})
ginkgo.It("node lease should be deleted when corresponding node is deleted", func() {
leaseClient := c.CoordinationV1beta1().Leases(v1.NamespaceNodeLease)
leaseClient := c.CoordinationV1().Leases(v1.NamespaceNodeLease)
err := e2enode.WaitForReadyNodes(c, framework.TestContext.CloudConfig.NumNodes, 10*time.Minute)
gomega.Expect(err).To(gomega.BeNil())