2020-07-16 03:34:41 +03:00
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package restserver
2020-07-16 12:51:11 +03:00
import (
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
)
2020-07-16 03:34:41 +03:00
// This file contains the internal functions called by either HTTP APIs (api.go) or
// internal APIs (definde in internalapi.go).
// This will be used internally (say by RequestController in case of AKS)
// GetPartitionKey - Get dnc/service partition key
func ( service * HTTPRestService ) GetPartitionKey ( ) ( dncPartitionKey string ) {
service . RLock ( )
dncPartitionKey = service . dncPartitionKey
service . RUnlock ( )
return
}
2020-07-16 12:51:11 +03:00
2020-07-16 13:05:28 +03:00
// This API will be called by CNS RequestController on CRD update.
func ( service * HTTPRestService ) CreateOrUpdateNetworkContainerInternal ( req cns . CreateNetworkContainerRequest ) int {
2020-07-16 12:51:11 +03:00
if req . NetworkContainerid == "" {
logger . Errorf ( "[Azure CNS] Error. NetworkContainerid is empty" )
return NetworkContainerNotSpecified
}
// For now only RequestController uses this API which will be initialized only for AKS scenario.
// Validate ContainerType is set as Docker
if service . state . OrchestratorType != cns . KubernetesCRD {
logger . Errorf ( "[Azure CNS] Error. Unsupported OrchestratorType: %s" , service . state . OrchestratorType )
return UnsupportedOrchestratorType
}
// Validate PrimaryCA must never be empty
2020-07-17 08:22:10 +03:00
err := validateIPConfig ( req . IPConfiguration . IPSubnet )
if err != nil {
logger . Errorf ( "[Azure CNS] Error. PrimaryCA is invalid, NC Req: %v" , req )
return InvalidPrimaryIPConfig
}
// Validate SecondaryIPConfig
for ipId , ipconfig := range req . SecondaryIPConfigs {
// Validate Ipconfig
err := validateIPConfig ( ipconfig . IPSubnet )
if err != nil {
logger . Errorf ( "[Azure CNS] Error. SecondaryIpConfig, Id:%s is invalid, NC Req: %v" , ipId , req )
2020-07-23 02:28:07 +03:00
return InvalidSecondaryIPConfig
2020-07-17 08:22:10 +03:00
}
2020-07-16 12:51:11 +03:00
}
// Validate if state exists already
2020-07-22 01:26:37 +03:00
existing , ok := service . getNetworkContainerDetails ( req . NetworkContainerid )
2020-07-16 12:51:11 +03:00
if ok {
existingReq := existing . CreateNetworkContainerRequest
if existingReq . PrimaryInterfaceIdentifier != req . PrimaryInterfaceIdentifier {
logger . Errorf ( "[Azure CNS] Error. PrimaryCA is not same, NCId %s, old CA %s, new CA %s" , req . NetworkContainerid , existingReq . PrimaryInterfaceIdentifier , req . PrimaryInterfaceIdentifier )
return PrimaryCANotSame
}
}
// This will Create Or Update the NC state.
returnCode , returnMessage := service . saveNetworkContainerGoalState ( req )
// If the NC was created successfully, log NC snapshot.
if returnCode == 0 {
logNCSnapshot ( req )
} else {
logger . Errorf ( returnMessage )
}
return returnCode
}