Refactor support dump CLI (#1301)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright 2018 The Fission Authors.
|
||||
Copyright 2019 The Fission Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@@ -24,9 +24,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
|
||||
"github.com/fission/fission/pkg/fission-cli/support/resources"
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd/support/resources"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
"github.com/fission/fission/pkg/utils"
|
||||
)
|
||||
@@ -36,12 +38,22 @@ const (
|
||||
DEFAULT_OUTPUT_DIR = "fission-dump"
|
||||
)
|
||||
|
||||
func DumpInfo(c *cli.Context) error {
|
||||
type DumpSubCommand struct {
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
func Dump(flags cli.Input) error {
|
||||
opts := &DumpSubCommand{
|
||||
client: cmd.GetServer(flags),
|
||||
}
|
||||
return opts.do(flags)
|
||||
}
|
||||
|
||||
func (opts *DumpSubCommand) do(flags cli.Input) error {
|
||||
fmt.Println("Start dumping process...")
|
||||
|
||||
nozip := c.Bool("nozip")
|
||||
outputDir := c.String("output")
|
||||
nozip := flags.Bool("nozip")
|
||||
outputDir := flags.String("output")
|
||||
|
||||
// check whether the dump directory exists.
|
||||
_, err := os.Stat(outputDir)
|
||||
@@ -59,7 +71,6 @@ func DumpInfo(c *cli.Context) error {
|
||||
panic(errors.Wrap(err, "Error creating dump directory for dumping files"))
|
||||
}
|
||||
|
||||
client := util.GetApiClient(util.GetServerUrl())
|
||||
_, k8sClient := util.GetKubernetesClient()
|
||||
|
||||
ress := map[string]resources.Resource{
|
||||
@@ -68,13 +79,15 @@ func DumpInfo(c *cli.Context) error {
|
||||
"kubernetes-nodes": resources.NewKubernetesObjectDumper(k8sClient, resources.KubernetesNode, ""),
|
||||
|
||||
// fission info
|
||||
"fission-version": resources.NewFissionVersion(client),
|
||||
"fission-version": resources.NewFissionVersion(opts.client),
|
||||
|
||||
// fission component logs & spec
|
||||
"fission-components-svc-spec": resources.NewKubernetesObjectDumper(k8sClient, resources.KubernetesService,
|
||||
"svc in (buildermgr, controller, executor, influxdb, kubewatcher, logger, mqtrigger, nats-streaming, redis, router, storagesvc, timer)"),
|
||||
"fission-components-deployment-spec": resources.NewKubernetesObjectDumper(k8sClient, resources.KubernetesDeployment,
|
||||
"svc in (buildermgr, controller, executor, influxdb, kubewatcher, logger, mqtrigger, nats-streaming, redis, router, storagesvc, timer)"),
|
||||
"fission-components-daemonset-spec": resources.NewKubernetesObjectDumper(k8sClient, resources.KubernetesDaemonSet,
|
||||
"svc in (buildermgr, controller, executor, influxdb, kubewatcher, logger, mqtrigger, nats-streaming, redis, router, storagesvc, timer)"),
|
||||
"fission-components-pod-spec": resources.NewKubernetesObjectDumper(k8sClient, resources.KubernetesPod,
|
||||
"svc in (buildermgr, controller, executor, influxdb, kubewatcher, logger, mqtrigger, nats-streaming, redis, router, storagesvc, timer)"),
|
||||
"fission-components-pod-log": resources.NewKubernetesPodLogDumper(k8sClient,
|
||||
@@ -93,13 +106,13 @@ func DumpInfo(c *cli.Context) error {
|
||||
"fission-function-pod-log": resources.NewKubernetesPodLogDumper(k8sClient, "executorType in (poolmgr, newdeploy)"),
|
||||
|
||||
// CRD resources
|
||||
"fission-crd-packages": resources.NewCrdDumper(client, resources.CrdPackage),
|
||||
"fission-crd-environments": resources.NewCrdDumper(client, resources.CrdEnvironment),
|
||||
"fission-crd-functions": resources.NewCrdDumper(client, resources.CrdFunction),
|
||||
"fission-crd-httptriggers": resources.NewCrdDumper(client, resources.CrdHttpTrigger),
|
||||
"fission-crd-kubewatchers": resources.NewCrdDumper(client, resources.CrdKubeWatcher),
|
||||
"fission-crd-mqtriggers": resources.NewCrdDumper(client, resources.CrdMessageQueueTrigger),
|
||||
"fission-crd-timetriggers": resources.NewCrdDumper(client, resources.CrdTimeTrigger),
|
||||
"fission-crd-packages": resources.NewCrdDumper(opts.client, resources.CrdPackage),
|
||||
"fission-crd-environments": resources.NewCrdDumper(opts.client, resources.CrdEnvironment),
|
||||
"fission-crd-functions": resources.NewCrdDumper(opts.client, resources.CrdFunction),
|
||||
"fission-crd-httptriggers": resources.NewCrdDumper(opts.client, resources.CrdHttpTrigger),
|
||||
"fission-crd-kubewatchers": resources.NewCrdDumper(opts.client, resources.CrdKubeWatcher),
|
||||
"fission-crd-mqtriggers": resources.NewCrdDumper(opts.client, resources.CrdMessageQueueTrigger),
|
||||
"fission-crd-timetriggers": resources.NewCrdDumper(opts.client, resources.CrdTimeTrigger),
|
||||
}
|
||||
|
||||
dumpName := fmt.Sprintf("%v_%v", DUMP_ARCHIVE_PREFIX, time.Now().Unix())
|
||||
@@ -107,8 +120,14 @@ func DumpInfo(c *cli.Context) error {
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
tempDir, err := utils.GetTempDir()
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating temporary directory: %v\n", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
for key, res := range ress {
|
||||
dir := fmt.Sprintf("%v/%v/", dumpDir, key)
|
||||
dir := fmt.Sprintf("%v/%v/", tempDir, key)
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
err = os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
@@ -125,15 +144,20 @@ func DumpInfo(c *cli.Context) error {
|
||||
wg.Wait()
|
||||
|
||||
if !nozip {
|
||||
defer os.Remove(dumpDir)
|
||||
defer os.RemoveAll(tempDir)
|
||||
path := filepath.Join(outputDir, fmt.Sprintf("%v.zip", dumpName))
|
||||
_, err := utils.MakeArchive(path, dumpDir)
|
||||
_, err := utils.MakeArchive(path, tempDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating archive for dump files: %v", err)
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
fmt.Printf("The archive dump file is %v\n", path)
|
||||
} else {
|
||||
err = os.Rename(tempDir, dumpDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating dump directory: %v\n", err.Error())
|
||||
return err
|
||||
}
|
||||
fmt.Printf("The dump files are placed at %v\n", dumpDir)
|
||||
}
|
||||
|
||||
+15
-8
@@ -29,7 +29,6 @@ import (
|
||||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
"github.com/fission/fission/pkg/fission-cli/log"
|
||||
"github.com/fission/fission/pkg/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -38,6 +37,7 @@ const (
|
||||
KubernetesPod = "Pod"
|
||||
KubernetesHPA = "HPA"
|
||||
KubernetesNode = "Node"
|
||||
KubernetesDaemonSet = "DaemonSet"
|
||||
)
|
||||
|
||||
// Kubernetes Version
|
||||
@@ -126,6 +126,18 @@ func (res KubernetesObjectDumper) Dump(dumpDir string) {
|
||||
writeToFile(f, item)
|
||||
}
|
||||
|
||||
case KubernetesDaemonSet:
|
||||
objs, err := res.client.AppsV1().DaemonSets(metav1.NamespaceAll).List(metav1.ListOptions{LabelSelector: res.selector})
|
||||
if err != nil {
|
||||
log.Info(fmt.Sprintf("Error getting %v list with selector %v: %v", res.objType, res.selector, err))
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range objs.Items {
|
||||
f := getFileName(dumpDir, item.ObjectMeta)
|
||||
writeToFile(f, item)
|
||||
}
|
||||
|
||||
case KubernetesNode:
|
||||
objs, err := res.client.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: res.selector})
|
||||
if err != nil {
|
||||
@@ -137,12 +149,12 @@ func (res KubernetesObjectDumper) Dump(dumpDir string) {
|
||||
item = nodeClean(item)
|
||||
// Node doesn't have namespace value, use name here
|
||||
f := filepath.Clean(fmt.Sprintf("%v/%v", dumpDir, item.Name))
|
||||
getFileName(dumpDir, item.ObjectMeta)
|
||||
writeToFile(f, item)
|
||||
}
|
||||
|
||||
default:
|
||||
log.Info(fmt.Sprintf("Unknown type: %v", res.objType))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,11 +211,6 @@ func (res KubernetesPodLogDumper) Dump(dumpDir string) {
|
||||
go func(pod corev1.Pod) {
|
||||
defer wg.Done()
|
||||
|
||||
if !utils.IsReadyPod(&pod) {
|
||||
log.Info(fmt.Sprintf("Pod %v is not in ready state, ignore it\n", pod.Name))
|
||||
return
|
||||
}
|
||||
|
||||
// dump logs from each containers
|
||||
for _, container := range append(pod.Spec.Containers, pod.Spec.InitContainers...) {
|
||||
req := res.client.CoreV1().Pods(pod.Namespace).
|
||||
@@ -235,7 +242,7 @@ func (res KubernetesPodLogDumper) Dump(dumpDir string) {
|
||||
}
|
||||
}
|
||||
|
||||
f := getFileName(dumpDir, pod.ObjectMeta)
|
||||
f := getPodFileName(dumpDir, pod.ObjectMeta, container.Name)
|
||||
writeToFile(f, buffer.String())
|
||||
|
||||
stream.Close()
|
||||
+5
@@ -37,6 +37,11 @@ func getFileName(dumpdir string, meta metav1.ObjectMeta) string {
|
||||
return filepath.Clean(f)
|
||||
}
|
||||
|
||||
func getPodFileName(dumpdir string, pod metav1.ObjectMeta, containerName string) string {
|
||||
f := fmt.Sprintf("%v/%v_%v_%v_%v.txt", dumpdir, pod.Namespace, pod.Name, pod.ResourceVersion, containerName)
|
||||
return filepath.Clean(f)
|
||||
}
|
||||
|
||||
func writeToFile(file string, obj interface{}) {
|
||||
bs, err := yaml.Marshal(obj)
|
||||
if err != nil {
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/urfave/cli"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -30,9 +31,9 @@ import (
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/driver/urfavecli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd/environment"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd/support"
|
||||
"github.com/fission/fission/pkg/fission-cli/log"
|
||||
"github.com/fission/fission/pkg/fission-cli/plugin"
|
||||
"github.com/fission/fission/pkg/fission-cli/support"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
"github.com/fission/fission/pkg/info"
|
||||
"github.com/fission/fission/pkg/types"
|
||||
@@ -286,7 +287,7 @@ func NewCliApp() *cli.App {
|
||||
supportOutputFlag := cli.StringFlag{Name: "output, o", Value: support.DEFAULT_OUTPUT_DIR, Usage: "Output directory to save dump archive/files"}
|
||||
supportNoZipFlag := cli.BoolFlag{Name: "nozip", Usage: "Save dump information into multiple files instead of single zip file"}
|
||||
supportSubCommands := []cli.Command{
|
||||
{Name: "dump", Usage: "Collect & dump all necessary for troubleshooting", Flags: []cli.Flag{supportOutputFlag, supportNoZipFlag}, Action: support.DumpInfo},
|
||||
{Name: "dump", Usage: "Collect & dump all necessary for troubleshooting", Flags: []cli.Flag{supportOutputFlag, supportNoZipFlag}, Action: urfavecli.Wrapper(support.Dump)},
|
||||
}
|
||||
|
||||
// canary configs
|
||||
@@ -395,7 +396,11 @@ To install it for your local Fission CLI:
|
||||
func versionPrinter(_ *cli.Context) {
|
||||
client := util.GetApiClient(util.GetServerUrl())
|
||||
ver := util.GetVersion(client)
|
||||
fmt.Print(string(ver))
|
||||
bs, err := yaml.Marshal(ver)
|
||||
if err != nil {
|
||||
log.Fatal("Error formatting versions: " + err.Error())
|
||||
}
|
||||
fmt.Print(string(bs))
|
||||
}
|
||||
|
||||
func flagValueParser(args []string) error {
|
||||
|
||||
@@ -3,8 +3,6 @@ package util
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/fission/fission/pkg/controller/client"
|
||||
"github.com/fission/fission/pkg/fission-cli/log"
|
||||
"github.com/fission/fission/pkg/fission-cli/plugin"
|
||||
@@ -17,7 +15,7 @@ type Versions struct {
|
||||
Server map[string]info.BuildMeta `json:"server"`
|
||||
}
|
||||
|
||||
func GetVersion(client *client.Client) []byte {
|
||||
func GetVersion(client *client.Client) Versions {
|
||||
// Fetch client versions
|
||||
versions := Versions{
|
||||
Client: map[string]info.BuildMeta{
|
||||
@@ -43,10 +41,6 @@ func GetVersion(client *client.Client) []byte {
|
||||
}
|
||||
|
||||
// FUTURE: fetch versions of plugins server-side
|
||||
bs, err := yaml.Marshal(versions)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to format versions: " + err.Error())
|
||||
}
|
||||
|
||||
return bs
|
||||
return versions
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user