-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fdbkubernetesmonitor: Add a check for new binaries in the shared binary directory and report them back in an annotation #12230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johscheuer
wants to merge
1
commit into
apple:main
Choose a base branch
from
johscheuer:fdbkubernetesmonitor-check-for-shared-binaries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -33,6 +33,7 @@ import ( | |
"os/exec" | ||
"os/signal" | ||
"path" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
@@ -107,6 +108,15 @@ type monitor struct { | |
|
||
// metrics represents the prometheus monitor metrics. | ||
metrics *metrics | ||
|
||
// runVersionCommand when set to false the monitor will not try to run the fdbserver --version command to ensure | ||
// that the binary is executable. | ||
runVersionCommand bool | ||
|
||
// availableBinaries represents all available binaries in the sharedBinaryDir. Most of the time this map will be | ||
// empty but during version incompatible upgrades, this information will be used to signal the operator that | ||
// the new fdbserver binary is present and executable in the sharedBinaryDir. | ||
availableBinaries map[string]struct{} | ||
} | ||
|
||
type httpConfig struct { | ||
|
@@ -129,6 +139,8 @@ func startMonitor(ctx context.Context, logger logr.Logger, configFile string, cu | |
processCount: processCount, | ||
processIDs: make([]int, processCount+1), | ||
currentContainerVersion: currentContainerVersion, | ||
runVersionCommand: true, | ||
availableBinaries: map[string]struct{}{}, | ||
} | ||
|
||
go func() { mon.watchPodTimestamps() }() | ||
|
@@ -253,7 +265,8 @@ func (monitor *monitor) readConfiguration() (*api.ProcessConfiguration, []byte) | |
configuration.BinaryPath = path.Join(sharedBinaryDir, configuration.Version.String(), "fdbserver") | ||
} | ||
|
||
err = checkOwnerExecutable(configuration.BinaryPath) | ||
// TODO (johscheuer): Should we run this check every time? | ||
err = checkOwnerExecutable(configuration.BinaryPath, monitor.runVersionCommand) | ||
if err != nil { | ||
monitor.logger.Error(err, "Error with binary path for latest configuration", "configuration", configuration, "binaryPath", configuration.BinaryPath) | ||
return nil, nil | ||
|
@@ -295,14 +308,26 @@ func (monitor *monitor) loadConfiguration() { | |
|
||
// checkOwnerExecutable validates that a path is a file that exists and is | ||
// executable by its owner. | ||
func checkOwnerExecutable(path string) error { | ||
func checkOwnerExecutable(path string, runVersionCommand bool) error { | ||
binaryStat, err := os.Stat(path) | ||
if err != nil { | ||
return err | ||
} | ||
if binaryStat.Mode()&0o100 == 0 { | ||
return fmt.Errorf("binary is not executable") | ||
} | ||
|
||
if !runVersionCommand { | ||
return nil | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
cmd := exec.CommandContext(ctx, path, "--version") | ||
if err = cmd.Run(); err != nil { | ||
return fmt.Errorf("could not run the version command with binary: %s, error: %w", path, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -523,7 +548,7 @@ func (monitor *monitor) watchConfiguration(watcher *fsnotify.Watcher) { | |
return | ||
} | ||
|
||
monitor.logger.Info("Detected event on monitor conf file or cluster file", "event", event) | ||
monitor.logger.Info("Detected event on monitor conf file, cluster file or shared binaries", "event", event) | ||
if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create { | ||
monitor.handleFileChange(event.Name) | ||
} else if event.Op&fsnotify.Remove == fsnotify.Remove { | ||
|
@@ -542,6 +567,59 @@ func (monitor *monitor) watchConfiguration(watcher *fsnotify.Watcher) { | |
} | ||
} | ||
|
||
// getBinariesFromSharedBinaryDir returns all fdbserver binaries that are found in the shared binary directory. | ||
func (monitor *monitor) waitForSharedBinariesAndUpdateAnnotation(dir string) error { | ||
var fdbserverBinaries []string | ||
|
||
startTime := time.Now() | ||
for len(fdbserverBinaries) == 0 { | ||
// If after 5 minutes the new fdbserver binary was not copied, something is probably wrong. | ||
if time.Since(startTime) > 5*time.Minute { | ||
return fmt.Errorf("could not find fdbserver binary in shared binary dir after more than 2 minutes") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/2/5/ |
||
} | ||
|
||
monitor.logger.Info("Checking shared binary dir for new fdbserver binary", "sharedBinaryDir", sharedBinaryDir, "fdbserverBinaries", fdbserverBinaries) | ||
err := filepath.Walk(dir, | ||
func(currentPath string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
if path.Base(currentPath) != "fdbserver" { | ||
return nil | ||
} | ||
|
||
monitor.logger.Info("found new fdbserver binary in shared binary dir", "sharedBinaryDir", sharedBinaryDir, "currentPath", currentPath) | ||
fdbserverBinaries = append(fdbserverBinaries, currentPath) | ||
|
||
return nil | ||
}) | ||
|
||
if err != nil { | ||
monitor.logger.Error(err, "Error getting binaries from sharedBinaryDir", "sharedBinaryDir", sharedBinaryDir) | ||
} | ||
|
||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
for _, binary := range fdbserverBinaries { | ||
err := checkOwnerExecutable(binary, monitor.runVersionCommand) | ||
if err != nil { | ||
monitor.logger.Error(err, "Error with binary in shared binary directory", "sharedBinaryDir", sharedBinaryDir, "binary", binary) | ||
continue | ||
} | ||
|
||
monitor.availableBinaries[binary] = struct{}{} | ||
monitor.logger.Info("Adding new binary to available binaries", "sharedBinaryDir", sharedBinaryDir, "binary", binary) | ||
} | ||
|
||
return monitor.podClient.updateAvailableBinariesAnnotation(monitor.availableBinaries) | ||
} | ||
|
||
// handleFileChange will perform the required action based on the changed/modified file. | ||
func (monitor *monitor) handleFileChange(changedFile string) { | ||
if changedFile == fdbClusterFilePath { | ||
|
@@ -552,6 +630,18 @@ func (monitor *monitor) handleFileChange(changedFile string) { | |
return | ||
} | ||
|
||
// If the changed file is in the shared binary path, check if the binary can be executed. If the binary is | ||
// executable then we can add it to the available binaries. | ||
if strings.HasPrefix(changedFile, sharedBinaryDir) { | ||
go func(dir string) { | ||
err := monitor.waitForSharedBinariesAndUpdateAnnotation(sharedBinaryDir) | ||
if err != nil { | ||
monitor.logger.Error(err, "Error getting binaries from sharedBinaryDir", "sharedBinaryDir", sharedBinaryDir, "changedFile", changedFile) | ||
return | ||
} | ||
}(sharedBinaryDir) | ||
} | ||
|
||
monitor.loadConfiguration() | ||
} | ||
|
||
|
@@ -615,6 +705,13 @@ func (monitor *monitor) run() { | |
panic(err) | ||
} | ||
|
||
// Create a watcher for the sharedBinaryDir, this watcher will update the available binaries during an upgrade. | ||
monitor.logger.Info("adding watch for shared binary path", "path", path.Dir(sharedBinaryDir)) | ||
err = watcher.Add(path.Dir(sharedBinaryDir)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
defer func(watcher *fsnotify.Watcher) { | ||
err := watcher.Close() | ||
if err != nil { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does a replace I presume?