-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (82 loc) · 2.46 KB
/
main.go
File metadata and controls
103 lines (82 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"k8s.io/klog/v2"
)
const (
mountPathsLocation = "/volumes/"
)
func main() {
klog.InitFlags(nil)
flag.Parse()
klog.Info("*** CUSTOM BUILD: Metadata Prefetch - Manual Test Version ***")
// Create cancellable context to pass into exec.
ctx, cancel := context.WithCancel(context.Background())
// Handle SIGTERM signal.
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
go func() {
<-sigs
klog.Info("Caught SIGTERM signal: Terminating...")
cancel()
os.Exit(0) // Exit gracefully
}()
// Start the "ls" command in the background.
// All our volumes are mounted under the /volumes/ directory.
cmd := exec.CommandContext(ctx, "ls", "-R", mountPathsLocation)
cmd.Stdout = nil // Connects file descriptor to the null device (os.DevNull).
// TODO(hime): We should research stratergies to parallelize ls execution and speed up cache population.
err := cmd.Start()
if err == nil {
mountPaths, err := getDirectoryNames(mountPathsLocation)
if err == nil {
klog.Infof("Running ls on mountPath(s): %s", strings.Join(mountPaths, ", "))
} else {
klog.Warningf("failed to get mountPaths: %v", err)
}
err = cmd.Wait()
if err != nil {
klog.Errorf("Error while executing ls command: %v", err)
} else {
klog.Info("Metadata prefetch complete")
}
} else {
klog.Errorf("Error starting ls command: %v.", err)
}
klog.Info("Going to sleep...")
// Keep the process running.
select {}
}
// getDirectoryNames returns a list of strings representing the names of
// the directories within the provided path.
func getDirectoryNames(dirPath string) ([]string, error) {
directories := []string{}
items, err := os.ReadDir(dirPath)
if err != nil {
return directories, err
}
for _, item := range items {
if item.IsDir() {
directories = append(directories, item.Name())
}
}
return directories, nil
}