Skip to content

Commit

Permalink
Implement CacheFSRuntime engine and controller
Browse files Browse the repository at this point in the history
Fixes: fluid-cloudnative#3674
Signed-off-by: Eryu Guan <eguan@linux.alibaba.com>
  • Loading branch information
eryugey committed Jan 15, 2024
1 parent 51d2a63 commit 650581c
Show file tree
Hide file tree
Showing 82 changed files with 18,792 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ JUICEFSRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/juicefsruntime-controller
THINRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/thinruntime-controller
EFCRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/efcruntime-controller
VINEYARDRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/vineyardruntime-controller
CACHEFSRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/cachefsruntime-controller
CSI_IMG ?= ${IMG_REPO}/fluid-csi
LOADER_IMG ?= ${IMG_REPO}/fluid-dataloader
INIT_USERS_IMG ?= ${IMG_REPO}/init-users
Expand Down Expand Up @@ -56,6 +57,7 @@ BINARY_BUILD += juicefsruntime-controller-build
BINARY_BUILD += thinruntime-controller-build
BINARY_BUILD += efcruntime-controller-build
BINARY_BUILD += vineyardruntime-controller-build
BINARY_BUILD += cachefsruntime-controller-build
BINARY_BUILD += csi-build
BINARY_BUILD += webhook-build

Expand All @@ -71,6 +73,7 @@ DOCKER_BUILD += docker-build-juicefsruntime-controller
DOCKER_BUILD += docker-build-thinruntime-controller
DOCKER_BUILD += docker-build-efcruntime-controller
DOCKER_BUILD += docker-build-vineyardruntime-controller
DOCKER_BUILD += docker-build-cachefsruntime-controller
DOCKER_BUILD += docker-build-init-users
DOCKER_BUILD += docker-build-crd-upgrader

Expand All @@ -86,6 +89,7 @@ DOCKER_PUSH += docker-push-juicefsruntime-controller
DOCKER_PUSH += docker-push-thinruntime-controller
DOCKER_PUSH += docker-push-efcruntime-controller
DOCKER_PUSH += docker-push-vineyardruntime-controller
DOCKER_PUSH += docker-push-cachefsruntime-controller
# Not need to push init-users image by default
# DOCKER_PUSH += docker-push-init-users
DOCKER_PUSH += docker-push-crd-upgrader
Expand All @@ -101,6 +105,7 @@ DOCKER_BUILDX_PUSH += docker-buildx-push-webhook
DOCKER_BUILDX_PUSH += docker-buildx-push-juicefsruntime-controller
DOCKER_BUILDX_PUSH += docker-buildx-push-thinruntime-controller
DOCKER_BUILDX_PUSH += docker-buildx-push-efcruntime-controller
DOCKER_BUILDX_PUSH += docker-buildx-push-cachefsruntime-controller
DOCKER_BUILDX_PUSH += docker-buildx-push-init-users
DOCKER_BUILDX_PUSH += docker-buildx-push-crd-upgrader

Expand Down Expand Up @@ -184,6 +189,9 @@ vineyardruntime-controller-build:
efcruntime-controller-build:
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=${GO_MODULE} go build ${GC_FLAGS} -a -o bin/efcruntime-controller -ldflags '${LDFLAGS}' cmd/efc/main.go

cachefsruntime-controller-build:
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=${GO_MODULE} go build ${GC_FLAGS} -a -o bin/cachefsruntime-controller -ldflags '-s -w ${LDFLAGS}' cmd/cachefs/main.go

webhook-build:
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=${GO_MODULE} go build ${GC_FLAGS} -a -o bin/fluid-webhook -ldflags '${LDFLAGS}' cmd/webhook/main.go

Expand Down Expand Up @@ -218,6 +226,9 @@ docker-build-efcruntime-controller:
docker-build-vineyardruntime-controller:
docker build --no-cache --build-arg TARGETARCH=${ARCH} . -f docker/Dockerfile.vineyardruntime -t ${VINEYARDRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-build-cachefsruntime-controller:
docker build --no-cache --build-arg TARGETARCH=${ARCH} . -f docker/Dockerfile.cachefsruntime -t ${CACHEFSRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-build-csi:
docker build --no-cache . -f docker/Dockerfile.csi -t ${CSI_IMG}:${GIT_VERSION}

Expand Down Expand Up @@ -258,6 +269,9 @@ docker-push-efcruntime-controller: docker-build-efcruntime-controller
docker-push-vineyardruntime-controller: docker-build-vineyardruntime-controller
docker push ${VINEYARDRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-push-cachefsruntime-controller: docker-build-cachefsruntime-controller
docker push ${CACHEFSRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-push-csi: docker-build-csi
docker push ${CSI_IMG}:${GIT_VERSION}

Expand Down Expand Up @@ -301,6 +315,9 @@ docker-buildx-push-efcruntime-controller:
docker-buildx-push-vineyardruntime-controller:
docker buildx build --push --platform linux/amd64,linux/arm64 --no-cache . -f docker/Dockerfile.vineyardruntime -t ${VINAYARDRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-buildx-push-cachefsruntime-controller:
docker buildx build --push --platform linux/amd64,linux/arm64 --no-cache . -f docker/Dockerfile.cachefsruntime -t ${CACHEFSRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-buildx-push-csi: generate fmt vet
docker buildx build --push --platform linux/amd64,linux/arm64 --no-cache . -f docker/Dockerfile.csi -t ${CSI_IMG}:${GIT_VERSION}

Expand Down
159 changes: 159 additions & 0 deletions cmd/cachefs/app/cachefs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
Copyright 2024 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"

"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"
cachefsctl "github.com/fluid-cloudnative/fluid/pkg/controllers/v1alpha1/cachefs"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/ddc/cachefs"
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
// Use compiler to check if the struct implements all the interface
_ base.Implement = (*cachefs.CacheFSEngine)(nil)

eventDriven bool
metricsAddr string
enableLeaderElection bool
leaderElectionNamespace string
development bool
portRange string
maxConcurrentReconciles int
pprofAddr string
portAllocatePolicy string
)

var startCmd = &cobra.Command{
Use: "start",
Short: "start cachefsruntime-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 CacheFS")
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 CacheFSRuntime controller")
}

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(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionNamespace: leaderElectionNamespace,
LeaderElectionID: "cachefs.data.fluid.io",
Port: 9443,
NewCache: cachefsctl.NewCache(scheme),
NewClient: NewControllerClient,
})
if err != nil {
setupLog.Error(err, "unable to start cachefsruntime manager")
os.Exit(1)
}

controllerOptions := controller.Options{
MaxConcurrentReconciles: maxConcurrentReconciles,
}

if err = (cachefsctl.NewRuntimeReconciler(mgr.GetClient(),
ctrl.Log.WithName("cachefsctl").WithName("CacheFSRuntime"),
mgr.GetScheme(),
mgr.GetEventRecorderFor("CacheFSRuntime"),
)).SetupWithManager(mgr, controllerOptions, eventDriven); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CacheFSRuntime")
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, cachefs.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 cachefsruntime-controller")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem cachefsruntime-controller")
os.Exit(1)
}
}
29 changes: 29 additions & 0 deletions cmd/cachefs/app/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2024 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 "github.com/spf13/cobra"

func NewCacheFSControllerCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cachefsruntime-controller",
Short: "Controller for cachefsruntime",
}
cmd.AddCommand(startCmd)
cmd.AddCommand(versionCmd)
return cmd
}
38 changes: 38 additions & 0 deletions cmd/cachefs/app/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 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 (
"github.com/fluid-cloudnative/fluid"
"github.com/spf13/cobra"
)

var (
short bool
)

func init() {
versionCmd.Flags().BoolVar(&short, "short", false, "print just the short version info")
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "print version information",
Run: func(cmd *cobra.Command, args []string) {
fluid.PrintVersion(short)
},
}
32 changes: 32 additions & 0 deletions cmd/cachefs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2024 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 main

import (
"fmt"
"os"

"github.com/fluid-cloudnative/fluid/cmd/cachefs/app"
)

func main() {
cmd := app.NewCacheFSControllerCommand()
if err := cmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s", err.Error())
os.Exit(1)
}
}
20 changes: 20 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ rules:
- get
- patch
- update
- apiGroups:
- data.fluid.io
resources:
- cachefsruntimes
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- data.fluid.io
resources:
- cachefsruntimes/status
verbs:
- get
- patch
- update
- apiGroups:
- data.fluid.io
resources:
Expand Down
Loading

0 comments on commit 650581c

Please sign in to comment.