Merge pull request #5182 from hashicorp/b-docker-fingerprint-log-once
Make driver logging less redundant
This commit is contained in:
commit
71aa7c2b37
|
@ -88,6 +88,11 @@ type Driver struct {
|
|||
|
||||
// gpuRuntime indicates nvidia-docker runtime availability
|
||||
gpuRuntime bool
|
||||
|
||||
// A tri-state boolean to know if the fingerprinting has happened and
|
||||
// whether it has been successful
|
||||
fingerprintSuccess *bool
|
||||
fingerprintLock sync.RWMutex
|
||||
}
|
||||
|
||||
// NewDockerDriver returns a docker implementation of a driver plugin
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/nomad/helper"
|
||||
"github.com/hashicorp/nomad/plugins/drivers"
|
||||
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
||||
)
|
||||
|
@ -16,6 +17,28 @@ func (d *Driver) Fingerprint(ctx context.Context) (<-chan *drivers.Fingerprint,
|
|||
return ch, nil
|
||||
}
|
||||
|
||||
// setFingerprintSuccess marks the driver as having fingerprinted successfully
|
||||
func (d *Driver) setFingerprintSuccess() {
|
||||
d.fingerprintLock.Lock()
|
||||
d.fingerprintSuccess = helper.BoolToPtr(true)
|
||||
d.fingerprintLock.Unlock()
|
||||
}
|
||||
|
||||
// setFingerprintFailure marks the driver as having failed fingerprinting
|
||||
func (d *Driver) setFingerprintFailure() {
|
||||
d.fingerprintLock.Lock()
|
||||
d.fingerprintSuccess = helper.BoolToPtr(false)
|
||||
d.fingerprintLock.Unlock()
|
||||
}
|
||||
|
||||
// fingerprintSuccessful returns true if the driver has
|
||||
// never fingerprinted or has successfully fingerprinted
|
||||
func (d *Driver) fingerprintSuccessful() bool {
|
||||
d.fingerprintLock.Lock()
|
||||
defer d.fingerprintLock.Unlock()
|
||||
return d.fingerprintSuccess == nil || *d.fingerprintSuccess
|
||||
}
|
||||
|
||||
func (d *Driver) handleFingerprint(ctx context.Context, ch chan *drivers.Fingerprint) {
|
||||
defer close(ch)
|
||||
ticker := time.NewTimer(0)
|
||||
|
@ -40,7 +63,10 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
}
|
||||
client, _, err := d.dockerClients()
|
||||
if err != nil {
|
||||
d.logger.Info("failed to initialize client", "error", err)
|
||||
if d.fingerprintSuccessful() {
|
||||
d.logger.Info("failed to initialize client", "error", err)
|
||||
}
|
||||
d.setFingerprintFailure()
|
||||
return &drivers.Fingerprint{
|
||||
Health: drivers.HealthStateUndetected,
|
||||
HealthDescription: "Failed to initialize docker client",
|
||||
|
@ -49,7 +75,10 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
|
||||
env, err := client.Version()
|
||||
if err != nil {
|
||||
d.logger.Debug("could not connect to docker daemon", "endpoint", client.Endpoint(), "error", err)
|
||||
if d.fingerprintSuccessful() {
|
||||
d.logger.Debug("could not connect to docker daemon", "endpoint", client.Endpoint(), "error", err)
|
||||
}
|
||||
d.setFingerprintFailure()
|
||||
return &drivers.Fingerprint{
|
||||
Health: drivers.HealthStateUnhealthy,
|
||||
HealthDescription: "Failed to connect to docker daemon",
|
||||
|
@ -84,7 +113,9 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
} else {
|
||||
// Docker 17.09.0-ce dropped the Gateway IP from the bridge network
|
||||
// See https://github.com/moby/moby/issues/32648
|
||||
d.logger.Debug("bridge_ip could not be discovered")
|
||||
if d.fingerprintSuccessful() {
|
||||
d.logger.Debug("bridge_ip could not be discovered")
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
@ -108,5 +139,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
strings.Join(runtimeNames, ","))
|
||||
}
|
||||
|
||||
d.setFingerprintSuccess()
|
||||
|
||||
return fp
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/hashicorp/nomad/client/fingerprint"
|
||||
"github.com/hashicorp/nomad/drivers/shared/eventer"
|
||||
"github.com/hashicorp/nomad/drivers/shared/executor"
|
||||
"github.com/hashicorp/nomad/helper"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
"github.com/hashicorp/nomad/plugins/drivers"
|
||||
"github.com/hashicorp/nomad/plugins/drivers/utils"
|
||||
|
@ -20,6 +21,7 @@ import (
|
|||
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
||||
"github.com/hashicorp/nomad/plugins/shared/loader"
|
||||
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -95,6 +97,11 @@ type Driver struct {
|
|||
|
||||
// logger will log to the Nomad agent
|
||||
logger hclog.Logger
|
||||
|
||||
// A tri-state boolean to know if the fingerprinting has happened and
|
||||
// whether it has been successful
|
||||
fingerprintSuccess *bool
|
||||
fingerprintLock sync.Mutex
|
||||
}
|
||||
|
||||
// TaskConfig is the driver configuration of a task within a job
|
||||
|
@ -126,6 +133,28 @@ func NewExecDriver(logger hclog.Logger) drivers.DriverPlugin {
|
|||
}
|
||||
}
|
||||
|
||||
// setFingerprintSuccess marks the driver as having fingerprinted successfully
|
||||
func (d *Driver) setFingerprintSuccess() {
|
||||
d.fingerprintLock.Lock()
|
||||
d.fingerprintSuccess = helper.BoolToPtr(true)
|
||||
d.fingerprintLock.Unlock()
|
||||
}
|
||||
|
||||
// setFingerprintFailure marks the driver as having failed fingerprinting
|
||||
func (d *Driver) setFingerprintFailure() {
|
||||
d.fingerprintLock.Lock()
|
||||
d.fingerprintSuccess = helper.BoolToPtr(false)
|
||||
d.fingerprintLock.Unlock()
|
||||
}
|
||||
|
||||
// fingerprintSuccessful returns true if the driver has
|
||||
// never fingerprinted or has successfully fingerprinted
|
||||
func (d *Driver) fingerprintSuccessful() bool {
|
||||
d.fingerprintLock.Lock()
|
||||
defer d.fingerprintLock.Unlock()
|
||||
return d.fingerprintSuccess == nil || *d.fingerprintSuccess
|
||||
}
|
||||
|
||||
func (d *Driver) PluginInfo() (*base.PluginInfoResponse, error) {
|
||||
return pluginInfo, nil
|
||||
}
|
||||
|
@ -177,6 +206,7 @@ func (d *Driver) handleFingerprint(ctx context.Context, ch chan<- *drivers.Finge
|
|||
|
||||
func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
||||
if runtime.GOOS != "linux" {
|
||||
d.setFingerprintFailure()
|
||||
return &drivers.Fingerprint{
|
||||
Health: drivers.HealthStateUndetected,
|
||||
HealthDescription: "exec driver unsupported on client OS",
|
||||
|
@ -192,6 +222,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
if !utils.IsUnixRoot() {
|
||||
fp.Health = drivers.HealthStateUndetected
|
||||
fp.HealthDescription = drivers.DriverRequiresRootMessage
|
||||
d.setFingerprintFailure()
|
||||
return fp
|
||||
}
|
||||
|
||||
|
@ -199,17 +230,22 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
if err != nil {
|
||||
fp.Health = drivers.HealthStateUnhealthy
|
||||
fp.HealthDescription = drivers.NoCgroupMountMessage
|
||||
d.logger.Warn(fp.HealthDescription, "error", err)
|
||||
if d.fingerprintSuccessful() {
|
||||
d.logger.Warn(fp.HealthDescription, "error", err)
|
||||
}
|
||||
d.setFingerprintFailure()
|
||||
return fp
|
||||
}
|
||||
|
||||
if mount == "" {
|
||||
fp.Health = drivers.HealthStateUnhealthy
|
||||
fp.HealthDescription = drivers.CgroupMountEmpty
|
||||
d.setFingerprintFailure()
|
||||
return fp
|
||||
}
|
||||
|
||||
fp.Attributes["driver.exec"] = pstructs.NewBoolAttribute(true)
|
||||
d.setFingerprintSuccess()
|
||||
return fp
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@ import (
|
|||
"github.com/hashicorp/nomad/client/taskenv"
|
||||
"github.com/hashicorp/nomad/drivers/shared/eventer"
|
||||
"github.com/hashicorp/nomad/drivers/shared/executor"
|
||||
"github.com/hashicorp/nomad/helper"
|
||||
"github.com/hashicorp/nomad/plugins/base"
|
||||
"github.com/hashicorp/nomad/plugins/drivers"
|
||||
"github.com/hashicorp/nomad/plugins/shared"
|
||||
|
@ -193,9 +194,10 @@ type Driver struct {
|
|||
// logger will log to the Nomad agent
|
||||
logger hclog.Logger
|
||||
|
||||
// hasFingerprinted is used to store whether we have fingerprinted before
|
||||
hasFingerprinted bool
|
||||
fingerprintLock sync.Mutex
|
||||
// A tri-state boolean to know if the fingerprinting has happened and
|
||||
// whether it has been successful
|
||||
fingerprintSuccess *bool
|
||||
fingerprintLock sync.Mutex
|
||||
}
|
||||
|
||||
func NewRktDriver(logger hclog.Logger) drivers.DriverPlugin {
|
||||
|
@ -264,25 +266,29 @@ func (d *Driver) handleFingerprint(ctx context.Context, ch chan *drivers.Fingerp
|
|||
}
|
||||
}
|
||||
|
||||
// setFingerprinted marks the driver as having fingerprinted once before
|
||||
func (d *Driver) setFingerprinted() {
|
||||
// setFingerprintSuccess marks the driver as having fingerprinted successfully
|
||||
func (d *Driver) setFingerprintSuccess() {
|
||||
d.fingerprintLock.Lock()
|
||||
d.hasFingerprinted = true
|
||||
d.fingerprintSuccess = helper.BoolToPtr(true)
|
||||
d.fingerprintLock.Unlock()
|
||||
}
|
||||
|
||||
// fingerprinted returns whether the driver has fingerprinted before
|
||||
func (d *Driver) fingerprinted() bool {
|
||||
// setFingerprintFailure marks the driver as having failed fingerprinting
|
||||
func (d *Driver) setFingerprintFailure() {
|
||||
d.fingerprintLock.Lock()
|
||||
d.fingerprintSuccess = helper.BoolToPtr(false)
|
||||
d.fingerprintLock.Unlock()
|
||||
}
|
||||
|
||||
// fingerprintSuccessful returns true if the driver has
|
||||
// never fingerprinted or has successfully fingerprinted
|
||||
func (d *Driver) fingerprintSuccessful() bool {
|
||||
d.fingerprintLock.Lock()
|
||||
defer d.fingerprintLock.Unlock()
|
||||
return d.hasFingerprinted
|
||||
return d.fingerprintSuccess == nil || *d.fingerprintSuccess
|
||||
}
|
||||
|
||||
func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
||||
defer func() {
|
||||
d.setFingerprinted()
|
||||
}()
|
||||
|
||||
fingerprint := &drivers.Fingerprint{
|
||||
Attributes: map[string]*pstructs.Attribute{},
|
||||
Health: drivers.HealthStateHealthy,
|
||||
|
@ -291,9 +297,10 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
|
||||
// Only enable if we are root
|
||||
if syscall.Geteuid() != 0 {
|
||||
if !d.fingerprinted() {
|
||||
if d.fingerprintSuccessful() {
|
||||
d.logger.Debug("must run as root user, disabling")
|
||||
}
|
||||
d.setFingerprintFailure()
|
||||
fingerprint.Health = drivers.HealthStateUndetected
|
||||
fingerprint.HealthDescription = drivers.DriverRequiresRootMessage
|
||||
return fingerprint
|
||||
|
@ -303,6 +310,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
if err != nil {
|
||||
fingerprint.Health = drivers.HealthStateUndetected
|
||||
fingerprint.HealthDescription = fmt.Sprintf("Failed to execute %s version: %v", rktCmd, err)
|
||||
d.setFingerprintFailure()
|
||||
return fingerprint
|
||||
}
|
||||
out := strings.TrimSpace(string(outBytes))
|
||||
|
@ -312,6 +320,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
if len(rktMatches) != 2 || len(appcMatches) != 2 {
|
||||
fingerprint.Health = drivers.HealthStateUndetected
|
||||
fingerprint.HealthDescription = "Unable to parse rkt version string"
|
||||
d.setFingerprintFailure()
|
||||
return fingerprint
|
||||
}
|
||||
|
||||
|
@ -321,10 +330,11 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
// Do not allow ancient rkt versions
|
||||
fingerprint.Health = drivers.HealthStateUndetected
|
||||
fingerprint.HealthDescription = fmt.Sprintf("Unsuported rkt version %s", currentVersion)
|
||||
if !d.fingerprinted() {
|
||||
if d.fingerprintSuccessful() {
|
||||
d.logger.Warn("unsupported rkt version please upgrade to >= "+minVersion.String(),
|
||||
"rkt_version", currentVersion)
|
||||
}
|
||||
d.setFingerprintFailure()
|
||||
return fingerprint
|
||||
}
|
||||
|
||||
|
@ -334,7 +344,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|||
if d.config.VolumesEnabled {
|
||||
fingerprint.Attributes["driver.rkt.volumes.enabled"] = pstructs.NewBoolAttribute(true)
|
||||
}
|
||||
|
||||
d.setFingerprintSuccess()
|
||||
return fingerprint
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue