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 May 31, 2023
1 parent 9988ab5 commit d23aecb
Show file tree
Hide file tree
Showing 21 changed files with 941 additions and 292 deletions.
2 changes: 1 addition & 1 deletion artifacts/deploy/karmada-apiserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ spec:
- --tls-cert-file=/etc/karmada/pki/apiserver.crt
- --tls-private-key-file=/etc/karmada/pki/apiserver.key
name: karmada-apiserver
image: registry.k8s.io/kube-apiserver:v1.25.4
image: registry.aliyuncs.com/google_containers/kube-apiserver:v1.25.4
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 8
Expand Down
2 changes: 1 addition & 1 deletion artifacts/deploy/karmada-etcd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
- operator: Exists
containers:
- name: etcd
image: registry.k8s.io/etcd:3.5.3-0
image: registry.aliyuncs.com/google_containers/etcd:3.5.3-0
imagePullPolicy: IfNotPresent
livenessProbe:
exec:
Expand Down
2 changes: 1 addition & 1 deletion artifacts/deploy/kube-controller-manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ spec:
- --service-cluster-ip-range=10.96.0.0/12
- --use-service-account-credentials=true
- --v=4
image: registry.k8s.io/kube-controller-manager:v1.25.4
image: registry.aliyuncs.com/google_containers/kube-controller-manager:v1.25.4
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 8
Expand Down
2 changes: 1 addition & 1 deletion cluster/images/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM alpine:3.17.1

RUN echo -e http://mirrors.ustc.edu.cn/alpine/v3.17/main/ > /etc/apk/repositories
ARG BINARY

RUN apk add --no-cache ca-certificates
Expand Down
2 changes: 1 addition & 1 deletion cluster/images/buildx.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM alpine:3.17.1

RUN echo -e http://mirrors.ustc.edu.cn/alpine/v3.17/main/ > /etc/apk/repositories
ARG BINARY
ARG TARGETPLATFORM

Expand Down
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
68 changes: 9 additions & 59 deletions pkg/karmadactl/top/metrics_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var (
corev1.ResourceCPU,
corev1.ResourceMemory,
}
NodeColumns = []string{"NAME", "CPU(cores)", "CPU%", "MEMORY(bytes)", "MEMORY%"}
PodColumns = []string{"NAME", "CPU(cores)", "MEMORY(bytes)"}
ClusterNameColumn = "CLUSTER"
NamespaceColumn = "NAMESPACE"
Expand All @@ -39,49 +38,17 @@ func NewTopCmdPrinter(out io.Writer) *TopCmdPrinter {
return &TopCmdPrinter{out: out}
}

func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics, availableResources map[string]corev1.ResourceList, noHeaders bool, sortBy string) error {
if len(metrics) == 0 {
return nil
}
w := printers.GetNewTabWriter(printer.out)
defer w.Flush()

sort.Sort(metricsutil.NewNodeMetricsSorter(metrics, sortBy))

if !noHeaders {
printColumnNames(w, NodeColumns)
}
var usage corev1.ResourceList
for _, m := range metrics {
m.Usage.DeepCopyInto(&usage)
printMetricsLine(w, &ResourceMetricsInfo{
Name: m.Name,
Metrics: usage,
Available: availableResources[m.Name],
})
delete(availableResources, m.Name)
}

// print lines for nodes of which the metrics is unreachable.
for nodeName := range availableResources {
printMissingMetricsNodeLine(w, nodeName)
}
return nil
}

func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, withCluster, printContainers, withNamespace, noHeaders bool, sortBy string, sum bool) error {
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)
columnWidth++
if !noHeaders {
if withCluster {
printValue(w, ClusterNameColumn)
columnWidth++
}
printValue(w, ClusterNameColumn)
if withNamespace {
printValue(w, NamespaceColumn)
columnWidth++
Expand All @@ -96,14 +63,11 @@ func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, w
sort.Sort(metricsutil.NewPodMetricsSorter(metrics, withNamespace, sortBy))

for _, m := range metrics {
if withCluster {

}
if printContainers {
sort.Sort(metricsutil.NewContainerMetricsSorter(m.Containers, sortBy))
printSinglePodContainerMetrics(w, &m, withCluster, withNamespace)
printSinglePodContainerMetrics(w, &m, withNamespace)
} else {
printSinglePodMetrics(w, &m, withCluster, withNamespace)
printSinglePodMetrics(w, &m, withNamespace)
}

}
Expand All @@ -126,11 +90,9 @@ func printColumnNames(out io.Writer, names []string) {
fmt.Fprint(out, "\n")
}

func printSinglePodMetrics(out io.Writer, m *metricsapi.PodMetrics, withCluster, withNamespace bool) {
func printSinglePodMetrics(out io.Writer, m *metricsapi.PodMetrics, withNamespace bool) {
podMetrics := getPodMetrics(m)
if withCluster {
printValue(out, m.Annotations["querySourceAnnotationKey"])
}
printValue(out, m.Annotations["resource.karmada.io/query-from-cluster"])
if withNamespace {
printValue(out, m.Namespace)
}
Expand All @@ -141,11 +103,9 @@ func printSinglePodMetrics(out io.Writer, m *metricsapi.PodMetrics, withCluster,
})
}

func printSinglePodContainerMetrics(out io.Writer, m *metricsapi.PodMetrics, withClusterName, withNamespace bool) {
func printSinglePodContainerMetrics(out io.Writer, m *metricsapi.PodMetrics, withNamespace bool) {
for _, c := range m.Containers {
if withClusterName {
printValue(out, m.Annotations["querySourceAnnotationKey"])
}
printValue(out, m.Annotations["resource.karmada.io/query-from-cluster"])
if withNamespace {
printValue(out, m.Namespace)
}
Expand Down Expand Up @@ -180,16 +140,6 @@ func printMetricsLine(out io.Writer, metrics *ResourceMetricsInfo) {
fmt.Fprint(out, "\n")
}

func printMissingMetricsNodeLine(out io.Writer, nodeName string) {
printValue(out, nodeName)
unknownMetricsStatus := "<unknown>"
for i := 0; i < len(MeasuredResources); i++ {
printValue(out, unknownMetricsStatus)
printValue(out, unknownMetricsStatus)
}
fmt.Fprint(out, "\n")
}

func printValue(out io.Writer, value interface{}) {
fmt.Fprintf(out, "%v\t", value)
}
Expand Down
1 change: 0 additions & 1 deletion pkg/karmadactl/top/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func NewCmdTop(f cmdutil.Factory, parentCommand string, streams genericclioption
}

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

return cmd
Expand Down
Loading

0 comments on commit d23aecb

Please sign in to comment.