-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_cp.go
97 lines (81 loc) · 2.59 KB
/
container_cp.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
package planners
import (
"context"
"path/filepath"
"github.com/pkg/errors"
"github.com/replicatedcom/support-bundle/pkg/collect/plans"
"github.com/replicatedcom/support-bundle/pkg/collect/types"
"k8s.io/api/core/v1"
)
func (k *Kubernetes) ContainerCp(spec types.Spec) []types.Task {
var err error
podNameProvided := spec.KubernetesContainerCp.Pod != ""
labelSelectorProvided :=
spec.KubernetesContainerCp.PodListOptions != nil &&
spec.KubernetesContainerCp.PodListOptions.LabelSelector != ""
namespaceProvided := spec.KubernetesContainerCp.Namespace != ""
if spec.KubernetesContainerCp == nil {
err = errors.New("spec for kubernetes.ContainerCp required")
}
if !podNameProvided && !labelSelectorProvided {
err = errors.New("spec for kubernetes.ContainerCp pod or list_options required")
}
if err != nil {
task := plans.PreparedError(err, spec)
return []types.Task{task}
}
type podLocation struct {
PodName string
Namespace string
}
var podLocations []podLocation
resourceListOpts := types.KubernetesResourceListOptions{
Kind: "pods",
Namespace: spec.KubernetesContainerCp.Namespace,
ListOptions: spec.KubernetesContainerCp.PodListOptions,
}
resources, err := k.producers.ResourceList(resourceListOpts)(context.Background())
if err != nil {
err := errors.Wrap(err, "Failed to list pods")
task := plans.PreparedError(err, spec)
return []types.Task{task}
}
podList := resources.(*v1.PodList)
pods := podList.Items
for _, pod := range pods {
if !podNameProvided || spec.KubernetesContainerCp.Pod == pod.Name {
podLocations = append(podLocations, podLocation{PodName: pod.Name, Namespace: pod.Namespace})
}
}
if len(podLocations) == 0 {
err := errors.New("unable to find any pods matching the provided pod/selector")
task := plans.PreparedError(err, spec)
return []types.Task{task}
}
var tasks []types.Task
for _, podLocation := range podLocations {
rawPath := spec.Shared().OutputDir
if !namespaceProvided {
rawPath = filepath.Join(rawPath, podLocation.Namespace)
}
if !podNameProvided {
rawPath = filepath.Join(rawPath, podLocation.PodName)
}
task := plans.StreamsSource{
RawPath: rawPath,
StreamFormat: plans.StreamFormatTar,
Producer: k.producers.ContainerCp(
podLocation.PodName,
spec.KubernetesContainerCp.Container,
podLocation.Namespace,
filepath.Clean(spec.KubernetesContainerCp.SrcPath)),
}
task, err = plans.SetCommonFieldsStreamsSource(task, spec)
if err != nil {
tasks = append(tasks, plans.PreparedError(err, spec))
} else {
tasks = append(tasks, &task)
}
}
return tasks
}