Skip to content

Commit

Permalink
add defaulters to netconf types
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Oct 3, 2023
1 parent c6fd2bd commit 9cd7567
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions internal/types/netconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -69,16 +84,21 @@ 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
err := json.Unmarshal(cmd.StdinData, &conf)
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
}

Expand Down

0 comments on commit 9cd7567

Please sign in to comment.