Skip to content

Commit 560e696

Browse files
committed
feat(standalone): Add errors on missing crictl on machine
1 parent 4dbd6f0 commit 560e696

File tree

9 files changed

+34
-15
lines changed

9 files changed

+34
-15
lines changed

cmd/standalone/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (d *Daemon) Start() error {
6262
}
6363
go controller.Run(ctx)
6464

65-
// Standalone requires pod level to be disabled
65+
// Initialize controller manager
6666
controllerMgr, err := cm.NewStandaloneControllerManager(daemonCfg, tel)
6767
if err != nil {
6868
mainLogger.Fatal("Failed to create controller manager", zap.Error(err))

deploy/standard/manifests/controller/helm/retina/values.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ image:
5151
tag: "v0.0.2"
5252

5353
enableConntrackMetrics: false
54-
enableStandalone: false
55-
EnableCrictl: false
5654
enablePodLevel: false
5755
remoteContext: false
5856
enableAnnotations: false

pkg/config/standalone_config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type StandaloneConfig struct {
2323
MetricsInterval time.Duration `yaml:"metricsInterval"`
2424
TelemetryInterval time.Duration `yaml:"telemetryInterval"`
2525
EnrichmentMode string `yaml:"enrichmentMode"`
26-
CriCtlCommandTimeout time.Duration `yaml:"crictlCommandTimeout"`
26+
CrictlCommandTimeout time.Duration `yaml:"crictlCommandTimeout"`
2727
StateFileLocation string `yaml:"stateFileLocation"`
2828
}
2929

@@ -36,7 +36,7 @@ var (
3636
MetricsInterval: time.Second,
3737
EnrichmentMode: "crictl",
3838
TelemetryInterval: DefaultTelemetryInterval,
39-
CriCtlCommandTimeout: 5 * time.Second,
39+
CrictlCommandTimeout: 5 * time.Second,
4040
}
4141

4242
ErrMissingStateFileLocation = errors.New("stateFileLocation must be set when using statefile enrichment mode")
@@ -80,9 +80,9 @@ func GetStandaloneConfig(cfgFilename string) (*StandaloneConfig, error) {
8080
config.TelemetryInterval = DefaultTelemetryInterval
8181
}
8282

83-
if config.CriCtlCommandTimeout == 0 {
84-
log.Printf("crictlCommandTimeout is not set, defaulting to %v", DefaultStandaloneConfig.CriCtlCommandTimeout)
85-
config.CriCtlCommandTimeout = DefaultStandaloneConfig.CriCtlCommandTimeout
83+
if config.CrictlCommandTimeout == 0 {
84+
log.Printf("crictlCommandTimeout is not set, defaulting to %v", DefaultStandaloneConfig.CrictlCommandTimeout)
85+
config.CrictlCommandTimeout = DefaultStandaloneConfig.CrictlCommandTimeout
8686
}
8787

8888
switch {

pkg/config/standalone_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestGetStandaloneConfig(t *testing.T) {
2323
c.MetricsInterval != 1*time.Second ||
2424
c.TelemetryInterval != 15*time.Minute ||
2525
c.EnrichmentMode != "azure-vnet-statefile" ||
26-
c.CriCtlCommandTimeout != 5*time.Second ||
26+
c.CrictlCommandTimeout != 5*time.Second ||
2727
c.StateFileLocation != "/generic/file/location/azure-vnet.json" {
2828
t.Errorf("Expeted config should be same as ./testwith/config-standalone.yaml; instead got %+v", c)
2929
}

pkg/controllers/daemon/standalone/controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ func New(config *kcfg.StandaloneConfig, cache *standalone.Cache, metricsModule *
3939

4040
switch {
4141
case config.EnrichmentMode == "crictl":
42-
src = ctrinfo.New(config.CriCtlCommandTimeout)
42+
src, err = ctrinfo.New(config.CrictlCommandTimeout)
43+
if err != nil {
44+
return nil, fmt.Errorf("failed to create crictl source: %w", err)
45+
}
4346

4447
case strings.HasSuffix(config.EnrichmentMode, "statefile"):
4548
src, err = statefile.New(config.EnrichmentMode, config.StateFileLocation)
@@ -103,7 +106,7 @@ func (c *Controller) Reconcile(ctx context.Context) error {
103106
func (c *Controller) Run(ctx context.Context) {
104107
c.l.Info("Starting controller")
105108

106-
ticker := time.NewTicker(c.config.MetricsInterval / 2)
109+
ticker := time.NewTicker(c.config.MetricsInterval)
107110
defer ticker.Stop()
108111

109112
for {

pkg/controllers/daemon/standalone/source/ctrinfo/ctrinfo.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,20 @@ var (
4848
errJSONRead = errors.New("error unmarshalling JSON")
4949
)
5050

51-
func New(commandTimeout time.Duration) *Ctrinfo {
51+
func new(commandTimeout time.Duration) *Ctrinfo {
5252
return &Ctrinfo{
5353
commandTimeout: commandTimeout,
5454
}
5555
}
5656

57+
func New(commandTimeout time.Duration) (*Ctrinfo, error) {
58+
_, err := exec.LookPath("crictl")
59+
if err != nil {
60+
return nil, fmt.Errorf("crictl not found in PATH: %w", err)
61+
}
62+
return new(commandTimeout), nil
63+
}
64+
5765
func (c *Ctrinfo) GetAllEndpoints() ([]*common.RetinaEndpoint, error) {
5866
// Using crictl to get all running pods
5967
runningPods, err := getPodsCmd(c)

pkg/controllers/daemon/standalone/source/ctrinfo/ctrinfo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestCtrinfoGetAllEndpoints(t *testing.T) {
2121
require.NoError(t, err, "failed to create invalid JSON file")
2222
defer os.Remove(invalidJSONPath)
2323

24-
src := New(5 * time.Second)
24+
src := new(1 * time.Second)
2525

2626
tests := []struct {
2727
name string

pkg/controllers/daemon/standalone/source/statefile/statefile.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package statefile
55

66
import (
7+
"os"
8+
79
"github.com/pkg/errors"
810

911
"github.com/microsoft/retina/pkg/controllers/daemon/standalone/source"
@@ -12,11 +14,19 @@ import (
1214

1315
var ErrUnsupportedStatefileType = errors.New("unsupported statefile enrichment type, valid types are: azure-vnet-statefile")
1416

15-
func New(enrichmentMode, location string) (source.Source, error) {
17+
func new(enrichmentMode, location string) (source.Source, error) {
1618
switch enrichmentMode {
1719
case "azure-vnet-statefile":
1820
return azure.New(location), nil
1921
default:
2022
return nil, errors.Wrapf(ErrUnsupportedStatefileType, "enrichmentMode=%s", enrichmentMode)
2123
}
2224
}
25+
26+
func New(enrichmentMode, location string) (source.Source, error) {
27+
if _, err := os.Stat(location); os.IsNotExist(err) {
28+
return nil, errors.Wrapf(err, "statefile does not exist at location: %s", location)
29+
}
30+
31+
return new(enrichmentMode, location)
32+
}

pkg/controllers/daemon/standalone/source/statefile/statefile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestNew(t *testing.T) {
3636

3737
for _, tt := range tests {
3838
t.Run(tt.name, func(t *testing.T) {
39-
src, err := New(tt.enrichmentMode, tt.location)
39+
src, err := new(tt.enrichmentMode, tt.location)
4040

4141
if tt.wantErr != nil {
4242
require.ErrorContains(t, err, tt.wantErr.Error())

0 commit comments

Comments
 (0)