Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement STATUS #121

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/containernetworking/cni/pkg/version"
)

const LoopbackNetworkName = "cni-loopback"

type CNI interface {
// Setup setup the network for the namespace
Setup(ctx context.Context, id string, path string, opts ...NamespaceOpts) (*Result, error)
Expand All @@ -45,6 +47,8 @@ type CNI interface {
Status() error
// GetConfig returns a copy of the CNI plugin configurations as parsed by CNI
GetConfig() *ConfigResult
// Status executes the status verb of the cni plugin
StatusDetail(context.Context) ([]*NetworkStatus, error)
}

type ConfigResult struct {
Expand Down Expand Up @@ -310,3 +314,27 @@ func (c *libcni) GetConfig() *ConfigResult {
func (c *libcni) reset() {
c.networks = nil
}

// StatusDetail returns a slice of network statuses
func (c *libcni) StatusDetail(ctx context.Context) ([]*NetworkStatus, error) {
samuelkarp marked this conversation as resolved.
Show resolved Hide resolved
if err := c.Status(); err != nil {
return nil, err
}

var networkStatuses []*NetworkStatus

for _, network := range c.Networks() {
// Skip checking the status of the loopback network. It would have
// always returned the same thing, and is being deprecated anyway.
if network.config.Name == LoopbackNetworkName {
continue
}

networkStatuses = append(networkStatuses, &NetworkStatus{
Network: network,
Status: network.Status(ctx),
})
}

return networkStatuses, nil
}
8 changes: 8 additions & 0 deletions cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func TestLibCNIType120(t *testing.T) {
Args: [][2]string(nil),
CapabilityArgs: map[string]interface{}{},
}
mockCNI.On("GetStatusNetworkList", l.networks[0].config).Return(nil)
mockCNI.On("AddNetworkList", l.networks[0].config, expectedRT).Return(&types100.Result{
CNIVersion: "1.1.0",
Interfaces: []*types100.Interface{
Expand Down Expand Up @@ -342,6 +343,7 @@ func TestLibCNIType120(t *testing.T) {
Args: [][2]string(nil),
CapabilityArgs: map[string]interface{}{},
}
mockCNI.On("GetStatusNetworkList", l.networks[1].config).Return(nil)
mockCNI.On("AddNetworkList", l.networks[1].config, expectedRT).Return(&types100.Result{
CNIVersion: "1.1.0",
Interfaces: []*types100.Interface{
Expand Down Expand Up @@ -374,6 +376,12 @@ func TestLibCNIType120(t *testing.T) {

err = l.Remove(ctx, "container-id1", "/proc/12345/ns/net")
assert.NoError(t, err)

statuses, err := l.StatusDetail(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fake configuration that is being generated, is that 1.1.0? Can you verify that I don't think the Status verb is actually being executed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikeZappa87 Thanks for the comment, just want to make sure I understand everything -- In the approach currently taken by this PR, StatusDetail isn't called anywhere yet (the proposal was to have containerd call it), so you're right that the Status verb isn't executed unless we explicitly call StatusDetail. Since we do call StatusDetail in the test here, I think this line https://github.com/containerd/go-cni/pull/121/files#diff-6cdf85f441267859d068399676e5f0ad8946811bd7e951b4e80db98d9fd2489cR318 asserts that the (mocked) status verb is being executed.

I don't see 1.1.0 specified anywhere in the fake config, but I think that doesn't affect the test because the version check happens inside cni.GetStatusNetworkList, and this function is mocked out. Is that right?

assert.NoError(t, err)
assert.Len(t, statuses, 2)
assert.Nil(t, statuses[0].Status)
assert.Nil(t, statuses[1].Status)
}

type MockCNI struct {
Expand Down
4 changes: 4 additions & 0 deletions namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (n *Network) Check(ctx context.Context, ns *Namespace) error {
return n.cni.CheckNetworkList(ctx, n.config, ns.config(n.ifName))
}

func (n *Network) Status(ctx context.Context) error {
return n.cni.GetStatusNetworkList(ctx, n.config)
}

type Namespace struct {
id string
path string
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to expose anything else here? Right now all the fields of Network are private.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it enough to make ifName public?

go-cni/namespace.go

Lines 26 to 30 in a45b44a

type Network struct {
cni cnilibrary.CNI
config *cnilibrary.NetworkConfigList
ifName string
}

Copy link
Contributor

@MikeZappa87 MikeZappa87 Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a consumer of the Status API does making this private or public help? Exposing ifName might be useful especially if we ever begin to support multiple interfaces upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikeZappa87 The first use case that came to mind was just the error message, like if the kubelet sees that one of the entries of StatusDetail has an error, in the log it can report ifName along with the error. But there are probably other possible use cases I'm not aware of.

What about config, should we expose the whole config or just certain fields like Name, CNIVersion?

type NetworkConfigList struct {
	Name                   string
	CNIVersion             string
	DisableCheck           bool
	DisableGC              bool
	LoadOnlyInlinedPlugins bool
	Plugins                []*PluginConfig
	Bytes                  []byte
}

Status error
}
Loading