/
container.go
143 lines (126 loc) · 4.48 KB
/
container.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
/*
Copyright 2021 The Skaffold Authors
Licensed 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 hooks
import (
"context"
"fmt"
"io"
"path"
"golang.org/x/sync/errgroup"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubectl"
kubernetesclient "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubernetes/client"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubernetes/logger"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/log/stream"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
)
// containerSelector represents a policy for selecting target containers for running a particular lifecycle hook
type containerSelector func(v1.Pod, v1.Container) (bool, error)
// runningImageSelector chooses containers that run the given image name
func runningImageSelector(image string) containerSelector {
return func(p v1.Pod, c v1.Container) (bool, error) {
if p.Status.Phase != v1.PodRunning {
return false, nil
}
for _, status := range p.Status.ContainerStatuses {
if status.Name == c.Name && status.State.Running == nil {
return false, nil
}
}
return c.Image == image, nil
}
}
// namePatternSelector chooses containers that match the glob patterns for pod and container names
func namePatternSelector(podName, containerName string) containerSelector {
return func(p v1.Pod, c v1.Container) (bool, error) {
if p.Status.Phase != v1.PodRunning {
return false, nil
}
for _, status := range p.Status.ContainerStatuses {
if status.Name == c.Name && status.State.Running == nil {
return false, nil
}
}
if matched, err := path.Match(podName, p.Name); err != nil {
return false, fmt.Errorf("failed to evaluate pod name pattern %q due to error %w", podName, err)
} else if podName != "" && !matched {
return false, nil
}
if matched, err := path.Match(containerName, c.Name); err != nil {
return false, fmt.Errorf("failed to evaluate container name pattern %q due to error %w", containerName, err)
} else if containerName != "" && !matched {
return false, nil
}
return true, nil
}
}
// containerHook represents a lifecycle hook to be executed inside a running container
type containerHook struct {
cfg latest.ContainerHook
cli *kubectl.CLI
selector containerSelector
namespaces []string
formatter logger.Formatter
}
// run executes the lifecycle hook inside the target container
func (h containerHook) run(ctx context.Context, out io.Writer) error {
errs, ctx := errgroup.WithContext(ctx)
client, err := kubernetesclient.Client(h.cli.KubeContext)
if err != nil {
return fmt.Errorf("getting Kubernetes client: %w", err)
}
for _, ns := range h.namespaces {
pods, err := client.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("getting pods for namespace %q: %w", ns, err)
}
for _, p := range pods.Items {
for _, c := range p.Spec.Containers {
if matched, err := h.selector(p, c); err != nil {
return err
} else if !matched {
continue
}
args := []string{p.Name, "--namespace", p.Namespace, "-c", c.Name, "--"}
args = append(args, h.cfg.Command...)
cmd := h.cli.Command(ctx, "exec", args...)
tr, tw := io.Pipe()
cmd.Stderr = tw
cmd.Stdout = tw
podName := p.Name
containerName := c.Name
errs.Go(func() error {
defer tw.Close()
err := util.RunCmd(ctx, cmd)
if err != nil {
return fmt.Errorf("hook execution failed for pod %q container %q: %w", podName, containerName, err)
}
return nil
})
pod := p
var containerStatus v1.ContainerStatus
for _, status := range pod.Status.ContainerStatuses {
if status.Name == c.Name {
containerStatus = status
}
}
errs.Go(func() error {
return stream.StreamRequest(ctx, out, h.formatter(pod, containerStatus, func() bool { return false }), tr)
})
}
}
}
return errs.Wait()
}