Skip to content

Commit

Permalink
Adding UsageNodeSelector (usage_node_selector) to calculate usage (#92)
Browse files Browse the repository at this point in the history
* Adding UsageNodeSelector (usage_node_selector) to calculate usage

* Update ocp_sandbox.go

* Fix 013_usage_nodeselector.up.sql

* Fix format

* Fix format
  • Loading branch information
agonzalezrh authored Nov 5, 2024
1 parent 24993fc commit 485d673
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
5 changes: 5 additions & 0 deletions db/migrations/013_usage_nodeselector.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN;

ALTER TABLE ocp_shared_cluster_configurations DROP COLUMN usage_node_selector;

COMMIT;
6 changes: 6 additions & 0 deletions db/migrations/013_usage_nodeselector.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BEGIN;
-- Add usage_node_selector column to the ocp_shared_cluster_configurations table of type string
-- default value "node-role.kubernetes.io/worker="
ALTER TABLE ocp_shared_cluster_configurations ADD COLUMN usage_node_selector VARCHAR(255) DEFAULT 'node-role.kubernetes.io/worker=';

COMMIT;
8 changes: 8 additions & 0 deletions docs/api-reference/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,8 @@ paths:
max_cpu_usage_percentage:
type: number
format: float
usage_node_selector:
type: string
token:
type: string
annotations:
Expand Down Expand Up @@ -1673,6 +1675,7 @@ paths:
skip_quota: false
max_memory_usage_percentage: 70
max_cpu_usage_percentage: 80
usage_node_selector: 'node-role.kubernetes.io/compute='
token: '...'
additional_vars:
fordeployer: '...'
Expand Down Expand Up @@ -2369,6 +2372,11 @@ components:
description: The maximum memory usage percentage for a cluster to be considered healthy
example: 80
default: 90
usage_node_selector:
type: string
description: Specify the nodeSelector value to calculate the current usage percentage
example: "node-role.kubernetes.io/compute="
default: "node-role.kubernetes.io/worker="
max_cpu_usage_percentage:
type: integer
description: The maximum CPU usage percentage for a cluster to be considered healthy
Expand Down
1 change: 1 addition & 0 deletions internal/api/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ type UpdateOcpSharedConfigurationRequest struct {
AdditionalVars map[string]any `json:"additional_vars,omitempty"`
MaxMemoryUsagePercentage *float64 `json:"max_memory_usage_percentage,omitempty"`
MaxCpuUsagePercentage *float64 `json:"max_cpu_usage_percentage,omitempty"`
UsageSelector *string `json:"usage_node_selector,omitempty"`
LimitRange *v1.LimitRange `json:"limit_range,omitempty"`
}

Expand Down
33 changes: 21 additions & 12 deletions internal/models/ocp_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type OcpSharedClusterConfiguration struct {
AdditionalVars map[string]any `json:"additional_vars,omitempty"`
MaxMemoryUsagePercentage float64 `json:"max_memory_usage_percentage"`
MaxCpuUsagePercentage float64 `json:"max_cpu_usage_percentage"`
UsageNodeSelector string `json:"usage_node_selector"`
DbPool *pgxpool.Pool `json:"-"`
VaultSecret string `json:"-"`
// For any new project (openshift namespace) created by the sandbox API
Expand Down Expand Up @@ -129,6 +130,7 @@ func MakeOcpSharedClusterConfiguration() *OcpSharedClusterConfiguration {
p.Valid = true
p.MaxMemoryUsagePercentage = 80
p.MaxCpuUsagePercentage = 100
p.UsageNodeSelector = "node-role.kubernetes.io/worker="
p.DefaultSandboxQuota = &v1.ResourceQuota{
ObjectMeta: metav1.ObjectMeta{
Name: "sandbox-quota",
Expand Down Expand Up @@ -268,12 +270,13 @@ func (p *OcpSharedClusterConfiguration) Save() error {
additional_vars,
max_memory_usage_percentage,
max_cpu_usage_percentage,
usage_node_selector,
default_sandbox_quota,
strict_default_sandbox_quota,
quota_required,
skip_quota,
limit_range)
VALUES ($1, $2, $3, pgp_sym_encrypt($4::text, $5), pgp_sym_encrypt($6::text, $5), $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
VALUES ($1, $2, $3, pgp_sym_encrypt($4::text, $5), pgp_sym_encrypt($6::text, $5), $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
RETURNING id`,
p.Name,
p.ApiUrl,
Expand All @@ -286,6 +289,7 @@ func (p *OcpSharedClusterConfiguration) Save() error {
p.AdditionalVars,
p.MaxMemoryUsagePercentage,
p.MaxCpuUsagePercentage,
p.UsageNodeSelector,
p.DefaultSandboxQuota,
p.StrictDefaultSandboxQuota,
p.QuotaRequired,
Expand All @@ -308,19 +312,20 @@ func (p *OcpSharedClusterConfiguration) Update() error {
`UPDATE ocp_shared_cluster_configurations
SET name = $1,
api_url = $2,
ingress_domain = $3,
ingress_domain = $3,
kubeconfig = pgp_sym_encrypt($4::text, $5),
token = pgp_sym_encrypt($6::text, $5),
annotations = $7,
valid = $8,
additional_vars = $9,
max_memory_usage_percentage = $11,
max_cpu_usage_percentage = $12,
default_sandbox_quota = $13,
strict_default_sandbox_quota = $14,
quota_required = $15,
skip_quota = $16,
limit_range = $17
usage_node_selector = $13,
default_sandbox_quota = $14,
strict_default_sandbox_quota = $15,
quota_required = $16,
skip_quota = $17,
limit_range = $18
WHERE id = $10`,
p.Name,
p.ApiUrl,
Expand All @@ -334,6 +339,7 @@ func (p *OcpSharedClusterConfiguration) Update() error {
p.ID,
p.MaxMemoryUsagePercentage,
p.MaxCpuUsagePercentage,
p.UsageNodeSelector,
p.DefaultSandboxQuota,
p.StrictDefaultSandboxQuota,
p.QuotaRequired,
Expand Down Expand Up @@ -402,6 +408,7 @@ func (p *OcpSandboxProvider) GetOcpSharedClusterConfigurationByName(name string)
additional_vars,
max_memory_usage_percentage,
max_cpu_usage_percentage,
usage_node_selector,
default_sandbox_quota,
strict_default_sandbox_quota,
quota_required,
Expand All @@ -426,6 +433,7 @@ func (p *OcpSandboxProvider) GetOcpSharedClusterConfigurationByName(name string)
&cluster.AdditionalVars,
&cluster.MaxMemoryUsagePercentage,
&cluster.MaxCpuUsagePercentage,
&cluster.UsageNodeSelector,
&cluster.DefaultSandboxQuota,
&cluster.StrictDefaultSandboxQuota,
&cluster.QuotaRequired,
Expand Down Expand Up @@ -460,6 +468,7 @@ func (p *OcpSandboxProvider) GetOcpSharedClusterConfigurations() (OcpSharedClust
additional_vars,
max_memory_usage_percentage,
max_cpu_usage_percentage,
usage_node_selector,
default_sandbox_quota,
strict_default_sandbox_quota,
quota_required,
Expand Down Expand Up @@ -493,6 +502,7 @@ func (p *OcpSandboxProvider) GetOcpSharedClusterConfigurations() (OcpSharedClust
&cluster.AdditionalVars,
&cluster.MaxMemoryUsagePercentage,
&cluster.MaxCpuUsagePercentage,
&cluster.UsageNodeSelector,
&cluster.DefaultSandboxQuota,
&cluster.StrictDefaultSandboxQuota,
&cluster.QuotaRequired,
Expand Down Expand Up @@ -841,20 +851,19 @@ func (a *OcpSharedClusterConfiguration) CreateRestConfig() (*rest.Config, error)
return clientcmd.RESTConfigFromKubeConfig([]byte(a.Kubeconfig))
}


func (a *OcpSharedClusterConfiguration) TestConnection() (error) {
func (a *OcpSharedClusterConfiguration) TestConnection() error {
// Get the OCP shared cluster configuration from the database
config, err := a.CreateRestConfig()
if err != nil {
log.Logger.Error("Error creating OCP config", "error", err)
return errors.New("Error creating OCP config: " + err.Error())
return errors.New("Error creating OCP config: " + err.Error())
}

// Create an OpenShift client
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Logger.Error("Error creating OCP client", "error", err)
return errors.New("Error creating OCP client: " + err.Error())
return errors.New("Error creating OCP client: " + err.Error())
}

// Check if we can access to "default" namespace
Expand Down Expand Up @@ -985,7 +994,7 @@ func (a *OcpSandboxProvider) Request(serviceUuid string, cloud_selector map[stri
continue providerLoop
}

nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: "node-role.kubernetes.io/worker="})
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{LabelSelector: cluster.UsageNodeSelector})
if err != nil {
log.Logger.Error("Error listing OCP nodes", "error", err)
rnew.SetStatus("error")
Expand Down

0 comments on commit 485d673

Please sign in to comment.