Skip to content

Commit

Permalink
feat: introducing a karmada top command
Browse files Browse the repository at this point in the history
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
  • Loading branch information
chaunceyjiang committed Aug 15, 2023
1 parent 25db1e0 commit 62482f4
Show file tree
Hide file tree
Showing 15 changed files with 1,517 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/karmada-io/karmada/pkg/karmadactl/register"
"github.com/karmada-io/karmada/pkg/karmadactl/taint"
"github.com/karmada-io/karmada/pkg/karmadactl/token"
"github.com/karmada-io/karmada/pkg/karmadactl/top"
"github.com/karmada-io/karmada/pkg/karmadactl/unjoin"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
"github.com/karmada-io/karmada/pkg/version/sharedcommand"
Expand Down Expand Up @@ -106,6 +107,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
Commands: []*cobra.Command{
apply.NewCmdApply(f, parentCommand, ioStreams),
promote.NewCmdPromote(f, parentCommand),
top.NewCmdTop(f, parentCommand, ioStreams),
},
},
}
Expand Down
207 changes: 207 additions & 0 deletions pkg/karmadactl/top/metrics_printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package top

import (
"fmt"
"io"
"sort"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/kubectl/pkg/metricsutil"
metricsapi "k8s.io/metrics/pkg/apis/metrics"

autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1"
)

var (
MeasuredResources = []corev1.ResourceName{
corev1.ResourceCPU,
corev1.ResourceMemory,
}
PodColumns = []string{"NAME", "CLUSTER", "CPU(cores)", "MEMORY(bytes)"}
NamespaceColumn = "NAMESPACE"
PodColumn = "POD"
)

type ResourceMetricsInfo struct {
Cluster string
Name string
Metrics corev1.ResourceList
Available corev1.ResourceList
}

type TopCmdPrinter struct {
out io.Writer
}

func NewTopCmdPrinter(out io.Writer) *TopCmdPrinter {
return &TopCmdPrinter{out: out}
}

func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, printContainers, withNamespace, noHeaders bool, sortBy string, sum bool) error {
if len(metrics) == 0 {
return nil
}
w := printers.GetNewTabWriter(printer.out)
defer w.Flush()

columnWidth := len(PodColumns)
if !noHeaders {
if withNamespace {
printValue(w, NamespaceColumn)
columnWidth++
}
if printContainers {
printValue(w, PodColumn)
columnWidth++
}
printColumnNames(w, PodColumns)
}

sort.Sort(NewPodMetricsSorter(metrics, withNamespace, sortBy))

for i := range metrics {
if printContainers {
sort.Sort(metricsutil.NewContainerMetricsSorter(metrics[i].Containers, sortBy))
printSinglePodContainerMetrics(w, &metrics[i], withNamespace)
} else {
printSinglePodMetrics(w, &metrics[i], withNamespace)
}
}

if sum {
adder := NewResourceAdder(MeasuredResources)
for i := range metrics {
adder.AddPodMetrics(&metrics[i])
}
printPodResourcesSum(w, adder.total, columnWidth)
}

return nil
}

func printColumnNames(out io.Writer, names []string) {
for _, name := range names {
printValue(out, name)
}
fmt.Fprint(out, "\n")
}

func printSinglePodMetrics(out io.Writer, m *metricsapi.PodMetrics, withNamespace bool) {
podMetrics := getPodMetrics(m)
if withNamespace {
printValue(out, m.Namespace)
}
printMetricsLine(out, &ResourceMetricsInfo{
Name: m.Name,
Cluster: m.Annotations[autoscalingv1alpha1.QuerySourceAnnotationKey],
Metrics: podMetrics,
Available: corev1.ResourceList{},
})
}

func printSinglePodContainerMetrics(out io.Writer, m *metricsapi.PodMetrics, withNamespace bool) {
for _, c := range m.Containers {
if withNamespace {
printValue(out, m.Namespace)
}
printValue(out, m.Name)
printMetricsLine(out, &ResourceMetricsInfo{
Name: c.Name,
Cluster: m.Annotations[autoscalingv1alpha1.QuerySourceAnnotationKey],
Metrics: c.Usage,
Available: corev1.ResourceList{},
})
}
}

func getPodMetrics(m *metricsapi.PodMetrics) corev1.ResourceList {
podMetrics := make(corev1.ResourceList)
for _, res := range MeasuredResources {
podMetrics[res], _ = resource.ParseQuantity("0")
}

for _, c := range m.Containers {
for _, res := range MeasuredResources {
quantity := podMetrics[res]
quantity.Add(c.Usage[res])
podMetrics[res] = quantity
}
}
return podMetrics
}

func printMetricsLine(out io.Writer, metrics *ResourceMetricsInfo) {
printValue(out, metrics.Name)
printValue(out, metrics.Cluster)
printAllResourceUsages(out, metrics)
fmt.Fprint(out, "\n")
}

func printValue(out io.Writer, value interface{}) {
fmt.Fprintf(out, "%v\t", value)
}

func printAllResourceUsages(out io.Writer, metrics *ResourceMetricsInfo) {
for _, res := range MeasuredResources {
quantity := metrics.Metrics[res]
printSingleResourceUsage(out, res, quantity)
fmt.Fprint(out, "\t")
if available, found := metrics.Available[res]; found {
fraction := float64(quantity.MilliValue()) / float64(available.MilliValue()) * 100
fmt.Fprintf(out, "%d%%\t", int64(fraction))
}
}
}

func printSingleResourceUsage(out io.Writer, resourceType corev1.ResourceName, quantity resource.Quantity) {
switch resourceType {
case corev1.ResourceCPU:
fmt.Fprintf(out, "%vm", quantity.MilliValue())
case corev1.ResourceMemory:
fmt.Fprintf(out, "%vMi", quantity.Value()/(1024*1024))
default:
fmt.Fprintf(out, "%v", quantity.Value())
}
}

func printPodResourcesSum(out io.Writer, total corev1.ResourceList, columnWidth int) {
for i := 0; i < columnWidth-2; i++ {
printValue(out, "")
}
printValue(out, "________")
printValue(out, "________")
fmt.Fprintf(out, "\n")
for i := 0; i < columnWidth-4; i++ {
printValue(out, "")
}
printMetricsLine(out, &ResourceMetricsInfo{
Name: "",
Metrics: total,
Available: corev1.ResourceList{},
})
}

type ResourceAdder struct {
resources []corev1.ResourceName
total corev1.ResourceList
}

func NewResourceAdder(resources []corev1.ResourceName) *ResourceAdder {
return &ResourceAdder{
resources: resources,
total: make(corev1.ResourceList),
}
}

// AddPodMetrics adds each pod metric to the total
func (adder *ResourceAdder) AddPodMetrics(m *metricsapi.PodMetrics) {
for _, c := range m.Containers {
for _, res := range adder.resources {
total := adder.total[res]
total.Add(c.Usage[res])
adder.total[res] = total
}
}
}
73 changes: 73 additions & 0 deletions pkg/karmadactl/top/metrics_sorter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package top

import (
corev1 "k8s.io/api/core/v1"
metricsapi "k8s.io/metrics/pkg/apis/metrics"

autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1"
)

type PodMetricsSorter struct {
metrics []metricsapi.PodMetrics
sortBy string
withNamespace bool
podMetrics []corev1.ResourceList
}

func (p *PodMetricsSorter) Len() int {
return len(p.metrics)
}

func (p *PodMetricsSorter) Swap(i, j int) {
p.metrics[i], p.metrics[j] = p.metrics[j], p.metrics[i]
p.podMetrics[i], p.podMetrics[j] = p.podMetrics[j], p.podMetrics[i]
}

func (p *PodMetricsSorter) Less(i, j int) bool {
switch p.sortBy {
case "cpu":
return p.podMetrics[i].Cpu().MilliValue() > p.podMetrics[j].Cpu().MilliValue()
case "memory":
return p.podMetrics[i].Memory().Value() > p.podMetrics[j].Memory().Value()
default:
if p.metrics[i].Annotations[autoscalingv1alpha1.QuerySourceAnnotationKey] != p.metrics[j].Annotations[autoscalingv1alpha1.QuerySourceAnnotationKey] {
return p.metrics[i].Annotations[autoscalingv1alpha1.QuerySourceAnnotationKey] < p.metrics[j].Annotations[autoscalingv1alpha1.QuerySourceAnnotationKey]
}
if p.withNamespace && p.metrics[i].Namespace != p.metrics[j].Namespace {
return p.metrics[i].Namespace < p.metrics[j].Namespace
}
return p.metrics[i].Name < p.metrics[j].Name
}
}

func NewPodMetricsSorter(metrics []metricsapi.PodMetrics, withNamespace bool, sortBy string) *PodMetricsSorter {
var podMetrics = make([]corev1.ResourceList, len(metrics))
if len(sortBy) > 0 {
for i := range metrics {
podMetrics[i] = getPodMetrics(&metrics[i])
}
}

return &PodMetricsSorter{
metrics: metrics,
sortBy: sortBy,
withNamespace: withNamespace,
podMetrics: podMetrics,
}
}
58 changes: 58 additions & 0 deletions pkg/karmadactl/top/top.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package top

import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
metricsapi "k8s.io/metrics/pkg/apis/metrics"
)

const (
sortByCPU = "cpu"
sortByMemory = "memory"
)

var (
supportedMetricsAPIVersions = []string{
"v1beta1",
}
topLong = templates.LongDesc(`
Display Resource (CPU/Memory) usage of member clusters.
The top command allows you to see the resource consumption for pods of member clusters.
This command requires karmada-metrics-adapter to be correctly configured and working on the Karmada control plane and
Metrics Server to be correctly configured and working on the member clusters.`)
)

func NewCmdTop(f cmdutil.Factory, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "top",
Short: "Display resource (CPU/memory) usage of member clusters",
Long: topLong,
Run: cmdutil.DefaultSubCommandRun(streams.ErrOut),
}

// create subcommands
cmd.AddCommand(NewCmdTopPod(f, parentCommand, nil, streams))

return cmd
}

func SupportedMetricsAPIVersionAvailable(discoveredAPIGroups *metav1.APIGroupList) bool {
for _, discoveredAPIGroup := range discoveredAPIGroups.Groups {
if discoveredAPIGroup.Name != metricsapi.GroupName {
continue
}
for _, version := range discoveredAPIGroup.Versions {
for _, supportedVersion := range supportedMetricsAPIVersions {
if version.Version == supportedVersion {
return true
}
}
}
}
return false
}
Loading

0 comments on commit 62482f4

Please sign in to comment.