Skip to content

Commit

Permalink
feat: allow to specify entire node config in configmap
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Oct 18, 2023
1 parent 3b1a25e commit be7a19b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/google/uuid v1.3.1
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af
github.com/knadh/koanf/parsers/json v0.1.0
github.com/knadh/koanf/providers/posflag v0.1.0
github.com/knadh/koanf/v2 v2.0.1
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -109,7 +110,6 @@ require (
github.com/knadh/koanf/parsers/yaml v0.1.0 // indirect
github.com/knadh/koanf/providers/env v0.1.0 // indirect
github.com/knadh/koanf/providers/file v0.1.0 // indirect
github.com/knadh/koanf/providers/posflag v0.1.0 // indirect
github.com/knadh/koanf/providers/rawbytes v0.1.0 // indirect
github.com/knadh/koanf/providers/structs v0.1.0 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
Expand Down
36 changes: 35 additions & 1 deletion internal/cmd/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"strings"
"time"

"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/v2"
"github.com/spf13/pflag"
storagev1 "github.com/webmeshproj/storage-provider-k8s/api/storage/v1"
storageprovider "github.com/webmeshproj/storage-provider-k8s/provider"
Expand All @@ -41,6 +44,7 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlconfig "sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -78,12 +82,17 @@ func pluginInArgs(pluginName string) bool {

// Main runs the webmesh-cni daemon.
func Main(build version.BuildInfo) {
// Parse flags and setup logging.
// Build the flagset
var configMap string
var configMapNamespace string
zapset := flag.NewFlagSet("zap", flag.ContinueOnError)
fs := pflag.NewFlagSet("webmesh-cni", pflag.ContinueOnError)
cniopts.BindFlags(fs)
zapopts.BindFlags(zapset)
fs.AddGoFlagSet(zapset)
fs.StringVar(&configMap, "configmap", "", "The name of the configmap to load configuration from.")
fs.StringVar(&configMapNamespace, "configmap-namespace", "kube-system", "The namespace of the configmap to load configuration from.")

// Create a separate flag set with all plugins for usage.
usage := pflag.NewFlagSet("usage", pflag.ContinueOnError)
usage.AddFlagSet(fs)
Expand All @@ -109,6 +118,7 @@ func Main(build version.BuildInfo) {
Flagset: usage,
})

// Parse flags and setup logging.
err := fs.Parse(os.Args[1:])
if err != nil {
if errors.Is(err, pflag.ErrHelp) {
Expand All @@ -119,6 +129,30 @@ func Main(build version.BuildInfo) {
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapopts)))

// Load the configuration from flags and configmap
k := koanf.New(".")
if configMap != "" {
provider := config.NewConfigMapProvider(ctrl.GetConfigOrDie(), client.ObjectKey{
Name: configMap,
Namespace: configMapNamespace,
})
err := k.Load(provider, json.Parser())
if err != nil {
log.Error(err, "Failed to load configuration from configmap")
os.Exit(1)
}
}
err = k.Load(posflag.Provider(fs, ".", k), nil)
if err != nil {
log.Error(err, "Failed to load configuration from flags")
os.Exit(1)
}
err = k.Unmarshal("", &cniopts)
if err != nil {
log.Error(err, "Failed to unmarshal configuration")
os.Exit(1)
}

// Validate the configuration.
err = cniopts.Validate()
if err != nil {
Expand Down

0 comments on commit be7a19b

Please sign in to comment.