Skip to content

Commit

Permalink
Rename Ready to Status and introduce new StatusDetail
Browse files Browse the repository at this point in the history
Signed-off-by: Archit Kulkarni <[email protected]>
  • Loading branch information
architkulkarni committed Oct 14, 2024
1 parent ae6bd6c commit 7d6da1e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
39 changes: 18 additions & 21 deletions cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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...)
Expand All @@ -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...)
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ type DNS struct {
// List of DNS options.
Options []string
}

type NetworkStatus struct {
Network *Network
Status error
}

0 comments on commit 7d6da1e

Please sign in to comment.