1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-20 05:49:40 +02:00
portainer/app/kubernetes/services/namespaceService.js
Chaim Lev-Ari 1830a80a61
feat(k8s/resource-pool): add the ability to mark/unmark resource pool as system (#5360)
* feat(k8s/resource-pool): add the ability to mark/unmark resource pool as system

fix(kube/ns): check label to see if namespace is system

refactor(k8s/namespaces): rename variables

feat(kubernetes): toggle system state in the server (#5361)

fix(app/resource-pool): UI fixes

feat(app/resource-pool): add confirmation modal when unamrking system namespace

* refactor(app): review changes

* feat(app/namespaces): introduce store to retrieve namespace system status without changing all the kubernetes models

refactor(app/namespaces): remove unused code first introduced for system tagging

fix(app/namespaces): cache namespaces to retrieve system status regardless of namespace reference format

refactor(app): migrate namespace store from helper to a separate singleton

refactor(app): remove KubernetesNamespaceHelper from DI cycle

* refactor(app): normalize usage of KubernetesNamespaceHelper functions

* refactor(app/k8s): change namespace store to functions instead of class

Co-authored-by: LP B <xAt0mZ@users.noreply.github.com>
2021-08-26 16:00:59 +02:00

101 lines
3.1 KiB
JavaScript

import _ from 'lodash-es';
import angular from 'angular';
import PortainerError from 'Portainer/error';
import { KubernetesCommonParams } from 'Kubernetes/models/common/params';
import KubernetesNamespaceConverter from 'Kubernetes/converters/namespace';
import { updateNamespaces } from 'Kubernetes/store/namespace';
import $allSettled from 'Portainer/services/allSettled';
class KubernetesNamespaceService {
/* @ngInject */
constructor($async, KubernetesNamespaces) {
this.$async = $async;
this.KubernetesNamespaces = KubernetesNamespaces;
this.getAsync = this.getAsync.bind(this);
this.getAllAsync = this.getAllAsync.bind(this);
this.createAsync = this.createAsync.bind(this);
this.deleteAsync = this.deleteAsync.bind(this);
}
/**
* GET
*/
async getAsync(name) {
try {
const params = new KubernetesCommonParams();
params.id = name;
await this.KubernetesNamespaces().status(params).$promise;
const [raw, yaml] = await Promise.all([this.KubernetesNamespaces().get(params).$promise, this.KubernetesNamespaces().getYaml(params).$promise]);
const ns = KubernetesNamespaceConverter.apiToNamespace(raw, yaml);
updateNamespaces([ns]);
return ns;
} catch (err) {
throw new PortainerError('Unable to retrieve namespace', err);
}
}
async getAllAsync() {
try {
const data = await this.KubernetesNamespaces().get().$promise;
const promises = _.map(data.items, (item) => this.KubernetesNamespaces().status({ id: item.metadata.name }).$promise);
const namespaces = await $allSettled(promises);
const visibleNamespaces = _.map(namespaces.fulfilled, (item) => {
if (item.status.phase !== 'Terminating') {
return KubernetesNamespaceConverter.apiToNamespace(item);
}
});
const res = _.without(visibleNamespaces, undefined);
updateNamespaces(res);
return res;
} catch (err) {
throw new PortainerError('Unable to retrieve namespaces', err);
}
}
get(name) {
if (name) {
return this.$async(this.getAsync, name);
}
return this.$async(this.getAllAsync);
}
/**
* CREATE
*/
async createAsync(namespace) {
try {
const payload = KubernetesNamespaceConverter.createPayload(namespace);
const params = {};
const data = await this.KubernetesNamespaces().create(params, payload).$promise;
return data;
} catch (err) {
throw new PortainerError('Unable to create namespace', err);
}
}
create(namespace) {
return this.$async(this.createAsync, namespace);
}
/**
* DELETE
*/
async deleteAsync(namespace) {
try {
const params = new KubernetesCommonParams();
params.id = namespace.Name;
await this.KubernetesNamespaces().delete(params).$promise;
} catch (err) {
throw new PortainerError('Unable to delete namespace', err);
}
}
delete(namespace) {
return this.$async(this.deleteAsync, namespace);
}
}
export default KubernetesNamespaceService;
angular.module('portainer.kubernetes').service('KubernetesNamespaceService', KubernetesNamespaceService);