Skip to content

Commit

Permalink
addressing the latest comments
Browse files Browse the repository at this point in the history
  • Loading branch information
behzad-mir committed Feb 8, 2024
1 parent 5dca27d commit 78f6922
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
18 changes: 3 additions & 15 deletions cns/cnireconciler/podinfoprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newCNIPodInfoProvider(exc exec.Interface, stateMigration bool) (cns.PodInfo
}
var endpointState map[string]*restserver.EndpointInfo
if stateMigration {
endpointState, err = cniStateToCnsEndpointState(state)
endpointState = cniStateToCnsEndpointState(state)
} else {
endpointState = nil
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func endpointStateToPodInfoByIP(state map[string]*restserver.EndpointInfo) (map[
// cniStateToCnsEndpointState converts an AzureCNIState dumped from a CNI exec
// into a EndpointInfo map, using the containerID as keys in the map.
// The map then will be saved on CNS endpoint state
func cniStateToCnsEndpointState(state *api.AzureCNIState) (map[string]*restserver.EndpointInfo, error) {
func cniStateToCnsEndpointState(state *api.AzureCNIState) map[string]*restserver.EndpointInfo {
logger.Printf("Generating CNS Endpoint State")
endpointState := map[string]*restserver.EndpointInfo{}
for epID, endpoint := range state.ContainerInterfaces {
Expand All @@ -126,22 +126,10 @@ func cniStateToCnsEndpointState(state *api.AzureCNIState) (map[string]*restserve
for _, epIP := range endpoint.IPAddresses {
if epIP.IP.To4() == nil { // is an ipv6 address
ipconfig := net.IPNet{IP: epIP.IP, Mask: epIP.Mask}
for _, ipconf := range ipInfo.IPv6 {
if ipconf.IP.Equal(ipconfig.IP) {
logger.Errorf("Found existing ipv6 ipconfig for infra container %s", endpoint.ContainerID)
return nil, restserver.ErrExistingIpconfigFound
}
}
ipInfo.IPv6 = append(ipInfo.IPv6, ipconfig)

} else {
ipconfig := net.IPNet{IP: epIP.IP, Mask: epIP.Mask}
for _, ipconf := range ipInfo.IPv4 {
if ipconf.IP.Equal(ipconfig.IP) {
logger.Errorf("Found existing ipv4 ipconfig for infra container %s", endpoint.ContainerID)
return nil, restserver.ErrExistingIpconfigFound
}
}
ipInfo.IPv4 = append(ipInfo.IPv4, ipconfig)
}
}
Expand All @@ -150,7 +138,7 @@ func cniStateToCnsEndpointState(state *api.AzureCNIState) (map[string]*restserve
endpointState[endpointID] = endpointInfo
logger.Printf("CNS endpoint state extracted from CNI: [%+v]", *endpointInfo)
}
return endpointState, nil
return endpointState
}

// extractEndpointInfo extract Interface Name and endpointID for each endpoint based the CNI state
Expand Down
2 changes: 1 addition & 1 deletion cns/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type CNSConfig struct {
UseHTTPS bool
WatchPods bool `json:"-"`
WireserverIP string
StatelessCNIMigration bool
StateMigration bool
}

type TelemetrySettings struct {
Expand Down
4 changes: 3 additions & 1 deletion cns/restserver/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ var (
ErrNoNCs = errors.New("no NCs found in the CNS internal state")
ErrOptManageEndpointState = errors.New("CNS is not set to manage the endpoint state")
ErrEndpointStateNotFound = errors.New("endpoint state could not be found in the statefile")
ErrExistingIpconfigFound = errors.New("Found existing ipconfig for infra container")
)

const (
Expand Down Expand Up @@ -1074,6 +1073,9 @@ func (service *HTTPRestService) GetEndpointHelper(endpointID string) (*EndpointI
logger.Warnf("[GetEndpointState] Found existing endpoint state for container %s", endpointID)
return endpointInfo, nil
}
// This part is a temprory fix if we have endpoint states belong to CNI version 1.4.X on Windows since the states don't have the containerID
// In case there was no endpoint founded with ContainerID as the key,
// then [First 8 character of containerid]-eth0 will be tried
legacyEndpointID := endpointID[:ContainerIDLength] + "-" + InterfaceName
if endpointInfo, ok := service.EndpointState[legacyEndpointID]; ok {
logger.Warnf("[GetEndpointState] Found existing endpoint state for container %s", legacyEndpointID)
Expand Down
4 changes: 2 additions & 2 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1521,11 +1521,11 @@ func InitializeStateFromCNS(cnsconfig *configuration.CNSConfig, endpointStateSto
if err != nil {
if errors.Is(err, store.ErrKeyNotFound) {
logger.Printf("[Azure CNS] No endpoint state found, skipping initializing CNS state")
if cnsconfig.StatelessCNIMigration {
if cnsconfig.StateMigration {
logger.Printf("StatelessCNI Migration is enabled")
logger.Printf("initializing from Statefull CNI")
var endpointState map[string]*restserver.EndpointInfo
podInfoByIPProvider, endpointState, err = cnireconciler.NewCNIPodInfoProvider(cnsconfig.StatelessCNIMigration)
podInfoByIPProvider, endpointState, err = cnireconciler.NewCNIPodInfoProvider(cnsconfig.StateMigration)
if err != nil {
return nil, errors.Wrap(err, "failed to create CNI PodInfoProvider")
}
Expand Down

0 comments on commit 78f6922

Please sign in to comment.