2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-20 23:26:32 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-07-09 07:22:42 +00:00
|
|
|
"sort"
|
2015-11-05 21:46:02 +00:00
|
|
|
"time"
|
2015-08-20 23:26:32 +00:00
|
|
|
|
2018-09-16 00:48:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2015-08-20 23:26:32 +00:00
|
|
|
)
|
|
|
|
|
2015-11-05 21:46:02 +00:00
|
|
|
// EmptyDuration is to be used by fingerprinters that are not periodic.
|
2016-07-09 07:22:42 +00:00
|
|
|
const (
|
|
|
|
EmptyDuration = time.Duration(0)
|
2017-07-21 05:34:24 +00:00
|
|
|
|
|
|
|
// TightenNetworkTimeoutsConfig is a config key that can be used during
|
|
|
|
// tests to tighten the timeouts for fingerprinters that make network calls.
|
|
|
|
TightenNetworkTimeoutsConfig = "test.tighten_network_timeouts"
|
2016-07-09 07:22:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-07-10 05:31:14 +00:00
|
|
|
|
2016-07-09 07:22:42 +00:00
|
|
|
// Initialize the list of available fingerprinters per platform. Each
|
|
|
|
// platform defines its own list of available fingerprinters.
|
2016-11-07 20:21:50 +00:00
|
|
|
initPlatformFingerprints(hostFingerprinters)
|
2016-07-09 07:22:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 20:21:50 +00:00
|
|
|
var (
|
|
|
|
// hostFingerprinters contains the host fingerprints which are available for a
|
|
|
|
// given platform.
|
|
|
|
hostFingerprinters = map[string]Factory{
|
2022-12-05 20:22:47 +00:00
|
|
|
"arch": NewArchFingerprint,
|
|
|
|
"consul": NewConsulFingerprint,
|
|
|
|
"cni": NewCNIFingerprint, // networks
|
|
|
|
"cpu": NewCPUFingerprint,
|
|
|
|
"host": NewHostFingerprint,
|
2022-12-07 22:02:25 +00:00
|
|
|
"landlock": NewLandlockFingerprint,
|
2022-12-05 20:22:47 +00:00
|
|
|
"memory": NewMemoryFingerprint,
|
|
|
|
"network": NewNetworkFingerprint,
|
|
|
|
"nomad": NewNomadFingerprint,
|
|
|
|
"plugins_cni": NewPluginsCNIFingerprint,
|
|
|
|
"signal": NewSignalFingerprint,
|
|
|
|
"storage": NewStorageFingerprint,
|
|
|
|
"vault": NewVaultFingerprint,
|
2016-11-07 20:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// envFingerprinters contains the fingerprints that are environment specific.
|
|
|
|
// This should run after the host fingerprinters as they may override specific
|
|
|
|
// node resources with more detailed information.
|
|
|
|
envFingerprinters = map[string]Factory{
|
2022-02-04 05:01:59 +00:00
|
|
|
"env_aws": NewEnvAWSFingerprint,
|
|
|
|
"env_gce": NewEnvGCEFingerprint,
|
|
|
|
"env_azure": NewEnvAzureFingerprint,
|
|
|
|
"env_digitalocean": NewEnvDigitalOceanFingerprint,
|
2016-11-07 20:21:50 +00:00
|
|
|
}
|
|
|
|
)
|
2015-11-05 21:46:02 +00:00
|
|
|
|
2016-01-29 13:34:29 +00:00
|
|
|
// BuiltinFingerprints is a slice containing the key names of all registered
|
2016-11-07 20:21:50 +00:00
|
|
|
// fingerprints available. The order of this slice should be preserved when
|
|
|
|
// fingerprinting.
|
2016-07-09 06:37:14 +00:00
|
|
|
func BuiltinFingerprints() []string {
|
2016-11-07 20:21:50 +00:00
|
|
|
fingerprints := make([]string, 0, len(hostFingerprinters))
|
|
|
|
for k := range hostFingerprinters {
|
2016-07-09 06:37:14 +00:00
|
|
|
fingerprints = append(fingerprints, k)
|
|
|
|
}
|
2016-07-09 07:22:42 +00:00
|
|
|
sort.Strings(fingerprints)
|
2016-11-07 20:21:50 +00:00
|
|
|
for k := range envFingerprinters {
|
|
|
|
fingerprints = append(fingerprints, k)
|
|
|
|
}
|
2016-07-09 06:37:14 +00:00
|
|
|
return fingerprints
|
2015-09-22 19:23:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 23:26:32 +00:00
|
|
|
// NewFingerprint is used to instantiate and return a new fingerprint
|
|
|
|
// given the name and a logger
|
2018-09-16 00:48:59 +00:00
|
|
|
func NewFingerprint(name string, logger log.Logger) (Fingerprint, error) {
|
2015-08-20 23:26:32 +00:00
|
|
|
// Lookup the factory function
|
2016-11-07 20:21:50 +00:00
|
|
|
factory, ok := hostFingerprinters[name]
|
2015-08-20 23:26:32 +00:00
|
|
|
if !ok {
|
2016-11-07 20:21:50 +00:00
|
|
|
factory, ok = envFingerprinters[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown fingerprint '%s'", name)
|
|
|
|
}
|
2015-08-20 23:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate the fingerprint
|
|
|
|
f := factory(logger)
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Factory is used to instantiate a new Fingerprint
|
2018-09-16 00:48:59 +00:00
|
|
|
type Factory func(log.Logger) Fingerprint
|
2015-08-20 23:26:32 +00:00
|
|
|
|
2018-01-25 16:30:15 +00:00
|
|
|
// HealthCheck is used for doing periodic health checks. On a given time
|
|
|
|
// interfal, a health check will be called by the fingerprint manager of the
|
|
|
|
// node.
|
|
|
|
type HealthCheck interface {
|
|
|
|
// Check is used to update properties of the node on the status of the health
|
|
|
|
// check
|
|
|
|
HealthCheck(*cstructs.HealthCheckRequest, *cstructs.HealthCheckResponse) error
|
|
|
|
|
|
|
|
// GetHealthCheckInterval is a mechanism for the health checker to indicate that
|
|
|
|
// it should be run periodically. The return value is a boolean indicating
|
|
|
|
// whether it should be done periodically, and the time interval at which
|
|
|
|
// this check should happen.
|
|
|
|
GetHealthCheckInterval(*cstructs.HealthCheckIntervalRequest, *cstructs.HealthCheckIntervalResponse) error
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:26:32 +00:00
|
|
|
// Fingerprint is used for doing "fingerprinting" of the
|
|
|
|
// host to automatically determine attributes, resources,
|
|
|
|
// and metadata about it. Each of these is a heuristic, and
|
|
|
|
// many of them can be applied on a particular host.
|
|
|
|
type Fingerprint interface {
|
|
|
|
// Fingerprint is used to update properties of the Node,
|
2018-01-24 14:09:53 +00:00
|
|
|
// and returns a diff of updated node attributes and a potential error.
|
2018-12-01 16:10:39 +00:00
|
|
|
Fingerprint(*FingerprintRequest, *FingerprintResponse) error
|
2015-11-05 21:46:02 +00:00
|
|
|
|
|
|
|
// Periodic is a mechanism for the fingerprinter to indicate that it should
|
|
|
|
// be run periodically. The return value is a boolean indicating if it
|
|
|
|
// should be periodic, and if true, a duration.
|
|
|
|
Periodic() (bool, time.Duration)
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:09:01 +00:00
|
|
|
// ReloadableFingerprint can be implemented if the fingerprinter needs to be run during client reload.
|
|
|
|
// If implemented, the client will call Reload during client reload then immediately Fingerprint
|
|
|
|
type ReloadableFingerprint interface {
|
|
|
|
Fingerprint
|
|
|
|
Reload()
|
|
|
|
}
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// StaticFingerprinter can be embedded in a struct that has a Fingerprint method
|
2015-11-05 21:46:02 +00:00
|
|
|
// to make it non-periodic.
|
|
|
|
type StaticFingerprinter struct{}
|
|
|
|
|
|
|
|
func (s *StaticFingerprinter) Periodic() (bool, time.Duration) {
|
|
|
|
return false, EmptyDuration
|
2015-08-20 23:26:32 +00:00
|
|
|
}
|