-
Notifications
You must be signed in to change notification settings - Fork 56
Add CalcOnlyRunning option to ProxMox scheduler #432
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/luthermonson/go-proxmox" | ||
|
@@ -69,7 +70,8 @@ var ( | |
// ProxmoxTokenID env variable that defines the Proxmox token id. | ||
ProxmoxTokenID string | ||
// ProxmoxSecret env variable that defines the Proxmox secret for the given token id. | ||
ProxmoxSecret string | ||
ProxmoxSecret string | ||
ProxmoxReserveOnlyRunning string | ||
|
||
proxmoxInsecure bool | ||
proxmoxRootCertFile string | ||
|
@@ -208,10 +210,12 @@ func setupProxmoxClient(ctx context.Context, logger logr.Logger) (capmox.Client, | |
} | ||
|
||
httpClient := &http.Client{Transport: tr} | ||
return goproxmox.NewAPIClient(ctx, logger, ProxmoxURL, | ||
proxmox.WithHTTPClient(httpClient), | ||
proxmox.WithAPIToken(ProxmoxTokenID, ProxmoxSecret), | ||
) | ||
opts := []any{proxmox.WithHTTPClient(httpClient), proxmox.WithAPIToken(ProxmoxTokenID, ProxmoxSecret)} | ||
if strings.ToLower(ProxmoxReserveOnlyRunning) == "true" { | ||
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. Use a |
||
opts = append(opts, goproxmox.WithCalcOnlyRunning()) | ||
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. Wouldn't be better to set it as field in the cluster/machine spec? 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. I agree. 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.
Maybe on Cluster scope, on mashine scope need special config structure for each physical node in physical proxmox Cluster. |
||
} | ||
|
||
return goproxmox.NewAPIClient(ctx, logger, ProxmoxURL, opts...) | ||
} | ||
|
||
func initFlagsAndEnv(fs *pflag.FlagSet) { | ||
|
@@ -220,6 +224,7 @@ func initFlagsAndEnv(fs *pflag.FlagSet) { | |
ProxmoxURL = env.GetString("PROXMOX_URL", "") | ||
ProxmoxTokenID = env.GetString("PROXMOX_TOKEN", "") | ||
ProxmoxSecret = env.GetString("PROXMOX_SECRET", "") | ||
ProxmoxReserveOnlyRunning = env.GetString("PROXMOX_RESERVE_ONLY_RUNNING", "") | ||
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. A new environment variable needs user documentation. |
||
|
||
fs.BoolVar(&proxmoxInsecure, "proxmox-insecure", | ||
env.GetString("PROXMOX_INSECURE", "true") == "true", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,29 +39,55 @@ var ErrVMIDFree = errors.New("VMID is free") | |
// APIClient Proxmox API client object. | ||
type APIClient struct { | ||
*proxmox.Client | ||
logger logr.Logger | ||
logger logr.Logger | ||
reserveOnlyRunning bool | ||
} | ||
|
||
type Option func(*APIClient) | ||
|
||
func WithCalcOnlyRunning() Option { | ||
return func(c *APIClient) { | ||
c.reserveOnlyRunning = true | ||
} | ||
} | ||
|
||
// NewAPIClient initializes a Proxmox API client. If the client is misconfigured, an error is returned. | ||
func NewAPIClient(ctx context.Context, logger logr.Logger, baseURL string, options ...proxmox.Option) (*APIClient, error) { | ||
// options can be goproxmox.Option or proxmox.Option | ||
func NewAPIClient(ctx context.Context, logger logr.Logger, baseURL string, options ...any) (*APIClient, error) { | ||
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. Definitely not |
||
var pOpts []proxmox.Option | ||
var apiOpts []Option | ||
for _, option := range options { | ||
switch o := option.(type) { | ||
case proxmox.Option: | ||
pOpts = append(pOpts, o) | ||
case Option: | ||
apiOpts = append(apiOpts, o) | ||
default: | ||
return nil, fmt.Errorf("invalid option %T", option) | ||
} | ||
} | ||
Comment on lines
+57
to
+68
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. This should've been a type. 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. Can you get example |
||
proxmoxAPIURL, err := url.JoinPath(baseURL, "api2", "json") | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid proxmox base URL %q: %w", baseURL, err) | ||
} | ||
|
||
options = append(options, proxmox.WithLogger(capmox.Logger{})) | ||
upstreamClient := proxmox.NewClient(proxmoxAPIURL, options...) | ||
pOpts = append(pOpts, proxmox.WithLogger(capmox.Logger{})) | ||
upstreamClient := proxmox.NewClient(proxmoxAPIURL, pOpts...) | ||
version, err := upstreamClient.Version(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to initialize proxmox api client: %w", err) | ||
} | ||
logger.Info("Proxmox client initialized") | ||
logger.Info("Proxmox server", "version", version.Release) | ||
|
||
return &APIClient{ | ||
ac := &APIClient{ | ||
Client: upstreamClient, | ||
logger: logger, | ||
}, nil | ||
} | ||
for _, o := range apiOpts { | ||
o(ac) | ||
} | ||
return ac, nil | ||
Comment on lines
-61
to
+90
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. Ditto. |
||
} | ||
|
||
// CloneVM clones a VM based on templateID and VMCloneRequest. | ||
|
@@ -275,6 +301,9 @@ func (c *APIClient) GetReservableMemoryBytes(ctx context.Context, nodeName strin | |
if vm.Template { | ||
continue | ||
} | ||
if !vm.IsRunning() && c.reserveOnlyRunning { | ||
continue | ||
} | ||
if reservableMemory < vm.MaxMem { | ||
reservableMemory = 0 | ||
} else { | ||
|
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.
The name of this variable isn't particularly descriptive and it's missing a comment.
The type should be
bool
.