Skip to content

Commit

Permalink
clean up and more unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
komer3 committed Jan 28, 2025
1 parent 08e3206 commit 340de8e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 253 deletions.
6 changes: 0 additions & 6 deletions deploy/kubernetes/base/ds-csi-linode-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ spec:
- mountPath: /tmp
name: tmp
volumes:
- name: linode-info
emptyDir: {}
- name: get-linode-id
configMap:
name: get-linode-id
defaultMode: 493
- name: registration-dir
hostPath:
path: /var/lib/kubelet/plugins_registry/
Expand Down
7 changes: 0 additions & 7 deletions deploy/kubernetes/base/ss-csi-linode-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,3 @@ spec:
volumes:
- name: socket-dir
emptyDir: {}
- name: linode-info
emptyDir: {}
- name: get-linode-id
configMap:
name: get-linode-id
# octal mode 755
defaultMode: 493
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
golang.org/x/sys v0.29.0
google.golang.org/grpc v1.70.0
google.golang.org/protobuf v1.36.3
k8s.io/api v0.32.1
k8s.io/apimachinery v0.32.1
k8s.io/client-go v0.32.1
k8s.io/klog/v2 v2.130.1
Expand Down Expand Up @@ -79,7 +80,6 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.32.1 // indirect
k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions helm-chart/csi-driver/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ spec:
- effect: NoExecute
operator: Exists
volumes:
- emptyDir: {}
name: linode-info
- hostPath:
path: /var/lib/kubelet/plugins_registry/
type: DirectoryOrCreate
Expand Down
76 changes: 0 additions & 76 deletions internal/driver/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"strconv"
"strings"

Expand All @@ -14,7 +13,6 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

"github.com/linode/linode-blockstorage-csi-driver/pkg/filesystem"
linodeclient "github.com/linode/linode-blockstorage-csi-driver/pkg/linode-client"
"github.com/linode/linode-blockstorage-csi-driver/pkg/logger"
)
Expand Down Expand Up @@ -192,78 +190,4 @@ func memoryToBytes(sizeMB int) uint {
// Linode instance type.
const minMemory uint = 1 << 30

// LinodeIDPath is the path to a file containing only the ID of the Linode
// instance the CSI node plugin is currently running on.
// This file is expected to be placed into the Linode by the init container
// provided with the CSI node plugin.
const LinodeIDPath = "/linode-info/linode-id"

var errNilClient = errors.New("nil client")

// GetMetadataFromAPI attempts to retrieve metadata about the current
// node/instance directly from the Linode API.
func GetMetadataFromAPI(ctx context.Context, client linodeclient.LinodeClient, fs filesystem.FileSystem) (Metadata, error) {
log, _, done := logger.GetLogger(ctx).WithMethod("GetMetadataFromAPI")
defer done()

log.V(2).Info("Processing request")

if client == nil {
return Metadata{}, errNilClient
}

log.V(4).Info("Checking LinodeIDPath", "path", LinodeIDPath)
if _, err := fs.Stat(LinodeIDPath); err != nil {
return Metadata{}, fmt.Errorf("stat %s: %w", LinodeIDPath, err)
}

log.V(4).Info("Opening LinodeIDPath", "path", LinodeIDPath)
fileObj, err := fs.Open(LinodeIDPath)
if err != nil {
return Metadata{}, fmt.Errorf("open: %w", err)
}
defer func() {
err = fileObj.Close()
}()

log.V(4).Info("Reading LinodeID from file")
// Read in the file, but use a LimitReader to make sure we are not
// reading in junk.
data, err := io.ReadAll(io.LimitReader(fileObj, 1<<10))
if err != nil {
return Metadata{}, fmt.Errorf("read all: %w", err)
}

log.V(4).Info("Parsing LinodeID")
linodeID, err := strconv.Atoi(string(data))
if err != nil {
return Metadata{}, fmt.Errorf("atoi: %w", err)
}

log.V(4).Info("Retrieving instance data from API", "linodeID", linodeID)
instance, err := client.GetInstance(ctx, linodeID)
if err != nil {
return Metadata{}, fmt.Errorf("get instance: %w", err)
}

memory := minMemory
if instance.Specs != nil {
memory = memoryToBytes(instance.Specs.Memory)
}

nodeMetadata := Metadata{
ID: linodeID,
Label: instance.Label,
Region: instance.Region,
Memory: memory,
}

log.V(4).Info("Successfully retrieved metadata",
"instanceID", nodeMetadata.ID,
"instanceLabel", nodeMetadata.Label,
"region", nodeMetadata.Region,
"memory", nodeMetadata.Memory)

log.V(2).Info("Successfully completed")
return nodeMetadata, nil
}
Loading

0 comments on commit 340de8e

Please sign in to comment.