From 859ff9b54882c4344cc5564c6cd4f993a300e20c Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Sun, 29 Dec 2024 14:37:33 +0000 Subject: [PATCH] change structure of client Signed-off-by: Austin Abro --- pkg/kube/client.go | 21 +++------------------ pkg/kube/client_test.go | 9 +++------ pkg/kube/interface.go | 10 +++++----- pkg/kube/kwait_test.go | 2 +- 4 files changed, 12 insertions(+), 30 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 149017b17..9e31a64e1 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -87,13 +87,8 @@ type Client struct { Namespace string kubeClient *kubernetes.Clientset - // I see a couple different options for how waiter could be handled here - // - The waiter could be instantiated in New or at the start of each wait function // - // - The waiter could be completely separate from the client interface, - // I don't like that this causes consumers to need another interface on top of kube - // - The waiter could be bundled with the resource manager into a client object. The waiter doesn't need factory / - // Another option still would be to - waiter Waiter + ResourceManager + Waiter } func init() { @@ -153,7 +148,7 @@ func New(getter genericclioptions.RESTClientGetter, waiter Waiter) *Client { return &Client{ Factory: factory, Log: nopLogger, - waiter: waiter, + Waiter: waiter, } } @@ -330,16 +325,6 @@ func getResource(info *resource.Info) (runtime.Object, error) { return obj, nil } -// Wait waits up to the given timeout for the specified resources to be ready. -func (c *Client) Wait(resources ResourceList, timeout time.Duration) error { - return c.waiter.Wait(resources, timeout) -} - -// WaitWithJobs wait up to the given timeout for the specified resources to be ready, including jobs. -func (c *Client) WaitWithJobs(resources ResourceList, timeout time.Duration) error { - return c.waiter.WaitWithJobs(resources, timeout) -} - // WaitForDelete wait up to the given timeout for the specified resources to be deleted. func (c *Client) WaitForDelete(resources ResourceList, timeout time.Duration) error { w := waiter{ diff --git a/pkg/kube/client_test.go b/pkg/kube/client_test.go index de61a3862..a6e095942 100644 --- a/pkg/kube/client_test.go +++ b/pkg/kube/client_test.go @@ -522,12 +522,11 @@ func TestWait(t *testing.T) { t.Fatal(err) } checker := NewReadyChecker(cs, c.Log, PausedAsReady(true)) - w := &waiter{ + c.Waiter = &waiter{ c: checker, log: c.Log, timeout: time.Second * 30, } - c.waiter = w resources, err := c.Build(objBody(&podList), false) if err != nil { t.Fatal(err) @@ -585,12 +584,11 @@ func TestWaitJob(t *testing.T) { t.Fatal(err) } checker := NewReadyChecker(cs, c.Log, PausedAsReady(true), CheckJobs(true)) - w := &waiter{ + c.Waiter = &waiter{ c: checker, log: c.Log, timeout: time.Second * 30, } - c.waiter = w resources, err := c.Build(objBody(job), false) if err != nil { t.Fatal(err) @@ -650,12 +648,11 @@ func TestWaitDelete(t *testing.T) { t.Fatal(err) } checker := NewReadyChecker(cs, c.Log, PausedAsReady(true)) - w := &waiter{ + c.Waiter = &waiter{ c: checker, log: c.Log, timeout: time.Second * 30, } - c.waiter = w resources, err := c.Build(objBody(&pod), false) if err != nil { t.Fatal(err) diff --git a/pkg/kube/interface.go b/pkg/kube/interface.go index 40880005a..d2230b244 100644 --- a/pkg/kube/interface.go +++ b/pkg/kube/interface.go @@ -29,6 +29,11 @@ import ( // // A KubernetesClient must be concurrency safe. type Interface interface { + ResourceManager + Waiter +} + +type ResourceManager interface { // Create creates one or more resources. Create(resources ResourceList) (*Result, error) @@ -38,7 +43,6 @@ type Interface interface { // Update updates one or more resources or creates the resource // if it doesn't exist. Update(original, target ResourceList, force bool) (*Result, error) - // WatchUntilReady watches the resources given and waits until it is ready. // // This method is mainly for hook implementations. It watches for a resource to @@ -50,11 +54,9 @@ type Interface interface { // error. // TODO: Is watch until ready really behavior we want over the resources actually being ready? WatchUntilReady(resources ResourceList, timeout time.Duration) error - // WaitAndGetCompletedPodPhase waits up to a timeout until a pod enters a completed phase // and returns said phase (PodSucceeded or PodFailed qualify). WaitAndGetCompletedPodPhase(name string, timeout time.Duration) (v1.PodPhase, error) - // Build creates a resource list from a Reader. // // Reader must contain a YAML stream (one or more YAML documents separated @@ -62,10 +64,8 @@ type Interface interface { // // Validates against OpenAPI schema if validate is true. Build(reader io.Reader, validate bool) (ResourceList, error) - // IsReachable checks whether the client is able to connect to the cluster. IsReachable() error - Waiter } // Waiter defines methods related to waiting for resource states. diff --git a/pkg/kube/kwait_test.go b/pkg/kube/kwait_test.go index 1702f0990..9854f2d60 100644 --- a/pkg/kube/kwait_test.go +++ b/pkg/kube/kwait_test.go @@ -96,7 +96,7 @@ func TestRunHealthChecks(t *testing.T) { err = fakeClient.Tracker().Create(podGVR, pod, pod.GetNamespace()) require.NoError(t, err) } - c.waiter = &kstatusWaiter{ + c.Waiter = &kstatusWaiter{ sw: statusWatcher, log: c.Log, }