Skip to content

Commit

Permalink
Add fake KV etcd client
Browse files Browse the repository at this point in the history
  • Loading branch information
Sedef committed Oct 15, 2020
1 parent 846573d commit c512676
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
16 changes: 12 additions & 4 deletions controlplane/kubeadm/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,18 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, clu
return ctrl.Result{}, err
}

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

// Gets all machines, not just control plane machines.
allMachines, err := r.managementCluster.GetMachinesForCluster(ctx, util.ObjectKey(cluster))
Expand Down
8 changes: 2 additions & 6 deletions controlplane/kubeadm/internal/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Client struct {
EtcdClient etcd
Endpoint string
LeaderID uint64
KVClient *clientv3.Client
KVClient clientv3.KV
}

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

func newEtcdClient(ctx context.Context, etcdClient etcd, kvClient *clientv3.Client) (*Client, error) {
func newEtcdClient(ctx context.Context, etcdClient etcd, kvClient clientv3.KV) (*Client, error) {
endpoints := etcdClient.Endpoints()
if len(endpoints) == 0 {
return nil, errors.New("etcd client was not configured with any endpoints")
Expand Down Expand Up @@ -242,10 +242,6 @@ func (c *Client) Alarms(ctx context.Context) ([]MemberAlarm, error) {
// HealthCheck checks the healthiness of endpoints specified in endpoints during Client creation.
// Using the same logic used in etcdctl health command.
func (c *Client) HealthCheck(ctx context.Context) error {
// This check is return nil if FakeEtcdClient.
if c.KVClient == nil {
return nil
}
_, err := c.KVClient.Get(ctx, "health")
if err == nil || err == rpctypes.ErrPermissionDenied {
return nil
Expand Down
6 changes: 5 additions & 1 deletion controlplane/kubeadm/internal/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ func TestEtcdMembers_WithSuccess(t *testing.T) {
StatusResponse: &clientv3.StatusResponse{},
}

client, err := newEtcdClient(ctx, fakeEtcdClient, nil)
fakeKVClient := &etcdfake.FakeKVClient{
GetResponse: &clientv3.GetResponse{},
}

client, err := newEtcdClient(ctx, fakeEtcdClient, fakeKVClient)
g.Expect(err).NotTo(HaveOccurred())

members, err := client.Members(ctx)
Expand Down
25 changes: 25 additions & 0 deletions controlplane/kubeadm/internal/etcd/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ import (
"go.etcd.io/etcd/clientv3"
)

type FakeKVClient struct {
GetResponse *clientv3.GetResponse
}

func (kv *FakeKVClient) Put(ctx context.Context, key, val string, opts ...clientv3.OpOption) (*clientv3.PutResponse, error) {
return nil, nil
}
func (kv *FakeKVClient) Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
return kv.GetResponse, nil
}
func (kv *FakeKVClient) Delete(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.DeleteResponse, error) {
return nil, nil
}
func (kv *FakeKVClient) Compact(ctx context.Context, rev int64, opts ...clientv3.CompactOption) (*clientv3.CompactResponse, error) {
return nil, nil
}

func (kv *FakeKVClient) Do(ctx context.Context, op clientv3.Op) (clientv3.OpResponse, error) {
return clientv3.OpResponse{}, nil
}

func (kv *FakeKVClient) Txn(ctx context.Context) clientv3.Txn {
return nil
}

type FakeEtcdClient struct {
AlarmResponse *clientv3.AlarmResponse
EtcdEndpoints []string
Expand Down

0 comments on commit c512676

Please sign in to comment.