Skip to content

Commit 87757ef

Browse files
committed
Create standalone daemon for non-k8s orchestration and add additional unit tests
1 parent 1595849 commit 87757ef

22 files changed

Lines changed: 1512 additions & 104 deletions

cmd/bootstrap_manager.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
10+
"github.com/microsoft/retina/cmd/standalone"
11+
"github.com/microsoft/retina/cmd/standard"
12+
"github.com/microsoft/retina/internal/buildinfo"
13+
"github.com/microsoft/retina/pkg/config"
14+
"github.com/microsoft/retina/pkg/log"
15+
"github.com/microsoft/retina/pkg/telemetry"
16+
"go.uber.org/zap"
17+
)
18+
19+
const (
20+
logFileName = "retina.log"
21+
)
22+
23+
type BootstrapManager struct {
24+
metricsAddr string
25+
probeAddr string
26+
enableLeaderElection bool
27+
configFile string
28+
}
29+
30+
func NewBootstrapManager(metricsAddr, probeAddr, configFile string, enableLeaderElection bool) *BootstrapManager {
31+
return &BootstrapManager{
32+
metricsAddr: metricsAddr,
33+
probeAddr: probeAddr,
34+
enableLeaderElection: enableLeaderElection,
35+
configFile: configFile,
36+
}
37+
}
38+
39+
func (b *BootstrapManager) Start() error {
40+
if buildinfo.ApplicationInsightsID != "" {
41+
telemetry.InitAppInsights(buildinfo.ApplicationInsightsID, buildinfo.Version)
42+
defer telemetry.ShutdownAppInsights()
43+
defer telemetry.TrackPanic()
44+
}
45+
46+
daemonConfig, err := config.GetConfig(b.configFile)
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
fmt.Println("init logger")
52+
zl, err := log.SetupZapLogger(&log.LogOpts{
53+
Level: daemonConfig.LogLevel,
54+
File: false,
55+
FileName: logFileName,
56+
MaxFileSizeMB: 100, //nolint:gomnd // defaults
57+
MaxBackups: 3, //nolint:gomnd // defaults
58+
MaxAgeDays: 30, //nolint:gomnd // defaults
59+
ApplicationInsightsID: buildinfo.ApplicationInsightsID,
60+
EnableTelemetry: daemonConfig.EnableTelemetry,
61+
},
62+
zap.String("version", buildinfo.Version),
63+
zap.String("plugins", strings.Join(daemonConfig.EnabledPlugin, `,`)),
64+
zap.String("data aggregation level", daemonConfig.DataAggregationLevel.String()),
65+
)
66+
if err != nil {
67+
panic(err)
68+
}
69+
defer zl.Close()
70+
71+
if daemonConfig.EnableStandalone {
72+
sd := standalone.NewDaemon(daemonConfig)
73+
if err = sd.Start(zl); err != nil {
74+
return fmt.Errorf("starting standalone daemon: %w", err)
75+
}
76+
return nil
77+
}
78+
79+
d := standard.NewDaemon(daemonConfig, b.metricsAddr, b.probeAddr, b.enableLeaderElection)
80+
if err := d.Start(zl); err != nil {
81+
return fmt.Errorf("starting daemon: %w", err)
82+
}
83+
return nil
84+
}

cmd/root.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88

9-
"github.com/microsoft/retina/cmd/standard"
109
"github.com/spf13/cobra"
1110
)
1211

@@ -27,9 +26,9 @@ var (
2726
Long: "Start Retina Agent",
2827
RunE: func(cmd *cobra.Command, args []string) error {
2928
// Do Stuff Here
30-
fmt.Println("Starting Retina Agent")
31-
d := standard.NewDaemon(metricsAddr, probeAddr, cfgFile, enableLeaderElection)
32-
if err := d.Start(); err != nil {
29+
fmt.Println("Bootstrapping Retina")
30+
b := NewBootstrapManager(metricsAddr, probeAddr, cfgFile, enableLeaderElection)
31+
if err := b.Start(); err != nil {
3332
return fmt.Errorf("starting daemon: %w", err)
3433
}
3534
return nil

cmd/standalone/daemon.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
package standalone
5+
6+
import (
7+
"fmt"
8+
"time"
9+
10+
"github.com/microsoft/retina/cmd/utils"
11+
"github.com/microsoft/retina/pkg/enricher"
12+
"github.com/microsoft/retina/pkg/log"
13+
"go.uber.org/zap"
14+
ctrl "sigs.k8s.io/controller-runtime"
15+
16+
"github.com/microsoft/retina/pkg/config"
17+
"github.com/microsoft/retina/pkg/controllers/cache"
18+
cm "github.com/microsoft/retina/pkg/managers/controllermanager"
19+
)
20+
21+
const TTL = 3 * time.Minute
22+
23+
type Daemon struct {
24+
config *config.Config
25+
}
26+
27+
func NewDaemon(daemonCfg *config.Config) *Daemon {
28+
return &Daemon{
29+
config: daemonCfg,
30+
}
31+
}
32+
33+
func (d *Daemon) Start(zl *log.ZapLogger) error {
34+
zl.Info("Starting Standalone Retina daemon")
35+
mainLogger := zl.Named("standalone-daemon").Sugar()
36+
37+
tel, err := utils.InitializeTelemetryClient(nil, d.config, mainLogger)
38+
if err != nil {
39+
return fmt.Errorf("failed to initialize telemetry client: %w", err)
40+
}
41+
42+
ctx := ctrl.SetupSignalHandler()
43+
44+
cache := cache.NewStandaloneCache(TTL)
45+
enrich := enricher.NewStandaloneEnricher(ctx, cache, d.config)
46+
enrich.Run()
47+
48+
// pod level needs to be disabled
49+
controllerMgr, err := cm.NewControllerManager(d.config, nil, tel)
50+
if err != nil {
51+
mainLogger.Fatal("Failed to create controller manager", zap.Error(err))
52+
}
53+
if err := controllerMgr.Init(ctx); err != nil {
54+
mainLogger.Fatal("Failed to initialize controller manager", zap.Error(err))
55+
}
56+
57+
// start heartbeat goroutine for application insights
58+
go tel.Heartbeat(ctx, d.config.TelemetryInterval)
59+
60+
// Start controller manager, which will start the http server and plugin manager
61+
go controllerMgr.Start(ctx)
62+
mainLogger.Info("Started controller manager")
63+
64+
<-ctx.Done()
65+
controllerMgr.Stop(ctx)
66+
67+
mainLogger.Info("Network observability exiting. Till next time!")
68+
return nil
69+
}

0 commit comments

Comments
 (0)