Skip to content

Commit c512676

Browse files
author
Sedef
committed
Add fake KV etcd client
1 parent 846573d commit c512676

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

controlplane/kubeadm/controllers/controller.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,18 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, clu
408408
return ctrl.Result{}, err
409409
}
410410

411-
// Ignore the health check results here as well as the errors, they are used to set health related conditions on Machines.
412-
// Errors may be dues not being able to get workload cluster nodes.
413-
r.managementCluster.TargetClusterControlPlaneHealthCheck(ctx, controlPlane, util.ObjectKey(cluster)) //nolint
414-
r.managementCluster.TargetClusterEtcdHealthCheck(ctx, controlPlane, util.ObjectKey(cluster)) //nolint
411+
// Ignore the health check results here as well as the errors, health check functions are to set health related conditions on Machines.
412+
// Errors may be due to not being able to get workload cluster nodes.
413+
_, err = r.managementCluster.TargetClusterControlPlaneHealthCheck(ctx, controlPlane, util.ObjectKey(cluster))
414+
if err != nil {
415+
// Do nothing
416+
r.Log.Info("Control plane did not pass control plane health check during delete reconciliation", "err", err.Error())
417+
}
418+
_, err = r.managementCluster.TargetClusterEtcdHealthCheck(ctx, controlPlane, util.ObjectKey(cluster))
419+
if err != nil {
420+
// Do nothing
421+
r.Log.Info("Control plane did not pass etcd health check during delete reconciliation", "err", err.Error())
422+
}
415423

416424
// Gets all machines, not just control plane machines.
417425
allMachines, err := r.managementCluster.GetMachinesForCluster(ctx, util.ObjectKey(cluster))

controlplane/kubeadm/internal/etcd/etcd.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Client struct {
5454
EtcdClient etcd
5555
Endpoint string
5656
LeaderID uint64
57-
KVClient *clientv3.Client
57+
KVClient clientv3.KV
5858
}
5959

6060
// MemberAlarm represents an alarm type association with a cluster member.
@@ -142,7 +142,7 @@ func NewClient(ctx context.Context, endpoints []string, p proxy.Proxy, tlsConfig
142142
return newEtcdClient(ctx, etcdClient, etcdClient)
143143
}
144144

145-
func newEtcdClient(ctx context.Context, etcdClient etcd, kvClient *clientv3.Client) (*Client, error) {
145+
func newEtcdClient(ctx context.Context, etcdClient etcd, kvClient clientv3.KV) (*Client, error) {
146146
endpoints := etcdClient.Endpoints()
147147
if len(endpoints) == 0 {
148148
return nil, errors.New("etcd client was not configured with any endpoints")
@@ -242,10 +242,6 @@ func (c *Client) Alarms(ctx context.Context) ([]MemberAlarm, error) {
242242
// HealthCheck checks the healthiness of endpoints specified in endpoints during Client creation.
243243
// Using the same logic used in etcdctl health command.
244244
func (c *Client) HealthCheck(ctx context.Context) error {
245-
// This check is return nil if FakeEtcdClient.
246-
if c.KVClient == nil {
247-
return nil
248-
}
249245
_, err := c.KVClient.Get(ctx, "health")
250246
if err == nil || err == rpctypes.ErrPermissionDenied {
251247
return nil

controlplane/kubeadm/internal/etcd/etcd_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ func TestEtcdMembers_WithSuccess(t *testing.T) {
8686
StatusResponse: &clientv3.StatusResponse{},
8787
}
8888

89-
client, err := newEtcdClient(ctx, fakeEtcdClient, nil)
89+
fakeKVClient := &etcdfake.FakeKVClient{
90+
GetResponse: &clientv3.GetResponse{},
91+
}
92+
93+
client, err := newEtcdClient(ctx, fakeEtcdClient, fakeKVClient)
9094
g.Expect(err).NotTo(HaveOccurred())
9195

9296
members, err := client.Members(ctx)

controlplane/kubeadm/internal/etcd/fake/client.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ import (
2222
"go.etcd.io/etcd/clientv3"
2323
)
2424

25+
type FakeKVClient struct {
26+
GetResponse *clientv3.GetResponse
27+
}
28+
29+
func (kv *FakeKVClient) Put(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
30+
return nil, nil
31+
}
32+
func (kv *FakeKVClient) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
33+
return kv.GetResponse, nil
34+
}
35+
func (kv *FakeKVClient) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error) {
36+
return nil, nil
37+
}
38+
func (kv *FakeKVClient) Compact(ctx context.Context, rev int64, opts ...clientv3.CompactOption) (*clientv3.CompactResponse, error) {
39+
return nil, nil
40+
}
41+
42+
func (kv *FakeKVClient) Do(ctx context.Context, op clientv3.Op) (clientv3.OpResponse, error) {
43+
return clientv3.OpResponse{}, nil
44+
}
45+
46+
func (kv *FakeKVClient) Txn(ctx context.Context) clientv3.Txn {
47+
return nil
48+
}
49+
2550
type FakeEtcdClient struct {
2651
AlarmResponse *clientv3.AlarmResponse
2752
EtcdEndpoints []string

0 commit comments

Comments
 (0)