Repository: hashicorp/nomad
File: client/fingerprint/plugins_cni.go (Go)

Code before:

	resp.Detected = false

	// iterate paths and validate plugins
	for _, cniPath = range cniPathList {
		// list the cni_path directory
		entries, err := f.lister(cniPath)
		switch {
		case err != nil:
			f.logger.Warn("failed to read CNI plugins directory", "cni_path", cniPath, "error", err)
		case len(entries) == 0:
			f.logger.Debug("no CNI plugins found", "cni_path", cniPath)
			resp.Detected = true
		}

		// for each file in cni_path, detect executables and try to get their version
		for _, entry := range entries {
			v, ok := f.detectOnePlugin(cniPath, entry)
			if ok {
				resp.AddAttribute(f.attribute(entry.Name()), v)
				resp.Detected = true
			}
		}
	}

	return nil
}

func (f *PluginsCNIFingerprint) attribute(filename string) string {
	return fmt.Sprintf("%s.%s", cniPluginAttribute, filename)
}

func (f *PluginsCNIFingerprint) detectOnePlugin(pluginPath string, entry os.DirEntry) (string, bool) {
	fi, err := entry.Info()
	if err != nil {
		f.logger.Debug("failed to read cni directory entry", "error", err)
		return "", false
	}

	if fi.Mode()&0o111 == 0 {
		f.logger.Debug("unexpected non-executable in cni plugin directory", "name", fi.Name())
		return "", false // not executable
	}

	exePath := filepath.Join(pluginPath, fi.Name())
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()

	// best effort attempt to get a version from the executable, otherwise
	// the version will be "unknown"
	// execute with no args; at least container-networking plugins respond with

Review comment (suggestion):
Was there a specific reason for moving this? The result of this change appears to be that it will not be included in the logged list of detected fingerprints. But all the other fingerprinters always mark this as true (regardless of any attribute additions) unless they encounter an error. So this might be a deviation there.

Code after:
	resp.Detected = true

	// cniPath could be a multi-path, e.g. /opt/cni/bin:/custom/cni/bin
	cniPathList := filepath.SplitList(cniPath)

	// iterate paths and validate plugins
	for _, cniPath = range cniPathList {
		// list the cni_path directory
		entries, err := f.lister(cniPath)
		switch {
		case err != nil:
			f.logger.Warn("failed to read CNI plugins directory", "cni_path", cniPath, "error", err)
		case len(entries) == 0:
			f.logger.Debug("no CNI plugins found", "cni_path", cniPath)
		}

		// for each file in cni_path, detect executables and try to get their version
		for _, entry := range entries {
			v, ok := f.detectOnePlugin(cniPath, entry)
			if ok {
				resp.AddAttribute(f.attribute(entry.Name()), v)
			}
		}
	}

	return nil
}

func (f *PluginsCNIFingerprint) attribute(filename string) string {
	return fmt.Sprintf("%s.%s", cniPluginAttribute, filename)
}

func (f *PluginsCNIFingerprint) detectOnePlugin(pluginPath string, entry os.DirEntry) (string, bool) {
	fi, err := entry.Info()
	if err != nil {
		f.logger.Debug("failed to read cni directory entry", "error", err)
		return "", false
	}

	if fi.Mode()&0o111 == 0 {
		f.logger.Debug("unexpected non-executable in cni plugin directory", "name", fi.Name())
		return "", false // not executable
	}

	exePath := filepath.Join(pluginPath, fi.Name())
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancel()

	// best effort attempt to get a version from the executable, otherwise
	// the version will be "unknown"
	// execute with no args; at least container-networking plugins respond with