-
Notifications
You must be signed in to change notification settings - Fork 228
/
podinfoprovider.go
46 lines (41 loc) · 1.55 KB
/
podinfoprovider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cnireconciler
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"
"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(exec.New())
}
func newCNIPodInfoProvider(exec exec.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 first endpoint IP as the key in the map.
func cniStateToPodInfoByIP(state *api.AzureCNIState) (map[string]cns.PodInfo, error) {
podInfoByIP := map[string]cns.PodInfo{}
for _, endpoint := range state.ContainerInterfaces {
if _, ok := podInfoByIP[endpoint.IPAddresses[0].IP.String()]; ok {
return nil, errors.Wrap(cns.ErrDuplicateIP, endpoint.IPAddresses[0].IP.String())
}
podInfoByIP[endpoint.IPAddresses[0].IP.String()] = cns.NewPodInfo(
endpoint.ContainerID,
endpoint.PodEndpointId,
endpoint.PodName,
endpoint.PodNamespace,
)
}
return podInfoByIP, nil
}