Skip to content

Commit

Permalink
Add support to enable metering component (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen committed Apr 15, 2021
1 parent c8595ee commit f5eb266
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
39 changes: 33 additions & 6 deletions kubectl-plugin/component/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
12 changes: 7 additions & 5 deletions kubectl-plugin/install/installer_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -168,7 +170,7 @@ rules:
verbs:
- '*'
- apiGroups:
- DevOps.kubesphere.io
- devops.kubesphere.io
resources:
- '*'
verbs:
Expand All @@ -180,7 +182,7 @@ rules:
verbs:
- '*'
- apiGroups:
- Logging.kubesphere.io
- logging.kubesphere.io
resources:
- '*'
verbs:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f5eb266

Please sign in to comment.