You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2021/03/25 07:33:47 UTC

[GitHub] [camel-k] astefanutti commented on a change in pull request #2171: Adding a dump command

astefanutti commented on a change in pull request #2171:
URL: https://github.com/apache/camel-k/pull/2171#discussion_r601147972



##########
File path: 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,

Review comment:
       Is is in the scope of that PR?

##########
File path: 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
+		}
+		dumpNamespace(o.Context, c, o.Namespace, writer)
+		defer writer.Close()
+	} else {
+		dumpNamespace(o.Context, c, o.Namespace, os.Stdout)
+	}
+	return nil
+}
+
+func dumpNamespace(ctx context.Context, c client.Client, ns string, out *os.File) error {
+
+	camelClient, err := versioned.NewForConfig(c.GetConfig())

Review comment:
       I wonder if that could be factorised with the code that dumps the namespace content when e2e tests fail?

##########
File path: 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
+		}
+		dumpNamespace(o.Context, c, o.Namespace, writer)
+		defer writer.Close()
+	} else {
+		dumpNamespace(o.Context, c, o.Namespace, os.Stdout)
+	}
+	return nil
+}
+
+func dumpNamespace(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)

Review comment:
       Maybe adding a command option could be useful. Sometimes errors appears after the 50 lines, and it's convenient for users to increase the amount of log lines retrieved.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org