Skip to content

Commit

Permalink
cmd/agent-test: add agent-test binary.
Browse files Browse the repository at this point in the history
Add a stand-alone binary for testing the agent.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Nov 21, 2023
1 parent 50e1b84 commit 4a09f7d
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions cmd/agent-test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"flag"
"fmt"

"sigs.k8s.io/yaml"

"github.com/containers/nri-plugins/pkg/agent"
cfgapi "github.com/containers/nri-plugins/pkg/apis/config/v1alpha1"
logger "github.com/containers/nri-plugins/pkg/log"
)

var (
log = logger.Get("agent-test")
configType string
)

func configUpdate(newCfg interface{}) error {
if newCfg == nil {
return fmt.Errorf("no effective configuration")
}

cfg, ok := newCfg.(cfgapi.ResmgrConfig)
if !ok {
return fmt.Errorf("configuration of wrong type %T", newCfg)
}

meta := cfg.(cfgapi.AnyConfig).GetObjectMeta()

log.Infof("configuration updated: %s (%s), generation %d, version %s",
meta.Name, meta.UID, meta.Generation, meta.ResourceVersion)

dump, _ := yaml.Marshal(cfg)
log.InfoBlock(" <"+configType+"> ", "%s", dump)

return nil
}

func main() {
var (
cfgIf agent.ConfigInterface
options []agent.Option
updateFn agent.NotifyFn
)

flag.StringVar(&configType, "config-type", "topology-aware",
"configuration type, one of {topology-aware, balloons}")
flag.Parse()

switch configType {
case "balloons":
cfgIf = agent.BalloonsConfigInterface()
updateFn = configUpdate
case "topology-aware":
cfgIf = agent.TopologyAwareConfigInterface()
updateFn = configUpdate
case "template":
cfgIf = agent.TemplateConfigInterface()
updateFn = configUpdate
default:
log.Fatalf("invalid config type %s", configType)
}

a, err := agent.New(cfgIf, options...)
if err != nil {
log.Fatalf("failed to create agent: %v", err)
}

err = a.Start(updateFn)
if err != nil {
log.Fatalf("failed to start agent: %v", err)
}
}

0 comments on commit 4a09f7d

Please sign in to comment.