diff --git a/db/migrations/013_usage_nodeselector.down.sql b/db/migrations/013_usage_nodeselector.down.sql new file mode 100644 index 0000000..d374ea5 --- /dev/null +++ b/db/migrations/013_usage_nodeselector.down.sql @@ -0,0 +1,5 @@ +BEGIN; + +ALTER TABLE ocp_shared_cluster_configurations DROP COLUMN usage_node_selector; + +COMMIT; diff --git a/db/migrations/013_usage_nodeselector.up.sql b/db/migrations/013_usage_nodeselector.up.sql new file mode 100644 index 0000000..b900524 --- /dev/null +++ b/db/migrations/013_usage_nodeselector.up.sql @@ -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; diff --git a/docs/api-reference/swagger.yaml b/docs/api-reference/swagger.yaml index ced4ef2..6f6ce46 100644 --- a/docs/api-reference/swagger.yaml +++ b/docs/api-reference/swagger.yaml @@ -1641,6 +1641,8 @@ paths: max_cpu_usage_percentage: type: number format: float + usage_node_selector: + type: string token: type: string annotations: @@ -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: '...' @@ -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 diff --git a/internal/api/v1/v1.go b/internal/api/v1/v1.go index c4895c5..cf6594b 100644 --- a/internal/api/v1/v1.go +++ b/internal/api/v1/v1.go @@ -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"` } diff --git a/internal/models/ocp_sandbox.go b/internal/models/ocp_sandbox.go index 24805cb..3d6071e 100644 --- a/internal/models/ocp_sandbox.go +++ b/internal/models/ocp_sandbox.go @@ -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 @@ -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", @@ -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, @@ -286,6 +289,7 @@ func (p *OcpSharedClusterConfiguration) Save() error { p.AdditionalVars, p.MaxMemoryUsagePercentage, p.MaxCpuUsagePercentage, + p.UsageNodeSelector, p.DefaultSandboxQuota, p.StrictDefaultSandboxQuota, p.QuotaRequired, @@ -308,7 +312,7 @@ 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, @@ -316,11 +320,12 @@ func (p *OcpSharedClusterConfiguration) Update() error { 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, @@ -334,6 +339,7 @@ func (p *OcpSharedClusterConfiguration) Update() error { p.ID, p.MaxMemoryUsagePercentage, p.MaxCpuUsagePercentage, + p.UsageNodeSelector, p.DefaultSandboxQuota, p.StrictDefaultSandboxQuota, p.QuotaRequired, @@ -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, @@ -426,6 +433,7 @@ func (p *OcpSandboxProvider) GetOcpSharedClusterConfigurationByName(name string) &cluster.AdditionalVars, &cluster.MaxMemoryUsagePercentage, &cluster.MaxCpuUsagePercentage, + &cluster.UsageNodeSelector, &cluster.DefaultSandboxQuota, &cluster.StrictDefaultSandboxQuota, &cluster.QuotaRequired, @@ -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, @@ -493,6 +502,7 @@ func (p *OcpSandboxProvider) GetOcpSharedClusterConfigurations() (OcpSharedClust &cluster.AdditionalVars, &cluster.MaxMemoryUsagePercentage, &cluster.MaxCpuUsagePercentage, + &cluster.UsageNodeSelector, &cluster.DefaultSandboxQuota, &cluster.StrictDefaultSandboxQuota, &cluster.QuotaRequired, @@ -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 @@ -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")