2020-07-05 23:21:03 +00:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
import angular from 'angular';
|
|
|
|
import PortainerError from 'Portainer/error';
|
|
|
|
|
|
|
|
import { KubernetesApplicationTypes } from 'Kubernetes/models/application/models';
|
|
|
|
import KubernetesApplicationHelper from 'Kubernetes/helpers/application';
|
|
|
|
import KubernetesApplicationRollbackHelper from 'Kubernetes/helpers/application/rollback';
|
|
|
|
import KubernetesApplicationConverter from 'Kubernetes/converters/application';
|
|
|
|
import { KubernetesDeployment } from 'Kubernetes/models/deployment/models';
|
|
|
|
import { KubernetesStatefulSet } from 'Kubernetes/models/stateful-set/models';
|
|
|
|
import { KubernetesDaemonSet } from 'Kubernetes/models/daemon-set/models';
|
|
|
|
import { KubernetesApplication } from 'Kubernetes/models/application/models';
|
|
|
|
import KubernetesServiceHelper from 'Kubernetes/helpers/serviceHelper';
|
|
|
|
import { KubernetesHorizontalPodAutoScalerHelper } from 'Kubernetes/horizontal-pod-auto-scaler/helper';
|
2020-08-04 22:08:11 +00:00
|
|
|
import { KubernetesHorizontalPodAutoScalerConverter } from 'Kubernetes/horizontal-pod-auto-scaler/converter';
|
2020-07-05 23:21:03 +00:00
|
|
|
|
|
|
|
class KubernetesApplicationService {
|
|
|
|
/* @ngInject */
|
|
|
|
constructor(
|
|
|
|
$async,
|
|
|
|
Authentication,
|
|
|
|
KubernetesDeploymentService,
|
|
|
|
KubernetesDaemonSetService,
|
|
|
|
KubernetesStatefulSetService,
|
|
|
|
KubernetesServiceService,
|
|
|
|
KubernetesSecretService,
|
|
|
|
KubernetesPersistentVolumeClaimService,
|
|
|
|
KubernetesNamespaceService,
|
|
|
|
KubernetesPodService,
|
|
|
|
KubernetesHistoryService,
|
2020-07-14 20:45:19 +00:00
|
|
|
KubernetesHorizontalPodAutoScalerService,
|
|
|
|
KubernetesIngressService
|
2020-07-05 23:21:03 +00:00
|
|
|
) {
|
|
|
|
this.$async = $async;
|
|
|
|
this.Authentication = Authentication;
|
|
|
|
this.KubernetesDeploymentService = KubernetesDeploymentService;
|
|
|
|
this.KubernetesDaemonSetService = KubernetesDaemonSetService;
|
|
|
|
this.KubernetesStatefulSetService = KubernetesStatefulSetService;
|
|
|
|
this.KubernetesServiceService = KubernetesServiceService;
|
|
|
|
this.KubernetesSecretService = KubernetesSecretService;
|
|
|
|
this.KubernetesPersistentVolumeClaimService = KubernetesPersistentVolumeClaimService;
|
|
|
|
this.KubernetesNamespaceService = KubernetesNamespaceService;
|
|
|
|
this.KubernetesPodService = KubernetesPodService;
|
|
|
|
this.KubernetesHistoryService = KubernetesHistoryService;
|
|
|
|
this.KubernetesHorizontalPodAutoScalerService = KubernetesHorizontalPodAutoScalerService;
|
2020-07-14 20:45:19 +00:00
|
|
|
this.KubernetesIngressService = KubernetesIngressService;
|
2020-07-05 23:21:03 +00:00
|
|
|
|
|
|
|
this.getAsync = this.getAsync.bind(this);
|
|
|
|
this.getAllAsync = this.getAllAsync.bind(this);
|
|
|
|
this.createAsync = this.createAsync.bind(this);
|
|
|
|
this.patchAsync = this.patchAsync.bind(this);
|
|
|
|
this.patchPartialAsync = this.patchPartialAsync.bind(this);
|
|
|
|
this.rollbackAsync = this.rollbackAsync.bind(this);
|
|
|
|
this.deleteAsync = this.deleteAsync.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UTILS
|
|
|
|
*/
|
|
|
|
_getApplicationApiService(app) {
|
|
|
|
let apiService;
|
|
|
|
if (app instanceof KubernetesDeployment || (app instanceof KubernetesApplication && app.ApplicationType === KubernetesApplicationTypes.DEPLOYMENT)) {
|
|
|
|
apiService = this.KubernetesDeploymentService;
|
|
|
|
} else if (app instanceof KubernetesDaemonSet || (app instanceof KubernetesApplication && app.ApplicationType === KubernetesApplicationTypes.DAEMONSET)) {
|
|
|
|
apiService = this.KubernetesDaemonSetService;
|
|
|
|
} else if (app instanceof KubernetesStatefulSet || (app instanceof KubernetesApplication && app.ApplicationType === KubernetesApplicationTypes.STATEFULSET)) {
|
|
|
|
apiService = this.KubernetesStatefulSetService;
|
|
|
|
} else {
|
|
|
|
throw new PortainerError('Unable to determine which association to use');
|
|
|
|
}
|
|
|
|
return apiService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET
|
|
|
|
*/
|
|
|
|
async getAsync(namespace, name) {
|
|
|
|
try {
|
2020-07-14 20:45:19 +00:00
|
|
|
const [deployment, daemonSet, statefulSet, pods, autoScalers, ingresses] = await Promise.allSettled([
|
2020-07-05 23:21:03 +00:00
|
|
|
this.KubernetesDeploymentService.get(namespace, name),
|
|
|
|
this.KubernetesDaemonSetService.get(namespace, name),
|
|
|
|
this.KubernetesStatefulSetService.get(namespace, name),
|
|
|
|
this.KubernetesPodService.get(namespace),
|
|
|
|
this.KubernetesHorizontalPodAutoScalerService.get(namespace),
|
2020-07-14 20:45:19 +00:00
|
|
|
this.KubernetesIngressService.get(namespace),
|
2020-07-05 23:21:03 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
let rootItem;
|
2020-07-14 20:45:19 +00:00
|
|
|
let converterFunc;
|
2020-07-05 23:21:03 +00:00
|
|
|
if (deployment.status === 'fulfilled') {
|
|
|
|
rootItem = deployment;
|
2020-07-14 20:45:19 +00:00
|
|
|
converterFunc = KubernetesApplicationConverter.apiDeploymentToApplication;
|
2020-07-05 23:21:03 +00:00
|
|
|
} else if (daemonSet.status === 'fulfilled') {
|
|
|
|
rootItem = daemonSet;
|
2020-07-14 20:45:19 +00:00
|
|
|
converterFunc = KubernetesApplicationConverter.apiDaemonSetToApplication;
|
2020-07-05 23:21:03 +00:00
|
|
|
} else if (statefulSet.status === 'fulfilled') {
|
|
|
|
rootItem = statefulSet;
|
2020-07-14 20:45:19 +00:00
|
|
|
converterFunc = KubernetesApplicationConverter.apiStatefulSetToapplication;
|
2020-07-05 23:21:03 +00:00
|
|
|
} else {
|
|
|
|
throw new PortainerError('Unable to determine which association to use');
|
|
|
|
}
|
|
|
|
|
|
|
|
const services = await this.KubernetesServiceService.get(namespace);
|
|
|
|
const boundService = KubernetesServiceHelper.findApplicationBoundService(services, rootItem.value.Raw);
|
|
|
|
const service = boundService ? await this.KubernetesServiceService.get(namespace, boundService.metadata.name) : {};
|
|
|
|
|
2020-07-14 20:45:19 +00:00
|
|
|
const application = converterFunc(rootItem.value.Raw, service.Raw, ingresses.value);
|
2020-07-05 23:21:03 +00:00
|
|
|
application.Yaml = rootItem.value.Yaml;
|
|
|
|
application.Raw = rootItem.value.Raw;
|
|
|
|
application.Pods = KubernetesApplicationHelper.associatePodsAndApplication(pods.value, application.Raw);
|
|
|
|
|
|
|
|
const boundScaler = KubernetesHorizontalPodAutoScalerHelper.findApplicationBoundScaler(autoScalers.value, application);
|
|
|
|
const scaler = boundScaler ? await this.KubernetesHorizontalPodAutoScalerService.get(namespace, boundScaler.Name) : undefined;
|
|
|
|
application.AutoScaler = scaler;
|
|
|
|
|
|
|
|
await this.KubernetesHistoryService.get(application);
|
|
|
|
|
|
|
|
if (service.Yaml) {
|
|
|
|
application.Yaml += '---\n' + service.Yaml;
|
|
|
|
}
|
|
|
|
if (scaler && scaler.Yaml) {
|
|
|
|
application.Yaml += '---\n' + scaler.Yaml;
|
|
|
|
}
|
2020-07-14 20:45:19 +00:00
|
|
|
// TODO: refactor
|
|
|
|
// append ingress yaml ?
|
2020-07-05 23:21:03 +00:00
|
|
|
return application;
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAllAsync(namespace) {
|
|
|
|
try {
|
|
|
|
const namespaces = namespace ? [namespace] : _.map(await this.KubernetesNamespaceService.get(), 'Name');
|
2020-07-14 20:45:19 +00:00
|
|
|
|
|
|
|
const convertToApplication = (item, converterFunc, services, pods, ingresses) => {
|
|
|
|
const service = KubernetesServiceHelper.findApplicationBoundService(services, item);
|
|
|
|
const application = converterFunc(item, service, ingresses);
|
|
|
|
application.Pods = KubernetesApplicationHelper.associatePodsAndApplication(pods, item);
|
|
|
|
return application;
|
|
|
|
};
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
const res = await Promise.all(
|
|
|
|
_.map(namespaces, async (ns) => {
|
2020-08-04 22:08:11 +00:00
|
|
|
const [deployments, daemonSets, statefulSets, services, pods, ingresses, autoScalers] = await Promise.all([
|
2020-07-05 23:21:03 +00:00
|
|
|
this.KubernetesDeploymentService.get(ns),
|
|
|
|
this.KubernetesDaemonSetService.get(ns),
|
|
|
|
this.KubernetesStatefulSetService.get(ns),
|
|
|
|
this.KubernetesServiceService.get(ns),
|
|
|
|
this.KubernetesPodService.get(ns),
|
2020-07-14 20:45:19 +00:00
|
|
|
this.KubernetesIngressService.get(ns),
|
2020-08-04 22:08:11 +00:00
|
|
|
this.KubernetesHorizontalPodAutoScalerService.get(ns),
|
2020-07-05 23:21:03 +00:00
|
|
|
]);
|
2020-07-14 20:45:19 +00:00
|
|
|
|
|
|
|
const deploymentApplications = _.map(deployments, (item) =>
|
|
|
|
convertToApplication(item, KubernetesApplicationConverter.apiDeploymentToApplication, services, pods, ingresses)
|
|
|
|
);
|
|
|
|
const daemonSetApplications = _.map(daemonSets, (item) =>
|
|
|
|
convertToApplication(item, KubernetesApplicationConverter.apiDaemonSetToApplication, services, pods, ingresses)
|
|
|
|
);
|
|
|
|
const statefulSetApplications = _.map(statefulSets, (item) =>
|
|
|
|
convertToApplication(item, KubernetesApplicationConverter.apiStatefulSetToapplication, services, pods, ingresses)
|
|
|
|
);
|
|
|
|
|
2020-08-04 22:08:11 +00:00
|
|
|
const applications = _.concat(deploymentApplications, daemonSetApplications, statefulSetApplications);
|
|
|
|
await Promise.all(
|
|
|
|
_.forEach(applications, async (application) => {
|
|
|
|
const boundScaler = KubernetesHorizontalPodAutoScalerHelper.findApplicationBoundScaler(autoScalers, application);
|
|
|
|
const scaler = boundScaler ? await this.KubernetesHorizontalPodAutoScalerService.get(ns, boundScaler.Name) : undefined;
|
|
|
|
application.AutoScaler = scaler;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return applications;
|
2020-07-05 23:21:03 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
return _.flatten(res);
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(namespace, name) {
|
|
|
|
if (name) {
|
|
|
|
return this.$async(this.getAsync, namespace, name);
|
|
|
|
}
|
|
|
|
return this.$async(this.getAllAsync, namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CREATE
|
|
|
|
*/
|
|
|
|
// TODO: review
|
|
|
|
// resource creation flow
|
|
|
|
// should we keep formValues > Resource_1 || Resource_2
|
|
|
|
// or should we switch to formValues > Composite > Resource_1 || Resource_2
|
|
|
|
async createAsync(formValues) {
|
|
|
|
try {
|
|
|
|
let [app, headlessService, service, claims] = KubernetesApplicationConverter.applicationFormValuesToApplication(formValues);
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
await this.KubernetesServiceService.create(service);
|
|
|
|
}
|
|
|
|
|
|
|
|
const apiService = this._getApplicationApiService(app);
|
|
|
|
|
|
|
|
if (app instanceof KubernetesStatefulSet) {
|
|
|
|
app.VolumeClaims = claims;
|
|
|
|
headlessService = await this.KubernetesServiceService.create(headlessService);
|
|
|
|
app.ServiceName = headlessService.metadata.name;
|
|
|
|
} else {
|
|
|
|
const claimPromises = _.map(claims, (item) => {
|
|
|
|
if (!item.PreviousName) {
|
|
|
|
return this.KubernetesPersistentVolumeClaimService.create(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await Promise.all(_.without(claimPromises, undefined));
|
|
|
|
}
|
|
|
|
|
2020-08-04 22:08:11 +00:00
|
|
|
if (formValues.AutoScaler.IsUsed) {
|
|
|
|
const kind = KubernetesHorizontalPodAutoScalerHelper.getApplicationTypeString(app);
|
|
|
|
const autoScaler = KubernetesHorizontalPodAutoScalerConverter.applicationFormValuesToModel(formValues, kind);
|
|
|
|
await this.KubernetesHorizontalPodAutoScalerService.create(autoScaler);
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
await apiService.create(app);
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
create(formValues) {
|
|
|
|
return this.$async(this.createAsync, formValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PATCH
|
|
|
|
*/
|
|
|
|
// this function accepts KubernetesApplicationFormValues as parameters
|
|
|
|
async patchAsync(oldFormValues, newFormValues) {
|
|
|
|
try {
|
|
|
|
const [oldApp, oldHeadlessService, oldService, oldClaims] = KubernetesApplicationConverter.applicationFormValuesToApplication(oldFormValues);
|
|
|
|
const [newApp, newHeadlessService, newService, newClaims] = KubernetesApplicationConverter.applicationFormValuesToApplication(newFormValues);
|
|
|
|
const oldApiService = this._getApplicationApiService(oldApp);
|
|
|
|
const newApiService = this._getApplicationApiService(newApp);
|
|
|
|
|
|
|
|
if (oldApiService !== newApiService) {
|
|
|
|
await this.delete(oldApp);
|
|
|
|
if (oldService) {
|
|
|
|
await this.KubernetesServiceService.delete(oldService);
|
|
|
|
}
|
|
|
|
return await this.create(newFormValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newApp instanceof KubernetesStatefulSet) {
|
|
|
|
await this.KubernetesServiceService.patch(oldHeadlessService, newHeadlessService);
|
|
|
|
} else {
|
|
|
|
const claimPromises = _.map(newClaims, (newClaim) => {
|
|
|
|
if (!newClaim.PreviousName) {
|
|
|
|
return this.KubernetesPersistentVolumeClaimService.create(newClaim);
|
|
|
|
}
|
|
|
|
const oldClaim = _.find(oldClaims, { Name: newClaim.PreviousName });
|
|
|
|
return this.KubernetesPersistentVolumeClaimService.patch(oldClaim, newClaim);
|
|
|
|
});
|
|
|
|
await Promise.all(claimPromises);
|
|
|
|
}
|
|
|
|
|
|
|
|
await newApiService.patch(oldApp, newApp);
|
|
|
|
|
|
|
|
if (oldService && newService) {
|
|
|
|
await this.KubernetesServiceService.patch(oldService, newService);
|
|
|
|
} else if (!oldService && newService) {
|
|
|
|
await this.KubernetesServiceService.create(newService);
|
|
|
|
} else if (oldService && !newService) {
|
|
|
|
await this.KubernetesServiceService.delete(oldService);
|
|
|
|
}
|
2020-08-04 22:08:11 +00:00
|
|
|
|
|
|
|
const newKind = KubernetesHorizontalPodAutoScalerHelper.getApplicationTypeString(newApp);
|
|
|
|
const newAutoScaler = KubernetesHorizontalPodAutoScalerConverter.applicationFormValuesToModel(newFormValues, newKind);
|
|
|
|
if (_.isEmpty(oldFormValues.AutoScaler)) {
|
|
|
|
await this.KubernetesHorizontalPodAutoScalerService.create(newAutoScaler);
|
|
|
|
} else {
|
|
|
|
const oldKind = KubernetesHorizontalPodAutoScalerHelper.getApplicationTypeString(oldApp);
|
|
|
|
const oldAutoScaler = KubernetesHorizontalPodAutoScalerConverter.applicationFormValuesToModel(oldFormValues, oldKind);
|
|
|
|
if (newFormValues.AutoScaler.IsUsed) {
|
|
|
|
await this.KubernetesHorizontalPodAutoScalerService.patch(oldAutoScaler, newAutoScaler);
|
|
|
|
} else {
|
|
|
|
await this.KubernetesHorizontalPodAutoScalerService.delete(oldAutoScaler);
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 23:21:03 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function accepts KubernetesApplication as parameters
|
|
|
|
async patchPartialAsync(oldApp, newApp) {
|
|
|
|
try {
|
|
|
|
const oldAppPayload = {
|
|
|
|
Name: oldApp.Name,
|
|
|
|
Namespace: oldApp.ResourcePool,
|
|
|
|
StackName: oldApp.StackName,
|
|
|
|
Note: oldApp.Note,
|
|
|
|
};
|
|
|
|
const newAppPayload = {
|
|
|
|
Name: newApp.Name,
|
|
|
|
Namespace: newApp.ResourcePool,
|
|
|
|
StackName: newApp.StackName,
|
|
|
|
Note: newApp.Note,
|
|
|
|
};
|
|
|
|
const apiService = this._getApplicationApiService(oldApp);
|
|
|
|
await apiService.patch(oldAppPayload, newAppPayload);
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// accept either formValues or applications as parameters
|
|
|
|
// depending on partial value
|
|
|
|
// true = KubernetesApplication
|
|
|
|
// false = KubernetesApplicationFormValues
|
|
|
|
patch(oldValues, newValues, partial = false) {
|
|
|
|
if (partial) {
|
|
|
|
return this.$async(this.patchPartialAsync, oldValues, newValues);
|
|
|
|
}
|
|
|
|
return this.$async(this.patchAsync, oldValues, newValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE
|
|
|
|
*/
|
|
|
|
async deleteAsync(application) {
|
|
|
|
try {
|
|
|
|
const payload = {
|
|
|
|
Namespace: application.ResourcePool || application.Namespace,
|
|
|
|
Name: application.Name,
|
|
|
|
};
|
|
|
|
const servicePayload = angular.copy(payload);
|
|
|
|
servicePayload.Name = application.Name;
|
|
|
|
|
|
|
|
const apiService = this._getApplicationApiService(application);
|
|
|
|
await apiService.delete(payload);
|
|
|
|
|
|
|
|
if (apiService === this.KubernetesStatefulSetService) {
|
|
|
|
const headlessServicePayload = angular.copy(payload);
|
|
|
|
headlessServicePayload.Name = application instanceof KubernetesStatefulSet ? application.ServiceName : application.HeadlessServiceName;
|
|
|
|
await this.KubernetesServiceService.delete(headlessServicePayload);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (application.ServiceType) {
|
|
|
|
await this.KubernetesServiceService.delete(servicePayload);
|
|
|
|
}
|
2020-08-04 22:08:11 +00:00
|
|
|
|
|
|
|
if (!_.isEmpty(application.AutoScaler)) {
|
|
|
|
await this.KubernetesHorizontalPodAutoScalerService.delete(application.AutoScaler);
|
|
|
|
}
|
2020-07-05 23:21:03 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(application) {
|
|
|
|
return this.$async(this.deleteAsync, application);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ROLLBACK
|
|
|
|
*/
|
|
|
|
async rollbackAsync(application, targetRevision) {
|
|
|
|
try {
|
|
|
|
const payload = KubernetesApplicationRollbackHelper.getPatchPayload(application, targetRevision);
|
|
|
|
const apiService = this._getApplicationApiService(application);
|
|
|
|
await apiService.rollback(application.ResourcePool, application.Name, payload);
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rollback(application, targetRevision) {
|
|
|
|
return this.$async(this.rollbackAsync, application, targetRevision);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default KubernetesApplicationService;
|
|
|
|
angular.module('portainer.kubernetes').service('KubernetesApplicationService', KubernetesApplicationService);
|