Skip to content

Commit

Permalink
Add EACRuntime Controller cmd (fluid-cloudnative#2359)
Browse files Browse the repository at this point in the history
* eac cmd

Signed-off-by: mayingchun <mayingchun321@gmail.com>

* ut

Signed-off-by: mayingchun <mayingchun321@gmail.com>

* fix

Signed-off-by: mayingchun <mayingchun321@gmail.com>

* more ut

Signed-off-by: mayingchun <mayingchun321@gmail.com>

Signed-off-by: mayingchun <mayingchun321@gmail.com>
  • Loading branch information
myccccccc authored Nov 26, 2022
1 parent 44c0eaf commit 9465d67
Show file tree
Hide file tree
Showing 17 changed files with 1,329 additions and 27 deletions.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ JINDORUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/jindoruntime-controller
GOOSEFSRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/goosefsruntime-controller
JUICEFSRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/juicefsruntime-controller
THINRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/thinruntime-controller
EACRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/eacruntime-controller
CSI_IMG ?= ${IMG_REPO}/fluid-csi
LOADER_IMG ?= ${IMG_REPO}/fluid-dataloader
INIT_USERS_IMG ?= ${IMG_REPO}/init-users
Expand Down Expand Up @@ -52,6 +53,7 @@ BINARY_BUILD += alluxioruntime-controller-build
BINARY_BUILD += jindoruntime-controller-build
BINARY_BUILD += juicefsruntime-controller-build
BINARY_BUILD += thinruntime-controller-build
BINARY_BUILD += eacruntime-controller-build
BINARY_BUILD += csi-build
BINARY_BUILD += webhook-build

Expand Down Expand Up @@ -137,6 +139,9 @@ juicefsruntime-controller-build: generate gen-openapi fmt vet
thinruntime-controller-build: generate gen-openapi fmt vet
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=${GO_MODULE} go build ${GC_FLAGS} -a -o bin/thinruntime-controller -ldflags '-s -w ${LDFLAGS}' cmd/thin/main.go

eacruntime-controller-build: generate gen-openapi fmt vet
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=${GO_MODULE} go build ${GC_FLAGS} -a -o bin/eacruntime-controller -ldflags '${LDFLAGS}' cmd/eac/main.go

webhook-build: generate fmt vet
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 @@ -209,6 +214,9 @@ docker-build-juicefsruntime-controller: generate gen-openapi fmt vet juicefsrunt
docker-build-thinruntime-controller: generate gen-openapi fmt vet thinruntime-controller-build
docker build --no-cache --build-arg TARGETARCH=${ARCH} . -f docker/Dockerfile.thinruntime -t ${THINRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-build-eacruntime-controller: generate gen-openapi fmt vet
docker build --no-cache --build-arg TARGETARCH=${ARCH} . -f docker/Dockerfile.eacruntime -t ${EACRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

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

Expand Down Expand Up @@ -246,6 +254,9 @@ docker-push-juicefsruntime-controller: docker-build-juicefsruntime-controller
docker-push-thinruntime-controller: docker-build-thinruntime-controller
docker push ${THINRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-push-eacruntime-controller: docker-build-eacruntime-controller
docker push ${EACRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

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

Expand Down Expand Up @@ -283,6 +294,9 @@ docker-buildx-push-juicefsruntime-controller: generate gen-openapi fmt vet juice
docker-buildx-push-thinruntime-controller: generate gen-openapi fmt vet thinruntime-controller-build
docker buildx build --push --platform linux/amd64,linux/arm64 --no-cache . -f docker/Dockerfile.thinruntime -t ${THINRUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

docker-buildx-push-eacruntime-controller: generate gen-openapi fmt vet
docker buildx build --push --platform linux/amd64,linux/arm64 --no-cache . -f docker/Dockerfile.eacruntime -t ${EACRUNTIME_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
136 changes: 136 additions & 0 deletions cmd/eac/app/eac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2022 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/pkg/ddc/base/portallocator"
"github.com/fluid-cloudnative/fluid/pkg/ddc/eac"
"k8s.io/apimachinery/pkg/util/net"
"os"

"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/spf13/cobra"
zapOpt "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/fluid-cloudnative/fluid"
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
eacctl "github.com/fluid-cloudnative/fluid/pkg/controllers/v1alpha1/eac"
"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
)

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

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

var startCmd = &cobra.Command{
Use: "start",
Short: "start eacruntime-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", "16000-17999", "Set available port range for EAC")
}

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)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionNamespace: leaderElectionNamespace,
LeaderElectionID: "eac.data.fluid.io",
Port: 9443,
})
if err != nil {
setupLog.Error(err, "unable to start eacruntime manager")
os.Exit(1)
}

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

if err = (eacctl.NewRuntimeReconciler(mgr.GetClient(),
ctrl.Log.WithName("eacctl").WithName("EACRuntime"),
mgr.GetScheme(),
mgr.GetEventRecorderFor("EACRuntime"),
)).SetupWithManager(mgr, controllerOptions, eventDriven); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EACRuntime")
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())

portallocator.SetupRuntimePortAllocator(mgr.GetClient(), pr, eac.GetReservedPorts)

setupLog.Info("starting eacruntime-controller")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem eacruntime-controller")
os.Exit(1)
}
}
29 changes: 29 additions & 0 deletions cmd/eac/app/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2022 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 NewEACControllerCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "eacruntime-controller",
Short: "Controller for eacruntime",
}
cmd.AddCommand(startCmd)
cmd.AddCommand(versionCmd)
return cmd
}
38 changes: 38 additions & 0 deletions cmd/eac/app/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2022 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)
},
}
31 changes: 31 additions & 0 deletions cmd/eac/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2022 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"
"github.com/fluid-cloudnative/fluid/cmd/eac/app"
"os"
)

func main() {
cmd := app.NewEACControllerCommand()
if err := cmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s", err.Error())
os.Exit(1)
}
}
35 changes: 35 additions & 0 deletions docker/Dockerfile.eacruntime
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Build the eacruntime-controller manager binary
FROM golang:1.17.8 as builder

WORKDIR /go/src/github.com/fluid-cloudnative/fluid
COPY . .

RUN make eacruntime-controller-build && \
cp bin/eacruntime-controller /go/bin/eacruntime-controller

# Debug
RUN go install github.com/go-delve/delve/cmd/dlv@v1.8.2

FROM alpine:3.10
RUN apk add --update curl tzdata iproute2 bash libc6-compat vim && \
rm -rf /var/cache/apk/* && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone

ARG TARGETARCH
RUN curl -o helm-v3.0.3-linux-${TARGETARCH}.tar.gz https://get.helm.sh/helm-v3.0.3-linux-${TARGETARCH}.tar.gz && \
tar -xvf helm-v3.0.3-linux-${TARGETARCH}.tar.gz && \
mv linux-${TARGETARCH}/helm /usr/local/bin/ddc-helm && \
chmod u+x /usr/local/bin/ddc-helm && \
rm -f helm-v3.0.3-linux-${TARGETARCH}.tar.gz

ENV K8S_VERSION v1.14.8
RUN curl -o /usr/local/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/linux/${TARGETARCH}/kubectl && chmod +x /usr/local/bin/kubectl

ADD charts/ /charts

COPY --from=builder /go/bin/eacruntime-controller /usr/local/bin/eacruntime-controller
COPY --from=builder /go/bin/dlv /usr/local/bin/dlv
RUN chmod -R u+x /usr/local/bin/

CMD ["eacruntime-controller", "start"]
24 changes: 24 additions & 0 deletions pkg/common/eac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2022 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 common

// Runtime for EAC
const (
EACRuntimeType = "eac"
EACRuntimeResourceFinalizerName = "eac-runtime-controller-finalizer"
EACRuntimeControllerName = "EACRuntimeController"
)
Loading

0 comments on commit 9465d67

Please sign in to comment.