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

Add checkReplicasChange function to detect changes in replicas field #5341

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Warashi
Copy link
Contributor

@Warashi Warashi commented Nov 15, 2024

What this PR does:

Why we need it:

We chose a quick sync strategy when the diffs are only replicas.
We must build a summary message with the workload changed, so we must check it.

Which issue(s) this PR fixes:

Part of #4980

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

  • How are users affected by this change:
    When users delete or add the spec.replicas field on the Deployment manifest, they can view the plan summary as expected. Without this PR, they will see an invalid value.

  • Is this breaking change: No

  • How to migrate (if breaking change): N/A

Comment on lines +434 to +437
if node.TypeX == nil {
// The replicas field is not found in the old manifest.
before = ""
}
Copy link
Contributor Author

@Warashi Warashi Nov 15, 2024

Choose a reason for hiding this comment

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

🤔 oops, we should put this before as "nil"?
I could not determine whether the empty string or "nil" is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@khanhtc1202 @t-kikuc @ffjlabo
WDYT about this?

Copy link
Member

Choose a reason for hiding this comment

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

@Warashi
When does node.TypeX == nil occur?
I think we should clarify the situation to determine it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ffjlabo Sorry for forgetting to write it.
When the replicas field is not found in the old manifests, node.TypeX becomes nil and node.ValueX becomes invalid value.

Copy link
Member

Choose a reason for hiding this comment

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

@Warashi

@ffjlabo Sorry for forgetting to write it.
When the replicas field is not found in the old manifests, node.TypeX becomes nil and node.ValueX becomes invalid value.

Thanks.

When the replicas field is not found in the old manifests,

It seems that the node is the one that has spec.replicas extracting by the logic before.
So we might not need to check it.

	const replicasQuery = `^spec\.replicas$`
	node, err := ns.FindOne(replicasQuery)

I'm sorry if I misunderstood. 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, I think one side of diff defines spec.replicas, and the other side does not; this occurs.

Copy link
Contributor Author

@Warashi Warashi Nov 15, 2024

Choose a reason for hiding this comment

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

@ffjlabo
Without this check, this test case fails. (this test is pipedv1's, but it's same logic because this comment thread is on the backported line)
https://github.com/pipe-cd/pipecd/pull/5341/files#diff-4c74652886416486eb7b42cc641e5fd09672015dbaeeb19e3dddefaf81e913dbR1351-R1381

--- FAIL: TestCheckReplicasChange (0.00s)
    --- FAIL: TestCheckReplicasChange/replicas_field_added (0.00s)
        /Users/s01062/ghq/github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/deployment/determine_test.go:1394: 
            	Error Trace:	/Users/s01062/ghq/github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/deployment/determine_test.go:1394
            	Error:      	Not equal: 
            	            	expected: ""
            	            	actual  : "<invalid Value>"
            	            	
            	            	Diff:
            	            	--- Expected
            	            	+++ Actual
            	            	@@ -1 +1 @@
            	            	-
            	            	+<invalid Value>
            	Test:       	TestCheckReplicasChange/replicas_field_added
            	Messages:   	before
FAIL
FAIL	github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/deployment	0.954s
FAIL

Copy link
Member

Choose a reason for hiding this comment

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

@Warashi

Hmm, I think one side of diff defines spec.replicas, and the other side does not; this occurs.

I got your point. You are right. Sorry, I misunderstood. 🙏
We need it in such a case.

IMO, It might be better to init before as empty first. I felt easy to guess the value.

before = ""
if node.TypeX != nil {
    // found spec.replicas in the old manifests
    before = node.StringX()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I revised the whole, how about this code?

// checkReplicasChange checks if the replicas field is changed.
func checkReplicasChange(ns diff.Nodes) (before, after string, changed bool) {
	const replicasQuery = `^spec\.replicas$`
	node, err := ns.FindOne(replicasQuery)
	if err != nil {
		// no difference between the before and after manifests, or unknown error occurred.
		return "", "", false
	}

	if node.TypeX == nil {
		// The replicas field is not found in the before manifest.
		// There is difference between the before and after manifests, So it means the replicas field is added in the after manifest.
		return "", node.StringY(), true
	}

	if node.TypeY == nil {
		// The replicas field is not found in the after manifest.
		// There is difference between the before and after manifests, So it means the replicas field is removed in the after manifest.
		return node.StringX(), "", true
	}

	return node.StringX(), node.StringY(), true
}

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 25.34%. Comparing base (00463a6) to head (26616b6).

Files with missing lines Patch % Lines
pkg/app/piped/planner/kubernetes/kubernetes.go 27.27% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5341      +/-   ##
==========================================
+ Coverage   25.31%   25.34%   +0.03%     
==========================================
  Files         444      444              
  Lines       47641    47671      +30     
==========================================
+ Hits        12059    12081      +22     
- Misses      34640    34646       +6     
- Partials      942      944       +2     

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants