generated from kubernetes/kubernetes-template-project
/
juicefs.go
190 lines (163 loc) · 7.93 KB
/
juicefs.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
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 (
"os"
"time"
"github.com/spf13/cobra"
zapOpt "go.uber.org/zap"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/net"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/fluid-cloudnative/fluid/pkg/controllers"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base/portallocator"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid"
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
juicefsctl "github.com/fluid-cloudnative/fluid/pkg/controllers/v1alpha1/juicefs"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/ddc/juicefs"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
// Use compiler to check if the struct implements all the interface
_ base.Implement = (*juicefs.JuiceFSEngine)(nil)
eventDriven bool
metricsAddr string
enableLeaderElection bool
leaderElectionNamespace string
development bool
portRange string
maxConcurrentReconciles int
pprofAddr string
portAllocatePolicy string
kubeClientQPS float32
kubeClientBurst int
)
// configuration for controllers' rate limiter
var (
controllerWorkqueueDefaultSyncBackoffStr string
controllerWorkqueueMaxSyncBackoffStr string
controllerWorkqueueQPS int
controllerWorkqueueBurst int
)
var startCmd = &cobra.Command{
Use: "start",
Short: "start juicefsruntime-controller in Kubernetes",
Run: func(cmd *cobra.Command, args []string) {
handle()
},
}
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = datav1alpha1.AddToScheme(scheme)
startCmd.Flags().StringVarP(&metricsAddr, "metrics-addr", "", ":8080", "The address the metric endpoint binds to.")
startCmd.Flags().BoolVarP(&enableLeaderElection, "enable-leader-election", "", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
startCmd.Flags().StringVarP(&leaderElectionNamespace, "leader-election-namespace", "", "fluid-system", "The namespace in which the leader election resource will be created.")
startCmd.Flags().StringVarP(&pprofAddr, "pprof-addr", "", "", "The address for pprof to use while exporting profiling results")
startCmd.Flags().BoolVarP(&development, "development", "", true, "Enable development mode for fluid controller.")
startCmd.Flags().BoolVar(&eventDriven, "event-driven", true, "The reconciler's loop strategy. if it's false, it indicates period driven.")
startCmd.Flags().StringVar(&portRange, "runtime-node-port-range", "14000-15999", "Set available port range for JuiceFS")
startCmd.Flags().StringVar(&portAllocatePolicy, "port-allocate-policy", "random", "Set port allocating policy, available choice is bitmap or random(default random).")
startCmd.Flags().IntVar(&maxConcurrentReconciles, "runtime-workers", 3, "Set max concurrent workers for JuiceFSRuntime controller")
startCmd.Flags().Float32VarP(&kubeClientQPS, "kube-api-qps", "", 20, "QPS to use while talking with kubernetes apiserver.") // 20 is the default qps in controller-runtime
startCmd.Flags().IntVarP(&kubeClientBurst, "kube-api-burst", "", 30, "Burst to use while talking with kubernetes apiserver.") // 30 is the default burst in controller-runtime
startCmd.Flags().StringVar(&controllerWorkqueueDefaultSyncBackoffStr, "workqueue-default-sync-backoff", "5ms", "base backoff period for failed reconcilation in controller's workqueue")
startCmd.Flags().StringVar(&controllerWorkqueueMaxSyncBackoffStr, "workqueue-max-sync-backoff", "1000s", "max backoff period for failed reconcilation in controller's workqueue")
startCmd.Flags().IntVar(&controllerWorkqueueQPS, "workqueue-qps", 10, "qps limit value for controller's workqueue")
startCmd.Flags().IntVar(&controllerWorkqueueBurst, "workqueue-burst", 100, "burst limit value for controller's workqueue")
}
func handle() {
fluid.LogVersion()
ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = development
}, func(o *zap.Options) {
o.ZapOpts = append(o.ZapOpts, zapOpt.AddCaller())
}, func(o *zap.Options) {
if !development {
encCfg := zapOpt.NewProductionEncoderConfig()
encCfg.EncodeLevel = zapcore.CapitalLevelEncoder
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
o.Encoder = zapcore.NewConsoleEncoder(encCfg)
}
}))
utils.NewPprofServer(setupLog, pprofAddr, development)
NewControllerClient := func(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (client.Client, error) {
return controllers.NewFluidControllerClient(cache, config, options,
append(uncachedObjects, &rbacv1.RoleBinding{}, &rbacv1.Role{}, &corev1.ServiceAccount{})...,
)
}
mgr, err := ctrl.NewManager(controllers.GetConfigOrDieWithQPSAndBurst(kubeClientQPS, kubeClientBurst), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionNamespace: leaderElectionNamespace,
LeaderElectionID: "juicefs.data.fluid.io",
Port: 9443,
NewCache: juicefsctl.NewCache(scheme),
NewClient: NewControllerClient,
})
if err != nil {
setupLog.Error(err, "unable to start juicefsruntime manager")
os.Exit(1)
}
defaultSyncBackoff, err := time.ParseDuration(controllerWorkqueueDefaultSyncBackoffStr)
if err != nil {
setupLog.Error(err, "workqueue-default-sync-backoff is not a valid duration, please use string like \"100ms\", \"5s\", \"3m\", ...")
os.Exit(1)
}
maxSyncBackoff, err := time.ParseDuration(controllerWorkqueueMaxSyncBackoffStr)
if err != nil {
setupLog.Error(err, "workqueue-max-sync-backoff is not a valid duration, please use string like \"100ms\", \"5s\", \"3m\", ...)")
os.Exit(1)
}
controllerOptions := controller.Options{
MaxConcurrentReconciles: maxConcurrentReconciles,
RateLimiter: controllers.NewFluidControllerRateLimiter(defaultSyncBackoff, maxSyncBackoff, controllerWorkqueueQPS, controllerWorkqueueBurst),
}
if err = (juicefsctl.NewRuntimeReconciler(mgr.GetClient(),
ctrl.Log.WithName("juicefsctl").WithName("JuiceFSRuntime"),
mgr.GetScheme(),
mgr.GetEventRecorderFor("JuiceFSRuntime"),
)).SetupWithManager(mgr, controllerOptions, eventDriven); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "JuiceFSRuntime")
os.Exit(1)
}
pr, err := net.ParsePortRange(portRange)
if err != nil {
setupLog.Error(err, "can't parse port range. Port range must be like <min>-<max>")
os.Exit(1)
}
setupLog.Info("port range parsed", "port range", pr.String())
err = portallocator.SetupRuntimePortAllocator(mgr.GetClient(), pr, portAllocatePolicy, juicefs.GetReservedPorts)
if err != nil {
setupLog.Error(err, "failed to setup runtime port allocator")
os.Exit(1)
}
setupLog.Info("Set up runtime port allocator", "policy", portAllocatePolicy)
setupLog.Info("starting juicefsruntime-controller")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem juicefsruntime-controller")
os.Exit(1)
}
}