-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ECS] Support Plan Preview for ECS #4881
Changes from 31 commits
42aa504
52d89e9
841459b
8c2157d
f99bebe
8190daa
98b740b
43d3ff8
b416577
5f4f2b9
46cff87
5325c36
0b33d5a
00c9dd8
c9f2975
7613dd5
17b8dfe
6b07291
9ff2fcf
323b4bb
b174d23
e2a38f0
dd40abb
59f3171
77dcef7
9279185
db54c29
eca1b74
46233d0
97405b4
33fdec1
3fee826
0cb5d29
c9764e4
55b89b6
c38c07b
6f542c6
e252b8d
64efe0a
a161cba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright 2024 The PipeCD 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 planpreview | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/app/piped/deploysource" | ||
provider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/ecs" | ||
"github.com/pipe-cd/pipecd/pkg/diff" | ||
"github.com/pipe-cd/pipecd/pkg/model" | ||
) | ||
|
||
func (b *builder) ecsdiff( | ||
ctx context.Context, | ||
app *model.Application, | ||
targetDSP deploysource.Provider, | ||
lastCommit string, | ||
buf *bytes.Buffer, | ||
) (*diffResult, error) { | ||
var ( | ||
oldManifests, newManifests provider.ECSManifests | ||
err error | ||
) | ||
|
||
newManifests, err = b.loadECSManifests(ctx, *app, targetDSP) | ||
if err != nil { | ||
fmt.Fprintf(buf, "failed to load ecs manifests at the head commit (%v)\n", err) | ||
return nil, err | ||
} | ||
|
||
if lastCommit == "" { | ||
fmt.Fprintf(buf, "failed to find the commit of the last successful deployment") | ||
return nil, fmt.Errorf("cannot get the old manifests without the last successful deployment") | ||
} | ||
|
||
runningDSP := deploysource.NewProvider( | ||
b.workingDir, | ||
deploysource.NewGitSourceCloner(b.gitClient, b.repoCfg, "running", lastCommit), | ||
*app.GitPath, | ||
b.secretDecrypter, | ||
) | ||
|
||
oldManifests, err = b.loadECSManifests(ctx, *app, runningDSP) | ||
if err != nil { | ||
fmt.Fprintf(buf, "failed to load ecs manifests at the running commit (%v)\n", err) | ||
return nil, err | ||
} | ||
|
||
result, err := provider.Diff( | ||
oldManifests, | ||
newManifests, | ||
diff.WithEquateEmpty(), | ||
diff.WithCompareNumberAndNumericString(), | ||
) | ||
if err != nil { | ||
fmt.Fprintf(buf, "failed to compare manifests (%v)\n", err) | ||
return nil, err | ||
} | ||
|
||
if result.NoChange() { | ||
fmt.Fprintln(buf, "No changes were detected") | ||
return &diffResult{ | ||
summary: "No changes were detected", | ||
noChange: true, | ||
}, nil | ||
} | ||
|
||
summary := fmt.Sprintf("%d changes were detected", len(result.Diff.Nodes())) | ||
details := result.Render(provider.DiffRenderOptions{ | ||
UseDiffCommand: true, | ||
}) | ||
fmt.Fprintf(buf, "--- Last Deploy\n+++ Head Commit\n\n%s\n", details) | ||
|
||
return &diffResult{ | ||
summary: summary, | ||
}, nil | ||
|
||
} | ||
|
||
func (b *builder) loadECSManifests(ctx context.Context, app model.Application, dsp deploysource.Provider) (provider.ECSManifests, error) { | ||
commit := dsp.Revision() | ||
cache := provider.ECSManifestsCache{ | ||
AppID: app.Id, | ||
Cache: b.appManifestsCache, | ||
Logger: b.logger, | ||
} | ||
|
||
manifests, ok := cache.Get(commit) | ||
if ok { | ||
return manifests, nil | ||
} | ||
|
||
ds, err := dsp.Get(ctx, io.Discard) | ||
if err != nil { | ||
return provider.ECSManifests{}, err | ||
} | ||
|
||
appCfg := ds.ApplicationConfig.ECSApplicationSpec | ||
if appCfg == nil { | ||
return provider.ECSManifests{}, fmt.Errorf("malformed application configuration file") | ||
} | ||
|
||
taskDef, err := provider.LoadTaskDefinition(ds.AppDir, appCfg.Input.TaskDefinitionFile) | ||
if err != nil { | ||
return provider.ECSManifests{}, err | ||
} | ||
serviceDef, err := provider.LoadServiceDefinition(ds.AppDir, appCfg.Input.ServiceDefinitionFile) | ||
if err != nil { | ||
return provider.ECSManifests{}, err | ||
} | ||
|
||
manifests = provider.ECSManifests{ | ||
TaskDefinition: &taskDef, | ||
ServiceDefinition: &serviceDef, | ||
} | ||
|
||
cache.Put(commit, manifests) | ||
return manifests, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2024 The PipeCD 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 ecs | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/cache" | ||
) | ||
|
||
type ECSManifestsCache struct { | ||
AppID string | ||
Cache cache.Cache | ||
Logger *zap.Logger | ||
} | ||
|
||
func (c ECSManifestsCache) Get(commit string) (ECSManifests, bool) { | ||
key := ecsManifestsCacheKey(c.AppID, commit) | ||
item, err := c.Cache.Get(key) | ||
if err == nil { | ||
return item.(ECSManifests), true | ||
} | ||
|
||
if errors.Is(err, cache.ErrNotFound) { | ||
c.Logger.Info("ecs manifests were not found in cache", | ||
zap.String("app-id", c.AppID), | ||
zap.String("commit-hash", commit), | ||
) | ||
return ECSManifests{}, false | ||
} | ||
|
||
c.Logger.Error("failed while retrieving ecs manifests from cache", | ||
zap.String("app-id", c.AppID), | ||
zap.String("commit-hash", commit), | ||
zap.Error(err), | ||
) | ||
return ECSManifests{}, false | ||
} | ||
|
||
func (c ECSManifestsCache) Put(commit string, sm ECSManifests) { | ||
key := ecsManifestsCacheKey(c.AppID, commit) | ||
if err := c.Cache.Put(key, sm); err != nil { | ||
c.Logger.Error("failed while putting ecs manifests from cache", | ||
zap.String("app-id", c.AppID), | ||
zap.String("commit-hash", commit), | ||
zap.Error(err), | ||
) | ||
} | ||
} | ||
|
||
func ecsManifestsCacheKey(appID, commit string) string { | ||
return fmt.Sprintf("%s/%s", appID, commit) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2024 The PipeCD 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 ecs | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/diff" | ||
) | ||
|
||
const ( | ||
diffCommand = "diff" | ||
) | ||
|
||
type DiffResult struct { | ||
Diff *diff.Result | ||
Old ECSManifests | ||
New ECSManifests | ||
} | ||
|
||
func (d *DiffResult) NoChange() bool { | ||
return len(d.Diff.Nodes()) == 0 | ||
} | ||
|
||
func Diff(old, new ECSManifests, opts ...diff.Option) (*DiffResult, error) { | ||
d, err := diff.DiffStructureds(old, new, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !d.HasDiff() { | ||
return &DiffResult{Diff: d}, nil | ||
} | ||
|
||
ret := &DiffResult{ | ||
Old: old, | ||
New: new, | ||
Diff: d, | ||
} | ||
return ret, nil | ||
} | ||
|
||
type DiffRenderOptions struct { | ||
// If true, use "diff" command to render. | ||
UseDiffCommand bool | ||
} | ||
|
||
func (d *DiffResult) Render(opt DiffRenderOptions) string { | ||
var b strings.Builder | ||
opts := []diff.RenderOption{ | ||
diff.WithLeftPadding(1), | ||
} | ||
renderer := diff.NewRenderer(opts...) | ||
if !opt.UseDiffCommand { | ||
b.WriteString(renderer.Render(d.Diff.Nodes())) | ||
} else { | ||
d, err := diffByCommand(diffCommand, d.Old, d.New) | ||
if err != nil { | ||
b.WriteString(fmt.Sprintf("An error occurred while rendering diff (%v)", err)) | ||
} else { | ||
b.Write(d) | ||
} | ||
} | ||
b.WriteString("\n") | ||
|
||
return b.String() | ||
} | ||
|
||
func diffByCommand(command string, old, new ECSManifests) ([]byte, error) { | ||
taskDiff, err := diff.DiffByCommand(command, old.TaskDefinition, new.TaskDefinition) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
serviceDiff, err := diff.DiffByCommand(command, old.ServiceDefinition, new.ServiceDefinition) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bytes.Join([][]byte{ | ||
[]byte("# 1. ServiceDefinition"), | ||
serviceDiff, | ||
[]byte("\n# 2. TaskDefinition"), | ||
taskDiff, | ||
}, []byte("\n")), nil | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [IMO] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, I fixed it. 0cb5d29
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] maybe be able to reduce the variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, I fixed
3fee826