Repository: microsoft/retina
File: pkg/plugin/ebpfwindows/ebpf_windows.go (Go)

Code before:

	p.parser = parser
	return nil
}

// Name returns the name of the ebpfwindows plugin
func (p *Plugin) Name() string {
	return name
}

// Start the plugin by starting a periodic timer.
func (p *Plugin) Start(ctx context.Context) error {
	p.l.Info("Start ebpfWindows plugin...")

	ciliumEnabled, err := plugincommon.IsCiliumOnWindowsEnabled()

	if ciliumEnabled {
		p.l.Info("Cilium is enabled on Windows, proceeding with ebpfWindows plugin initialization")
		p.pullMetricsAndEvents(ctx)
		p.l.Info("Complete ebpfWindows plugin...")
		return nil
	}

	if err != nil {
		p.l.Error("Error while checking if Cilium is enabled on Windows", zap.Error(err))
	}

	// Wait for either context cancellation
	select {
	case <-ctx.Done():
		p.l.Info("Context cancelled, exiting Start loop")
		return nil
	}

}

// metricsMapIterateCallback is the callback function that is called for each key-value pair in the metrics map.
func (p *Plugin) metricsMapIterateCallback(key *MetricsKey, value *MetricsValues) {
	if key == nil {
		p.l.Error("MetricsMapIterateCallback key is nil")
		return
	}
	if value == nil {
		p.l.Error("MetricsMapIterateCallback value is nil")
		return
	}
	if key.IsDrop() {
		p.l.Debug("MetricsMapIterateCallback Drop", zap.String("key", key.String()))
		if key.IsEgress() {
			metrics.DropBytesGauge.WithLabelValues(key.DropForwardReason(), egressLabel).Set(float64(value.BytesSum()))
			metrics.DropPacketsGauge.WithLabelValues(key.DropForwardReason(), egressLabel).Set(float64(value.Sum()))

Review comment (suggestion):
We should check for error first. What should be expected behavior in case of error? I would imagine plugin would return the error without running the plugin.

Code after:

	p.parser = parser
	return nil
}

// Name returns the name of the ebpfwindows plugin
func (p *Plugin) Name() string {
	return name
}

// Start the plugin by starting a periodic timer.
func (p *Plugin) Start(ctx context.Context) error {
	p.l.Info("Start ebpfWindows plugin...")

	ciliumEnabled, err := plugincommon.IsCiliumOnWindowsEnabled()

	if err != nil {
		p.l.Error("Error while checking if Cilium is enabled on Windows", zap.Error(err))
		return fmt.Errorf("Failed to check if Cilium is enabled on Windows: %w", err)
	}

	if !ciliumEnabled {
		p.l.Warn("Cilium is not enabled on Windows, skipping ebpfWindows plugin initialization")
		return nil
	}

	p.l.Info("Cilium is enabled on Windows, proceeding with ebpfWindows plugin initialization")
	p.pullMetricsAndEvents(ctx)
	p.l.Info("Complete ebpfWindows plugin...")
	return nil
}

// metricsMapIterateCallback is the callback function that is called for each key-value pair in the metrics map.
func (p *Plugin) metricsMapIterateCallback(key *MetricsKey, value *MetricsValues) {
	if key == nil {
		p.l.Error("MetricsMapIterateCallback key is nil")
		return
	}
	if value == nil {
		p.l.Error("MetricsMapIterateCallback value is nil")
		return
	}
	if key.IsDrop() {
		p.l.Debug("MetricsMapIterateCallback Drop", zap.String("key", key.String()))
		if key.IsEgress() {
			metrics.DropBytesGauge.WithLabelValues(key.DropForwardReason(), egressLabel).Set(float64(value.BytesSum()))
			metrics.DropPacketsGauge.WithLabelValues(key.DropForwardReason(), egressLabel).Set(float64(value.Sum()))
		} else if key.IsIngress() {
			metrics.DropBytesGauge.WithLabelValues(key.DropForwardReason(), ingressLabel).Set(float64(value.BytesSum()))
			metrics.DropPacketsGauge.WithLabelValues(key.DropForwardReason(), ingressLabel).Set(float64(value.Sum()))
		}