-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/agent-test: add agent-test binary.
Add a stand-alone binary for testing the agent. Signed-off-by: Krisztian Litkey <[email protected]>
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |