Skip to content

Commit

Permalink
Merge pull request #45 from ykulazhenkov/pr-cni-args
Browse files Browse the repository at this point in the history
Add support for additional CNI arguments: ips, poolNames, poolType, allocateDefaultGateway
  • Loading branch information
ykulazhenkov committed Jun 14, 2024
2 parents 24b9875 + 3917ecc commit 755beb9
Show file tree
Hide file tree
Showing 13 changed files with 738 additions and 226 deletions.
314 changes: 203 additions & 111 deletions api/grpc/nvidia/ipam/node/v1/node.pb.go

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion api/grpc/proto/nvidia/ipam/node/v1/node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ message IPAMParameters {
string cni_ifname = 3;
// required, additional metadata to identify IP allocation
IPAMMetadata metadata = 4;
// type of the pool which is refered by the name in the pools field
// optional, type of the pool which is refered by the name in the pools field
PoolType pool_type = 5;
// optional, conatins IP that were statically requested
repeated string requested_ips = 6;
// optional, conatins extra features requested for the allocation
IPAMFeatures features = 7;
}

// IPAMMetadata contains metadata for IPAM calls
Expand All @@ -87,6 +91,12 @@ message IPAMMetadata {
string device_id = 4;
}

// IPAMFeatures contains extra features requested for the IPAM call
message IPAMFeatures {
// optional, request IP of the default gateway from the pool be allocated for container
bool allocate_default_gateway = 1;
}

// IsAllocatedRequest contains parameters for IsAllocated rpc call
message IsAllocatedRequest {
// required, IPAMParameters contains parameters IPAM parameters related to the request
Expand Down
53 changes: 20 additions & 33 deletions pkg/cni/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (p *Plugin) prepareCMD(args *skel.CmdArgs) (cmdContext, error) {
c cmdContext
err error
)
c.Config, err = p.ConfLoader.LoadConf(args.StdinData)
c.Config, err = p.ConfLoader.LoadConf(args)
if err != nil {
return cmdContext{}, fmt.Errorf("failed to load config. %v", err)
}
Expand All @@ -136,10 +136,8 @@ func (p *Plugin) prepareCMD(args *skel.CmdArgs) (cmdContext, error) {
if err != nil {
return cmdContext{}, fmt.Errorf("failed to connect to IPAM daemon: %v", err)
}
c.ReqParams, err = cniConfToGRPCReq(c.Config, args)
if err != nil {
return cmdContext{}, fmt.Errorf("failed to convert CNI parameters to GRPC request: %v", err)
}
c.ReqParams = cniConfToGRPCReq(c.Config, args)

return c, nil
}

Expand Down Expand Up @@ -192,40 +190,38 @@ func grpcRespToResult(resp *nodev1.AllocateResponse) (*current.Result, error) {
return result, nil
}

func cniConfToGRPCReq(conf *types.NetConf, args *skel.CmdArgs) (*nodev1.IPAMParameters, error) {
cniExtraArgs := &kubernetesCNIArgs{}
err := cnitypes.LoadArgs(args.Args, cniExtraArgs)
if err != nil {
return nil, fmt.Errorf("failed to load extra CNI args: %v", err)
}

func cniConfToGRPCReq(conf *types.NetConf, args *skel.CmdArgs) *nodev1.IPAMParameters {
poolType := nodev1.PoolType_POOL_TYPE_IPPOOL
if conf.IPAM.PoolType == common.PoolTypeCIDRPool {
poolType = nodev1.PoolType_POOL_TYPE_CIDRPOOL
}

requestedIPs := make([]string, 0, len(conf.IPAM.RequestedIPs))
for _, ipAddr := range conf.IPAM.RequestedIPs {
requestedIPs = append(requestedIPs, ipAddr.String())
}

req := &nodev1.IPAMParameters{
Pools: conf.IPAM.Pools,
PoolType: poolType,
CniIfname: args.IfName,
CniContainerid: args.ContainerID,
Metadata: &nodev1.IPAMMetadata{
K8SPodName: string(cniExtraArgs.K8S_POD_NAME),
K8SPodNamespace: string(cniExtraArgs.K8S_POD_NAMESPACE),
K8SPodUid: string(cniExtraArgs.K8S_POD_UID),
K8SPodName: conf.IPAM.K8SMetadata.PodName,
K8SPodNamespace: conf.IPAM.K8SMetadata.PodNamespace,
K8SPodUid: conf.IPAM.K8SMetadata.PodUID,
DeviceId: conf.DeviceID,
},
}

if req.Metadata.K8SPodName == "" {
return nil, log.Errorf("CNI_ARGS: K8S_POD_NAME is not provided by container runtime")
}
if req.Metadata.K8SPodNamespace == "" {
return nil, log.Errorf("CNI_ARGS: K8S_POD_NAMESPACE is not provided by container runtime")
RequestedIps: requestedIPs,
Features: &nodev1.IPAMFeatures{
AllocateDefaultGateway: conf.IPAM.Features.AllocateDefaultGateway,
},
}
if req.Metadata.K8SPodUid == "" {
log.Warningf("CNI_ARGS: K8S_POD_UID is not provided by container runtime")
log.Warningf("K8S_POD_UID is not provided by container runtime")
}
return req, nil

return req
}

// default NewGRPCClientFunc, initializes insecure GRPC connection to provided daemon socket
Expand All @@ -236,12 +232,3 @@ func defaultNewGRPCClientFunc(daemonSocket string) (GRPCClient, error) {
}
return nodev1.NewIPAMServiceClient(conn), nil
}

// kubernetesCNIArgs is the container for extra CNI Args which set by container runtimes
// in Kubernetes
type kubernetesCNIArgs struct {
cnitypes.CommonArgs
K8S_POD_NAME cnitypes.UnmarshallableString //nolint
K8S_POD_NAMESPACE cnitypes.UnmarshallableString //nolint
K8S_POD_UID cnitypes.UnmarshallableString //nolint
}
16 changes: 13 additions & 3 deletions pkg/cni/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ var _ = Describe("plugin tests", func() {
Pools: []string{"my-pool"},
LogFile: path.Join(tmpDir, "nv-ipam.log"),
LogLevel: "debug",
K8SMetadata: struct {
PodName string
PodNamespace string
PodUID string
}{
PodName: "test",
PodNamespace: "test",
},
},
}

Expand All @@ -80,7 +88,7 @@ var _ = Describe("plugin tests", func() {

Context("CmdAdd()", func() {
It("executes successfully", func() {
mockConfLoader.On("LoadConf", args.StdinData).Return(testConf, nil)
mockConfLoader.On("LoadConf", args).Return(testConf, nil)
mockDaemonClient.On("Allocate", mock.Anything, &nodev1.AllocateRequest{
Parameters: &nodev1.IPAMParameters{
Pools: []string{"my-pool"},
Expand All @@ -91,6 +99,8 @@ var _ = Describe("plugin tests", func() {
K8SPodName: "test",
K8SPodNamespace: "test",
},
RequestedIps: []string{},
Features: &nodev1.IPAMFeatures{},
}}).Return(&nodev1.AllocateResponse{
Allocations: []*nodev1.AllocationInfo{{
Pool: "my-pool",
Expand All @@ -106,7 +116,7 @@ var _ = Describe("plugin tests", func() {

Context("CmdDel()", func() {
It("executes successfully", func() {
mockConfLoader.On("LoadConf", args.StdinData).Return(testConf, nil)
mockConfLoader.On("LoadConf", args).Return(testConf, nil)
mockDaemonClient.On("Deallocate", mock.Anything, mock.Anything).Return(nil, nil)
err := p.CmdDel(args)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -115,7 +125,7 @@ var _ = Describe("plugin tests", func() {

Context("CmdCheck()", func() {
It("executes successfully", func() {
mockConfLoader.On("LoadConf", args.StdinData).Return(testConf, nil)
mockConfLoader.On("LoadConf", args).Return(testConf, nil)
mockDaemonClient.On("IsAllocated", mock.Anything, mock.Anything).Return(nil, nil)
err := p.CmdCheck(args)
Expect(err).ToNot(HaveOccurred())
Expand Down
34 changes: 18 additions & 16 deletions pkg/cni/types/mocks/ConfLoader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 755beb9

Please sign in to comment.