-
Notifications
You must be signed in to change notification settings - Fork 2
Add host resource validation before creating VMs #46
base: main
Are you sure you want to change the base?
Changes from all commits
78be69f
e0dd3ce
36aa176
5e3fe04
2fafb89
aabe7e7
80586bd
8bdf584
3f4f64b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "sync" | ||
|
|
||
| "github.com/mrgb7/playground/internal/multipass" | ||
| "github.com/mrgb7/playground/internal/validator" | ||
| "github.com/mrgb7/playground/pkg/logger" | ||
| "github.com/mrgb7/playground/types" | ||
| "github.com/spf13/cobra" | ||
|
|
@@ -34,6 +35,8 @@ var ( | |
| workerCPUs int | ||
| workerMemory string | ||
| workerDisk string | ||
| skipValidation bool | ||
| forceCreation bool | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -78,6 +81,68 @@ func createCluster(config *types.ClusterConfig) error { | |
| return fmt.Errorf("multipass is not installed or not in PATH") | ||
| } | ||
|
|
||
| if !skipValidation { | ||
| requirements, err := validator.CalculateResourceRequirements( | ||
| config.MasterCPUs, config.MasterMemory, config.MasterDisk, | ||
| config.WorkerCPUs, config.WorkerMemory, config.WorkerDisk, | ||
| config.Size-1, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to calculate resource requirements: %w", err) | ||
| } | ||
|
|
||
| status, err := validator.ValidateResources(requirements) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to validate host resources: %w", err) | ||
| } | ||
|
|
||
| portStatus, err := validator.ValidatePorts() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain what is the problem on Ports, is not
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you mention in issue feature file, you want to validate if port is available? , if not ->
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, please keep in mind that when I outline a new issue or feature, it's not necessarily the finalized design. Some assumptions may be inaccurate or incomplete, so it's important that you validate and critically evaluate the requirements before proceeding |
||
| if err != nil { | ||
| return fmt.Errorf("failed to validate port availability: %w", err) | ||
| } | ||
|
|
||
| if !status.IsValid || !portStatus.IsValid { | ||
| fmt.Println("Resource validation failed:") | ||
| for _, msg := range status.Messages { | ||
| if strings.Contains(msg, "❌") { | ||
| fmt.Println(msg) | ||
| } | ||
| } | ||
| for _, msg := range portStatus.Messages { | ||
| if strings.Contains(msg, "❌") { | ||
| fmt.Println(msg) | ||
| } | ||
| } | ||
| for _, msg := range status.Messages { | ||
| if strings.Contains(msg, "⚠️") { | ||
| fmt.Println(msg) | ||
| } | ||
| } | ||
| for _, msg := range portStatus.Messages { | ||
| if strings.Contains(msg, "⚠️") { | ||
| fmt.Println(msg) | ||
| } | ||
| } | ||
| fmt.Println("\nRecommendations:") | ||
| for _, rec := range status.Recommendations { | ||
| fmt.Println("-", rec) | ||
| } | ||
| for _, rec := range portStatus.Recommendations { | ||
| fmt.Println("-", rec) | ||
| } | ||
| fmt.Println("\nTo bypass this check, use: playground create cluster --skip-host-validation") | ||
| return fmt.Errorf("insufficient resources for cluster creation") | ||
| } | ||
|
|
||
| fmt.Println("✅ Host resources validated successfully") | ||
| for _, msg := range status.Messages { | ||
| fmt.Println(msg) | ||
| } | ||
| for _, msg := range portStatus.Messages { | ||
| fmt.Println(msg) | ||
| } | ||
| } | ||
|
|
||
| cl := types.NewCluster(config.Name) | ||
|
|
||
| err := cl.Validate(*config) | ||
|
|
@@ -300,6 +365,8 @@ func init() { | |
| createCmd.Flags().IntVarP(&workerCPUs, "worker-cpus", "w", DefaultWorkerCPUs, "Number of CPUs for each worker node") | ||
| createCmd.Flags().StringVarP(&workerMemory, "worker-memory", "W", "2G", "Memory for each worker node") | ||
| createCmd.Flags().StringVarP(&workerDisk, "worker-disk", "d", "20G", "Disk for each worker node") | ||
| createCmd.Flags().BoolVar(&skipValidation, "skip-host-validation", false, "Skip host resource validation") | ||
| createCmd.Flags().BoolVar(&forceCreation, "force", false, "Force cluster creation despite resource warnings") | ||
| if err := createCmd.MarkFlagRequired("name"); err != nil { | ||
| logger.Errorln("Failed to mark name flag as required: %v", err) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see an issue on calculating disk it returns 0GB free