Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/kube restrictions #4316

Merged
merged 3 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const component = {
guideUrl: '<',
nodePoolsLink: '<',
serviceLink: '<',
restrictionsLink: '<',
restrictions: '<',
},
template,
controller,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import routing from './details.routing';
import containersComponent from './containers/index';
import nodePools from './node-pool';
import serviceComponent from './service/index';
import restrictions from './restrictions';

import './index.scss';

Expand All @@ -28,6 +29,7 @@ angular
containersComponent,
nodePools,
serviceComponent,
restrictions,
])
.config(routing)
.run(/* @ngTranslationsInject:json ./translations */)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export default /* @ngInject */ ($stateProvider) => {
kubeId,
projectId,
}),
restrictionsLink: /* @ngInject */ ($state, kubeId, projectId) =>
$state.href('pci.projects.project.kubernetes.details.restrictions', {
kubeId,
projectId,
}),
currentActiveLink: /* @ngInject */ ($transition$, $state) => () =>
$state.href($state.current.name, $transition$.params()),
goToKubernetesDetails: ($state, CucCloudMessage, kubeId, projectId) => (
Expand Down Expand Up @@ -56,6 +61,11 @@ export default /* @ngInject */ ($stateProvider) => {
kubeId,
projectId,
}),
loadRestrictions: /* @ngInject */ ($http, kubeId, projectId) => () =>
$http
.get(`/cloud/project/${projectId}/kube/${kubeId}/ipRestrictions`)
.then(({ data }) => data)
.catch(() => []),
},

redirectTo: 'pci.projects.project.kubernetes.details.service',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default class IPv4 {
constructor(ip) {
const [cidr, mask] = ip.split('/');
this.address = ip;
this.cidr = cidr;
this.mask = parseInt(mask, 10) || 32;
}

format() {
return `${this.cidr}/${this.mask}`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import controller from './controller';
import template from './template.html';

const component = {
bindings: {
restrictions: '<',
updateRestrictions: '<',
deleteRestriction: '<',
},
controller,
template,
};

export default component;
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import IPv4 from './IPv4.class';

export default class KubernetesRestrictionsCtrl {
/* @ngInject */
constructor($translate, CucCloudMessage) {
this.$translate = $translate;
this.CucCloudMessage = CucCloudMessage;
}

$onInit() {
this.loadMessages();
this.getIps(this.restrictions);
this.selectRow(this.ips.length);
this.currentIp = null;
}

loadMessages() {
this.CucCloudMessage.unSubscribe(
'pci.projects.project.kubernetes.details.restrictions',
);
this.messageHandler = this.CucCloudMessage.subscribe(
'pci.projects.project.kubernetes.details.restrictions',
{ onMessage: () => this.refreshMessages() },
);
}

refreshMessages() {
this.messages = this.messageHandler.getMessages();
}

getIps(restrictions) {
this.ips = restrictions.map((ip) => new IPv4(ip));
}

selectRow(index) {
this.currentEditedRow = index;
}

addRestriction() {
this.currentIp = null;
this.ips.push({});
this.selectRow(this.ips.length - 1);
}

editRestriction(rowIndex) {
this.selectRow(rowIndex);
this.currentIp = this.ips[rowIndex].address;
if (!this.ips[this.ips.length - 1].address) {
this.ips.pop();
}
}

deleteIp(ip, index) {
this.isLoading = true;
return this.deleteRestriction(ip.address)
.then(() => {
this.ips.splice(index, 1);
return this.CucCloudMessage.success(
this.$translate.instant('kube_restrictions_add_success'),
);
})
.catch(
(error) =>
`${this.CucCloudMessage.error(
this.$translate.instant('kube_restrictions_add_error'),
)}${error.data}`,
)
.finally(() => {
this.isLoading = false;
});
}

saveRestrictions(index) {
this.isLoading = true;
this.ips[index] = new IPv4(this.currentIp);
this.selectRow(this.ips.length);
return this.updateRestrictions(this.ips.map((ip) => ip.format()))
.then((restrictions) => {
this.getIps(restrictions);
return this.CucCloudMessage.success(
this.$translate.instant('kube_restrictions_add_success'),
);
})
.catch(
(error) =>
`${this.CucCloudMessage.error(
this.$translate.instant('kube_restrictions_add_error'),
)}${error.data}`,
)
.finally(() => {
this.isLoading = false;
});
}

isIPValid() {
JeremyDec marked this conversation as resolved.
Show resolved Hide resolved
try {
const [cidr, mask] = this.currentIp.split('/');
const splittedCidr = cidr.split('.');
if (splittedCidr.length !== 4) {
return false;
}
if (mask || mask === '') {
splittedCidr.push(mask);
}
return splittedCidr.every(
(value) => parseInt(value, 10) >= 0 && parseInt(value, 10) < 256,
);
} catch (error) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import angular from 'angular';
import '@uirouter/angularjs';
import 'oclazyload';

const moduleName = 'ovhManagerPciProjectKubernetesDetailsLazyloading';

angular.module(moduleName, ['ui.router', 'oc.lazyLoad']).config(
/* @ngInject */ ($stateProvider) => {
$stateProvider.state(
'pci.projects.project.kubernetes.details.restrictions.**',
{
url: '/restrictions',
lazyLoad: ($transition$) => {
const $ocLazyLoad = $transition$.injector().get('$ocLazyLoad');

return import('./restrictions.module').then((mod) =>
$ocLazyLoad.inject(mod.default || mod),
);
},
},
);
},
);

export default moduleName;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import angular from 'angular';

import component from './component';
import routing from './routing';

const moduleName = 'ovhManagerPciProjectKubernetesRestrictions';

angular
.module(moduleName, [])
.config(routing)
.component('pciProjectKubernetesRestrictions', component)
.run(/* @ngTranslationsInject:json ./translations */);

export default moduleName;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default /* @ngInject */ ($stateProvider) => {
$stateProvider.state('pci.projects.project.kubernetes.details.restrictions', {
url: '/restrictions',
views: {
kubernetesView: 'pciProjectKubernetesRestrictions',
},
resolve: {
breadcrumb: /* @ngInject */ ($translate) =>
$translate.instant('kube_restrictions'),
restrictions: /* @ngInject */ (loadRestrictions) => loadRestrictions(),
updateRestrictions: /* @ngInject */ ($http, kubeId, projectId) => (ips) =>
$http
.put(`/cloud/project/${projectId}/kube/${kubeId}/ipRestrictions`, {
ips,
})
.then(({ data }) => data),
deleteRestriction: /* @ngInject */ ($http, kubeId, projectId) => (ip) =>
$http
.delete(
`/cloud/project/${projectId}/kube/${kubeId}/ipRestrictions/${encodeURIComponent(
ip,
)}`,
)
.then(({ data }) => data),
},
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<div>
<h2 data-translate="kube_restrictions_manage"></h2>
<p data-translate="kube_restrictions_manage_description"></p>
<cui-message-container
data-messages="$ctrl.messages"
></cui-message-container>

<oui-button
variant="secondary"
on-click="$ctrl.addRestriction()"
icon-left="oui-icon-add"
class="mb-3"
>
<span data-translate="kube_restrictions_add"></span>
</oui-button>
<oui-datagrid rows="$ctrl.ips" data-ng-if="$ctrl.ips.length">
<oui-datagrid-column
title=":: 'kube_restrictions_allowed_clients' | translate"
>
<span
data-ng-if="$ctrl.currentEditedRow !== $rowIndex"
data-ng-bind=":: $row.format()"
></span>
<label
data-ng-if="$ctrl.currentEditedRow === $rowIndex"
class="sr-only"
for="{{:: 'ip-' + $rowIndex}}"
data-translate="kube_restrictions_allowed_clients_add"
></label>
<input
data-ng-if="$ctrl.currentEditedRow === $rowIndex"
class="oui-input oui-input_m"
data-ng-class="{
'oui-input_error': !$ctrl.isIPValid() && $ctrl.currentIp
}"
id="{{:: 'ip' + $rowIndex }}"
type="text"
required
data-ng-model="$ctrl.currentIp"
/>
</oui-datagrid-column>
<oui-datagrid-column>
<div class="float-right">
<oui-button
data-ng-if="$ctrl.currentEditedRow === $rowIndex"
size="s"
variant="secondary"
disabled="$ctrl.isLoading && !$ctrl.isIPValid()"
on-click="$ctrl.saveRestrictions($rowIndex)"
>
<span
class="oui-icon oui-icon-success"
aria-hidden="true"
></span>
<span
class="sr-only"
data-translate="kube_restrictions_update"
></span>
</oui-button>
<oui-button
data-ng-if="$ctrl.currentEditedRow === $rowIndex"
size="s"
variant="secondary"
disabled="$ctrl.isLoading"
on-click="$ctrl.selectRow($ctrl.ips.length)"
>
<span
class="oui-icon oui-icon-close"
aria-hidden="true"
></span>
<span
class="sr-only"
data-translate="kube_restrictions_cancel"
></span>
</oui-button>
<oui-button
data-ng-if="$ctrl.currentEditedRow !== $rowIndex"
size="s"
variant="secondary"
disabled="$ctrl.isLoading"
on-click="$ctrl.editRestriction($rowIndex)"
>
<span
class="oui-icon oui-icon-pen"
aria-hidden="true"
></span>
<span
class="sr-only"
data-translate="kube_restrictions_edit"
></span>
</oui-button>
<oui-button
data-ng-if="$ctrl.currentEditedRow !== $rowIndex"
size="s"
variant="secondary"
disabled="$ctrl.isLoading"
on-click="$ctrl.deleteIp($row, $rowIndex)"
>
<span
class="oui-icon oui-icon-bin"
aria-hidden="true"
></span>
<span
class="sr-only"
data-translate="kube_restrictions_delete"
></span>
</oui-button>
</div>
</oui-datagrid-column>
</oui-datagrid>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"kube_restrictions": "Zugang zum APIServer",
"kube_restrictions_manage": "Verwaltung der Zugänge zum APIServer",
"kube_restrictions_manage_description": "Wählen Sie die IPv4-Bereiche aus, die auf den APIServer Ihres Clusters zugreifen können. Wenn keine IP-Adresse besteht, werden keine Einschränkungen angewandt.",
"kube_restrictions_allowed_clients": "Autorisierte Clients",
"kube_restrictions_add": "Eine neue Einschränkung hinzufügen",
"kube_restrictions_allowed_clients_add": "Hinzuzufügende IP",
"kube_restrictions_add_success": "Die Einschränkungen für den APIServer wurden erfolgreich geändert.",
"kube_restrictions_add_error": "Bei der Änderung der Einschränkungen ist ein Fehler aufgetreten.",
"kube_restrictions_update": "Bestätigen",
"kube_restrictions_cancel": "Abbrechen",
"kube_restrictions_edit": "Update",
"kube_restrictions_delete": "Löschen"
}
Loading