Skip to content

Commit 481b931

Browse files
authored
feat: implement pktvisor config parser (#70)
1 parent 371a08a commit 481b931

File tree

2 files changed

+61
-21
lines changed

2 files changed

+61
-21
lines changed

agent/backend/pktvisor/pktvisor.go

+59-18
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package pktvisor
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"net/http"
8+
"os"
79
"os/exec"
810
"strconv"
911
"time"
1012

1113
"github.com/go-cmd/cmd"
1214
"go.uber.org/zap"
15+
"gopkg.in/yaml.v3"
1316

1417
"github.com/netboxlabs/orb-agent/agent/backend"
1518
"github.com/netboxlabs/orb-agent/agent/config"
@@ -27,7 +30,6 @@ const (
2730
versionTimeout = 2
2831
scrapeTimeout = 5
2932
tapsTimeout = 5
30-
defaultConfigPath = "/opt/orb/agent.yaml"
3133
defaultAPIHost = "localhost"
3234
defaultAPIPort = "10853"
3335
)
@@ -110,13 +112,7 @@ func (p *pktvisorBackend) Start(ctx context.Context, cancelFunc context.CancelFu
110112
return err
111113
}
112114

113-
pvOptions := []string{
114-
"--admin-api",
115-
"-l",
116-
p.adminAPIHost,
117-
"-p",
118-
p.adminAPIPort,
119-
}
115+
pvOptions := []string{"--admin-api"}
120116
if len(p.configFile) > 0 {
121117
pvOptions = append(pvOptions, "--config", p.configFile)
122118
}
@@ -235,20 +231,65 @@ func (p *pktvisorBackend) Configure(logger *zap.Logger, repo policies.PolicyRepo
235231
p.logger = logger
236232
p.policyRepo = repo
237233

238-
var prs bool
239-
if p.binary, prs = config["binary"].(string); !prs {
240-
p.binary = defaultBinary
234+
p.binary = defaultBinary
235+
p.adminAPIHost = defaultAPIHost
236+
p.adminAPIPort = defaultAPIPort
237+
p.agentLabels = common.Otel.AgentLabels
238+
239+
// Create temp config file
240+
tmpDir := os.TempDir()
241+
tmpFile, err := os.CreateTemp(tmpDir, "pktvisor-*.yaml")
242+
if err != nil {
243+
return fmt.Errorf("failed to create pktvisor temp config file: %w", err)
241244
}
242-
if p.configFile, prs = config["config_file"].(string); !prs {
243-
p.configFile = defaultConfigPath
245+
246+
// Prepare the visor configuration structure
247+
visorConfig := make(map[string]interface{})
248+
configSection := make(map[string]interface{})
249+
250+
// Process all config entries
251+
for key, value := range config {
252+
switch key {
253+
case "host":
254+
if v, ok := value.(string); ok {
255+
p.adminAPIHost = v
256+
}
257+
configSection[key] = value
258+
case "port":
259+
if v, ok := value.(string); ok {
260+
p.adminAPIPort = v
261+
}
262+
configSection[key] = value
263+
case "taps":
264+
visorConfig["taps"] = value
265+
default:
266+
configSection[key] = value
267+
}
244268
}
245-
if p.adminAPIHost, prs = config["api_host"].(string); !prs {
246-
p.adminAPIHost = defaultAPIHost
269+
270+
if len(configSection) > 0 {
271+
visorConfig["config"] = configSection
247272
}
248-
if p.adminAPIPort, prs = config["api_port"].(string); !prs {
249-
p.adminAPIPort = defaultAPIPort
273+
274+
fullConfig := map[string]any{
275+
"visor": visorConfig,
250276
}
251-
p.agentLabels = common.Otel.AgentLabels
277+
278+
yamlData, err := yaml.Marshal(fullConfig)
279+
if err != nil {
280+
if rerr := os.Remove(tmpFile.Name()); rerr != nil {
281+
p.logger.Error("failed to remove temp config file", zap.String("file", tmpFile.Name()), zap.Error(rerr))
282+
}
283+
return fmt.Errorf("failed to marshal config: %w", err)
284+
}
285+
if err := os.WriteFile(tmpFile.Name(), yamlData, 0o644); err != nil {
286+
if rerr := os.Remove(tmpFile.Name()); rerr != nil {
287+
p.logger.Error("failed to remove temp config file", zap.String("file", tmpFile.Name()), zap.Error(rerr))
288+
}
289+
return fmt.Errorf("failed to write config file: %w", err)
290+
}
291+
292+
p.configFile = tmpFile.Name()
252293

253294
if common.Otel.Host != "" && common.Otel.Port != 0 {
254295
p.otelReceiverHost = common.Otel.Host

agent/backend/pktvisor/vars.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
// RegisterBackendSpecificVariables registers the backend specific variables for the pktvisor backend
88
func RegisterBackendSpecificVariables(v *viper.Viper) {
99
v.SetDefault("orb.backends.pktvisor.binary", "/usr/local/bin/pktvisord")
10-
v.SetDefault("orb.backends.pktvisor.config_file", "/opt/orb/agent.yaml")
11-
v.SetDefault("orb.backends.pktvisor.api_host", "localhost")
12-
v.SetDefault("orb.backends.pktvisor.api_port", "10853")
10+
v.SetDefault("orb.backends.pktvisor.host", "localhost")
11+
v.SetDefault("orb.backends.pktvisor.port", "10853")
1312
}

0 commit comments

Comments
 (0)