Skip to content

Commit

Permalink
Merge pull request #170 from linode/config.env
Browse files Browse the repository at this point in the history
Configure the CSI driver through environment variables
  • Loading branch information
Nick Saika authored Jun 7, 2024
2 parents ee6cca3 + 1df2183 commit 4b2deba
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 117 deletions.
246 changes: 174 additions & 72 deletions README.md

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions deploy/kubernetes/base/ds-csi-linode-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ spec:
- name: csi-linode-plugin
image: linode/linode-blockstorage-csi-driver:latest
args :
- "--endpoint=$(CSI_ENDPOINT)"
- "--token=$(LINODE_TOKEN)"
- "--url=$(LINODE_API_URL)"
- "--node=$(NODE_NAME)"
- "--v=2"
env:
- name: CSI_ENDPOINT
value: unix:///csi/csi.sock
- name: LINODE_API_URL
- name: LINODE_URL
value: https://api.linode.com/v4
- name: NODE_NAME
valueFrom:
Expand Down
7 changes: 1 addition & 6 deletions deploy/kubernetes/base/ss-csi-linode-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,11 @@ spec:
- name: linode-csi-plugin
image: linode/linode-blockstorage-csi-driver:latest
args:
- "--endpoint=$(CSI_ENDPOINT)"
- "--token=$(LINODE_TOKEN)"
- "--url=$(LINODE_API_URL)"
- "--node=$(NODE_NAME)"
- "--bs-prefix=$(LINODE_BS_PREFIX)"
- "--v=2"
env:
- name: CSI_ENDPOINT
value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
- name: LINODE_API_URL
- name: LINODE_URL
value: https://api.linode.com/v4
- name: LINODE_BS_PREFIX
- name: NODE_NAME
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/container-storage-interface/spec v1.3.0
github.com/ianschenck/envflag v0.0.0-20140720210342-9111d830d133
github.com/linode/go-metadata v0.2.0
github.com/linode/linodego v1.23.0
golang.org/x/net v0.17.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianschenck/envflag v0.0.0-20140720210342-9111d830d133 h1:h6FO/Da7rdYqJbRYMW9f+SMBWnJVguWh+0ERefW8zp8=
github.com/ianschenck/envflag v0.0.0-20140720210342-9111d830d133/go.mod h1:pyYc5lldRtL0l5YitYVv1dLKuC0qhMfAfiR7BLsN2pA=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
Expand Down
9 changes: 3 additions & 6 deletions helm-chart/csi-driver/templates/csi-linode-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,14 @@ spec:
- mountPath: /var/lib/csi/sockets/pluginproxy/
name: socket-dir
- args:
- --endpoint=$(CSI_ENDPOINT)
- --token=$(LINODE_TOKEN)
- --url=$(LINODE_API_URL)
- --bs-prefix=$(LINODE_BS_PREFIX)
- --v=2
env:
- name: CSI_ENDPOINT
value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
- name: LINODE_API_URL
- name: LINODE_URL
value: https://api.linode.com/v4
- name: LINODE_BS_PREFIX
- name: LINODE_VOLUME_LABEL_PREFIX
value: {{ .Values.volumeLabelPrefix | default "" | quote }}
- name: NODE_NAME
valueFrom:
fieldRef:
Expand Down
5 changes: 1 addition & 4 deletions helm-chart/csi-driver/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ spec:
- mountPath: /registration
name: registration-dir
- args:
- --endpoint=$(CSI_ENDPOINT)
- --token=$(LINODE_TOKEN)
- --url=$(LINODE_API_URL)
- --v=2
env:
- name: CSI_ENDPOINT
value: unix:///csi/csi.sock
- name: LINODE_API_URL
- name: LINODE_URL
value: https://api.linode.com/v4
- name: NODE_NAME
valueFrom:
Expand Down
4 changes: 4 additions & 0 deletions helm-chart/csi-driver/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ apiToken: ""
# region [Required if secretRef is not set] - Must be a Linode region. (https://api.linode.com/v4/regions)
region: ""

# (OPTIONAL) Label prefix for the Linode Block Storage volumes created by this
# driver.
volumeLabelPrefix: ""

# Default namespace is "kube-system" but it can be set to another namespace
namespace: kube-system

Expand Down
72 changes: 48 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,56 @@ import (
"fmt"
"os"

"github.com/ianschenck/envflag"
"github.com/linode/linodego"
"k8s.io/klog/v2"

driver "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-bs"
linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client"
mountmanager "github.com/linode/linode-blockstorage-csi-driver/pkg/mount-manager"
"k8s.io/klog/v2"
)

const (
driverName = "linodebs.csi.linode.com"
)
const driverName = "linodebs.csi.linode.com"

var (
vendorVersion string
endpoint = flag.String("endpoint", "unix:/tmp/csi.sock", "CSI endpoint")
token = flag.String("token", "", "Linode API Token")
url = flag.String("url", "", "Linode API URL")
bsPrefix = flag.String("bs-prefix", "", "Linode BlockStorage Volume label prefix")
)
var vendorVersion string // set by the linker

func init() {
_ = flag.Set("logtostderr", "true")
type configuration struct {
// The UNIX socket to listen on for RPC requests.
csiEndpoint string

// Linode personal access token, used to make requests to the Linode
// API.
linodeToken string

// Linode API URL.
linodeURL string

// Optional label prefix to use when creating new Linode Block Storage
// Volumes.
volumeLabelPrefix string

// Name of the current node, when running as the node plugin.
//
// deprecated: This is not needed as the CSI driver now uses the Linode
// Metadata Service to source information about the current
// node/instance. It will be removed in a future change.
nodeName string
}

func loadConfig() configuration {
var cfg configuration
envflag.StringVar(&cfg.csiEndpoint, "CSI_ENDPOINT", "unix:/tmp/csi.sock", "Path to the CSI endpoint socket")
envflag.StringVar(&cfg.linodeToken, "LINODE_TOKEN", "", "Linode API token")
envflag.StringVar(&cfg.linodeURL, "LINODE_URL", linodego.APIHost, "Linode API URL")
envflag.StringVar(&cfg.volumeLabelPrefix, "LINODE_VOLUME_LABEL_PREFIX", "", "Linode Block Storage volume label prefix")
envflag.StringVar(&cfg.nodeName, "NODE_NAME", "", "Name of the current node") // deprecated
envflag.Parse()
return cfg
}

func main() {
klog.InitFlags(nil)
_ = flag.Set("logtostderr", "true")
flag.Parse()
if err := handle(); err != nil {
klog.Fatal(err)
Expand All @@ -55,16 +81,18 @@ func handle() error {
if vendorVersion == "" {
return errors.New("vendorVersion must be set at compile time")
}
if *token == "" {
klog.V(4).Infof("Driver vendor version %v", vendorVersion)

cfg := loadConfig()
if cfg.linodeToken == "" {
return errors.New("linode token required")
}
klog.V(4).Infof("Driver vendor version %v", vendorVersion)

linodeDriver := driver.GetLinodeDriver()

// Initialize Linode Driver (Move setup to main?)
uaPrefix := fmt.Sprintf("LinodeCSI/%s", vendorVersion)
cloudProvider, err := linodeclient.NewLinodeClient(*token, uaPrefix, *url)
cloudProvider, err := linodeclient.NewLinodeClient(cfg.linodeToken, uaPrefix, cfg.linodeURL)
if err != nil {
return fmt.Errorf("failed to set up linode client: %s", err)
}
Expand All @@ -74,28 +102,24 @@ func handle() error {

metadata, err := driver.GetMetadata(context.Background())
if err != nil {
klog.ErrorS(err, "Metadata service not available, falling back to API")
if metadata, err = driver.GetMetadataFromAPI(context.Background(), cloudProvider); err != nil {
return fmt.Errorf("get metadata from api: %w", err)
}
}

prefix := ""
if bsPrefix != nil {
prefix = *bsPrefix
}

if err := linodeDriver.SetupLinodeDriver(
cloudProvider,
mounter,
deviceUtils,
metadata,
driverName,
vendorVersion,
prefix,
cfg.volumeLabelPrefix,
); err != nil {
return fmt.Errorf("failed to initialize Linode CSI Driver: %v", err)
return fmt.Errorf("setup driver: %v", err)
}

linodeDriver.Run(*endpoint)
linodeDriver.Run(cfg.csiEndpoint)
return nil
}

0 comments on commit 4b2deba

Please sign in to comment.