-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
119 lines (97 loc) · 3.55 KB
/
main.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2022 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"
"fmt"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"
"time"
sidecarmounter "github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/sidecar_mounter"
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/webhook"
"k8s.io/klog/v2"
)
var (
gcsfusePath = flag.String("gcsfuse-path", "/gcsfuse", "gcsfuse path")
volumeBasePath = flag.String("volume-base-path", webhook.SidecarContainerTmpVolumeMountPath+"/.volumes", "volume base path")
_ = flag.Int("grace-period", 0, "grace period for gcsfuse termination. This flag has been deprecated, has no effect and will be removed in the future.")
// This is set at compile time.
version = "unknown"
)
func main() {
klog.InitFlags(nil)
flag.Parse()
klog.Infof("Running Google Cloud Storage FUSE CSI driver sidecar mounter version %v", version)
socketPathPattern := *volumeBasePath + "/*/socket"
socketPaths, err := filepath.Glob(socketPathPattern)
if err != nil {
klog.Fatalf("failed to look up socket paths: %v", err)
}
mounter := sidecarmounter.New(*gcsfusePath)
ctx, cancel := context.WithCancel(context.Background())
for _, sp := range socketPaths {
// sleep 1.5 seconds before launch the next gcsfuse to avoid
// 1. different gcsfuse logs mixed together.
// 2. memory usage peak.
time.Sleep(1500 * time.Millisecond)
mc := sidecarmounter.NewMountConfig(sp)
if mc != nil {
if err := mounter.Mount(ctx, mc); err != nil {
mc.ErrWriter.WriteMsg(fmt.Sprintf("failed to mount bucket %q for volume %q: %v\n", mc.BucketName, mc.VolumeName, err))
}
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM)
klog.Info("waiting for SIGTERM signal...")
// Function that monitors the exit file used in regular sidecar containers.
monitorExitFile := func() {
ticker := time.NewTicker(5 * time.Second)
for {
<-ticker.C
// If exit file is detected, send a syscall.SIGTERM signal to the signal channel.
if _, err := os.Stat(*volumeBasePath + "/exit"); err == nil {
klog.Info("all the other containers terminated in the Pod, exiting the sidecar container.")
// After the Kubernetes native sidecar container feature is adopted,
// we should propagate the SIGTERM signal outside of this goroutine.
cancel()
if err := os.Remove(*volumeBasePath + "/exit"); err != nil {
klog.Error("failed to remove the exit file from emptyDir.")
}
c <- syscall.SIGTERM
return
}
}
}
envVar := os.Getenv("NATIVE_SIDECAR")
isNativeSidecar, err := strconv.ParseBool(envVar)
if envVar != "" && err != nil {
klog.Warningf(`env variable "%s" could not be converted to boolean`, envVar)
}
// When the pod contains a regular container, we monitor for the exit file.
if !isNativeSidecar {
go monitorExitFile()
}
<-c // blocking the process
klog.Info("received SIGTERM signal, waiting for all the gcsfuse processes exit...")
if isNativeSidecar {
cancel()
}
mounter.WaitGroup.Wait()
klog.Info("exiting sidecar mounter...")
}