Skip to content

Commit

Permalink
feat: improve CSI robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
iExalt committed Jan 2, 2025
1 parent ef9f7c7 commit 3f06d3e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
12 changes: 6 additions & 6 deletions .gitlab/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
.gitlab/CODEOWNERS @lanwall12 @nitper @bsherrycrusoe @ksosnowski3 @dpattishall

# more restrictive ownership for adding dependencies and changing ci/build
.gitlab-ci.yml @lanwall12 @nitper @crusoeenergy/island/ccx
.golangci-lint.yml @lanwall12 @nitper @crusoeenergy/island/ccx
go.mod @lanwall12 @nitper @crusoeenergy/island/ccx
Makefile @lanwall12 @nitper @crusoeenergy/island/ccx
Dockerfile @lanwall12 @nitper @crusoeenergy/island/ccx
Dockerfile.* @lanwall12 @nitper @crusoeenergy/island/ccx
.gitlab-ci.yml @lanwall12 @nitper @crusoeenergy/island/managed
.golangci-lint.yml @lanwall12 @nitper @crusoeenergy/island/managed
go.mod @lanwall12 @nitper @crusoeenergy/island/managed
Makefile @lanwall12 @nitper @crusoeenergy/island/managed
Dockerfile @lanwall12 @nitper @crusoeenergy/island/managed
Dockerfile.* @lanwall12 @nitper @crusoeenergy/island/managed
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ issues:
source: "^//go:generate "

run:
# include test files or not
# include test files or not
tests: true

# golangci.com configuration
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/antihax/optional v1.0.0
github.com/container-storage-interface/spec v1.10.0
github.com/crusoecloud/client-go v0.1.59
github.com/google/uuid v1.6.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/thediveo/enumflag/v2 v2.0.5
Expand Down Expand Up @@ -34,7 +35,6 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions internal/controller/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
csi.VolumeCapability_AccessMode_SINGLE_NODE_READER_ONLY: {},
csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER: {},
csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER: {},
csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY: {},
}
//nolint:gochecknoglobals // can't construct const map
fsAllowedAccessModes = map[csi.VolumeCapability_AccessMode_Mode]struct{}{
Expand Down
2 changes: 1 addition & 1 deletion internal/crusoe/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func signMessageV1_0(message []byte, encodedKey string) ([]byte, error) {
// Key is b64 encoded.
expectedKey, err := base64.RawURLEncoding.DecodeString(encodedKey)
if err != nil {
return nil, fmt.Errorf("failed to decode key: %w", err)
return nil, fmt.Errorf("failed to decode Crusoe Secret Key: %w", err)
}

mac := hmac.New(sha256.New, expectedKey)
Expand Down
38 changes: 23 additions & 15 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"sync"
"syscall"

"github.com/google/uuid"

"github.com/crusoecloud/crusoe-csi-driver/internal/controller"

"github.com/crusoecloud/crusoe-csi-driver/internal/common"
Expand Down Expand Up @@ -42,15 +44,16 @@ const (
projectIDEnvKey = "CRUSOE_PROJECT_ID"
projectIDLabelKey = "crusoe.ai/project.id"

numExpectedComponents = 2
vmIDFilePath = "/sys/class/dmi/id/product_uuid"
)

var (
errInstanceNotFound = errors.New("instance not found")
errMultipleInstances = errors.New("multiple instances found")
errVMIDReadFailed = fmt.Errorf("failed to read %s for VM ID", vmIDFilePath)
errVMIDParseFailed = fmt.Errorf("failed to parse %s for VM ID", vmIDFilePath)
errProjectIDNotFound = fmt.Errorf("project ID not found in %s env var or %s node label",
projectIDEnvKey, projectIDLabelKey)
errInvalidNodeName = errors.New("invalid node name")
)

func interruptHandler() (*sync.WaitGroup, context.Context) {
Expand All @@ -76,6 +79,7 @@ func interruptHandler() (*sync.WaitGroup, context.Context) {
return &wg, ctx
}

//nolint:cyclop // function is already fairly clean
func getHostInstance(ctx context.Context) (*crusoeapi.InstanceV1Alpha5, error) {
crusoeClient := crusoe.NewCrusoeClient(
viper.GetString(CrusoeAPIEndpointFlag),
Expand All @@ -84,28 +88,32 @@ func getHostInstance(ctx context.Context) (*crusoeapi.InstanceV1Alpha5, error) {
"crusoe-csi-driver/0.0.1",
)

fullNodeName := viper.GetString(NodeNameFlag)
nodeNameSplit := strings.SplitN(fullNodeName, ".", numExpectedComponents)
if len(nodeNameSplit) < 1 {
return nil, errInvalidNodeName
vmIDStringByteArray, err := os.ReadFile(vmIDFilePath)
if err != nil {
return nil, fmt.Errorf("%w: %w", errVMIDReadFailed, err)
}

vmIDString := strings.TrimSpace(string(vmIDStringByteArray))
_, err = uuid.Parse(vmIDString)
if err != nil {
return nil, fmt.Errorf("%w: %w", errVMIDParseFailed, err)
}
shortNodeName := nodeNameSplit[0]

var projectID string

projectID = viper.GetString(CrusoeProjectIDFlag)
if projectID == "" {
var ok bool
kubeClientConfig, err := rest.InClusterConfig()
if err != nil {
kubeClientConfig, configErr := rest.InClusterConfig()
if configErr != nil {
return nil, fmt.Errorf("could not get kube client config: %w", err)
}

kubeClient, err := kubernetes.NewForConfig(kubeClientConfig)
if err != nil {
kubeClient, clientErr := kubernetes.NewForConfig(kubeClientConfig)
if clientErr != nil {
return nil, fmt.Errorf("could not get kube client: %w", err)
}
hostNode, nodeFetchErr := kubeClient.CoreV1().Nodes().Get(ctx, fullNodeName, metav1.GetOptions{})
hostNode, nodeFetchErr := kubeClient.CoreV1().Nodes().Get(ctx, viper.GetString(NodeNameFlag), metav1.GetOptions{})
if nodeFetchErr != nil {
return nil, fmt.Errorf("could not fetch current node with kube client: %w", err)
}
Expand All @@ -118,16 +126,16 @@ func getHostInstance(ctx context.Context) (*crusoeapi.InstanceV1Alpha5, error) {

instances, _, err := crusoeClient.VMsApi.ListInstances(ctx, projectID,
&crusoeapi.VMsApiListInstancesOpts{
Names: optional.NewString(shortNodeName),
Ids: optional.NewString(vmIDString),
})
if err != nil {
return nil, fmt.Errorf("failed to list instances: %w", err)
}

if len(instances.Items) == 0 {
return nil, fmt.Errorf("%w: %s", errInstanceNotFound, shortNodeName)
return nil, fmt.Errorf("%w: %s", errInstanceNotFound, vmIDString)
} else if len(instances.Items) > 1 {
return nil, fmt.Errorf("%w: %s", errMultipleInstances, shortNodeName)
return nil, fmt.Errorf("%w: %s", errMultipleInstances, vmIDString)
}

return &instances.Items[0], nil
Expand Down

0 comments on commit 3f06d3e

Please sign in to comment.