-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fingerprint: add fingerprinting for CNI plugins presense and version
This PR adds a fingerprinter to set the attributes "cni.plugins" -> "true" or "false" if <cni_path> contains CNI plugins "cni.plugins.version" -> version of the CNI plugins if present
- Loading branch information
Showing
5 changed files
with
154 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
fingerprint: Add fingerprinting for CNI containernetworking plugins | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package fingerprint | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
const ( | ||
defaultCNIPath = "/opt/cni/bin" | ||
) | ||
|
||
// CNIPluginsFingerprint creates a fingerprint of the CNI plugins present on the | ||
// CNI plugin path specified for the Nomad client. | ||
type CNIPluginsFingerprint struct { | ||
StaticFingerprinter | ||
logger hclog.Logger | ||
lister func(string) ([]os.DirEntry, error) | ||
} | ||
|
||
func NewCNIPluginsFingerprint(logger hclog.Logger) Fingerprint { | ||
return &CNIPluginsFingerprint{ | ||
logger: logger.Named("cni_plugins"), | ||
lister: os.ReadDir, | ||
} | ||
} | ||
|
||
func (f *CNIPluginsFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error { | ||
cniPath := req.Config.CNIPath | ||
if cniPath == "" { | ||
cniPath = defaultCNIPath | ||
} | ||
|
||
entries, err := f.lister(cniPath) | ||
switch { | ||
case err != nil: | ||
f.logger.Debug("failed to read CNI plugins directory", "cni_path", cniPath, "error", err) | ||
resp.Detected = true | ||
resp.AddAttribute("cni.plugins", "false") | ||
return nil | ||
case len(entries) == 0: | ||
f.logger.Debug("no CNI plugins found", "cni_path", cniPath) | ||
resp.Detected = true | ||
resp.AddAttribute("cni.plugins", "false") | ||
return nil | ||
} | ||
|
||
bridgePath := filepath.Join(cniPath, "bridge") | ||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
defer cancel() | ||
cmd := exec.CommandContext(ctx, bridgePath, "--version") | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
f.logger.Debug("failed to detect CNI plugins version", "error", err) | ||
resp.AddAttribute("cni.plugins", "false") | ||
return nil | ||
} | ||
tokens := strings.Fields(string(output)) | ||
version := tokens[len(tokens)-1] | ||
f.logger.Debug("detected CNI plugins", "version", version) | ||
resp.AddAttribute("cni.plugins", "true") | ||
resp.AddAttribute("cni.plugins.version", version) | ||
resp.Detected = true | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package fingerprint | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/ci" | ||
"github.com/hashicorp/nomad/client/config" | ||
"github.com/hashicorp/nomad/helper/testlog" | ||
"github.com/shoenig/test/must" | ||
) | ||
|
||
func TestCNIPluginsFingerprint_Fingerprint_default(t *testing.T) { | ||
ci.Parallel(t) | ||
|
||
f := NewCNIPluginsFingerprint(testlog.HCLogger(t)) | ||
request := &FingerprintRequest{ | ||
Config: new(config.Config), | ||
} | ||
response := new(FingerprintResponse) | ||
|
||
err := f.Fingerprint(request, response) | ||
must.NoError(t, err) | ||
must.True(t, response.Detected) | ||
must.Eq(t, "true", response.Attributes["cni.plugins"]) | ||
must.StrHasPrefix(t, "v1", response.Attributes["cni.plugins.version"]) | ||
} | ||
|
||
func TestCNIPluginsFingerprint_Fingerprint_absent(t *testing.T) { | ||
ci.Parallel(t) | ||
|
||
f := NewCNIPluginsFingerprint(testlog.HCLogger(t)) | ||
request := &FingerprintRequest{ | ||
Config: &config.Config{ | ||
CNIPath: "/does/not/exist", | ||
}, | ||
} | ||
response := new(FingerprintResponse) | ||
|
||
err := f.Fingerprint(request, response) | ||
must.False(t, response.Detected) | ||
must.NoError(t, err) | ||
must.Eq(t, "false", response.Attributes["cni.plugins"]) | ||
} | ||
|
||
func TestCNIPluginsFingerprint_Fingerprint_empty(t *testing.T) { | ||
ci.Parallel(t) | ||
|
||
lister := func(string) ([]os.DirEntry, error) { | ||
return nil, nil | ||
} | ||
|
||
f := NewCNIPluginsFingerprint(testlog.HCLogger(t)) | ||
f.(*CNIPluginsFingerprint).lister = lister | ||
request := &FingerprintRequest{ | ||
Config: new(config.Config), | ||
} | ||
response := new(FingerprintResponse) | ||
|
||
err := f.Fingerprint(request, response) | ||
must.NoError(t, err) | ||
must.True(t, response.Detected) | ||
must.Eq(t, "false", response.Attributes["cni.plugins"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters