|
| 1 | +/* |
| 2 | +Copyright 2021 The Skaffold Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package hooks |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "strings" |
| 24 | + "sync" |
| 25 | + |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + |
| 28 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl" |
| 29 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/logger" |
| 30 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/output" |
| 31 | + v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" |
| 32 | +) |
| 33 | + |
| 34 | +func NewDeployRunner(cli *kubectl.CLI, d v1.DeployHooks, namespaces []string, formatter logger.Formatter, opts DeployEnvOpts) Runner { |
| 35 | + return deployRunner{d, cli, namespaces, formatter, opts, new(sync.Map)} |
| 36 | +} |
| 37 | + |
| 38 | +func NewDeployEnvOpts(runID string, kubeContext string, namespaces []string) DeployEnvOpts { |
| 39 | + return DeployEnvOpts{ |
| 40 | + RunID: runID, |
| 41 | + KubeContext: kubeContext, |
| 42 | + Namespaces: strings.Join(namespaces, ","), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +type deployRunner struct { |
| 47 | + v1.DeployHooks |
| 48 | + cli *kubectl.CLI |
| 49 | + namespaces []string |
| 50 | + formatter logger.Formatter |
| 51 | + opts DeployEnvOpts |
| 52 | + visitedPods *sync.Map // maintain a list of previous iteration pods, so that they can be skipped |
| 53 | +} |
| 54 | + |
| 55 | +func (r deployRunner) RunPreHooks(ctx context.Context, out io.Writer) error { |
| 56 | + return r.run(ctx, out, r.PreHooks, phases.PreDeploy) |
| 57 | +} |
| 58 | + |
| 59 | +func (r deployRunner) RunPostHooks(ctx context.Context, out io.Writer) error { |
| 60 | + return r.run(ctx, out, r.PostHooks, phases.PostDeploy) |
| 61 | +} |
| 62 | + |
| 63 | +func (r deployRunner) getEnv() []string { |
| 64 | + common := getEnv(staticEnvOpts) |
| 65 | + deploy := getEnv(r.opts) |
| 66 | + return append(common, deploy...) |
| 67 | +} |
| 68 | + |
| 69 | +func (r deployRunner) run(ctx context.Context, out io.Writer, hooks []v1.DeployHookItem, phase phase) error { |
| 70 | + if len(hooks) > 0 { |
| 71 | + output.Default.Fprintln(out, fmt.Sprintf("Starting %s hooks...", phase)) |
| 72 | + } |
| 73 | + env := r.getEnv() |
| 74 | + for _, h := range hooks { |
| 75 | + if h.HostHook != nil { |
| 76 | + hook := hostHook{*h.HostHook, env} |
| 77 | + if err := hook.run(ctx, out); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + } else if h.ContainerHook != nil { |
| 81 | + hook := containerHook{ |
| 82 | + cfg: v1.ContainerHook{Command: h.ContainerHook.Command}, |
| 83 | + cli: r.cli, |
| 84 | + selector: filterPodsSelector(r.visitedPods, phase, namePatternSelector(h.ContainerHook.PodName, h.ContainerHook.ContainerName)), |
| 85 | + namespaces: r.namespaces, |
| 86 | + formatter: r.formatter, |
| 87 | + } |
| 88 | + if err := hook.run(ctx, out); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + if len(hooks) > 0 { |
| 94 | + output.Default.Fprintln(out, fmt.Sprintf("Completed %s hooks", phase)) |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// filterPodsSelector filters the pods that have already been processed from a previous deploy iteration |
| 100 | +func filterPodsSelector(visitedPods *sync.Map, phase phase, selector containerSelector) containerSelector { |
| 101 | + return func(p corev1.Pod, c corev1.Container) (bool, error) { |
| 102 | + key := fmt.Sprintf("%s:%s", phase, p.GetName()) |
| 103 | + if _, found := visitedPods.LoadOrStore(key, struct{}{}); found { |
| 104 | + return false, nil |
| 105 | + } |
| 106 | + return selector(p, c) |
| 107 | + } |
| 108 | +} |
0 commit comments