|
| 1 | +/* |
| 2 | +Copyright 2024 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 tofu |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + |
| 24 | + "github.com/segmentio/textio" |
| 25 | + "go.opentelemetry.io/otel/trace" |
| 26 | + |
| 27 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/access" |
| 28 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/debug" |
| 29 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/deploy/label" |
| 30 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/graph" |
| 31 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/instrumentation" |
| 32 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/kubernetes/manifest" |
| 33 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/log" |
| 34 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest" |
| 35 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/status" |
| 36 | + "github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/sync" |
| 37 | +) |
| 38 | + |
| 39 | +// Deployer deploys Workspaces using tofu CLI. |
| 40 | +type Deployer struct { |
| 41 | + configName string |
| 42 | + |
| 43 | + *latest.TofuDeploy |
| 44 | + |
| 45 | + tofu CLI |
| 46 | +} |
| 47 | + |
| 48 | +// NewDeployer returns a new Deployer for a DeployConfig filled |
| 49 | +// with the needed configuration for `tofu apply` |
| 50 | +func NewDeployer(cfg Config, labeller *label.DefaultLabeller, d *latest.TofuDeploy, artifacts []*latest.Artifact, configName string) (*Deployer, error) { |
| 51 | + |
| 52 | + if d.Workspace == "" { |
| 53 | + return nil, fmt.Errorf("tofu needs Workspace to deploy from: %s", d.Workspace) |
| 54 | + } |
| 55 | + |
| 56 | + tofu := NewCLI(cfg) |
| 57 | + |
| 58 | + return &Deployer{ |
| 59 | + configName: configName, |
| 60 | + TofuDeploy: d, |
| 61 | + tofu: tofu, |
| 62 | + }, nil |
| 63 | +} |
| 64 | + |
| 65 | +func (k *Deployer) ConfigName() string { |
| 66 | + return k.configName |
| 67 | +} |
| 68 | + |
| 69 | +// GetAccessor not supported |
| 70 | +func (k *Deployer) GetAccessor() access.Accessor { |
| 71 | + return &access.NoopAccessor{} |
| 72 | +} |
| 73 | + |
| 74 | +// GetDebugger not supported. |
| 75 | +func (k *Deployer) GetDebugger() debug.Debugger { |
| 76 | + return &debug.NoopDebugger{} |
| 77 | +} |
| 78 | + |
| 79 | +// GetLogger not supported. |
| 80 | +func (k *Deployer) GetLogger() log.Logger { |
| 81 | + return &log.NoopLogger{} |
| 82 | +} |
| 83 | + |
| 84 | +// GetStatusMonitor not supported. |
| 85 | +func (k *Deployer) GetStatusMonitor() status.Monitor { |
| 86 | + return &status.NoopMonitor{} |
| 87 | +} |
| 88 | + |
| 89 | +// GetSyncer not supported. |
| 90 | +func (k *Deployer) GetSyncer() sync.Syncer { |
| 91 | + return &sync.NoopSyncer{} |
| 92 | +} |
| 93 | + |
| 94 | +// RegisterLocalImages not implemented |
| 95 | +func (k *Deployer) RegisterLocalImages(images []graph.Artifact) { |
| 96 | +} |
| 97 | + |
| 98 | +// TrackBuildArtifacts not implemented |
| 99 | +func (k *Deployer) TrackBuildArtifacts(builds, deployedImages []graph.Artifact) { |
| 100 | +} |
| 101 | + |
| 102 | +// Deploy runs `tofu apply` on Workspaces |
| 103 | +func (k *Deployer) Deploy(ctx context.Context, out io.Writer, builds []graph.Artifact, manifestsByConfig manifest.ManifestListByConfig) error { |
| 104 | + |
| 105 | + var ( |
| 106 | + childCtx context.Context |
| 107 | + endTrace func(...trace.SpanEndOption) |
| 108 | + ) |
| 109 | + instrumentation.AddAttributesToCurrentSpanFromContext(ctx, map[string]string{ |
| 110 | + "DeployerType": "tofu", |
| 111 | + }) |
| 112 | + |
| 113 | + childCtx, endTrace = instrumentation.StartTrace(ctx, "Deploy_TofuApply") |
| 114 | + if err := k.tofu.Apply(childCtx, textio.NewPrefixWriter(out, " - ")); err != nil { |
| 115 | + endTrace(instrumentation.TraceEndError(err)) |
| 116 | + return err |
| 117 | + } |
| 118 | + endTrace() |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +// HasRunnableHooks not supported |
| 123 | +func (k *Deployer) HasRunnableHooks() bool { |
| 124 | + return false |
| 125 | +} |
| 126 | + |
| 127 | +// PreDeployHooks not supported |
| 128 | +func (k *Deployer) PreDeployHooks(ctx context.Context, out io.Writer) error { |
| 129 | + _, endTrace := instrumentation.StartTrace(ctx, "Deploy_PreHooks") |
| 130 | + endTrace() |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +// PostDeployHooks not supported |
| 135 | +func (k *Deployer) PostDeployHooks(ctx context.Context, out io.Writer) error { |
| 136 | + _, endTrace := instrumentation.StartTrace(ctx, "Deploy_PostHooks") |
| 137 | + endTrace() |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +// Cleanup deletes what was deployed by calling Deploy. |
| 142 | +func (k *Deployer) Cleanup(ctx context.Context, out io.Writer, dryRun bool, manifestsByConfig manifest.ManifestListByConfig) error { |
| 143 | + |
| 144 | + instrumentation.AddAttributesToCurrentSpanFromContext(ctx, map[string]string{ |
| 145 | + "DeployerType": "tofu", |
| 146 | + }) |
| 147 | + if dryRun { |
| 148 | + return nil |
| 149 | + } |
| 150 | + // TODO: Implement delete |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +// Dependencies lists all the files that describe what needs to be deployed. |
| 156 | +func (k *Deployer) Dependencies() ([]string, error) { |
| 157 | + return []string{}, nil |
| 158 | +} |
0 commit comments