generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement necoperf-cli Signed-off-by: zeroalphat <taichi-takemura@cybozu.co.jp> --------- Signed-off-by: zeroalphat <taichi-takemura@cybozu.co.jp> Co-authored-by: morimoto-cybozu <kenji_morimoto@cybozu.co.jp>
- Loading branch information
1 parent
b0eb382
commit 546147e
Showing
14 changed files
with
688 additions
and
10 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 @@ | ||
E2ETEST_K8S_VERSION := 1.27.1 |
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,100 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/cybozu-go/necoperf/internal/client" | ||
"github.com/spf13/cobra" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
var config struct { | ||
namespace string | ||
podName string | ||
outputDir string | ||
containerName string | ||
necoperfNS string | ||
timeout time.Duration | ||
} | ||
|
||
func isPodReady(pod *corev1.Pod) bool { | ||
for _, cond := range pod.Status.Conditions { | ||
if cond.Type != corev1.PodReady { | ||
continue | ||
} | ||
return cond.Status == corev1.ConditionTrue | ||
} | ||
return false | ||
} | ||
|
||
func NewProfileCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "profile", | ||
Short: "Perform CPU profiling on the target container", | ||
Long: "Perform CPU profiling on the target container", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cmd.SilenceUsage = true | ||
config.podName = args[0] | ||
handler := slog.NewTextHandler(os.Stderr, nil) | ||
logger := slog.New(handler) | ||
|
||
client, err := client.New(logger, config.timeout) | ||
if err != nil { | ||
return err | ||
} | ||
ds, err := client.SetupDiscovery() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.Background() | ||
pod, err := ds.GetPod(ctx, config.namespace, config.podName) | ||
if err != nil { | ||
return err | ||
} | ||
if !isPodReady(pod) { | ||
return fmt.Errorf("pod %s is not ready", pod.Name) | ||
} | ||
containerID, err := ds.GetContainerID(pod, config.containerName) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Info("get container id", "podName", config.podName, "containerID", containerID) | ||
|
||
pods, err := ds.GetPodList(ctx, config.necoperfNS) | ||
if err != nil { | ||
return err | ||
} | ||
addr, err := ds.DiscoveryServerAddr(pods, pod.Status.HostIP) | ||
if err != nil { | ||
return err | ||
} | ||
err = client.SetupGrpcClient(addr) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Info("connect grpc server", "addr", addr) | ||
|
||
err = client.Profile(ctx, config.podName, containerID, config.outputDir) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Info("profile is finished", "output directory", filepath.Join(config.outputDir, config.podName+".script")) | ||
|
||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVar(&config.necoperfNS, "necoperf-namespace", "necoperf", "Namespace in which necoperf-daemon is running") | ||
cmd.Flags().StringVarP(&config.namespace, "namespace", "n", "default", "Namespace in pod being profiled is running") | ||
cmd.Flags().StringVarP(&config.containerName, "container", "c", "", "Specify the container name to profile") | ||
cmd.Flags().DurationVar(&config.timeout, "timeout", 30*time.Second, "Time to run cpu profiling on server") | ||
cmd.Flags().StringVar(&config.outputDir, "output-dir", "/tmp", "Directory to output profiling result") | ||
|
||
return cmd | ||
} |
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,28 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewRootCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "necoperf-cli", | ||
Short: "necoperf-cli is a command line tool for necoperf", | ||
Long: "necoperf-cli is a command line tool for necoperf", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func Execute() { | ||
rootCmd := NewRootCommand() | ||
rootCmd.AddCommand(NewProfileCommand()) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,7 @@ | ||
package main | ||
|
||
import "github.com/cybozu-go/necoperf/cmd/necoperf-cli/cmd" | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
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,19 @@ | ||
# necoperf-cli command reference | ||
|
||
```console | ||
necoperf-cli <subcommand> args... | ||
``` | ||
|
||
- [`necoperf-cli profile PODNAME`](#necoperf-cli-profile-podname) | ||
|
||
## `necoperf-cli profile PODNAME` | ||
|
||
Perform profiling for container on pod. | ||
|
||
| Option | Default value |Description | | ||
|:-------|:--------------|:-----------| | ||
| `--necoperf-namespace`|`necoperf`| Namespace in which necoperf-daemon is running| | ||
| `-n`,`--namespace` | `default` | Namespace in which the pod being profiled is running | | ||
| `--container` ||Specify the container name to profile. If no container name is specified, the first container of the pod is set as the target of profiling.| | ||
| `--timeout` |`30s`| Time to run cpu profiling on server| | ||
| `--output-dir` |`/tmp`|Directory for output of profiling results| |
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
Oops, something went wrong.