Skip to content

Commit

Permalink
add fluid app controller (fluid-cloudnative#1481)
Browse files Browse the repository at this point in the history
* add fluid app controller

Signed-off-by: zwwhdls <zww@hdls.me>

* fix ci-lint

Signed-off-by: zwwhdls <zww@hdls.me>

* add watch func in fluid app controller

Signed-off-by: zwwhdls <zww@hdls.me>

* fix chart lint

Signed-off-by: zwwhdls <zww@hdls.me>

* reduce rbac

Signed-off-by: zwwhdls <zww@hdls.me>

* use util instead

Signed-off-by: zwwhdls <zww@hdls.me>

* update rbac

Signed-off-by: zwwhdls <zww@hdls.me>

* update rbac

Signed-off-by: zwwhdls <zww@hdls.me>
  • Loading branch information
zwwhdls authored Mar 13, 2022
1 parent 1b6f78b commit a4b9edd
Show file tree
Hide file tree
Showing 18 changed files with 1,328 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CRD_OPTIONS ?= "crd"
# IMG_REPO ?= registry.aliyuncs.com/fluid
IMG_REPO ?= fluidcloudnative
DATASET_CONTROLLER_IMG ?= ${IMG_REPO}/dataset-controller
fluid-app_CONTROLLER_IMG ?= ${IMG_REPO}/fluid-app-controller
ALLUXIORUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/alluxioruntime-controller
JINDORUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/jindoruntime-controller
GOOSEFSRUNTIME_CONTROLLER_IMG ?= ${IMG_REPO}/goosefsruntime-controller
Expand Down Expand Up @@ -35,6 +36,7 @@ PACKAGE=github.com/fluid-cloudnative/fluid

# Build binaries
BINARY_BUILD := dataset-controller-build
BINARY_BUILD += fluid-app-controller-build
BINARY_BUILD += alluxioruntime-controller-build
BINARY_BUILD += jindoruntime-controller-build
BINARY_BUILD += juicefsruntime-controller-build
Expand All @@ -43,6 +45,7 @@ BINARY_BUILD += webhook-build

# Build docker images
DOCKER_BUILD := docker-build-dataset-controller
DOCKER_BUILD += docker-build-fluid-app-controller
DOCKER_BUILD += docker-build-alluxioruntime-controller
DOCKER_BUILD += docker-build-jindoruntime-controller
DOCKER_BUILD += docker-build-goosefsruntime-controller
Expand All @@ -53,6 +56,7 @@ DOCKER_BUILD += docker-build-init-users

# Push docker images
DOCKER_PUSH := docker-push-dataset-controller
DOCKER_PUSH += docker-push-fluid-app-controller
DOCKER_PUSH += docker-push-alluxioruntime-controller
DOCKER_PUSH += docker-push-jindoruntime-controller
DOCKER_PUSH += docker-push-csi
Expand Down Expand Up @@ -104,6 +108,9 @@ juicefsruntime-controller-build: generate fmt vet
webhook-build: generate fmt vet
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=${GO_MODULE} go build -gcflags="-N -l" -a -o bin/fluid-webhook -ldflags '${LDFLAGS}' cmd/webhook/main.go

fluid-app-controller-build: generate fmt vet
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=${GO_MODULE} go build -gcflags="-N -l" -a -o bin/fluid-app-controller -ldflags '${LDFLAGS}' cmd/fluidapp/main.go

# Debug against the configured Kubernetes cluster in ~/.kube/config, add debug
debug: generate fmt vet manifests
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=${GO_MODULE} dlv debug --headless --listen ":12345" --log --api-version=2 cmd/controller/main.go
Expand Down Expand Up @@ -152,6 +159,9 @@ update-api-doc:
docker-build-dataset-controller: generate fmt vet
docker build --no-cache . -f docker/Dockerfile.dataset -t ${DATASET_CONTROLLER_IMG}:${GIT_VERSION}

docker-build-fluid-app-controller: generate fmt vet
docker build --no-cache . -f docker/Dockerfile.fluid-app -t ${fluid-app_CONTROLLER_IMG}:${GIT_VERSION}

docker-build-alluxioruntime-controller: generate fmt vet
docker build --no-cache . -f docker/Dockerfile.alluxioruntime -t ${ALLUXIORUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

Expand Down Expand Up @@ -180,6 +190,9 @@ docker-build-webhook:
docker-push-dataset-controller: docker-build-dataset-controller
docker push ${DATASET_CONTROLLER_IMG}:${GIT_VERSION}

docker-push-fluid-app-controller: docker-build-fluid-app-controller
docker push ${fluid-app_CONTROLLER_IMG}:${GIT_VERSION}

docker-push-alluxioruntime-controller: docker-build-alluxioruntime-controller
docker push ${ALLUXIORUNTIME_CONTROLLER_IMG}:${GIT_VERSION}

Expand Down
57 changes: 57 additions & 0 deletions charts/fluid/fluid/templates/controller/fluidapp_controller.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{{ if .Values.fluidapp.enabled -}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: fluidapp-controller
namespace: fluid-system
labels:
control-plane: fluidapp-controller
spec:
selector:
matchLabels:
control-plane: fluidapp-controller
replicas: 1
template:
metadata:
labels:
control-plane: fluidapp-controller
spec:
serviceAccountName: fluidapp-controller
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: type
operator: NotIn
values:
- virtual-kubelet
tolerations:
- operator: Exists
#hostNetwork: true
containers:
- image: "{{ .Values.fluidapp.controller.image }}"
name: manager
command: ["fluidapp-controller", "start"]
args:
- --development=false
- --pprof-addr=:6060
env:
{{- if .Values.workdir }}
- name: FLUID_WORKDIR
value: {{ .Values.workdir | quote }}
{{- end }}
ports:
- containerPort: 8080
name: metrics
protocol: TCP
resources:
limits:
cpu: 100m
memory: 1536Mi
requests:
cpu: 100m
memory: 200Mi
terminationGracePeriodSeconds: 10
{{- end }}
44 changes: 44 additions & 0 deletions charts/fluid/fluid/templates/role/fluidapp/rbac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: fluidapp-controller
rules:
- apiGroups:
- ""
resources:
- events
verbs:
- get
- watch
- create
- apiGroups:
- ""
resources:
- pods
- pods/exec
verbs:
- get
- list
- patch
- update
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: fluidapp-clusterrolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: fluidapp-controller
subjects:
- kind: ServiceAccount
name: fluidapp-controller
namespace: fluid-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: fluidapp-controller
namespace: fluid-system
4 changes: 4 additions & 0 deletions charts/fluid/fluid/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@ webhook:
image: fluidcloudnative/fluid-webhook:v0.8.0-da5d3a2
replicas: 1

fluidapp:
enabled: false
controller:
image: fluidcloudnative/fluidapp-controller:v0.7.0-394621b
111 changes: 111 additions & 0 deletions cmd/fluidapp/app/fluidapp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
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/fluid-cloudnative/fluid/pkg/controllers/v1alpha1/fluidapp"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/spf13/cobra"
zapOpt "go.uber.org/zap"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")

metricsAddr string
enableLeaderElection bool
development bool
maxConcurrentReconciles int
pprofAddr string
)

var fluidAppCmd = &cobra.Command{
Use: "start",
Short: "start fluidapp-controller in Kubernetes",
Run: func(cmd *cobra.Command, args []string) {
handle()
},
}

func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = corev1.AddToScheme(scheme)
fluidAppCmd.Flags().StringVarP(&metricsAddr, "metrics-addr", "", ":8080", "The address the metric endpoint binds to.")
fluidAppCmd.Flags().BoolVarP(&enableLeaderElection, "enable-leader-election", "", false, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
fluidAppCmd.Flags().BoolVarP(&development, "development", "", true, "Enable development mode for fluid controller.")
fluidAppCmd.Flags().StringVarP(&pprofAddr, "pprof-addr", "", "", "The address for pprof to use while exporting profiling results")
fluidAppCmd.Flags().IntVar(&maxConcurrentReconciles, "runtime-workers", 3, "Set max concurrent workers for Fluid App 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)

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

controllerOptions := controller.Options{
MaxConcurrentReconciles: maxConcurrentReconciles,
}
if err = (fluidapp.NewFluidAppReconciler(
mgr.GetClient(),
ctrl.Log.WithName("appctrl"),
mgr.GetEventRecorderFor("FluidApp"),
)).SetupWithManager(mgr, controllerOptions); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "FluidApp")
os.Exit(1)
}

setupLog.Info("starting fluidapp-controller")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running fluidapp-controller")
os.Exit(1)
}
}
30 changes: 30 additions & 0 deletions cmd/fluidapp/app/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
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 NewFluidAppCommand() *cobra.Command {
command := &cobra.Command{
Use: "fluidapp-controller",
Short: "controller for fluidapp",
}

command.AddCommand(versionCmd, fluidAppCmd)

return command
}
38 changes: 38 additions & 0 deletions cmd/fluidapp/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
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "print version information",
Run: func(cmd *cobra.Command, args []string) {
fluid.PrintVersion(short)
},
}

func init() {
versionCmd.Flags().BoolVar(&short, "short", false, "print just the short version info")
}
31 changes: 31 additions & 0 deletions cmd/fluidapp/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/fluidapp/app"
"os"
)

func main() {
command := app.NewFluidAppCommand()
if err := command.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s", err.Error())
os.Exit(1)
}
}
Loading

0 comments on commit a4b9edd

Please sign in to comment.