Skip to content

Commit

Permalink
Making changes to the CNI state migration code.
Browse files Browse the repository at this point in the history
  • Loading branch information
behzad-mir committed Nov 29, 2023
1 parent 8ec3f65 commit 458ec6d
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 49 deletions.
4 changes: 2 additions & 2 deletions cni/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func New(exec utilexec.Interface) *client {
return &client{exec: exec}
}

func (c *client) GetEndpointState(cniBinaryPath string) (*api.AzureCNIState, error) {
cmd := c.exec.Command(cniBinaryPath)
func (c *client) GetEndpointState() (*api.AzureCNIState, error) {
cmd := c.exec.Command(platform.CNIBinaryPath)
cmd.SetDir(CNIExecDir)
envs := os.Environ()
cmdenv := fmt.Sprintf("%s=%s", cni.Cmd, cni.CmdGetEndpointsState)
Expand Down
5 changes: 2 additions & 3 deletions cni/client/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"github.com/Azure/azure-container-networking/cni/api"
"github.com/Azure/azure-container-networking/platform"
testutils "github.com/Azure/azure-container-networking/test/utils"
ver "github.com/hashicorp/go-version"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestMain(m *testing.M) {
// todo: enable this test in CI, requires built azure vnet
func TestGetStateFromAzureCNI(t *testing.T) {
c := New(exec.New())
state, err := c.GetEndpointState(platform.CNIBinaryPath)
state, err := c.GetEndpointState()
require.NoError(t, err)

res := &api.AzureCNIState{
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestGetStateWithEmptyStateFile(t *testing.T) {
require.NoError(t, err)
out.Close()

state, err := c.GetEndpointState(platform.CNIBinaryPath)
state, err := c.GetEndpointState()
require.NoError(t, err)
require.Exactly(t, 0, len(state.ContainerInterfaces))
}
3 changes: 1 addition & 2 deletions cni/client/client_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/Azure/azure-container-networking/cni/api"
"github.com/Azure/azure-container-networking/platform"
testutils "github.com/Azure/azure-container-networking/test/utils"
ver "github.com/hashicorp/go-version"
"github.com/stretchr/testify/require"
Expand All @@ -21,7 +20,7 @@ func TestGetState(t *testing.T) {
fakeexec := testutils.GetFakeExecWithScripts(calls)

c := New(fakeexec)
state, err := c.GetEndpointState(platform.CNIBinaryPath)
state, err := c.GetEndpointState()
require.NoError(t, err)

res := &api.AzureCNIState{
Expand Down
11 changes: 3 additions & 8 deletions cns/cnireconciler/podinfoprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/Azure/azure-container-networking/cni/client"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/restserver"
"github.com/Azure/azure-container-networking/platform"
"github.com/Azure/azure-container-networking/store"
"github.com/pkg/errors"
"k8s.io/utils/exec"
Expand All @@ -16,11 +15,7 @@ import (
// 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(), platform.CNIBinaryPath)
}

func NewStatefullCNIPodInfoProvider() (cns.PodInfoByIPProvider, error) {
return newCNIPodInfoProvider(exec.New(), platform.StatefullCNIBinaryPath)
return newCNIPodInfoProvider(exec.New())
}

func NewCNSPodInfoProvider(endpointStore store.KeyValueStore) (cns.PodInfoByIPProvider, error) {
Expand All @@ -44,9 +39,9 @@ func newCNSPodInfoProvider(endpointStore store.KeyValueStore) (cns.PodInfoByIPPr
}), nil
}

func newCNIPodInfoProvider(exec exec.Interface, cniBinaryPath string) (cns.PodInfoByIPProvider, error) {
func newCNIPodInfoProvider(exec exec.Interface) (cns.PodInfoByIPProvider, error) {
cli := client.New(exec)
state, err := cli.GetEndpointState(cniBinaryPath)
state, err := cli.GetEndpointState()
if err != nil {
return nil, fmt.Errorf("failed to invoke CNI client.GetEndpointState(): %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
if os.Getenv("StatelessCNIMigration") == "true" {
logger.Printf("StatelessCNI Migration is enabled")
logger.Printf("Initializing from Statefull CNI")
podInfoByIPProvider, err = cnireconciler.NewStatefullCNIPodInfoProvider()
podInfoByIPProvider, err = cnireconciler.NewCNIPodInfoProvider()
if err != nil {
return errors.Wrap(err, "failed to create CNI PodInfoProvider")
}
Expand Down
18 changes: 18 additions & 0 deletions network/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,21 @@ func GetPodNameWithoutSuffix(podName string) string {
logger.Info("Pod name after splitting based on", zap.Any("nameSplit", nameSplit))
return strings.Join(nameSplit, "-")
}

// GetEndpointID returns a unique endpoint ID based on the CNI mode.
func (epInfo *EndpointInfo) GetEndpointInfoByIP(ipAddresses []net.IPNet, networkID string) (*EndpointInfo, error) {
// Call the platform implementation.
endpointInfo, err := epInfo.GetEndpointInfoByIPImpl(ipAddresses, networkID)
if err != nil {
return nil, err
}
return endpointInfo, nil
}

// IsEndpointStateComplete returns true if.
func (epInfo *EndpointInfo) IsEndpointStateIncomplete() bool {
if epInfo.HNSEndpointID == "" && epInfo.IfName == "" {
return true
}
return false
}
8 changes: 4 additions & 4 deletions network/endpoint_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package network
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"net"
"strings"
Expand Down Expand Up @@ -529,7 +528,8 @@ func getDefaultGateway(routes []RouteInfo) net.IP {
return nil
}

// getHNSEndpointIdByIP returns an HNS Endpoint IP that matches an specific IPAddress.
func (nm *networkManager) GetEndpointInfoByIPImpl(ipAddresses []net.IPNet, _ string) (string, error) {
return "", errors.New("No HostVethName matches the IPAddress: " + ipAddresses[0].IP.String())
// GetEndpointInfoByIPImpl returns an endpointInfo that contains corresponding HostVethName.
// TODO: It needs to be tested to see if HostVethName is required for SingleTenancy
func (epInfo *EndpointInfo) GetEndpointInfoByIPImpl(ipAddresses []net.IPNet, _ string) (*EndpointInfo, error) {

Check warning on line 533 in network/endpoint_linux.go

View workflow job for this annotation

GitHub Actions / Lint (1.20.x, ubuntu-latest)

unused-parameter: parameter 'ipAddresses' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 533 in network/endpoint_linux.go

View workflow job for this annotation

GitHub Actions / Lint (1.21.x, ubuntu-latest)

unused-parameter: parameter 'ipAddresses' seems to be unused, consider removing or renaming it as _ (revive)
return epInfo, nil
}
9 changes: 5 additions & 4 deletions network/endpoint_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,9 @@ func (nm *networkManager) updateEndpointImpl(nw *network, existingEpInfo *Endpoi
}

// getHNSEndpointIdByIP returns an HNS Endpoint IP that matches an specific IPAddress.
func (nm *networkManager) GetEndpointInfoByIPImpl(ipAddresses []net.IPNet, networkId string) (string, error) {
func (epInfo *EndpointInfo) GetEndpointInfoByIPImpl(ipAddresses []net.IPNet, networkID string) (*EndpointInfo, error) {
// check if network exists, only create the network does not exist
hnsResponse, err := Hnsv2.GetNetworkByName(networkId)
hnsResponse, err := Hnsv2.GetNetworkByName(networkID)
if err != nil {
return "", err
}
Expand All @@ -510,10 +510,11 @@ func (nm *networkManager) GetEndpointInfoByIPImpl(ipAddresses []net.IPNet, netwo
for _, ipAddress := range ipAddresses {
prefixLength, _ := ipAddress.Mask.Size()
if ipConfiguration.IpAddress == ipAddress.IP.String() && ipConfiguration.PrefixLength == uint8(prefixLength) {
return hcnEndpoint.Id, nil
epInfo.HNSEndpointID = hcnEndpoint.Id
return epInfo, nil
}
}
}
}
return "", errors.New("No HNSEndpointID matches the IPAddress: " + ipAddresses[0].IP.String())
return nil, errors.New("No HNSEndpointID matches the IPAddress: " + ipAddresses[0].IP.String())
}
22 changes: 2 additions & 20 deletions network/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package network
import (
"context"
"net"
"runtime"
"sync"
"time"

Expand Down Expand Up @@ -102,7 +101,6 @@ type NetworkManager interface {
CreateEndpoint(client apipaClient, networkID string, epInfo []*EndpointInfo) error
DeleteEndpoint(networkID string, endpointID string, epInfo *EndpointInfo) error
GetEndpointInfo(networkID string, endpointID string) (*EndpointInfo, error)
GetEndpointInfoByIP(ipAddresses []net.IPNet, networkId string) (string, error)
GetAllEndpoints(networkID string) (map[string]*EndpointInfo, error)
GetEndpointInfoBasedOnPODDetails(networkID string, podName string, podNameSpace string, doExactMatchForPodName bool) (*EndpointInfo, error)
AttachEndpoint(networkID string, endpointID string, sandboxKey string) (*endpoint, error)
Expand Down Expand Up @@ -498,16 +496,11 @@ func (nm *networkManager) GetEndpointInfo(networkId string, endpointId string) (
epInfo.IPAddresses = append(epInfo.IPAddresses, ip.IPv6...)

}
if epInfo.HNSEndpointID == "" && epInfo.IfName == "" {
endpointInfoData, err := nm.GetEndpointInfoByIP(epInfo.IPAddresses, networkId)
if epInfo.IsEndpointStateIncomplete() {
epInfo, err = epInfo.GetEndpointInfoByIP(epInfo.IPAddresses, networkId)
if err != nil {
return nil, errors.Wrapf(err, "Get endpoint API returend with error")
}
if runtime.GOOS == "windows" {
epInfo.HNSEndpointID = endpointInfoData
} else {
epInfo.IfName = endpointInfoData
}
}
logger.Info("returning getEndpoint API with", zap.String("Endpoint Info: ", epInfo.PrettyString()), zap.String("HNISID : ", epInfo.HNSEndpointID))
return epInfo, nil
Expand Down Expand Up @@ -685,15 +678,4 @@ func (nm *networkManager) GetEndpointID(containerID, ifName string) string {
return ""
}
return containerID + "-" + ifName

}

// GetEndpointID returns a unique endpoint ID based on the CNI mode.
func (nm *networkManager) GetEndpointInfoByIP(ipAddresses []net.IPNet, networkId string) (string, error) {
// Call the platform implementation.
endpointData, err := nm.GetEndpointInfoByIPImpl(ipAddresses, networkId)
if err != nil {
return "", err
}
return endpointData, nil
}
2 changes: 0 additions & 2 deletions platform/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ const (
CNIIpamStatePath = "/var/run/azure-vnet-ipam.json"
// CNIBinaryPath is the path to the CNI binary
CNIBinaryPath = "/opt/cni/bin/azure-vnet"
// StatefullCNIBinaryPath is the path to the Statefull CNI binary
StatefullCNIBinaryPath = "/opt/cni/bin/azure-vnet-statefull"
// CNSRuntimePath is the path where CNS state files are stored.
CNSRuntimePath = "/var/run/"
// CNI runtime path on a Kubernetes cluster
Expand Down
3 changes: 0 additions & 3 deletions platform/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const (
// CNIBinaryPath is the path to the CNI binary
CNIBinaryPath = "C:\\k\\azurecni\\bin\\azure-vnet.exe"

// StatefullCNIBinaryPath is the path to the Statefull CNI binary
StatefullCNIBinaryPath = "C:\\k\\azurecni\\bin\\azure-vnet-statefull.exe"

// CNI runtime path on a Kubernetes cluster
K8SCNIRuntimePath = "C:\\k\\azurecni\\bin"

Expand Down

0 comments on commit 458ec6d

Please sign in to comment.