From f5eb266020a5e53e4ef57f6f7ac49adff318ed60 Mon Sep 17 00:00:00 2001 From: Zhao Xiaojie <1450685+LinuxSuRen@users.noreply.github.com> Date: Thu, 15 Apr 2021 11:14:57 +0800 Subject: [PATCH] Add support to enable metering component (#116) --- kubectl-plugin/component/enable.go | 39 ++++++++++++++++--- kubectl-plugin/install/installer_resources.go | 12 +++--- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/kubectl-plugin/component/enable.go b/kubectl-plugin/component/enable.go index 955b688..cc89726 100644 --- a/kubectl-plugin/component/enable.go +++ b/kubectl-plugin/component/enable.go @@ -7,6 +7,7 @@ import ( kstypes "github.com/linuxsuren/ks/kubectl-plugin/types" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic" "strconv" @@ -68,6 +69,7 @@ func (o *EnableOption) enablePreRunE(cmd *cobra.Command, args []string) (err err } func (o *EnableOption) enableRunE(cmd *cobra.Command, args []string) (err error) { + ctx := context.TODO() if o.Edit { err = common.UpdateWithEditor(kstypes.GetClusterConfiguration(), "kubesphere-system", "ks-installer", o.Client) } else { @@ -85,6 +87,30 @@ func (o *EnableOption) enableRunE(cmd *cobra.Command, args []string) (err error) err = integrateSonarQube(o.Client, ns, name, o.SonarQube, o.SonarQubeToken) } return + case "metering": + patchTarget = "metering" + if _, err = o.Client.Resource(kstypes.GetConfigMapSchema()).Namespace("kubesphere-system"). + Get(ctx, "ks-metering-config", metav1.GetOptions{}); err != nil { + var data *unstructured.Unstructured + if data, err = kstypes.GetObjectFromYaml(` +apiVersion: v1 +kind: ConfigMap +metadata: + name: ks-metering-config +data: + ks-metering.yaml: | + retentionDay: 7d + billing: + priceInfo: + currencyUnit: "USD" + cpuPerCorePerHour: 1.5 + memPerGigabytesPerHour: 5 + ingressNetworkTrafficPerGiagabytesPerHour: 3.5 + egressNetworkTrafficPerGigabytesPerHour: 0.5 + pvcPerGigabytesPerHour: 2.1`); err == nil { + _, err = o.Client.Resource(kstypes.GetConfigMapSchema()).Namespace("kubesphere-system").Create(ctx, data, metav1.CreateOptions{}) + } + } case "all": for _, item := range common.GetPluginAbleComponents() { o.Name = item @@ -98,12 +124,13 @@ func (o *EnableOption) enableRunE(cmd *cobra.Command, args []string) (err error) return } - patch := fmt.Sprintf(`[{"op": "replace", "path": "/spec/%s/enabled", "value": %s}]`, patchTarget, enabled) - ctx := context.TODO() - _, err = o.Client.Resource(kstypes.GetClusterConfiguration()).Namespace(ns).Patch(ctx, - name, types.JSONPatchType, - []byte(patch), - metav1.PatchOptions{}) + if err == nil { + patch := fmt.Sprintf(`[{"op": "replace", "path": "/spec/%s/enabled", "value": %s}]`, patchTarget, enabled) + _, err = o.Client.Resource(kstypes.GetClusterConfiguration()).Namespace(ns).Patch(ctx, + name, types.JSONPatchType, + []byte(patch), + metav1.PatchOptions{}) + } } return } diff --git a/kubectl-plugin/install/installer_resources.go b/kubectl-plugin/install/installer_resources.go index 0e3d32f..c9f61f0 100644 --- a/kubectl-plugin/install/installer_resources.go +++ b/kubectl-plugin/install/installer_resources.go @@ -54,6 +54,8 @@ spec: logging: # (CPU: 57 m, Memory: 2.76 G) Whether to install KubeSphere Logging system. Flexible Logging functions are provided for log query, collection and management in a unified console. Additional log collectors can be added, such as Elasticsearch, Kafka and Fluentd. enabled: {{ .Logging.Enabled }} logsidecarReplicas: 2 + metering: + enabled: false metrics_server: # (CPU: 56 m, Memory: 44.35 MiB) Whether to install metrics-server. IT enables HPA (Horizontal Pod Autoscaler). enabled: {{ .MetricsServer.Enabled }} monitoring: @@ -168,7 +170,7 @@ rules: verbs: - '*' - apiGroups: - - DevOps.kubesphere.io + - devops.kubesphere.io resources: - '*' verbs: @@ -180,7 +182,7 @@ rules: verbs: - '*' - apiGroups: - - Logging.kubesphere.io + - logging.kubesphere.io resources: - '*' verbs: @@ -234,19 +236,19 @@ rules: verbs: - '*' - apiGroups: - - Notification.kubesphere.io + - notification.kubesphere.io resources: - '*' verbs: - '*' - apiGroups: - - Auditing.kubesphere.io + - auditing.kubesphere.io resources: - '*' verbs: - '*' - apiGroups: - - Events.kubesphere.io + - events.kubesphere.io resources: - '*' verbs: