generated from kubernetes/kubernetes-template-project
/
csi.go
167 lines (140 loc) · 4.93 KB
/
csi.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Copyright 2021 The Fluid Authors.
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
http://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 app
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"net/http/pprof"
"os"
"time"
"github.com/fluid-cloudnative/fluid"
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/csi"
"github.com/fluid-cloudnative/fluid/pkg/csi/config"
"github.com/fluid-cloudnative/fluid/pkg/utils"
utilfeature "github.com/fluid-cloudnative/fluid/pkg/utils/feature"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
)
var (
endpoint string
nodeID string
metricsAddr string
pprofAddr string
pruneFs []string
prunePath string
kubeletKubeConfigPath string
)
var scheme = runtime.NewScheme()
var startCmd = &cobra.Command{
Use: "start",
Short: "start fluid driver on node",
Run: func(cmd *cobra.Command, args []string) {
handle()
},
}
func init() {
// Register k8s-native resources and Fluid CRDs
_ = clientgoscheme.AddToScheme(scheme)
_ = datav1alpha1.AddToScheme(scheme)
if err := flag.Set("logtostderr", "true"); err != nil {
fmt.Printf("Failed to flag.set due to %v", err)
os.Exit(1)
}
startCmd.Flags().StringVarP(&nodeID, "nodeid", "", "", "node id")
if err := startCmd.MarkFlagRequired("nodeid"); err != nil {
ErrorAndExit(err)
}
startCmd.Flags().StringVarP(&endpoint, "endpoint", "", "", "CSI endpoint")
if err := startCmd.MarkFlagRequired("endpoint"); err != nil {
ErrorAndExit(err)
}
startCmd.Flags().StringSliceVarP(&pruneFs, "prune-fs", "", []string{"fuse.alluxio-fuse", "fuse.jindofs-fuse", "fuse.juicefs", "fuse.goosefs-fuse", "ossfs"}, "Prune fs to add in /etc/updatedb.conf, separated by comma")
startCmd.Flags().StringVarP(&prunePath, "prune-path", "", "/runtime-mnt", "Prune path to add in /etc/updatedb.conf")
startCmd.Flags().StringVarP(&metricsAddr, "metrics-addr", "", ":8080", "The address the metrics endpoint binds to.")
startCmd.Flags().StringVarP(&pprofAddr, "pprof-addr", "", "", "The address for pprof to use while exporting profiling results")
startCmd.Flags().StringVarP(&kubeletKubeConfigPath, "kubelet-kube-config", "", "/etc/kubernetes/kubelet.conf", "The file path to kubelet kube config")
utilfeature.DefaultMutableFeatureGate.AddFlag(startCmd.Flags())
startCmd.Flags().AddGoFlagSet(flag.CommandLine)
}
func ErrorAndExit(err error) {
fmt.Fprintf(os.Stderr, "%s", err.Error())
os.Exit(1)
}
func handle() {
// startReaper()
fluid.LogVersion()
if pprofAddr != "" {
newPprofServer(pprofAddr)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
})
if err != nil {
panic(fmt.Sprintf("csi: unable to create controller manager due to error %v", err))
}
runningContext := config.RunningContext{
Config: config.Config{
NodeId: nodeID,
Endpoint: endpoint,
PruneFs: pruneFs,
PrunePath: prunePath,
KubeletConfigPath: kubeletKubeConfigPath,
},
VolumeLocks: utils.NewVolumeLocks(),
}
if err = csi.SetupWithManager(mgr, runningContext); err != nil {
panic(fmt.Sprintf("unable to set up manager due to error %v", err))
}
ctx := ctrl.SetupSignalHandler()
if err = mgr.Start(ctx); err != nil {
panic(fmt.Sprintf("unable to start controller recover due to error %v", err))
}
}
func newPprofServer(pprofAddr string) {
glog.Infof("Enabling pprof with address %s", pprofAddr)
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
pprofServer := http.Server{
Addr: pprofAddr,
Handler: mux,
}
glog.Infof("Starting pprof HTTP server at %s", pprofServer.Addr)
go func() {
go func() {
ctx := context.Background()
<-ctx.Done()
ctx, cancelFunc := context.WithTimeout(context.Background(), 60*time.Minute)
defer cancelFunc()
if err := pprofServer.Shutdown(ctx); err != nil {
glog.Error(err, "Failed to shutdown debug HTTP server")
}
}()
if err := pprofServer.ListenAndServe(); !errors.Is(http.ErrServerClosed, err) {
glog.Error(err, "Failed to start debug HTTP server")
panic(err)
}
}()
}