From 093647a4416b17660a269ff60325803254500c26 Mon Sep 17 00:00:00 2001 From: honpey Date: Thu, 26 May 2022 10:20:47 +0800 Subject: [PATCH] Introduce main for runtime-manager (#171) Signed-off-by: honpey --- .github/workflows/ci.yaml | 2 +- Makefile | 6 ++- cmd/runtime-manager/main.go | 55 +++++++++++++++++++++ cmd/runtime-manager/options/options.go | 37 ++++++++++++++ pkg/runtime-manager/server/cri/criserver.go | 21 +++++--- pkg/runtime-manager/server/utils/utils.go | 25 ---------- 6 files changed, 112 insertions(+), 34 deletions(-) create mode 100644 cmd/runtime-manager/main.go create mode 100644 cmd/runtime-manager/options/options.go delete mode 100644 pkg/runtime-manager/server/utils/utils.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 84ced4e00..99dce7d20 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -88,7 +88,7 @@ jobs: make vet && git add pkg cmd && git diff --cached --exit-code || (echo 'Please run "make vet" to verify govet' && exit 1); - name: Run Go build - run: make build-koordlet build-koord-manager build-koord-scheduler + run: make build-koordlet build-koord-manager build-koord-scheduler build-runtime-manager - name: Run Go test run: make test - name: Upload coverage to Codecov diff --git a/Makefile b/Makefile index 9e14b0adf..552e03460 100644 --- a/Makefile +++ b/Makefile @@ -90,7 +90,7 @@ test: manifests generate fmt vet envtest ## Run tests. ##@ Build .PHONY: build -build: generate fmt vet lint build-koordlet build-koord-manager build-koord-scheduler +build: generate fmt vet lint build-koordlet build-koord-manager build-koord-scheduler build-runtime-manager .PHONY: build-koordlet build-koordlet: ## Build koordlet binary. @@ -104,6 +104,10 @@ build-koord-manager: ## Build koord-manager binary. build-koord-scheduler: ## Build koord-scheduler binary. go build -o bin/koord-scheduler cmd/koord-scheduler/main.go +.PHONY: build-runtime-manager +build-runtime-manager: ## Build runtime-manager binary. + go build -o bin/runtime-manager cmd/runtime-manager/main.go + .PHONY: docker-build docker-build: test docker-build-koordlet docker-build-koord-manager docker-build-koord-scheduler diff --git a/cmd/runtime-manager/main.go b/cmd/runtime-manager/main.go new file mode 100644 index 000000000..f1e07e068 --- /dev/null +++ b/cmd/runtime-manager/main.go @@ -0,0 +1,55 @@ +/* +Copyright 2022 The Koordinator 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 ( + "flag" + + "github.com/spf13/pflag" + genericapiserver "k8s.io/apiserver/pkg/server" + "k8s.io/klog/v2" + + "github.com/koordinator-sh/koordinator/cmd/runtime-manager/options" + "github.com/koordinator-sh/koordinator/pkg/runtime-manager/server/cri" +) + +func main() { + flag.StringVar(&options.RuntimeManagerEndpoint, "runtime-manager-endpoint", options.DefaultRuntimeManagerEndpoint, + "runtime-manager service endpoint.") + flag.StringVar(&options.RemoteRuntimeServiceEndpoint, "remote-runtime-service-endpoint", options.DefaultContainerdRuntimeServiceEndpoint, + "backend runtime service endpoint.") + flag.StringVar(&options.RemoteImageServiceEndpoint, "remote-image-service-endpoint", options.DefaultContainerdImageServiceEndpoint, + "backend image service endpoint.") + flag.StringVar(&options.BackendRuntimeMode, "backend-runtime-mode", options.DefaultBackendRuntimeMode, + "backend container engine(Containerd|Docker).") + + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) + pflag.Parse() + + switch options.BackendRuntimeMode { + case options.BackendRuntimeModeContainerd: + server := cri.NewRuntimeManagerCriServer() + go server.Run() + case options.BackendRuntimeModeDocker: + default: + klog.Fatalf("unknown runtime engine backend %v", options.BackendRuntimeMode) + } + + stopCh := genericapiserver.SetupSignalHandler() + <-stopCh + klog.Info("RuntimeManager shutting down") +} diff --git a/cmd/runtime-manager/options/options.go b/cmd/runtime-manager/options/options.go new file mode 100644 index 000000000..0b6f5eb78 --- /dev/null +++ b/cmd/runtime-manager/options/options.go @@ -0,0 +1,37 @@ +/* +Copyright 2022 The Koordinator 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 options + +const ( + DefaultRuntimeManagerEndpoint = "/var/run/runtime-manager/runtimemanager.sock" + + DefaultContainerdRuntimeServiceEndpoint = "/var/run/containerd/containerd.sock" + DefaultContainerdImageServiceEndpoint = "/var/run/containerd/containerd.sock" + + BackendRuntimeModeContainerd = "Containerd" + BackendRuntimeModeDocker = "Docker" + DefaultBackendRuntimeMode = BackendRuntimeModeContainerd +) + +var ( + RuntimeManagerEndpoint string + RemoteRuntimeServiceEndpoint string + RemoteImageServiceEndpoint string + + // BackendRuntimeMode default to 'containerd' + BackendRuntimeMode string +) diff --git a/pkg/runtime-manager/server/cri/criserver.go b/pkg/runtime-manager/server/cri/criserver.go index 2ae2ba13b..c187b4959 100644 --- a/pkg/runtime-manager/server/cri/criserver.go +++ b/pkg/runtime-manager/server/cri/criserver.go @@ -20,17 +20,18 @@ import ( "context" "net" "os" + "path/filepath" "time" "google.golang.org/grpc" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" "k8s.io/klog/v2" + "github.com/koordinator-sh/koordinator/cmd/runtime-manager/options" "github.com/koordinator-sh/koordinator/pkg/runtime-manager/config" "github.com/koordinator-sh/koordinator/pkg/runtime-manager/dispatcher" resource_executor "github.com/koordinator-sh/koordinator/pkg/runtime-manager/resource-executor" cri_resource_executor "github.com/koordinator-sh/koordinator/pkg/runtime-manager/resource-executor/cri" - "github.com/koordinator-sh/koordinator/pkg/runtime-manager/server/utils" ) const ( @@ -43,9 +44,9 @@ type RuntimeManagerCriServer struct { backendImageServiceClient runtimeapi.ImageServiceClient } -func NewRuntimeManagerCriServer(dispatcher *dispatcher.RuntimeHookDispatcher) *RuntimeManagerCriServer { +func NewRuntimeManagerCriServer() *RuntimeManagerCriServer { criInterceptor := &RuntimeManagerCriServer{ - hookDispatcher: dispatcher, + hookDispatcher: dispatcher.NewRuntimeDispatcher(), } return criInterceptor } @@ -55,18 +56,24 @@ func (c *RuntimeManagerCriServer) Name() string { } func (c *RuntimeManagerCriServer) Run() error { - if err := c.initBackendServer(utils.DefaultRuntimeServiceSocketPath, utils.DefaultImageServiceSocketPath); err != nil { + if err := c.initBackendServer(options.RemoteRuntimeServiceEndpoint, options.RemoteImageServiceEndpoint); err != nil { return err } c.failOver() + klog.Infof("do failOver done") - if err := os.Remove(utils.DefaultRuntimeManagerSocketPath); err != nil && os.IsNotExist(err) { - klog.Errorf("fail to unlink %v: %v", utils.DefaultRuntimeManagerSocketPath, err) + if err := os.Remove(options.RuntimeManagerEndpoint); err != nil && !os.IsNotExist(err) { + klog.Errorf("fail to unlink %v: %v", options.RuntimeManagerEndpoint, err) + return err + } + + if err := os.MkdirAll(filepath.Dir(options.RuntimeManagerEndpoint), 0755); err != nil { + klog.Errorf("fail to mkdir %v: %v", filepath.Base(options.RuntimeManagerEndpoint), err) return err } - lis, err := net.Listen("unix", utils.DefaultRuntimeManagerSocketPath) + lis, err := net.Listen("unix", options.RuntimeManagerEndpoint) if err != nil { klog.Errorf("fail to create the lis %v", err) return err diff --git a/pkg/runtime-manager/server/utils/utils.go b/pkg/runtime-manager/server/utils/utils.go deleted file mode 100644 index 3e55d8a68..000000000 --- a/pkg/runtime-manager/server/utils/utils.go +++ /dev/null @@ -1,25 +0,0 @@ -/* -Copyright 2022 The Koordinator 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 utils - -const ( - DefaultRuntimeManagerSocketPath = "/var/run/runtime-manager/runtimemanager.sock" - DefaultRuntimeServiceSocketPath = "/run/containerd/containerd.sock" - DefaultImageServiceSocketPath = "/run/containerd/containerd.sock" - - UnKnownCgroupParent = "UnKnownCgroupParent" -)