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

Remove Kubernetes PodInfoProvider and default to CNI initialization #2087

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 2 additions & 19 deletions cns/NetworkContainerContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,6 @@ func (f PodInfoByIPProviderFunc) PodInfoByIP() (map[string]PodInfo, error) {
return f()
}

var GlobalPodInfoScheme podInfoScheme

// podInfoScheme indicates which schema should be used when generating
// the map key in the Key() function on a podInfo object.
type podInfoScheme int

const (
KubernetesPodInfoScheme podInfoScheme = iota
InterfaceIDPodInfoScheme
)

// PodInfo represents the object that we are providing network for.
type PodInfo interface {
// InfraContainerID the CRI infra container for the pod namespace.
Expand Down Expand Up @@ -187,7 +176,6 @@ type podInfo struct {
KubernetesPodInfo
PodInfraContainerID string
PodInterfaceID string
Version podInfoScheme
}

func (p podInfo) String() string {
Expand Down Expand Up @@ -219,10 +207,7 @@ func (p *podInfo) InterfaceID() string {
// composed of the CNI interfaceID, which is generated from the CRI infra
// container ID and the pod net ns primary interface name.
func (p *podInfo) Key() string {
if p.Version == InterfaceIDPodInfoScheme {
return p.PodInterfaceID
}
return p.PodName + ":" + p.PodNamespace
return p.PodInterfaceID
}

func (p *podInfo) Name() string {
Expand Down Expand Up @@ -251,7 +236,6 @@ func NewPodInfo(infraContainerID, interfaceID, name, namespace string) PodInfo {
},
PodInfraContainerID: infraContainerID,
PodInterfaceID: interfaceID,
Version: GlobalPodInfoScheme,
}
}

Expand All @@ -262,7 +246,6 @@ func UnmarshalPodInfo(b []byte) (PodInfo, error) {
if err := json.Unmarshal(b, p); err != nil {
return nil, err
}
p.Version = GlobalPodInfoScheme
return p, nil
}

Expand All @@ -273,7 +256,7 @@ func NewPodInfoFromIPConfigsRequest(req IPConfigsRequest) (PodInfo, error) {
if err != nil {
return nil, err
}
if GlobalPodInfoScheme == InterfaceIDPodInfoScheme && req.PodInterfaceID == "" {
if req.PodInterfaceID == "" {
return nil, fmt.Errorf("need interfaceID for pod info but request was empty")
}
p.(*podInfo).PodInfraContainerID = req.InfraContainerID
Expand Down
3 changes: 0 additions & 3 deletions cns/NetworkContainerContract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func TestUnmarshalPodInfo(t *testing.T) {
}

func TestNewPodInfoFromIPConfigsRequest(t *testing.T) {
GlobalPodInfoScheme = InterfaceIDPodInfoScheme
defer func() { GlobalPodInfoScheme = KubernetesPodInfoScheme }()
tests := []struct {
name string
req IPConfigsRequest
Expand All @@ -79,7 +77,6 @@ func TestNewPodInfoFromIPConfigsRequest(t *testing.T) {
},
PodInterfaceID: "abcdef-eth0",
PodInfraContainerID: "abcdef",
Version: InterfaceIDPodInfoScheme,
},
},
{
Expand Down
103 changes: 0 additions & 103 deletions cns/cnireconciler/podinfoprovider.go

This file was deleted.

42 changes: 0 additions & 42 deletions cns/cnireconciler/version.go

This file was deleted.

76 changes: 0 additions & 76 deletions cns/cnireconciler/version_test.go

This file was deleted.

49 changes: 49 additions & 0 deletions cns/podprovider/cni/podinfoprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cni

import (
"fmt"

"github.com/Azure/azure-container-networking/cni/api"
"github.com/Azure/azure-container-networking/cni/client"
"github.com/Azure/azure-container-networking/cns"
"github.com/pkg/errors"
kexec "k8s.io/utils/exec"
)

// NewCNIPodInfoProvider returns an implementation of cns.PodInfoByIPProvider
// that execs out to the CNI and uses the response to build the PodInfo map.
func NewCNIPodInfoProvider() (cns.PodInfoByIPProvider, error) {
return newCNIPodInfoProvider(kexec.New())
}

func newCNIPodInfoProvider(exec kexec.Interface) (cns.PodInfoByIPProvider, error) {
cli := client.New(exec)
state, err := cli.GetEndpointState()
if err != nil {
return nil, fmt.Errorf("failed to invoke CNI client.GetEndpointState(): %w", err)
}
return cns.PodInfoByIPProviderFunc(func() (map[string]cns.PodInfo, error) {
return cniStateToPodInfoByIP(state)
}), nil
}

// cniStateToPodInfoByIP converts an AzureCNIState dumped from a CNI exec
// into a PodInfo map, using the endpoint IPs as keys in the map.
// for pods with multiple IPs (such as in dualstack cases), this means multiple keys in the map
// will point to the same pod information.
func cniStateToPodInfoByIP(state *api.AzureCNIState) (map[string]cns.PodInfo, error) {
podInfoByIP := map[string]cns.PodInfo{}
for _, endpoint := range state.ContainerInterfaces {
for _, epIP := range endpoint.IPAddresses {
podInfo := cns.NewPodInfo(endpoint.ContainerID, endpoint.PodEndpointId, endpoint.PodName, endpoint.PodNamespace)

ipKey := epIP.IP.String()
if prevPodInfo, ok := podInfoByIP[ipKey]; ok {
return nil, errors.Wrapf(cns.ErrDuplicateIP, "duplicate ip %s found for different pods: pod: %+v, pod: %+v", ipKey, podInfo, prevPodInfo)
}

podInfoByIP[ipKey] = podInfo
}
}
return podInfoByIP, nil
}
Loading