Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pkg/skaffold/hooks/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (r deployRunner) run(ctx context.Context, out io.Writer, hooks []latest.Dep
output.Default.Fprintln(out, fmt.Sprintf("Starting %s hooks...", phase))
}
env := r.getEnv(manifestsNs)
for _, h := range hooks {
for i, h := range hooks {
if h.HostHook != nil {
hook := hostHook{*h.HostHook, env}
if err := hook.run(ctx, nil, out); err != nil && !errors.Is(err, &Skip{}) {
Expand All @@ -119,7 +119,7 @@ func (r deployRunner) run(ctx context.Context, out io.Writer, hooks []latest.Dep
hook := containerHook{
cfg: latest.ContainerHook{Command: h.ContainerHook.Command},
cli: r.cli,
selector: filterContainersSelector(r.visitedContainers, phase, namePatternSelector(h.ContainerHook.PodName, h.ContainerHook.ContainerName)),
selector: filterContainersSelector(r.visitedContainers, phase, i, namePatternSelector(h.ContainerHook.PodName, h.ContainerHook.ContainerName)),
namespaces: *r.namespaces,
formatter: r.formatter,
}
Expand All @@ -134,10 +134,12 @@ func (r deployRunner) run(ctx context.Context, out io.Writer, hooks []latest.Dep
return nil
}

// filterContainersSelector filters the containers that have already been processed from a previous deploy iteration
func filterContainersSelector(visitedContainers *sync.Map, phase phase, selector containerSelector) containerSelector {
// filterContainersSelector filters the containers that have already been processed from a previous deploy iteration.
// The hookIndex parameter differentiates between distinct hook items so that multiple hooks targeting the same
// container are each allowed to execute.
func filterContainersSelector(visitedContainers *sync.Map, phase phase, hookIndex int, selector containerSelector) containerSelector {
return func(p corev1.Pod, c corev1.Container) (bool, error) {
key := fmt.Sprintf("%s:%s:%s", phase, p.GetName(), c.Name)
key := fmt.Sprintf("%s:%d:%s:%s", phase, hookIndex, p.GetName(), c.Name)
if _, found := visitedContainers.LoadOrStore(key, struct{}{}); found {
return false, nil
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/skaffold/hooks/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ func TestDeployHooks(t *testing.T) {
})
}

func TestMultipleContainerHooksOnSamePod(t *testing.T) {
testutil.Run(t, "TestMultipleContainerHooksOnSamePod", func(t *testutil.T) {
hooks := latest.DeployHooks{
PostHooks: []latest.DeployHookItem{
{
ContainerHook: &latest.NamedContainerHook{
ContainerHook: latest.ContainerHook{
Command: []string{"echo", "container-hook-0"},
},
PodName: "pod1",
ContainerName: "container1",
},
},
{
ContainerHook: &latest.NamedContainerHook{
ContainerHook: latest.ContainerHook{
Command: []string{"echo", "container-hook-1"},
},
PodName: "pod1",
ContainerName: "container1",
},
},
},
}

namespaces := []string{"np1", "np2"}
opts := NewDeployEnvOpts("run_id", testKubeContext, namespaces)
formatter := func(corev1.Pod, corev1.ContainerStatus, func() bool) log.Formatter { return mockLogFormatter{} }
runner := NewDeployRunner(&kubectl.CLI{KubeContext: testKubeContext}, hooks, &namespaces, formatter, opts, nil)

t.Override(&util.DefaultExecCommand,
testutil.CmdRunWithOutput("kubectl --context context1 exec pod1 --namespace np1 -c container1 -- echo container-hook-0", "container-hook-0").
AndRunWithOutput("kubectl --context context1 exec pod1 --namespace np1 -c container1 -- echo container-hook-1", "container-hook-1"))
t.Override(&kubernetesclient.Client, fakeKubernetesClient)
var out bytes.Buffer
err := runner.RunPostHooks(context.Background(), &out)
t.CheckNoError(err)
t.CheckContains("container-hook-0", out.String())
t.CheckContains("container-hook-1", out.String())
})
}

func TestNewCloudRunDeployRunnerHooksMapping(t *testing.T) {
tests := []struct {
description string
Expand Down