Skip to content

Commit

Permalink
zap: change cni component structure (#2143)
Browse files Browse the repository at this point in the history
* change cni component structure

* fix component name

* add pid back

* add pid back
  • Loading branch information
paulyufan2 authored Aug 15, 2023
1 parent a307bd4 commit 0f835df
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 91 deletions.
39 changes: 13 additions & 26 deletions cni/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,25 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
// Initialize base plugin.
err := plugin.Initialize(config)
if err != nil {
log.Logger.Error("Failed to initialize base plugin.",
zap.Error(err), zap.String("component", "cni-ipam"))
log.Logger.Error("Failed to initialize base plugin.", zap.Error(err))
return err
}

// Log platform information.
log.Logger.Info("Plugin version.", zap.String("name", plugin.Name),
zap.String("version", plugin.Version),
zap.String("component", "cni-ipam"))
zap.String("version", plugin.Version))
log.Logger.Info("Running on",
zap.String("platform", platform.GetOSInfo()),
zap.String("component", "cni-ipam"))
zap.String("platform", platform.GetOSInfo()))

// Initialize address manager. rehyrdration not required on reboot for cni ipam plugin
err = plugin.am.Initialize(config, false, plugin.Options)
if err != nil {
log.Logger.Error("Failed to initialize address manager",
zap.String("error", err.Error()),
zap.String("component", "cni-ipam"))
zap.String("error", err.Error()))
return err
}

log.Logger.Info("Plugin started", zap.String("component", "cni-ipam"))
log.Logger.Info("Plugin started")

return nil
}
Expand All @@ -93,7 +89,7 @@ func (plugin *ipamPlugin) Start(config *common.PluginConfig) error {
func (plugin *ipamPlugin) Stop() {
plugin.am.Uninitialize()
plugin.Uninitialize()
log.Logger.Info("Plugin stopped", zap.String("component", "cni-ipam"))
log.Logger.Info("Plugin stopped")
}

// Configure parses and applies the given network configuration.
Expand All @@ -105,8 +101,7 @@ func (plugin *ipamPlugin) Configure(stdinData []byte) (*cni.NetworkConfig, error
}

log.Logger.Info("Read network configuration",
zap.Any("config", nwCfg),
zap.String("component", "cni-ipam"))
zap.Any("config", nwCfg))

// Apply IPAM configuration.

Expand Down Expand Up @@ -151,14 +146,12 @@ func (plugin *ipamPlugin) Add(args *cniSkel.CmdArgs) error {
zap.String("IfName", args.IfName),
zap.String("Args", args.Args),
zap.String("Path", args.Path),
zap.ByteString("StdinData", args.StdinData),
zap.String("component", "cni-ipam"))
zap.ByteString("StdinData", args.StdinData))

defer func() {
log.Logger.Info("ADD command completed",
zap.Any("result", result),
zap.Any("error:", err),
zap.String("component", "cni-ipam"))
zap.Any("error:", err))
}()

// Parse network configuration from stdin.
Expand Down Expand Up @@ -196,17 +189,15 @@ func (plugin *ipamPlugin) Add(args *cniSkel.CmdArgs) error {
defer func() {
if err != nil && poolID != "" {
log.Logger.Info("Releasing pool",
zap.String("poolId", poolID),
zap.String("component", "cni-ipam"))
zap.String("poolId", poolID))
_ = plugin.am.ReleasePool(nwCfg.IPAM.AddrSpace, poolID)
}
}()

nwCfg.IPAM.Subnet = subnet
log.Logger.Info("Allocated address with subnet",
zap.String("poolId", poolID),
zap.String("subnet", subnet),
zap.String("component", "cni-ipam"))
zap.String("subnet", subnet))
}

// Allocate an address for the endpoint.
Expand All @@ -219,16 +210,12 @@ func (plugin *ipamPlugin) Add(args *cniSkel.CmdArgs) error {
// On failure, release the address.
defer func() {
if err != nil && address != "" {
log.Logger.Info("Releasing address",
zap.String("address", address),
zap.String("component", "cni-ipam"))
log.Logger.Info("Releasing address", zap.String("address", address))
_ = plugin.am.ReleaseAddress(nwCfg.IPAM.AddrSpace, nwCfg.IPAM.Subnet, address, options)
}
}()

log.Logger.Info("Allocated address",
zap.String("address", address),
zap.String("component", "cni-ipam"))
log.Logger.Info("Allocated address", zap.String("address", address))

// Parse IP address.
ipAddress, err := platform.ConvertStringToIPNet(address)
Expand Down
2 changes: 2 additions & 0 deletions cni/ipam/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
name = "azure-vnet-ipam"
maxLogFileSizeInMb = 5
maxLogFileCount = 8
component = "cni"
)

// Version is populated by make during build.
Expand Down Expand Up @@ -48,6 +49,7 @@ func main() {
MaxSizeInMB: maxLogFileSizeInMb,
MaxBackups: maxLogFileCount,
Name: name,
Component: component,
}
zaplog.Initialize(ctx, loggerCfg)

Expand Down
2 changes: 2 additions & 0 deletions cni/ipam/pluginv6/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
name = "azure-vnet-ipamv6"
maxLogFileSizeInMb = 5
maxLogFileCount = 8
component = "cni"
)

// Version is populated by make during build.
Expand Down Expand Up @@ -48,6 +49,7 @@ func main() {
MaxSizeInMB: maxLogFileSizeInMb,
MaxBackups: maxLogFileCount,
Name: name,
Component: component,
}
zaplog.Initialize(ctx, loggerCfg)

Expand Down
2 changes: 2 additions & 0 deletions cni/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
MaxSizeInMB int
MaxBackups int
Name string
Component string
}

var Logger *zap.Logger
Expand Down Expand Up @@ -48,6 +49,7 @@ func newFileLogger(cfg *Config) *zap.Logger {
core := zapcore.NewCore(jsonEncoder, logFileWriter, logLevel)
Logger = zap.New(core)
Logger = Logger.With(zap.Int("pid", os.Getpid()))
Logger = Logger.With(zap.String("component", cfg.Component))

return Logger
}
10 changes: 4 additions & 6 deletions cni/network/invoker_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
func (invoker *AzureIPAMInvoker) deleteIpamState() {
cniStateExists, err := platform.CheckIfFileExists(platform.CNIStateFilePath)
if err != nil {
log.Logger.Error("Error checking CNI state exist",
zap.Error(err),
zap.String("component", "cni"))
log.Logger.Error("Error checking CNI state exist", zap.Error(err))
return
}

Expand All @@ -116,15 +114,15 @@ func (invoker *AzureIPAMInvoker) deleteIpamState() {

ipamStateExists, err := platform.CheckIfFileExists(platform.CNIIpamStatePath)
if err != nil {
log.Logger.Error("Error checking IPAM state exist", zap.Error(err), zap.String("component", "cni"))
log.Logger.Error("Error checking IPAM state exist", zap.Error(err))
return
}

if ipamStateExists {
log.Logger.Info("Deleting IPAM state file", zap.String("component", "cni"))
log.Logger.Info("Deleting IPAM state file")
err = os.Remove(platform.CNIIpamStatePath)
if err != nil {
log.Logger.Error("Error deleting state file", zap.Error(err), zap.String("component", "cni"))
log.Logger.Error("Error deleting state file", zap.Error(err))
return
}
}
Expand Down
3 changes: 1 addition & 2 deletions cni/network/invoker_cns.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro

log.Logger.Info("Received info for pod",
zap.Any("ipv4info", info),
zap.Any("podInfo", podInfo),
zap.String("component", "cni-invoker-cns"))
zap.Any("podInfo", podInfo))
ip, ncIPNet, err := net.ParseCIDR(info.podIPAddress + "/" + fmt.Sprint(info.ncSubnetPrefix))
if ip == nil {
return IPAMAddResult{}, errors.Wrap(err, "Unable to parse IP from response: "+info.podIPAddress+" with err %w")
Expand Down
21 changes: 7 additions & 14 deletions cni/network/multitenancy.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ func (m *Multitenancy) DetermineSnatFeatureOnHost(snatFile, nmAgentSupportedApis
jsonFile.Close()
if retrieveSnatConfigErr = json.Unmarshal(bytes, &snatConfig); retrieveSnatConfigErr != nil {
log.Logger.Error("failed to unmarshal to snatConfig with error %v",
zap.Error(retrieveSnatConfigErr),
zap.String("component", "cni-net"))
zap.Error(retrieveSnatConfigErr))
}
}

Expand Down Expand Up @@ -128,8 +127,7 @@ func (m *Multitenancy) DetermineSnatFeatureOnHost(snatFile, nmAgentSupportedApis
} else {
log.Logger.Error("failed to save snat settings",
zap.String("snatConfgFile", snatConfigFile),
zap.Error(err),
zap.String("component", "cni-net"))
zap.Error(err))
}
}
} else {
Expand All @@ -141,26 +139,21 @@ func (m *Multitenancy) DetermineSnatFeatureOnHost(snatFile, nmAgentSupportedApis
// Log and return the error when we fail acquire snat configuration for host and dns
if retrieveSnatConfigErr != nil {
log.Logger.Error("failed to acquire SNAT configuration with error %v",
zap.Error(retrieveSnatConfigErr),
zap.String("component", "cni-net"))
zap.Error(retrieveSnatConfigErr))
return snatConfig.EnableSnatForDns, snatConfig.EnableSnatOnHost, retrieveSnatConfigErr
}

log.Logger.Info("saved snat settings",
zap.Any("snatConfig", snatConfig),
zap.String("snatConfigfile", snatConfigFile),
zap.String("component", "cni-net"))
zap.String("snatConfigfile", snatConfigFile))
if snatConfig.EnableSnatOnHost {
log.Logger.Info("enabling SNAT on container host for outbound connectivity",
zap.String("component", "cni-net"))
log.Logger.Info("enabling SNAT on container host for outbound connectivity")
}
if snatConfig.EnableSnatForDns {
log.Logger.Info("enabling SNAT on container host for DNS traffic",
zap.String("component", "cni-net"))
log.Logger.Info("enabling SNAT on container host for DNS traffic")
}
if !snatConfig.EnableSnatForDns && !snatConfig.EnableSnatOnHost {
log.Logger.Info("disabling SNAT on container host",
zap.String("component", "cni-net"))
log.Logger.Info("disabling SNAT on container host")
}

return snatConfig.EnableSnatForDns, snatConfig.EnableSnatOnHost, nil
Expand Down
16 changes: 6 additions & 10 deletions cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,16 @@ func (plugin *NetPlugin) Start(config *common.PluginConfig) error {
// Log platform information.
log.Logger.Info("Plugin Info",
zap.String("name", plugin.Name),
zap.String("version", plugin.Version),
zap.String("component", "cni-net"))
zap.String("version", plugin.Version))

// Initialize network manager. rehyrdration not required on reboot for cni plugin
err = plugin.nm.Initialize(config, false)
if err != nil {
log.Logger.Error("Failed to initialize network manager",
zap.Error(err),
zap.String("component", "cni-net"))
log.Logger.Error("Failed to initialize network manager", zap.Error(err))
return err
}

log.Logger.Info("Plugin started", zap.String("component", "cni-net"))
log.Logger.Info("Plugin started")

return nil
}
Expand Down Expand Up @@ -206,7 +203,7 @@ func (plugin *NetPlugin) GetAllEndpointState(networkid string) (*api.AzureCNISta
func (plugin *NetPlugin) Stop() {
plugin.nm.Uninitialize()
plugin.Uninitialize()
log.Logger.Info("Plugin stopped", zap.String("component", "cni-net"))
log.Logger.Info("Plugin stopped")
}

// FindMasterInterface returns the name of the master interface.
Expand Down Expand Up @@ -270,8 +267,7 @@ func (plugin *NetPlugin) getPodInfo(args string) (name, ns string, err error) {

func SetCustomDimensions(cniMetric *telemetry.AIMetric, nwCfg *cni.NetworkConfig, err error) {
if cniMetric == nil {
log.Logger.Error("Unable to set custom dimension. Report is nil",
zap.String("component", "cni"))
log.Logger.Error("Unable to set custom dimension. Report is nil")
return
}

Expand Down Expand Up @@ -313,7 +309,7 @@ func addNatIPV6SubnetInfo(nwCfg *cni.NetworkConfig,
Gateway: resultV6.IPs[0].Gateway,
}
log.Logger.Info("ipv6 subnet info",
zap.Any("ipv6SubnetInfo", ipv6SubnetInfo), zap.String("component", "net"))
zap.Any("ipv6SubnetInfo", ipv6SubnetInfo))
nwInfo.Subnets = append(nwInfo.Subnets, ipv6SubnetInfo)
}
}
Expand Down
18 changes: 6 additions & 12 deletions cni/network/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,20 @@ func (plugin *NetPlugin) handleConsecutiveAdd(args *cniSkel.CmdArgs, endpointId
hnsEndpoint, err := network.Hnsv1.GetHNSEndpointByName(endpointId)
if hnsEndpoint != nil {
log.Logger.Info("Found existing endpoint through hcsshim",
zap.Any("endpoint", hnsEndpoint),
zap.String("component", "net"))
zap.Any("endpoint", hnsEndpoint))
endpoint, _ := network.Hnsv1.GetHNSEndpointByID(hnsEndpoint.Id)
isAttached, _ := network.Hnsv1.IsAttached(endpoint, args.ContainerID)
// Attach endpoint if it's not attached yet.
if !isAttached {
log.Logger.Info("Attaching endpoint to container",
zap.String("endpoint", hnsEndpoint.Id),
zap.String("container", args.ContainerID),
zap.String("component", "net"))
zap.String("container", args.ContainerID))
err := network.Hnsv1.HotAttachEndpoint(args.ContainerID, hnsEndpoint.Id)
if err != nil {
log.Logger.Error("Failed to hot attach shared endpoint to container",
zap.String("endpoint", hnsEndpoint.Id),
zap.String("container", args.ContainerID),
zap.Error(err),
zap.String("component", "cni-net"))
zap.Error(err))
return nil, err
}
}
Expand Down Expand Up @@ -254,8 +251,7 @@ func getEndpointDNSSettings(nwCfg *cni.NetworkConfig, result *cniTypesCurr.Resul
// getPoliciesFromRuntimeCfg returns network policies from network config.
func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig, isIPv6Enabled bool) []policy.Policy {
log.Logger.Info("Runtime Info",
zap.Any("config", nwCfg.RuntimeConfig),
zap.String("component", "net"))
zap.Any("config", nwCfg.RuntimeConfig))
var policies []policy.Policy
var protocol uint32

Expand Down Expand Up @@ -290,8 +286,7 @@ func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig, isIPv6Enabled bool) []p
}

log.Logger.Info("Creating port mapping policyv4",
zap.Any("policy", policyv4),
zap.String("component", "net"))
zap.Any("policy", policyv4))
policies = append(policies, policyv4)

// add port mapping policy for v6 if we have IPV6 enabled
Expand All @@ -317,8 +312,7 @@ func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig, isIPv6Enabled bool) []p
}

log.Logger.Info("Creating port mapping policyv6",
zap.Any("policy", policyv6),
zap.String("component", "net"))
zap.Any("policy", policyv6))
policies = append(policies, policyv6)
}
}
Expand Down
2 changes: 2 additions & 0 deletions cni/network/plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
name = "azure-vnet"
maxLogFileSizeInMb = 5
maxLogFileCount = 8
component = "cni"
)

// Version is populated by make during build.
Expand Down Expand Up @@ -311,6 +312,7 @@ func main() {
MaxSizeInMB: maxLogFileSizeInMb,
MaxBackups: maxLogFileCount,
Name: name,
Component: component,
}
zaplog.Initialize(ctx, loggerCfg)

Expand Down
Loading

0 comments on commit 0f835df

Please sign in to comment.