/
dump.go
235 lines (211 loc) · 6.63 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
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"
"io"
"os"
"time"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/spf13/cobra"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/apache/camel-k/v2/pkg/client"
"github.com/apache/camel-k/v2/pkg/client/camel/clientset/versioned"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/tar"
)
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, options.Flags),
RunE: options.dump,
}
cmd.Flags().Int("logLines", 100, "Number of log lines to dump")
cmd.Flags().Bool("compressed", false, "If the log file must be compressed in a tar.")
return &cmd, &options
}
type dumpCmdOptions struct {
*RootCmdOptions
LogLines int `mapstructure:"logLines"`
Compressed bool `mapstructure:"compressed" yaml:",omitempty"`
}
func (o *dumpCmdOptions) dump(cmd *cobra.Command, args []string) error {
c, err := o.GetCmdClient()
if err != nil {
return err
}
if len(args) == 1 {
err = util.WithFile(args[0], os.O_RDWR|os.O_CREATE, 0o644, func(file *os.File) error {
if !o.Compressed {
return dumpNamespace(o.Context, c, o.Namespace, file, o.LogLines)
}
err = dumpNamespace(o.Context, c, o.Namespace, file, o.LogLines)
if err != nil {
return err
}
tar.CreateTarFile([]string{file.Name()}, "dump."+file.Name()+"."+time.Now().Format(time.RFC3339)+".tar.gz", cmd)
return nil
})
} else {
return dumpNamespace(o.Context, c, o.Namespace, cmd.OutOrStdout(), o.LogLines)
}
return nil
}
func dumpNamespace(ctx context.Context, c client.Client, ns string, out io.Writer, logLines int) error {
camelClient, err := versioned.NewForConfig(c.GetConfig())
if err != nil {
return err
}
// Integrations
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))
}
// IntegrationKits
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))
}
// ConfigMaps
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
deployments, err := c.AppsV1().Deployments(ns).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
fmt.Fprintf(out, "Found %d deployments:\n", len(deployments.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))
}
// IntegrationPlatforms
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))
}
// CamelCatalogs
cat, err := camelClient.CamelV1().CamelCatalogs(ns).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
fmt.Fprintf(out, "Found %d catalogs:\n", len(pls.Items))
for _, c := range cat.Items {
ref := c
cdata, err := kubernetes.ToYAML(&ref)
if err != nil {
return err
}
fmt.Fprintf(out, "---\n%s\n---\n", string(cdata))
}
// Pods and Logs
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, logLines)
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 io.Writer) {
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 io.Writer, logLines int) error {
lines := int64(logLines)
stream, err := c.CoreV1().Pods(ns).GetLogs(name, &v1.PodLogOptions{
Container: container,
TailLines: &lines,
}).Stream(ctx)
if err != nil {
return err
}
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 stream.Close()
}