This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit e6074d1e4f07f2edd6a33202fd06c6717a9bb997 Author: Andrea Cosentino <[email protected]> AuthorDate: Thu Mar 25 08:12:06 2021 +0100 Adding a dump command --- pkg/cmd/dump.go | 200 +++++++++++++++++++++++++++++++++++++++++++++ pkg/cmd/root.go | 1 + pkg/resources/resources.go | 4 +- 3 files changed, 203 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/dump.go b/pkg/cmd/dump.go new file mode 100644 index 0000000..70bcd51 --- /dev/null +++ b/pkg/cmd/dump.go @@ -0,0 +1,200 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You 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 cmd + +import ( + "bufio" + "context" + "fmt" + "os" + + "github.com/spf13/cobra" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/apache/camel-k/pkg/client" + "github.com/apache/camel-k/pkg/client/camel/clientset/versioned" + "github.com/apache/camel-k/pkg/util/kubernetes" +) + +func newCmdDump(rootCmdOptions *RootCmdOptions) (*cobra.Command, *dumpCmdOptions) { + options := dumpCmdOptions{ + RootCmdOptions: rootCmdOptions, + } + cmd := cobra.Command{ + Use: "dump [filename]", + Short: "Dump the state of namespace", + Long: `Dump the state of currently used namespace. If no filename will be specified, the output will be on stdout`, + PreRunE: decode(&options), + RunE: options.dump, + } + + return &cmd, &options +} + +type dumpCmdOptions struct { + *RootCmdOptions +} + +func (o *dumpCmdOptions) dump(_ *cobra.Command, args []string) error { + c, err := o.GetCmdClient() + if err != nil { + return err + } + if len(args) == 1 { + fileName := args[0] + writer, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0777) + if err != nil { + return err + } + Dump(o.Context, c, o.Namespace, writer) + defer writer.Close() + } else { + Dump(o.Context, c, o.Namespace, os.Stdout) + } + return nil +} + +func Dump(ctx context.Context, c client.Client, ns string, out *os.File) error { + + camelClient, err := versioned.NewForConfig(c.GetConfig()) + if err != nil { + return err + } + pls, err := camelClient.CamelV1().IntegrationPlatforms(ns).List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + fmt.Fprintf(out, "Found %d platforms:\n", len(pls.Items)) + for _, p := range pls.Items { + ref := p + pdata, err := kubernetes.ToYAML(&ref) + if err != nil { + return err + } + fmt.Fprintf(out, "---\n%s\n---\n", string(pdata)) + } + + its, err := camelClient.CamelV1().Integrations(ns).List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + fmt.Fprintf(out, "Found %d integrations:\n", len(its.Items)) + for _, integration := range its.Items { + ref := integration + pdata, err := kubernetes.ToYAML(&ref) + if err != nil { + return err + } + fmt.Fprintf(out, "---\n%s\n---\n", string(pdata)) + } + + iks, err := camelClient.CamelV1().IntegrationKits(ns).List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + fmt.Fprintf(out, "Found %d integration kits:\n", len(iks.Items)) + for _, ik := range iks.Items { + ref := ik + pdata, err := kubernetes.ToYAML(&ref) + if err != nil { + return err + } + fmt.Fprintf(out, "---\n%s\n---\n", string(pdata)) + } + + cms, err := c.CoreV1().ConfigMaps(ns).List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + fmt.Fprintf(out, "Found %d ConfigMaps:\n", len(cms.Items)) + for _, cm := range cms.Items { + ref := cm + pdata, err := kubernetes.ToYAML(&ref) + if err != nil { + return err + } + fmt.Fprintf(out, "---\n%s\n---\n", string(pdata)) + } + + deployments, err := c.AppsV1().Deployments(ns).List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + fmt.Fprintf(out, "Found %d deployments:\n", len(iks.Items)) + for _, deployment := range deployments.Items { + ref := deployment + data, err := kubernetes.ToYAML(&ref) + if err != nil { + return err + } + fmt.Fprintf(out, "---\n%s\n---\n", string(data)) + } + + lst, err := c.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{}) + if err != nil { + return err + } + + fmt.Fprintf(out, "\nFound %d pods:\n", len(lst.Items)) + for _, pod := range lst.Items { + fmt.Fprintf(out, "name=%s\n", pod.Name) + dumpConditions(" ", pod.Status.Conditions, out) + fmt.Fprintf(out, " logs:\n") + var allContainers []v1.Container + allContainers = append(allContainers, pod.Spec.InitContainers...) + allContainers = append(allContainers, pod.Spec.Containers...) + for _, container := range allContainers { + pad := " " + fmt.Fprintf(out, "%s%s\n", pad, container.Name) + err := dumpLogs(ctx, c, fmt.Sprintf("%s> ", pad), ns, pod.Name, container.Name, out) + if err != nil { + fmt.Fprintf(out, "%sERROR while reading the logs: %v\n", pad, err) + } + } + } + return nil +} + +func dumpConditions(prefix string, conditions []v1.PodCondition, out *os.File) { + for _, cond := range conditions { + fmt.Fprintf(out, "%scondition type=%s, status=%s, reason=%s, message=%q\n", prefix, cond.Type, cond.Status, cond.Reason, cond.Message) + } +} + +func dumpLogs(ctx context.Context, c client.Client, prefix string, ns string, name string, container string, out *os.File) error { + lines := int64(50) + stream, err := c.CoreV1().Pods(ns).GetLogs(name, &v1.PodLogOptions{ + Container: container, + TailLines: &lines, + }).Stream(ctx) + if err != nil { + return err + } + defer stream.Close() + scanner := bufio.NewScanner(stream) + printed := false + for scanner.Scan() { + printed = true + fmt.Fprintf(out, "%s%s\n", prefix, scanner.Text()) + } + if !printed { + fmt.Fprintf(out, "%s[no logs available]\n", prefix) + } + return nil +} diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index b08f580..c61a2fa 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -145,6 +145,7 @@ func addKamelSubcommands(cmd *cobra.Command, options *RootCmdOptions) { cmd.AddCommand(cmdOnly(newCmdBuilder(options))) cmd.AddCommand(cmdOnly(newCmdInit(options))) cmd.AddCommand(cmdOnly(newCmdDebug(options))) + cmd.AddCommand(cmdOnly(newCmdDump(options))) cmd.AddCommand(newCmdLocal(options)) } diff --git a/pkg/resources/resources.go b/pkg/resources/resources.go index 2a21a12..26f7a55 100644 --- a/pkg/resources/resources.go +++ b/pkg/resources/resources.go @@ -474,9 +474,9 @@ var assets = func() http.FileSystem { "/traits.yaml": &vfsgen۰CompressedFileInfo{ name: "traits.yaml", modTime: time.Time{}, - uncompressedSize: 35740, + uncompressedSize: 37013, - compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\xff\x6f\x1b\x37\xf6\xe0\xef\xfd\x2b\x08\xef\x01\xb6\x0c\x69\xec\x74\xd1\x6d\x57\x77\xb9\x85\x37\xc9\x76\xdd\x34\x8e\x2f\x4e\xbb\x38\xf4\x8a\x8a\x9a\x79\x92\x18\x73\xc8\x59\x92\x23\x47\x3d\xdc\xff\x7e\xe0\xe3\xd7\x19\x8d\xed\x71\x12\x17\x5e\xe0\x83\xfc\x10\x4b\x1a\x3e\x3e\x3e\x3e\xbe\xef\x8f\x63\x14\x65\x46\xcf\xbf\x9a\x11\x41\x6b\x98\x13\xba\x5a\x31\xc1\xcc\xee\x2b\x42\x1a\x4e\xcd\x4a\xaa\x7a\x4e\x56\x94\x6b\x [...] + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xec\x7d\xed\x72\x1c\xb7\x11\xe0\x7f\x3d\x05\x8a\xb9\x2a\x7e\xd4\xee\x90\x72\xca\xb1\xc3\x3b\x5d\x8a\x96\x94\x84\xb6\x25\xf1\x44\xd9\xa9\x2b\x9f\x2b\x8b\x9d\xe9\xdd\x85\x88\x01\x26\x00\x66\xa9\xf5\xd5\xbd\xfb\x15\xba\x01\x0c\x66\x76\xb8\x5c\xca\xa6\xcb\xbc\xba\xe4\x87\x45\x72\xa6\xd1\x68\x34\xfa\xbb\x7b\x9c\xe1\xc2\xd9\xf3\x67\x53\xa6\x78\x0d\xe7\x8c\x2f\x16\x42\x09\xb7\x79\xc6\x58\x23\xb9\x5b\x68\x53\x9f\xb3\x05\x97\x [...] }, } fs["/"].(*vfsgen۰DirInfo).entries = []os.FileInfo{
