-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scheduler predictor #64
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4dc96e6
A scheduler plugin imitator that tries to verify if proposed pods cou…
burmanm 4c5db3a
Sort out the go.mod changes, update golint
burmanm d64425d
Add new command, "tools estimate" which gets the parameters amount of…
burmanm 29b26a6
Address cmd line return values
burmanm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package tools | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/k8ssandra/k8ssandra-client/pkg/kubernetes" | ||
"github.com/k8ssandra/k8ssandra-client/pkg/scheduler" | ||
"github.com/spf13/cobra" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"k8s.io/apimachinery/pkg/api/resource" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
var ( | ||
estimateExample = ` | ||
# Estimate if pods will fit the cluster | ||
%[1]s estimate [<args>] | ||
|
||
# Estimate if 4 pods each with 2Gi of memory and 2 vCPUs will be able to run on the cluster. | ||
# All CPU values and memory use Kubernetes notation | ||
%[1]s estimate --count 4 --memory 2Gi --cpu 2000m | ||
` | ||
errInvalidCount = errors.New("Count of pods must be higher than 0") | ||
) | ||
|
||
type estimateOptions struct { | ||
configFlags *genericclioptions.ConfigFlags | ||
genericclioptions.IOStreams | ||
|
||
count int | ||
memory string | ||
cpu string | ||
|
||
cpuQuantity resource.Quantity | ||
memoryQuantity resource.Quantity | ||
} | ||
|
||
func newEstimateOptions(streams genericclioptions.IOStreams) *estimateOptions { | ||
return &estimateOptions{ | ||
configFlags: genericclioptions.NewConfigFlags(true), | ||
IOStreams: streams, | ||
} | ||
} | ||
|
||
// NewCmd provides a cobra command wrapping newAddOptions | ||
func NewEstimateCmd(streams genericclioptions.IOStreams) *cobra.Command { | ||
o := newEstimateOptions(streams) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "estimate [flags]", | ||
Short: "Estimate if datacenter can be expanded", | ||
Example: fmt.Sprintf(estimateExample, "kubectl k8ssandra tools estimate"), | ||
RunE: func(c *cobra.Command, args []string) error { | ||
if err := o.Complete(c, args); err != nil { | ||
return err | ||
} | ||
if err := o.Validate(); err != nil { | ||
return err | ||
} | ||
if err := o.Run(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
fl := cmd.Flags() | ||
fl.IntVar(&o.count, "count", 0, "new nodes to create") | ||
fl.StringVar(&o.cpu, "cpu", "0", "how many cores per node") | ||
fl.StringVar(&o.memory, "memory", "0", "how much memory per node") | ||
|
||
if err := cmd.MarkFlagRequired("memory"); err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := cmd.MarkFlagRequired("cpu"); err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := cmd.MarkFlagRequired("count"); err != nil { | ||
panic(err) | ||
} | ||
|
||
o.configFlags.AddFlags(fl) | ||
return cmd | ||
} | ||
|
||
// Complete parses the arguments and necessary flags to options | ||
func (c *estimateOptions) Complete(cmd *cobra.Command, args []string) error { | ||
return nil | ||
} | ||
|
||
// Validate ensures that all required arguments and flag values are provided | ||
func (c *estimateOptions) Validate() error { | ||
if c.count < 0 { | ||
burmanm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return errInvalidCount | ||
} | ||
|
||
var err error | ||
cpuQuantity, err := resource.ParseQuantity(c.cpu) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
memoryQuantity, err := resource.ParseQuantity(c.memory) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.memoryQuantity = memoryQuantity | ||
c.cpuQuantity = cpuQuantity | ||
|
||
return nil | ||
} | ||
|
||
// Run processes the input, creates a connection to Kubernetes and processes a secret to add the users | ||
func (c *estimateOptions) Run() error { | ||
restConfig, err := c.configFlags.ToRESTConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kubeClient, err := kubernetes.GetClient(restConfig) | ||
if err != nil { | ||
return err | ||
} | ||
ctx := context.Background() | ||
|
||
proposedPods := makePods(c.count, makeResources(c.cpuQuantity.MilliValue(), c.memoryQuantity.Value())) | ||
|
||
if err := scheduler.TryScheduling(ctx, kubeClient, proposedPods); err != nil { | ||
return errors.Wrap(err, "Unable to schedule the pods") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func makePods(count int, resources corev1.ResourceList) []*corev1.Pod { | ||
pods := make([]*corev1.Pod, count) | ||
for i := 0; i < count; i++ { | ||
pods[i] = &corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "a", | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Resources: corev1.ResourceRequirements{ | ||
Requests: resources, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
return pods | ||
} | ||
|
||
func makeResources(milliCPU, memory int64) corev1.ResourceList { | ||
return corev1.ResourceList{ | ||
corev1.ResourceCPU: *resource.NewMilliQuantity(milliCPU, resource.DecimalSI), | ||
corev1.ResourceMemory: *resource.NewQuantity(memory, resource.BinarySI), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tools | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
type ClientOptions struct { | ||
configFlags *genericclioptions.ConfigFlags | ||
genericclioptions.IOStreams | ||
} | ||
|
||
// NewToolsOptions provides an instance of NamespaceOptions with default values | ||
func NewToolsOptions(streams genericclioptions.IOStreams) *ClientOptions { | ||
return &ClientOptions{ | ||
configFlags: genericclioptions.NewConfigFlags(true), | ||
IOStreams: streams, | ||
} | ||
} | ||
|
||
// NewToolsCmd provides a cobra command wrapping ClientOptions | ||
func NewToolsCmd(streams genericclioptions.IOStreams) *cobra.Command { | ||
o := NewToolsOptions(streams) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "tools [subcommand] [flags]", | ||
} | ||
|
||
// Add subcommands | ||
cmd.AddCommand(NewEstimateCmd(streams)) | ||
|
||
o.configFlags.AddFlags(cmd.Flags()) | ||
|
||
return cmd | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Issue: doing things this way always spits out the help text for the command in the console, along with the actual error:
It makes it a little challenging to figure out what's going on.
Also we don't get a message if the pods can successfully be scheduled.
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.
Changed to this: