func kernelVersionLinux()

in sharedlibraries/statushelper/statushelper.go [149:198]


func kernelVersionLinux(ctx context.Context, exec commandlineexecutor.Execute) (*spb.KernelVersion, error) {
	result := exec(ctx, commandlineexecutor.Params{
		Executable: "uname",
		Args:       []string{"-r"},
	})
	if result.Error != nil {
		return nil, fmt.Errorf("failed to fetch kernel version data: %s", result.Error)
	}

	version := &spb.KernelVersion{RawString: result.StdOut}
	parts := strings.SplitN(result.StdOut, "-", 2)
	if len(parts) != 2 {
		log.CtxLogger(ctx).Debugw("Failed to parse kernel version data from stdout", "stdout", result.StdOut)
		return version, nil
	}

	safeAtoi := func(s string) int32 {
		if s == "" {
			return 0
		}
		// Suppressing the error here as the regex ensures that the string is numeric.
		number, _ := strconv.Atoi(s)
		return int32(number)
	}
	osKernelMatch := osKernelRegex.FindStringSubmatch(parts[0])
	if osKernelMatch == nil {
		log.CtxLogger(ctx).Debugw("failed to parse linux kernel version from stdout", "stdout", result.StdOut)
	} else {
		version.OsKernel = &spb.KernelVersion_Version{
			Major: safeAtoi(osKernelMatch[1]),
			Minor: safeAtoi(osKernelMatch[2]),
			Build: safeAtoi(osKernelMatch[3]),
			Patch: safeAtoi(osKernelMatch[4]),
		}
	}
	distroKernelMatch := distroKernelRegex.FindStringSubmatch(parts[1])
	if distroKernelMatch == nil {
		log.CtxLogger(ctx).Debugw("failed to parse distro kernel version from stdout", "stdout", result.StdOut)
	} else {
		version.DistroKernel = &spb.KernelVersion_Version{
			Major:     safeAtoi(distroKernelMatch[1]),
			Minor:     safeAtoi(distroKernelMatch[2]),
			Build:     safeAtoi(distroKernelMatch[3]),
			Patch:     safeAtoi(distroKernelMatch[4]),
			Remainder: distroKernelMatch[5],
		}
	}

	return version, nil
}