From 7d6da1e7eab9c9cce815612a995eca1c70fd1230 Mon Sep 17 00:00:00 2001 From: Archit Kulkarni Date: Mon, 14 Oct 2024 19:58:21 +0000 Subject: [PATCH] Rename Ready to Status and introduce new StatusDetail Signed-off-by: Archit Kulkarni --- cni.go | 39 ++++++++++++++++++--------------------- cni_test.go | 8 ++++---- types.go | 5 +++++ 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/cni.go b/cni.go index f27ff67..ef63072 100644 --- a/cni.go +++ b/cni.go @@ -45,8 +45,8 @@ type CNI interface { Status(ctx context.Context) error // GetConfig returns a copy of the CNI plugin configurations as parsed by CNI GetConfig() *ConfigResult - // Ready checks the readiness of the cni system - Ready() error + // Status checks the readiness of the cni system + Status() error } type ConfigResult struct { @@ -135,7 +135,7 @@ func (c *libcni) Load(opts ...Opt) error { return nil } -func (c *libcni) Ready() error { +func (c *libcni) Status() error { c.RLock() defer c.RUnlock() if len(c.networks) < c.networkCount { @@ -154,7 +154,7 @@ func (c *libcni) Networks() []*Network { // Setup setups the network in the namespace and returns a Result func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) { - if err := c.Ready(); err != nil { + if err := c.Status(); err != nil { return nil, err } ns, err := newNamespace(id, path, opts...) @@ -170,7 +170,7 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name // SetupSerially setups the network in the namespace and returns a Result func (c *libcni) SetupSerially(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error) { - if err := c.Ready(); err != nil { + if err := c.Status(); err != nil { return nil, err } ns, err := newNamespace(id, path, opts...) @@ -233,7 +233,7 @@ func (c *libcni) attachNetworks(ctx context.Context, ns *Namespace) ([]*types100 // Remove removes the network config from the namespace func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...NamespaceOpts) error { - if err := c.Ready(); err != nil { + if err := c.Status(); err != nil { return err } ns, err := newNamespace(id, path, opts...) @@ -261,7 +261,7 @@ func (c *libcni) Remove(ctx context.Context, id string, path string, opts ...Nam // Check checks if the network is still in desired state func (c *libcni) Check(ctx context.Context, id string, path string, opts ...NamespaceOpts) error { - if err := c.Ready(); err != nil { + if err := c.Status(); err != nil { return err } ns, err := newNamespace(id, path, opts...) @@ -312,25 +312,22 @@ func (c *libcni) reset() { c.networks = nil } -// Status executes the status verb of the cni plugin -func (c *libcni) Status(ctx context.Context) error { - err := c.Ready() +// StatusDetail returns a slice of network statuses +func (c *libcni) StatusDetail(ctx context.Context) ([]*NetworkStatus, error) { + err := c.Status() if err != nil { - return err + return nil, err } - for _, network := range c.Networks() { - if network.config.Name == "cni-loopback" { - continue - } - - err = network.Status(ctx) + var networks []*NetworkStatus - if err != nil { - return err - } + for _, network := range c.Networks() { + networks = append(networks, &NetworkStatus{ + Network: network, + Status: network.Status(ctx), + }) } - return nil + return networks, nil } diff --git a/cni_test.go b/cni_test.go index 3d575c1..f453b43 100644 --- a/cni_test.go +++ b/cni_test.go @@ -46,7 +46,7 @@ func TestLibCNIType020(t *testing.T) { err := l.Load(WithAllConf) assert.NoError(t, err) - err = l.Ready() + err = l.Status() assert.NoError(t, err) mockCNI := &MockCNI{} @@ -122,7 +122,7 @@ func TestLibCNIType040(t *testing.T) { err := l.Load(WithAllConf) assert.NoError(t, err) - err = l.Ready() + err = l.Status() assert.NoError(t, err) mockCNI := &MockCNI{} @@ -210,7 +210,7 @@ func TestLibCNIType100(t *testing.T) { err := l.Load(WithAllConf) assert.NoError(t, err) - err = l.Ready() + err = l.Status() assert.NoError(t, err) mockCNI := &MockCNI{} @@ -300,7 +300,7 @@ func TestLibCNIType120(t *testing.T) { err := l.Load(WithAllConf) assert.NoError(t, err) - err = l.Ready() + err = l.Status() assert.NoError(t, err) mockCNI := &MockCNI{} diff --git a/types.go b/types.go index 18616c0..92c6254 100644 --- a/types.go +++ b/types.go @@ -60,3 +60,8 @@ type DNS struct { // List of DNS options. Options []string } + +type NetworkStatus struct { + Network *Network + Status error +}