Skip to content
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

Merged
merged 40 commits into from
Apr 30, 2024
Merged

[ECS] Support Plan Preview for ECS #4881

merged 40 commits into from
Apr 30, 2024

Conversation

t-kikuc
Copy link
Member

@t-kikuc t-kikuc commented Apr 19, 2024

What this PR does / why we need it:

  • Support Plan Preview for ECS
  • Including Diff() func, which compares manifests(i.e. serviceDef&taskDef in ECS) for non-k8s resources like Lambda

Which issue(s) this PR fixes:

Fixes #4870

Does this PR introduce a user-facing change?: no

  • How are users affected by this change: no
  • Is this breaking change: no
  • How to migrate (if breaking change): no

How it works:

image

Plan Preview is a feature for visualizing model-level diff, not file-level diff(Files Changed on GitHub covers it).
So we can ignore following points in the output:

  • Each key becomes UpperCamelCase.
    e.g. image in taskdef.yaml ->Image in planpreview result
  • The result also shows attributes that a user did not specify in taskdef.yaml or servicedef.yaml.
    e.g. EnableExecuteCommand is shown above but I did not specify it.

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Copy link

codecov bot commented Apr 19, 2024

Codecov Report

Attention: Patch coverage is 32.78689% with 164 lines in your changes are missing coverage. Please review.

Project coverage is 29.30%. Comparing base (fa0708b) to head (a161cba).
Report is 3 commits behind head on master.

Files Patch % Lines
pkg/app/piped/planpreview/ecsdiff.go 0.00% 87 Missing ⚠️
pkg/app/piped/platformprovider/ecs/cache.go 0.00% 30 Missing ⚠️
pkg/diff/renderer.go 56.25% 14 Missing and 7 partials ⚠️
pkg/app/piped/platformprovider/ecs/diff.go 75.51% 7 Missing and 5 partials ⚠️
pkg/diff/diff.go 57.14% 8 Missing and 4 partials ⚠️
pkg/app/piped/planpreview/builder.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4881      +/-   ##
==========================================
+ Coverage   29.23%   29.30%   +0.07%     
==========================================
  Files         318      321       +3     
  Lines       40591    40835     +244     
==========================================
+ Hits        11868    11968     +100     
- Misses      27784    27910     +126     
- Partials      939      957      +18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 40 to 41
// DiffBytesByCommand runs the command to compare the given bytes.
func DiffBytesByCommand(command string, oldBytes, newBytes []byte) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of separate this function from DiffByCommand? 🤔

Copy link
Member Author

@t-kikuc t-kikuc Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed to merge DiffBytesByCommand() into DiffByCommand().

I used DiffBytesByCommand() from outside in previous commits, but it is not used now.

pkg/diff/diff.go Outdated
@@ -97,6 +98,42 @@ func DiffUnstructureds(x, y unstructured.Unstructured, key string, opts ...Optio
return d.result, nil
}

// DiffStructs calulates the diff between two struct objects,
// which are not k8s manifests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can imagine this related to this line in your PR description.

Including Diff() func, which compares manifests(i.e. serviceDef&taskDef in ECS) for non-k8s resources like Lambda

But it is still hard to imagine when to use DiffUnstructureds and DiffStructs. Maybe we need a better name, or we need to add comments that describe these two functions better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we also should rename this to DiffStructureds either ways 🤔

Copy link
Member Author

@t-kikuc t-kikuc Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I also felt the name DiffStructs was unclear.

First, I renamed it to DiffStructureds.

Second, I added comments about which func to use in both DiffUnstructureds and DiffStructureds.
commit: 77dcef7

pkg/diff/diff.go Outdated
opt(d)
}

d.initIgnoredPaths(key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this would work? 🤔 Or could you introduce the possible key for this? Since this key in case of k8s manifests could look like this https://pipecd.dev/docs-v0.47.x/user-guide/managing-application/configuration-drift-detection/#ignore-drift-detection-for-specific-fields

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, you're right.
It did not work, so I deleted them in dd40abb

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
@t-kikuc
Copy link
Member Author

t-kikuc commented Apr 23, 2024

@khanhtc1202 Thank you for your reviewing, I fixed 3 points.

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
@t-kikuc
Copy link
Member Author

t-kikuc commented Apr 25, 2024

@khanhtc1202
I fixed:

  • Moved LoadECSManifest() to the test code
  • Modified tests of DiffByCommand()
  • Renamed 'ECSManifest' to 'ECSManifests'

Copy link
Member

@ffjlabo ffjlabo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the great work! I left some nit and imo comment!

Comment on lines 84 to 92
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
Copy link
Member

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

Suggested change
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
details := result.Render(provider.DiffRenderOptions{
UseDiffCommand: true,
})
fmt.Fprintf(buf, "--- Last Deploy\n+++ Head Commit\n\n%s\n", details)
return &diffResult{
summary: fmt.Sprintf("%d changes were detected", len(result.Diff.Nodes())),
}, nil

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I fixed

3fee826

Comment on lines 83 to 100
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[IMO]
It would be better to define it as renderByCommand.
I feel the responsibility of the method is to render the diffResult.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I fixed it. 0cb5d29

renderByCommand is better especially because this func contains the following code.

return bytes.Join([][]byte{
		[]byte("# 1. ServiceDefinition"),
		serviceDiff,
		[]byte("\n# 2. TaskDefinition"),
		taskDiff,
	}, []byte("\n")), nil

Comment on lines 26 to 27
// DiffByCommand converts the given objects into yaml and then runs the command to compare them.
func DiffByCommand(command string, old, new interface{}) ([]byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[IMO]
It would be better to define it as renderByCommand.
I feel the responsibility of the method is to render the diffResult.

Similar to the reason above, how about renaming it as RenderByCommand and moving it in the render.go like below↓?
https://github.com/pipe-cd/pipecd/blob/master/pkg/diff/renderer.go#L54

I think the DiffXX methods in the diff pkg return diff.Result and have the responsibility to extract the differences. It is ideal to unify the interface and the responsivility as much as possible.

WDYT? I want your opinion @t-kikuc @khanhtc1202

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ffjlabo
Thanks, I agree with you.
TBH, when I referred to CloudRun's diff.go, I thought it was confusing and should be renamed and moved.
cf. https://github.com/pipe-cd/pipecd/blob/master/pkg/app/piped/platformprovider/cloudrun/diff.go

Comment on lines 27 to 28
func DiffByCommand(command string, old, new interface{}) ([]byte, error) {
oldBytes, err := yaml.Marshal(old)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Check whether the command exists or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I fixed it. c9764e4

t-kikuc and others added 7 commits April 26, 2024 13:11
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
…into ecs-plan-preview

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Copy link
Member

@ffjlabo ffjlabo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@t-kikuc thanks! added one nit comment for test! I'll approve after the fix :)

Comment on lines 336 to 340
if tc.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit]

Suggested change
if tc.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tc.expectErr, err != nil)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ffjlabo
Thanks!!! I fixed it: 64efe0a

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
…into ecs-plan-preview

Signed-off-by: t-kikuc <tkikuchi07f@gmail.com>
Copy link
Member

@ffjlabo ffjlabo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 🚀

Copy link
Member

@khanhtc1202 khanhtc1202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, can't wait to see this on prod 🚀

@t-kikuc t-kikuc merged commit b17c25b into master Apr 30, 2024
14 checks passed
@t-kikuc t-kikuc deleted the ecs-plan-preview branch April 30, 2024 11:08
@github-actions github-actions bot mentioned this pull request May 13, 2024
@github-actions github-actions bot mentioned this pull request Jun 12, 2024
@github-actions github-actions bot mentioned this pull request Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/go kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ECS] Support Plan Preview for ECS
3 participants