From 9cd75677147ea8197e8b446df5169990ca580129 Mon Sep 17 00:00:00 2001 From: Avi Zimmerman Date: Tue, 3 Oct 2023 12:12:52 +0300 Subject: [PATCH] add defaulters to netconf types --- internal/types/netconf.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/types/netconf.go b/internal/types/netconf.go index a19b114..80626bc 100644 --- a/internal/types/netconf.go +++ b/internal/types/netconf.go @@ -25,6 +25,7 @@ import ( "github.com/containernetworking/cni/pkg/skel" cnitypes "github.com/containernetworking/cni/pkg/types" + meshsys "github.com/webmeshproj/webmesh/pkg/meshnet/system" meshtypes "github.com/webmeshproj/webmesh/pkg/storage/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -46,6 +47,14 @@ type NetConf struct { LogLevel string `json:"logLevel"` } +func (n *NetConf) Default() { + n.Kubernetes.Default() + n.Interface.Default() + if n.LogLevel == "" { + n.LogLevel = "info" + } +} + // Interface is the configuration for a single interface. type Interface struct { // MTU is the MTU to set on interfaces. @@ -56,6 +65,12 @@ type Interface struct { DisableIPv6 bool `json:"disableIPv6"` } +func (i *Interface) Default() { + if i.MTU <= 0 { + i.MTU = meshsys.DefaultMTU + } +} + // Kubernetes is the configuration for the Kubernetes API server and // information about the node we are running on. type Kubernetes struct { @@ -69,6 +84,13 @@ type Kubernetes struct { Namespace string `json:"namespace"` } +// Default sets the default values for the Kubernetes configuration. +func (k *Kubernetes) Default() { + if k.Kubeconfig == "" { + k.Kubeconfig = DefaultKubeconfigPath + } +} + // LoadConfigFromArgs loads the configuration from the given CNI arguments. func LoadConfigFromArgs(cmd *skel.CmdArgs) (*NetConf, error) { var conf NetConf @@ -76,9 +98,7 @@ func LoadConfigFromArgs(cmd *skel.CmdArgs) (*NetConf, error) { if err != nil { return nil, fmt.Errorf("failed to load netconf from stdin data: %w", err) } - if conf.Kubernetes.Kubeconfig == "" { - conf.Kubernetes.Kubeconfig = DefaultKubeconfigPath - } + conf.Default() return &conf, nil }