@@ -3,13 +3,16 @@ package pktvisor
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "fmt"
6
7
"net/http"
8
+ "os"
7
9
"os/exec"
8
10
"strconv"
9
11
"time"
10
12
11
13
"github.com/go-cmd/cmd"
12
14
"go.uber.org/zap"
15
+ "gopkg.in/yaml.v3"
13
16
14
17
"github.com/netboxlabs/orb-agent/agent/backend"
15
18
"github.com/netboxlabs/orb-agent/agent/config"
@@ -27,7 +30,6 @@ const (
27
30
versionTimeout = 2
28
31
scrapeTimeout = 5
29
32
tapsTimeout = 5
30
- defaultConfigPath = "/opt/orb/agent.yaml"
31
33
defaultAPIHost = "localhost"
32
34
defaultAPIPort = "10853"
33
35
)
@@ -110,13 +112,7 @@ func (p *pktvisorBackend) Start(ctx context.Context, cancelFunc context.CancelFu
110
112
return err
111
113
}
112
114
113
- pvOptions := []string {
114
- "--admin-api" ,
115
- "-l" ,
116
- p .adminAPIHost ,
117
- "-p" ,
118
- p .adminAPIPort ,
119
- }
115
+ pvOptions := []string {"--admin-api" }
120
116
if len (p .configFile ) > 0 {
121
117
pvOptions = append (pvOptions , "--config" , p .configFile )
122
118
}
@@ -235,20 +231,65 @@ func (p *pktvisorBackend) Configure(logger *zap.Logger, repo policies.PolicyRepo
235
231
p .logger = logger
236
232
p .policyRepo = repo
237
233
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 )
241
244
}
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
+ }
244
268
}
245
- if p .adminAPIHost , prs = config ["api_host" ].(string ); ! prs {
246
- p .adminAPIHost = defaultAPIHost
269
+
270
+ if len (configSection ) > 0 {
271
+ visorConfig ["config" ] = configSection
247
272
}
248
- if p .adminAPIPort , prs = config ["api_port" ].(string ); ! prs {
249
- p .adminAPIPort = defaultAPIPort
273
+
274
+ fullConfig := map [string ]any {
275
+ "visor" : visorConfig ,
250
276
}
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 ()
252
293
253
294
if common .Otel .Host != "" && common .Otel .Port != 0 {
254
295
p .otelReceiverHost = common .Otel .Host
0 commit comments