From bdeedb4018b1b04d49f61775123a3889b4c1c8ed Mon Sep 17 00:00:00 2001 From: Matt Hook Date: Fri, 16 Feb 2024 08:20:24 +1300 Subject: [PATCH] fix(namespace): fix default namespace quota [EE-6700] (#11185) --- .../migrator/migrate_dbversion110.go | 26 ++++++++++++++++++ api/datastore/migrator/migrator.go | 1 + .../test_assets/handler_test/portainer.db | 0 api/kubernetes.go | 9 ++++--- api/kubernetes/cli/namespace.go | 27 +++++++++---------- .../ResourceQuotaFormSection.tsx | 6 ++--- 6 files changed, 48 insertions(+), 21 deletions(-) delete mode 100644 api/http/handler/backup/test_assets/handler_test/portainer.db diff --git a/api/datastore/migrator/migrate_dbversion110.go b/api/datastore/migrator/migrate_dbversion110.go index 88f0a4f19..13a797b99 100644 --- a/api/datastore/migrator/migrate_dbversion110.go +++ b/api/datastore/migrator/migrate_dbversion110.go @@ -23,3 +23,29 @@ func (migrator *Migrator) updateAppTemplatesVersionForDB110() error { return migrator.settingsService.UpdateSettings(settings) } + +// In PortainerCE the resource overcommit option should always be true across all endpoints +func (migrator *Migrator) updateResourceOverCommitToDB110() error { + log.Info().Msg("updating resource overcommit setting to true") + + endpoints, err := migrator.endpointService.Endpoints() + if err != nil { + return err + } + + for _, endpoint := range endpoints { + if endpoint.Type == portainer.KubernetesLocalEnvironment || + endpoint.Type == portainer.AgentOnKubernetesEnvironment || + endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment { + + endpoint.Kubernetes.Configuration.EnableResourceOverCommit = true + + err = migrator.endpointService.UpdateEndpoint(endpoint.ID, &endpoint) + if err != nil { + return err + } + } + } + + return nil +} diff --git a/api/datastore/migrator/migrator.go b/api/datastore/migrator/migrator.go index da7bb7ca2..beeb73078 100644 --- a/api/datastore/migrator/migrator.go +++ b/api/datastore/migrator/migrator.go @@ -230,6 +230,7 @@ func (m *Migrator) initMigrations() { ) m.addMigrations("2.20", m.updateAppTemplatesVersionForDB110, + m.updateResourceOverCommitToDB110, ) // Add new migrations below... diff --git a/api/http/handler/backup/test_assets/handler_test/portainer.db b/api/http/handler/backup/test_assets/handler_test/portainer.db deleted file mode 100644 index e69de29bb..000000000 diff --git a/api/kubernetes.go b/api/kubernetes.go index e95b7ffa2..b6bf967bd 100644 --- a/api/kubernetes.go +++ b/api/kubernetes.go @@ -3,10 +3,11 @@ package portainer func KubernetesDefault() KubernetesData { return KubernetesData{ Configuration: KubernetesConfiguration{ - UseLoadBalancer: false, - UseServerMetrics: false, - StorageClasses: []KubernetesStorageClassConfig{}, - IngressClasses: []KubernetesIngressClassConfig{}, + UseLoadBalancer: false, + UseServerMetrics: false, + EnableResourceOverCommit: true, + StorageClasses: []KubernetesStorageClassConfig{}, + IngressClasses: []KubernetesIngressClassConfig{}, }, Snapshots: []KubernetesSnapshot{}, } diff --git a/api/kubernetes/cli/namespace.go b/api/kubernetes/cli/namespace.go index d4157c1b6..b78276ca5 100644 --- a/api/kubernetes/cli/namespace.go +++ b/api/kubernetes/cli/namespace.go @@ -73,31 +73,30 @@ func (kcl *KubeClient) CreateNamespace(info models.K8sNamespaceDetails) error { ns.Annotations = info.Annotations ns.Labels = portainerLabels - resourceQuota := &v1.ResourceQuota{ - ObjectMeta: metav1.ObjectMeta{ - Name: "portainer-rq-" + info.Name, - Namespace: info.Name, - Labels: portainerLabels, - }, - Spec: v1.ResourceQuotaSpec{ - Hard: v1.ResourceList{}, - }, - } - _, err := kcl.cli.CoreV1().Namespaces().Create(context.Background(), &ns, metav1.CreateOptions{}) if err != nil { log.Error(). Err(err). Str("Namespace", info.Name). - Interface("ResourceQuota", resourceQuota). - Msg("Failed to create the namespace due to a resource quota issue.") + Msg("Failed to create the namespace") return err } - if info.ResourceQuota != nil { + if info.ResourceQuota != nil && info.ResourceQuota.Enabled { log.Info().Msgf("Creating resource quota for namespace %s", info.Name) log.Debug().Msgf("Creating resource quota with details: %+v", info.ResourceQuota) + resourceQuota := &v1.ResourceQuota{ + ObjectMeta: metav1.ObjectMeta{ + Name: "portainer-rq-" + info.Name, + Namespace: info.Name, + Labels: portainerLabels, + }, + Spec: v1.ResourceQuotaSpec{ + Hard: v1.ResourceList{}, + }, + } + if info.ResourceQuota.Enabled { memory := resource.MustParse(info.ResourceQuota.Memory) cpu := resource.MustParse(info.ResourceQuota.CPU) diff --git a/app/react/kubernetes/namespaces/components/ResourceQuotaFormSection/ResourceQuotaFormSection.tsx b/app/react/kubernetes/namespaces/components/ResourceQuotaFormSection/ResourceQuotaFormSection.tsx index f4d5bf3a6..6b0a70075 100644 --- a/app/react/kubernetes/namespaces/components/ResourceQuotaFormSection/ResourceQuotaFormSection.tsx +++ b/app/react/kubernetes/namespaces/components/ResourceQuotaFormSection/ResourceQuotaFormSection.tsx @@ -44,15 +44,15 @@ export function ResourceQuotaFormSection({ onChange({ ...values, enabled })} /> - {(values.enabled || !!enableResourceOverCommit) && ( + {(values.enabled || !enableResourceOverCommit) && (
Resource Limits