diff --git a/Dockerfile b/Dockerfile index e3d2744e4..84a1e5520 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine LABEL maintainers="Kubernetes Authors" -LABEL description="CSI Driver registrar" +LABEL description="CSI Node driver registrar" -COPY ./bin/driver-registrar driver-registrar -ENTRYPOINT ["/driver-registrar"] +COPY ./bin/node-driver-registrar node-driver-registrar +ENTRYPOINT ["/node-driver-registrar"] diff --git a/Makefile b/Makefile index 29c5f079c..0cce52fd7 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -.PHONY: all driver-registrar clean test +.PHONY: all node-driver-registrar clean test REGISTRY_NAME=quay.io/k8scsi -IMAGE_NAME=driver-registrar +IMAGE_NAME=node-driver-registrar IMAGE_VERSION=canary IMAGE_TAG=$(REGISTRY_NAME)/$(IMAGE_NAME):$(IMAGE_VERSION) @@ -28,16 +28,16 @@ TESTARGS = endif -all: driver-registrar +all: node-driver-registrar -driver-registrar: +node-driver-registrar: mkdir -p bin - CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-X main.version=$(REV) -extldflags "-static"' -o ./bin/driver-registrar ./cmd/driver-registrar + CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-X main.version=$(REV) -extldflags "-static"' -o ./bin/node-driver-registrar ./cmd/node-driver-registrar clean: rm -rf bin -container: driver-registrar +container: node-driver-registrar docker build -t $(IMAGE_TAG) . push: container diff --git a/README.md b/README.md index 4d6930e65..f7916fb2b 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ -[![Build Status](https://travis-ci.org/kubernetes-csi/driver-registrar.svg?branch=master)](https://travis-ci.org/kubernetes-csi/driver-registrar) -# Driver Registrar +[![Build Status](https://travis-ci.org/kubernetes-csi/node-driver-registrar.svg?branch=master)](https://travis-ci.org/kubernetes-csi/node-driver-registrar) +# Node Driver Registrar A sidecar container that 1. Registers the containerized CSI driver with kubelet (in the future). -2. Adds the drivers custom `NodeId` (retrieved via `GetNodeID` call) to an annotation on the Kubernetes Node API Object. ## Community, discussion, contribution, and support diff --git a/cmd/driver-registrar/k8s_register.go b/cmd/driver-registrar/k8s_register.go deleted file mode 100644 index 3d1a470d7..000000000 --- a/cmd/driver-registrar/k8s_register.go +++ /dev/null @@ -1,152 +0,0 @@ -/* -Copyright 2018 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 main - -import ( - "os" - "os/signal" - "time" - - "github.com/golang/glog" - crdclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/client-go/rest" - "k8s.io/client-go/util/retry" - k8scsi "k8s.io/csi-api/pkg/apis/csi/v1alpha1" - k8scsiclient "k8s.io/csi-api/pkg/client/clientset/versioned" - k8scsicrd "k8s.io/csi-api/pkg/crd" - - "github.com/kubernetes-csi/driver-registrar/pkg/connection" -) - -func kubernetesRegister( - config *rest.Config, - csiConn connection.CSIConnection, - csiDriverName string, -) { - // Get client info to CSIDriver - clientset, err := k8scsiclient.NewForConfig(config) - if err != nil { - glog.Error(err.Error()) - os.Exit(1) - } - - // Set spec - spec := &k8scsi.CSIDriverSpec{ - AttachRequired: k8sAttachmentRequired, - PodInfoOnMountVersion: k8sPodInfoOnMountVersion, - } - glog.V(1).Infof("AttachRequired: %v", *k8sAttachmentRequired) - glog.V(1).Infof("PodInfoOnMountVersion: %v", *k8sPodInfoOnMountVersion) - - // Register CRD - glog.V(1).Info("Registering " + k8scsi.CsiDriverResourcePlural) - crdclient, err := crdclient.NewForConfig(config) - if err != nil { - glog.Error(err.Error()) - os.Exit(1) - } - crdv1beta1client := crdclient.ApiextensionsV1beta1().CustomResourceDefinitions() - _, err = crdv1beta1client.Create(k8scsicrd.CSIDriverCRD()) - if err == nil { - glog.V(1).Info("CSIDriver CRD registered") - } else if apierrors.IsAlreadyExists(err) { - glog.V(1).Info("CSIDriver CRD already had been registered") - } else if err != nil { - glog.Error(err.Error()) - os.Exit(1) - } - - // Set up goroutine to cleanup (aka deregister) on termination. - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - go func() { - <-c - verifyAndDeleteCSIDriverInfo( - clientset, - csiDriverName, - spec) - os.Exit(1) - }() - - // Run forever - for { - verifyAndAddCSIDriverInfo(clientset, csiDriverName, spec) - time.Sleep(sleepDuration) - } -} - -// Registers CSI driver by creating a CSIDriver object -func verifyAndAddCSIDriverInfo( - csiClientset *k8scsiclient.Clientset, - csiDriverName string, - spec *k8scsi.CSIDriverSpec, -) error { - retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error { - csidrivers := csiClientset.CsiV1alpha1().CSIDrivers() - - // Create it - csiDriver := &k8scsi.CSIDriver{ - ObjectMeta: metav1.ObjectMeta{ - Name: csiDriverName, - }, - Spec: *spec, - } - - _, err := csidrivers.Create(csiDriver) - if err == nil { - glog.V(1).Infof("CSIDRiver object created for driver %s", csiDriverName) - return nil - } else if apierrors.IsAlreadyExists(err) { - return nil - } else { - glog.Errorf("Failed to create CSIDriver object: %v", err) - return err - } - }) - if retryErr != nil { - return retryErr - } - return nil -} - -// Deregister CSI Driver by deleting CSIDriver object -func verifyAndDeleteCSIDriverInfo( - csiClientset *k8scsiclient.Clientset, - csiDriverName string, - spec *k8scsi.CSIDriverSpec, -) error { - retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error { - csidrivers := csiClientset.CsiV1alpha1().CSIDrivers() - err := csidrivers.Delete(csiDriverName, &metav1.DeleteOptions{}) - if err == nil { - glog.V(1).Infof("CSIDRiver object deleted for driver %s", csiDriverName) - return nil - } else if apierrors.IsNotFound(err) { - glog.V(1).Info("No need to clean up CSIDriver since it does not exist") - return nil - } else { - glog.Errorf("Failed to create CSIDriver object: %v", err) - return err - } - }) - if retryErr != nil { - return retryErr - } - return nil -} diff --git a/cmd/driver-registrar/main.go b/cmd/node-driver-registrar/main.go similarity index 63% rename from cmd/driver-registrar/main.go rename to cmd/node-driver-registrar/main.go index 47bf6d826..7325c1792 100644 --- a/cmd/driver-registrar/main.go +++ b/cmd/node-driver-registrar/main.go @@ -28,7 +28,7 @@ import ( "k8s.io/client-go/tools/clientcmd" registerapi "k8s.io/kubernetes/pkg/kubelet/apis/pluginregistration/v1alpha1" - "github.com/kubernetes-csi/driver-registrar/pkg/connection" + "github.com/kubernetes-csi/node-driver-registrar/pkg/connection" ) const ( @@ -41,49 +41,22 @@ const ( // Verify (and update, if needed) the node ID at this freqeuency. sleepDuration = 2 * time.Minute - - modeNodeRegister = "node-register" - modeKubeRegister = "kubernetes-register" - defaultMode = modeNodeRegister ) // Command line flags var ( - kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.") - runMode = flag.String("mode", defaultMode, "Mode to run the program. Supported modes: node-register, kubernetes-register.\n"+ - "In "+modeNodeRegister+" mode, the program will register the CSI driver with the "+ - "node, setting up the node information accordingly.\n"+ - "In "+modeKubeRegister+" mode, the program will register the driver with "+ - "Kubernetes. In this mode this program will setup all the necessary information "+ - "to register the CSI driver with Kubernetes. This mode requires that this "+ - "container be run in a StateFul set of 1, and not in a DaemonSet.") - k8sAttachmentRequired = flag.Bool("driver-requires-attachment", - true, - "Indicates this CSI volume driver requires an attach operation (because it "+ - "implements the CSI ControllerPublishVolume() method), and that Kubernetes "+ - "should call attach and wait for any attach operation to complete before "+ - "proceeding to mounting. If value is not specified, default is false meaning "+ - "attach will not be called.") - k8sPodInfoOnMountVersion = flag.String("pod-info-mount-version", - "", - "This indicates that the associated CSI volume driver"+ - "requires additional pod information (like podName, podUID, etc.) during mount."+ - "A version of value \"v1\" will cause the Kubelet send the followings pod information "+ - "during NodePublishVolume() calls to the driver as VolumeAttributes:"+ - "- csi.storage.k8s.io/pod.name: pod.Name\n"+ - "- csi.storage.k8s.io/pod.namespace: pod.Namespace\n"+ - "- csi.storage.k8s.io/pod.uid: string(pod.UID)", - ) + kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.") connectionTimeout = flag.Duration("connection-timeout", 1*time.Minute, "Timeout for waiting for CSI driver socket.") csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.") kubeletRegistrationPath = flag.String("kubelet-registration-path", "", `Enables Kubelet Plugin Registration service, and returns the specified path as "endpoint" in "PluginInfo" response. - If this option is set, the driver-registrar expose a unix domain socket to handle Kubelet Plugin Registration, - this socket MUST be surfaced on the host in the kubelet plugin registration directory (in addition to the CSI driver socket). - If plugin registration is enabled on kubelet (kubelet flag KubeletPluginsWatcher is set), then this option should be set - and the value should be the path of the CSI driver socket on the host machine.`) +If this option is set, the driver-registrar expose a unix domain socket to handle Kubelet Plugin Registration, +this socket MUST be surfaced on the host in the kubelet plugin registration directory (in addition to the CSI driver socket). +If plugin registration is enabled on kubelet (kubelet flag KubeletPluginsWatcher is set), then this option should be set +and the value should be the path of the CSI driver socket on the host machine.`) showVersion = flag.Bool("version", false, "Show version.") version = "unknown" + // List of supported versions supportedVersions = []string{"1.0.0"} ) @@ -139,7 +112,7 @@ func main() { // Once https://github.com/container-storage-interface/spec/issues/159 is // resolved, if plugin does not support PUBLISH_UNPUBLISH_VOLUME, then we - // can skip adding mappting to "csi.volume.kubernetes.io/nodeid" annotation. + // can skip adding mapping to "csi.volume.kubernetes.io/nodeid" annotation. // Connect to CSI. glog.V(1).Infof("Attempting to open a gRPC connection with: %q", *csiAddress) @@ -169,16 +142,8 @@ func main() { os.Exit(1) } - // Check mode - switch *runMode { - case modeNodeRegister: - nodeRegister(config, csiConn, csiDriverName) - case modeKubeRegister: - kubernetesRegister(config, csiConn, csiDriverName) - default: - glog.Errorf("Unknown mode: %s", *runMode) - fmt.Fprintf(os.Stderr, "Unknown mode: %s", *runMode) - } + // Run forever + nodeRegister(config, csiConn, csiDriverName) } func buildConfig(kubeconfig string) (*rest.Config, error) { diff --git a/cmd/driver-registrar/node_register.go b/cmd/node-driver-registrar/node_register.go similarity index 99% rename from cmd/driver-registrar/node_register.go rename to cmd/node-driver-registrar/node_register.go index 8d696e997..2dbe3d6c9 100644 --- a/cmd/driver-registrar/node_register.go +++ b/cmd/node-driver-registrar/node_register.go @@ -36,7 +36,7 @@ import ( "k8s.io/client-go/util/retry" registerapi "k8s.io/kubernetes/pkg/kubelet/apis/pluginregistration/v1alpha1" - "github.com/kubernetes-csi/driver-registrar/pkg/connection" + "github.com/kubernetes-csi/node-driver-registrar/pkg/connection" ) func nodeRegister(