Repository: microsoft/retina
File: pkg/plugin/dropreason/ebpfsetup_linux.go (Go)

Code before:
	kv, err := version.GetKernelVersion()
	if err != nil {
		dr.l.Warn("Failed to get kernel version", zap.Error(err))
		kv, err = plugincommon.GetKernelVersionMajMin()
		if err != nil {
			return nil, nil, false, fmt.Errorf("failed to get kernel version: %w", err) //nolint:goerr113 //wrapping error from external module
		}
	}
	dr.l.Info("Detected kernel", zap.String("version", kv.String()))

	objs, maps, isFexit = resolvePayload(runtime.GOARCH, kv, isMariner)
	return objs, maps, isFexit, nil
}

func resolvePayload(arch string, kv semver.Version, isMariner bool) (interface{}, *kprobeMaps, bool) {
	minVersionAmd64, _ := versioncheck.Version(MinAmdVersionNum)
	minVersionArm64, _ := versioncheck.Version(MinArmVersionNum)

	isFexit := (arch == "amd64" && kv.GTE(minVersionAmd64)) ||
		(arch == "arm64" && kv.GTE(minVersionArm64))

	var objs interface{}
	var maps *kprobeMaps

	switch {
	case !isFexit:
		objs = &kprobeObjectsOld{} //nolint:typecheck // this is a generated struct
		maps = &objs.(*kprobeObjectsOld).kprobeMaps
	case !isMariner:
		objs = &fexitObjects{} //nolint:typecheck // this is a generated struct
		maps = &objs.(*fexitObjects).kprobeMaps
	case isMariner:
		objs = &marinerObjects{} //nolint:typecheck // needs to match a generated struct until we fix Mariner
		maps = &objs.(*marinerObjects).kprobeMaps
	}

	return objs, maps, isFexit
}

func (dr *dropReason) attachKprobes(kprobes, kprobesRet map[string]*ebpf.Program) error {
	for name := range kprobes {
		progLink, err := link.Kprobe(name, kprobes[name], nil)
		if err != nil {
			dr.l.Error("Failed to attach kprobe", zap.String("program", name), zap.Error(err))
		} else {
			dr.hooks = append(dr.hooks, progLink)
			dr.l.Info("Attached kprobe", zap.String("program", name))
		}
	}

	// The kretprobes set metric values. If none were attached, report an error.

Review comment (refactor):
Can this be refactored to be positive check? Ex:
```
switch {
case fexit:
objs = &fexitObjects{}
...
case Mariner:
objs = &marinerObjects{}
... 
}
```

I think this is more readable. 

Code after:
	kv, err := version.GetKernelVersion()
	if err != nil {
		kv, err = plugincommon.GetKernelVersionMajMin()
		if err != nil {
			return nil, nil, false, fmt.Errorf("failed to get kernel version: %w", err) //nolint:goerr113 //wrapping error from external module
		}
	}
	dr.l.Info("Detected kernel", zap.String("version", kv.String()))

	objs, maps, supportsFexit = resolvePayload(runtime.GOARCH, kv, isMariner, dr.cfg.EnablePodLevel)
	return objs, maps, supportsFexit, nil
}

func resolvePayload(arch string, kv semver.Version, isMariner, isPodLevel bool) (interface{}, *kprobeMaps, bool) {
	minVersionAmd64, _ := versioncheck.Version(MinAmdVersionNum)
	minVersionArm64, _ := versioncheck.Version(MinArmVersionNum)

	supportsFexit := (arch == "amd64" && kv.GTE(minVersionAmd64)) ||
		(arch == "arm64" && kv.GTE(minVersionArm64))

	var objs interface{}
	var maps *kprobeMaps

	switch {
	case isPodLevel: // TODO: fexit support is being rolled out in two stages, remove this when we have it for advanced metrics.
		objs = &allKprobeObjects{} //nolint:typecheck // this is a generated struct
		maps = &objs.(*allKprobeObjects).kprobeMaps
	case isMariner && supportsFexit: // Mariner supports a subset of the fexit programs, need to check for it first.
		objs = &marinerObjects{} //nolint:typecheck // needs to match a generated struct until we fix Mariner
		maps = &objs.(*marinerObjects).kprobeMaps
	case supportsFexit:
		objs = &allFexitObjects{} //nolint:typecheck // this is a generated struct
		maps = &objs.(*allFexitObjects).kprobeMaps
	default:
		objs = &allKprobeObjects{} //nolint:typecheck // this is a generated struct
		maps = &objs.(*allKprobeObjects).kprobeMaps
	}

	return objs, maps, (supportsFexit && !isPodLevel)
}

func (dr *dropReason) attachKprobes(kprobes, kprobesRet map[string]*ebpf.Program) error {
	for name := range kprobes {
		progLink, err := link.Kprobe(name, kprobes[name], nil)
		if err != nil {
			dr.l.Error("Failed to attach kprobe", zap.String("program", name), zap.Error(err))
		} else {
			dr.hooks = append(dr.hooks, progLink)
			dr.l.Info("Attached kprobe", zap.String("program", name))
		}
	}