From 756ef060dbff8e840a9f4de91096aa7b623c000a Mon Sep 17 00:00:00 2001 From: Richard Wei <54336863+WaysonWei@users.noreply.github.com> Date: Fri, 10 Sep 2021 22:42:25 +1200 Subject: [PATCH] feat(k8s):add kubeconfig expiry days on mouse hover EE-1300 (#5589) * add kubeconfig expiry days on mouse hover * replace settings with publicSettings for non-admin user --- api/http/handler/settings/settings_public.go | 3 ++ .../kube-config-download-button.controller.js | 34 +++++++++++++++++-- .../kube-config-download-button.html | 4 ++- app/portainer/models/settings.js | 1 + 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/api/http/handler/settings/settings_public.go b/api/http/handler/settings/settings_public.go index aab33e59e..dbe8c243e 100644 --- a/api/http/handler/settings/settings_public.go +++ b/api/http/handler/settings/settings_public.go @@ -22,6 +22,8 @@ type publicSettingsResponse struct { OAuthLogoutURI string `json:"OAuthLogoutURI" example:"https://gitlab.com/oauth/logout"` // Whether telemetry is enabled EnableTelemetry bool `json:"EnableTelemetry" example:"true"` + // The expiry of a Kubeconfig + KubeconfigExpiry string `example:"24h" default:"0"` } // @id SettingsPublic @@ -49,6 +51,7 @@ func generatePublicSettings(appSettings *portainer.Settings) *publicSettingsResp AuthenticationMethod: appSettings.AuthenticationMethod, EnableEdgeComputeFeatures: appSettings.EnableEdgeComputeFeatures, EnableTelemetry: appSettings.EnableTelemetry, + KubeconfigExpiry: appSettings.KubeconfigExpiry, } //if OAuth authentication is on, compose the related fields from application settings if publicSettings.AuthenticationMethod == portainer.AuthenticationOAuth { diff --git a/app/kubernetes/components/kube-config-download-button/kube-config-download-button.controller.js b/app/kubernetes/components/kube-config-download-button/kube-config-download-button.controller.js index 643dcefa0..55a8ec548 100644 --- a/app/kubernetes/components/kube-config-download-button/kube-config-download-button.controller.js +++ b/app/kubernetes/components/kube-config-download-button/kube-config-download-button.controller.js @@ -1,15 +1,45 @@ export default class KubeConfigController { /* @ngInject */ - constructor($window, KubernetesConfigService) { + constructor($async, $window, KubernetesConfigService, SettingsService) { + this.$async = $async; this.$window = $window; this.KubernetesConfigService = KubernetesConfigService; + this.SettingsService = SettingsService; } async downloadKubeconfig() { await this.KubernetesConfigService.downloadConfig(); } + async expiryHoverMessage() { + const settings = await this.SettingsService.publicSettings(); + const expiryDays = settings.KubeconfigExpiry; + switch (expiryDays) { + case '0': + this.state.expiryDays = 'not expire'; + break; + case '24h': + this.state.expiryDays = 'expire in 1 day'; + break; + case '168h': + this.state.expiryDays = 'expire in 7 days'; + break; + case '720h': + this.state.expiryDays = 'expire in 30 days'; + break; + case '8640h': + this.state.expiryDays = 'expire in 1 year'; + break; + } + } + $onInit() { - this.state = { isHTTPS: this.$window.location.protocol === 'https:' }; + return this.$async(async () => { + this.state = { + isHTTPS: this.$window.location.protocol === 'https:', + expiryDays: '', + }; + await this.expiryHoverMessage(); + }); } } diff --git a/app/kubernetes/components/kube-config-download-button/kube-config-download-button.html b/app/kubernetes/components/kube-config-download-button/kube-config-download-button.html index b2bae2cdf..343073af9 100644 --- a/app/kubernetes/components/kube-config-download-button/kube-config-download-button.html +++ b/app/kubernetes/components/kube-config-download-button/kube-config-download-button.html @@ -1,4 +1,5 @@ - + diff --git a/app/portainer/models/settings.js b/app/portainer/models/settings.js index 232175560..05bdc832b 100644 --- a/app/portainer/models/settings.js +++ b/app/portainer/models/settings.js @@ -21,6 +21,7 @@ export function PublicSettingsViewModel(settings) { this.OAuthLoginURI = settings.OAuthLoginURI; this.EnableTelemetry = settings.EnableTelemetry; this.OAuthLogoutURI = settings.OAuthLogoutURI; + this.KubeconfigExpiry = settings.KubeconfigExpiry; } export function LDAPSettingsViewModel(data) {