|
| 1 | +// Copyright 2026 The PipeCD Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package planpreview |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + |
| 23 | + "github.com/aws/aws-sdk-go-v2/service/ecs/types" |
| 24 | + sdk "github.com/pipe-cd/piped-plugin-sdk-go" |
| 25 | + "sigs.k8s.io/yaml" |
| 26 | + |
| 27 | + "github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/ecs/config" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + _ sdk.PlanPreviewPlugin[config.ECSPluginConfig, config.ECSDeployTargetConfig, config.ECSApplicationSpec] = (*Plugin)(nil) |
| 32 | +) |
| 33 | + |
| 34 | +// Plugin implements the PlanPreview feature for the ECS plugin |
| 35 | +type Plugin struct{} |
| 36 | + |
| 37 | +func (p *Plugin) GetPlanPreview( |
| 38 | + ctx context.Context, |
| 39 | + _ *config.ECSPluginConfig, |
| 40 | + dts []*sdk.DeployTarget[config.ECSDeployTargetConfig], |
| 41 | + input *sdk.GetPlanPreviewInput[config.ECSApplicationSpec], |
| 42 | +) (*sdk.GetPlanPreviewResponse, error) { |
| 43 | + targetDS := input.Request.TargetDeploymentSource |
| 44 | + targetAppCfg, err := targetDS.AppConfig() |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to load target app config: %w", err) |
| 47 | + } |
| 48 | + targetInput := targetAppCfg.Spec.Input |
| 49 | + |
| 50 | + targetTaskDef, err := loadTaskDef(targetDS.ApplicationDirectory, targetInput.TaskDefinitionFile) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("failed to load target task definition: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + runningDS := input.Request.RunningDeploymentSource |
| 56 | + var runningTaskDef *types.TaskDefinition |
| 57 | + |
| 58 | + if runningDS.CommitHash != "" { |
| 59 | + runningAppCfg, err := runningDS.AppConfig() |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to load running app config: %w", err) |
| 62 | + } |
| 63 | + td, err := loadTaskDef(runningDS.ApplicationDirectory, runningAppCfg.Spec.Input.TaskDefinitionFile) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("failed to load running task definition: %w", err) |
| 66 | + } |
| 67 | + runningTaskDef = &td |
| 68 | + } |
| 69 | + |
| 70 | + taskDefDiff, err := diffDefinitions(runningTaskDef, &targetTaskDef, "taskdef") |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("failed to diff task definitions: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + var serviceDiff string |
| 76 | + if targetInput.ServiceDefinitionFile != "" { |
| 77 | + targetServiceDef, err := loadServiceDef(targetDS.ApplicationDirectory, targetInput.ServiceDefinitionFile) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("failed to load target service definition: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + var runningServiceDef *types.Service |
| 83 | + if runningDS.CommitHash != "" { |
| 84 | + runningAppCfg, err := runningDS.AppConfig() |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("failed to load running app config: %w", err) |
| 87 | + } |
| 88 | + if runningAppCfg.Spec.Input.ServiceDefinitionFile != "" { |
| 89 | + sd, err := loadServiceDef(runningDS.ApplicationDirectory, runningAppCfg.Spec.Input.ServiceDefinitionFile) |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("failed to load running service definition: %w", err) |
| 92 | + } |
| 93 | + runningServiceDef = &sd |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + serviceDiff, err = diffDefinitions(runningServiceDef, &targetServiceDef, "servicedef") |
| 98 | + if err != nil { |
| 99 | + return nil, fmt.Errorf("failed to diff service definitions: %w", err) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return toResponse(dts[0].Name, taskDefDiff, serviceDiff), nil |
| 104 | +} |
| 105 | + |
| 106 | +func loadTaskDef(appDir, filename string) (types.TaskDefinition, error) { |
| 107 | + data, err := os.ReadFile(filepath.Join(appDir, filename)) |
| 108 | + if err != nil { |
| 109 | + return types.TaskDefinition{}, err |
| 110 | + } |
| 111 | + var obj types.TaskDefinition |
| 112 | + if err := yaml.Unmarshal(data, &obj); err != nil { |
| 113 | + return types.TaskDefinition{}, err |
| 114 | + } |
| 115 | + return obj, nil |
| 116 | +} |
| 117 | + |
| 118 | +func loadServiceDef(appDir, filename string) (types.Service, error) { |
| 119 | + data, err := os.ReadFile(filepath.Join(appDir, filename)) |
| 120 | + if err != nil { |
| 121 | + return types.Service{}, err |
| 122 | + } |
| 123 | + var obj types.Service |
| 124 | + if err := yaml.Unmarshal(data, &obj); err != nil { |
| 125 | + return types.Service{}, err |
| 126 | + } |
| 127 | + return obj, nil |
| 128 | +} |
| 129 | + |
0 commit comments