-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial crossplane top command setup
Signed-off-by: Piotr Zaniewski <piotr@upbound.io>
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
Copyright 2023 The Crossplane 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 contains the top command. | ||
package top | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/alecthomas/kong" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/metrics/pkg/client/clientset/versioned" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/logging" | ||
) | ||
|
||
// TODO:(piotr1215) implement kubeconfig using ctrl "sigs.k8s.io/controller-runtime" | ||
func getKubeConfig() (string, error) { | ||
kubeconfig := os.Getenv("KUBECONFIG") | ||
if kubeconfig != "" { | ||
return kubeconfig, nil | ||
} | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", fmt.Errorf("could not get user home directory: %w", err) | ||
} | ||
return filepath.Join(home, ".kube", "config"), nil | ||
} | ||
|
||
// Cmd represents the top command. | ||
type Cmd struct{} | ||
|
||
// Help returns help instructions for the top command. | ||
func (c *Cmd) Help() string { | ||
return `Usage: crossplane top` | ||
} | ||
|
||
// Run runs the top command. | ||
func (c *Cmd) Run(_ *kong.Context, logger logging.Logger) error { //nolint:gocyclo // TODO:(piotr1215) refactor to lower cyclomatic complexity | ||
// TODO:(piotr1215) add logging in key places | ||
logger = logger.WithValues("cmd", "top") | ||
|
||
// Build the config from the kubeconfig path | ||
kubeconfig, err := getKubeConfig() | ||
if err != nil { | ||
// TODO:(piotr1215) use crossplane-runtime error package | ||
return fmt.Errorf("failed to get kubeconfig: %w", err) | ||
} | ||
logger.Debug("Found kubeconfig") | ||
|
||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
if err != nil { | ||
return fmt.Errorf("could not build config from flags: %w", err) | ||
} | ||
|
||
// Create the clientset for Kubernetes | ||
k8sClientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return fmt.Errorf("could not create the clientset for Kubernetes: %w", err) | ||
} | ||
|
||
// Create the clientset for Metrics | ||
metricsClientset, err := versioned.NewForConfig(config) | ||
if err != nil { | ||
return fmt.Errorf("could not create the clientset for Metrics: %w", err) | ||
} | ||
|
||
// Fetch all pods from all namespaces in case of Crossplane pods being installed elswhere | ||
pods, err := k8sClientset.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
return fmt.Errorf("could not fetch all pods from all namespaces: %w", err) | ||
} | ||
|
||
// Loop through pods and print metrics | ||
for _, pod := range pods.Items { | ||
for labelKey := range pod.GetLabels() { | ||
if strings.HasPrefix(labelKey, "pkg.crossplane.io/provider") || strings.HasPrefix(labelKey, "pkg.crossplane.io/function") { | ||
podMetrics, err := metricsClientset.MetricsV1beta1().PodMetricses(pod.Namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{}) | ||
if err != nil { | ||
fmt.Printf("Error getting metrics for pod %s: %v\n", pod.Name, err) | ||
continue | ||
} | ||
|
||
for _, container := range podMetrics.Containers { | ||
cpuUsage := container.Usage.Cpu().ScaledValue(resource.Milli) | ||
memoryUsage := fmt.Sprintf("%dMi", container.Usage.Memory().ScaledValue(resource.Mega)) | ||
fmt.Printf("%-20s %-55s %-12d %-15s\n", pod.Namespace, pod.Name, cpuUsage, memoryUsage) | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters