mirror of
https://github.com/SynologyOpenSource/synology-csi.git
synced 2026-08-19 11:36:33 +00:00
The tool executor is capable of running given tools in chroot either by automatically locating the tools in path using the `env` command or directly if the path is known. This removes the need for the shell script to wrap commands and reduces number of needed external binaries on the node.
132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
/*
|
|
Copyright 2021 Synology Inc.
|
|
|
|
Copyright 2017 The Kubernetes 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 driver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
|
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
|
|
log "github.com/sirupsen/logrus"
|
|
"google.golang.org/grpc"
|
|
clientset "k8s.io/client-go/kubernetes"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/mount-utils"
|
|
"k8s.io/utils/exec"
|
|
)
|
|
|
|
func ParseEndpoint(ep string) (string, string, error) {
|
|
if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
|
|
s := strings.SplitN(ep, "://", 2)
|
|
if s[1] != "" {
|
|
return s[0], s[1], nil
|
|
}
|
|
}
|
|
return "", "", fmt.Errorf("Invalid endpoint: %v", ep)
|
|
}
|
|
|
|
func NewControllerServer(d *Driver) *controllerServer {
|
|
return &controllerServer{
|
|
Driver: d,
|
|
dsmService: d.DsmService,
|
|
}
|
|
}
|
|
|
|
func getK8sClient() clientset.Interface {
|
|
config, err := rest.InClusterConfig()
|
|
if err != nil {
|
|
log.Fatalf("Failed to read in-cluster config: %v", err)
|
|
}
|
|
|
|
client, err := clientset.NewForConfig(config)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create Kubernetes client: %v", err)
|
|
}
|
|
return client
|
|
}
|
|
|
|
func NewNodeServer(d *Driver) *nodeServer {
|
|
return &nodeServer{
|
|
Driver: d,
|
|
dsmService: d.DsmService,
|
|
Mounter: &mount.SafeFormatAndMount{
|
|
Interface: mount.New(""),
|
|
Exec: exec.New(),
|
|
},
|
|
Initiator: &initiatorDriver{
|
|
chapUser: "",
|
|
chapPassword: "",
|
|
tools: d.tools,
|
|
},
|
|
Client: getK8sClient(),
|
|
tools: d.tools,
|
|
}
|
|
}
|
|
|
|
func NewIdentityServer(d *Driver) *identityServer {
|
|
return &identityServer{
|
|
Driver: d,
|
|
}
|
|
}
|
|
|
|
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode {
|
|
return &csi.VolumeCapability_AccessMode{Mode: mode}
|
|
}
|
|
|
|
func NewControllerServiceCapability(cap csi.ControllerServiceCapability_RPC_Type) *csi.ControllerServiceCapability {
|
|
return &csi.ControllerServiceCapability{
|
|
Type: &csi.ControllerServiceCapability_Rpc{
|
|
Rpc: &csi.ControllerServiceCapability_RPC{
|
|
Type: cap,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewNodeServiceCapability(cap csi.NodeServiceCapability_RPC_Type) *csi.NodeServiceCapability {
|
|
return &csi.NodeServiceCapability{
|
|
Type: &csi.NodeServiceCapability_Rpc{
|
|
Rpc: &csi.NodeServiceCapability_RPC{
|
|
Type: cap,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func RunControllerandNodePublishServer(endpoint string, d *Driver, cs csi.ControllerServer, ns csi.NodeServer) {
|
|
ids := NewIdentityServer(d)
|
|
|
|
s := NewNonBlockingGRPCServer()
|
|
s.Start(endpoint, ids, cs, ns)
|
|
}
|
|
|
|
func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
log.Infof("GRPC call: %s", info.FullMethod)
|
|
log.Infof("GRPC request: %s", protosanitizer.StripSecrets(req))
|
|
resp, err := handler(ctx, req)
|
|
if err != nil {
|
|
log.Errorf("GRPC error: %v", err)
|
|
} else {
|
|
log.Infof("GRPC response: %s", protosanitizer.StripSecrets(resp))
|
|
}
|
|
return resp, err
|
|
}
|