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.
Signed-off-by: zeroalphat <[email protected]>
- Loading branch information
1 parent
2da88b6
commit 967ce8a
Showing
11 changed files
with
601 additions
and
9 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,98 @@ | ||
package cmd | ||
|
||
import ( | ||
"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: "Exec perf profile of the target container", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
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 | ||
} | ||
if err := client.SetupDiscovery(); err != nil { | ||
return err | ||
} | ||
|
||
ctx := cmd.Context() | ||
pod, err := client.Discovery.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 := client.Discovery.GetContainerID(pod, config.containerName) | ||
if err != nil { | ||
return err | ||
} | ||
logger.Info("get container id", "podName", config.podName, "containerID", containerID) | ||
|
||
pods, err := client.Discovery.GetPodList(ctx, config.necoperfNS) | ||
if err != nil { | ||
return err | ||
} | ||
addr, err := client.Discovery.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 | ||
}, | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
} | ||
cmd.Flags().StringVar(&config.namespace, "namespace", "default", "kubernetes namespace") | ||
cmd.Flags().StringVar(&config.outputDir, "outputDir", "/tmp", "output data directory") | ||
cmd.Flags().StringVar(&config.containerName, "container", "", "container name") | ||
cmd.Flags().StringVar(&config.necoperfNS, "necoperf-namespace", "kube-system", "necoperf namespace") | ||
cmd.Flags().DurationVar(&config.timeout, "timeout", time.Second*30, "timeout seconds") | ||
|
||
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,26 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewRootCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "necoperf-cli", | ||
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
Oops, something went wrong.