Skip to content

Commit

Permalink
feat: use product_uuid file to infer VM ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Clement Liaw committed Dec 18, 2024
1 parent ef9f7c7 commit 6879752
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
12 changes: 6 additions & 6 deletions .gitlab/CODEOWNERS
Validating CODEOWNERS rules …
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
39 changes: 23 additions & 16 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"net/url"
"os"
"os/signal"
"strings"
"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 +43,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 +78,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 +87,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 := 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 +125,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 6879752

Please sign in to comment.