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

Ver 97254 shutdown webhook #979

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Ver 97254 shutdown webhook #979

wants to merge 14 commits into from

Conversation

LiboYu2
Copy link
Collaborator

@LiboYu2 LiboYu2 commented Nov 12, 2024

Here are the rules implemented in this PR:

  • you cannot sandbox a subcluster with shutdown true(spec or status).
  • you cannot add a subcluster to a sandbox that has the shutdown field set to true
  • you cannot add a subcluster to a sandbox that already has at least one subcluster with shutdown true
  • you cannot scale (up/down) a subcluster with shutdown true(spec or status)
  • You cannot remove a subcluster with shutdown true(spec or status)

statusSclusterIndexMap := v.GenStatusSClusterIndexMap()
oldSclusterMap := oldObj.GenSubclusterMap()
newSclusterIndexMap := v.GenSubclusterIndexMap()
for subclusterName, sandboxName := range newSclusterSboxMap {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This loop will only check subclusters in the sandbox. What if the subclusters not in a sandbox?

Copy link
Collaborator Author

@LiboYu2 LiboYu2 Nov 13, 2024

Choose a reason for hiding this comment

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

Withdraw my previous comment. I thought you were talking about the other function. You are right. I will make change to adopt that scenario.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't this function for this rule: you cannot scale (up/down) a subcluster with shutdown true(spec or status)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This will not include subclusters in the main cluster. You should iterate over new.spec.subclusters, for each subcluster , check if size has changed, if true, check if shutdown(spec/status) is true, if true append an error, then check if it is part of a sandbox, if true check sandbox.Shutdown, if true, append an error

Copy link
Collaborator

Choose a reason for hiding this comment

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

Rectification, given that there is already a rule that prevents scaling a subcluster that is part of a sandbox, you do not need to check the sandbox.

newSandboxMap := v.GenSandboxMap()
statusSclusterIndexMap := v.GenStatusSClusterIndexMap()
sandboxWithError := map[string]bool{}
for subclusterName, sandboxName := range newSclusterSboxMap {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor: rename subclusterName to newSC, sandboxName to newSB for bettern readability.

// or an existing subcluster (found in status), the latter of which includes two scenarios:
// 1 the subcluster was in a sandbox (whose name is different from current sandbox name)
// 2 the subcluster was not in a sandbox previously
if !isInOldSandbox || isInOldSandbox && oldSandboxName != sandboxName {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use parenthesis to separate the conditions for readability.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The case isInOldSandbox && oldSandboxName != sandboxName is for unsandboxing. I remember this is checked in another rule. Will we get two errors for this case from two different rules?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good memory! Here is the related rule from my previous PR:
You cannot unsandbox a subcluster with shutdown true(spec and/or status) or part of a sandbox with shutdown true

There will be two error messages:
When a subcluster is moved from one sandbox to another,

  • this related rule will raise an error for unsandboxing scenario.
  • The rule implmented in this PR will raise another error messages for sandboxing scenario.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed this check "isInOldSandbox && oldSandboxName != sandboxName" per offline discussion to avoid duplicate error messages.

statusSclusterIndexMap := v.GenStatusSClusterIndexMap()
oldSclusterMap := oldObj.GenSubclusterMap()
newSclusterIndexMap := v.GenSubclusterIndexMap()
for subclusterName, sandboxName := range newSclusterSboxMap {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't this function for this rule: you cannot scale (up/down) a subcluster with shutdown true(spec or status)?

oldSandboxName, isInOldSandbox := statusScluterSboxMap[subclusterName]
_, foundSubcluster := newSclusterMap[subclusterName]
for newSC, newSB := range newSclusterSboxMap {
_, foundSubcluster := newSclusterMap[newSC]
if !foundSubcluster {
continue // avoid panic. this error should have been reported by other functions
}
// the subcluster to be sandboxed is either a new subcluster to be added (not found in status)
// or an existing subcluster (found in status), the latter of which includes two scenarios:
// 1 the subcluster was in a sandbox (whose name is different from current sandbox name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove this line and modify the comment accordingly.

if !foundSubcluster {
continue // avoid panic. this error should have been reported by other functions
}
// the subcluster to be sandboxed is either a new subcluster to be added (not found in status)
// or an existing subcluster (found in status), the latter of which includes two scenarios:
// 1 the subcluster was in a sandbox (whose name is different from current sandbox name)
// 2 the subcluster was not in a sandbox previously
if !isInOldSandbox || isInOldSandbox && oldSandboxName != sandboxName {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We still need the condition if !isInOldSandbox to cover the case 2: the subcluster was not in a sandbox previously

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

After testing, I have to put back the condition "isInOldSandbox && oldSandboxName != sandboxName".
This is what I found:
When a subcluster is moved from sandbox A to sandbox B and A is shut down, , the previous rule kicks in.
When A is not shut down but B is shut down, the new rule applies.
Only when both A and B are shut down, there will be two different error messages: one from unsandboxing and one from sandboxing.

subclustersToBeRemoved := vutil.MapKeyDiff(oldSubclusterMap, newSubclusterMap)
for _, subclusterToBeRemoved := range subclustersToBeRemoved {
subclusterstatus, foundInStatus := statusSubclusterMap[subclusterToBeRemoved]
if oldSubclusterMap[subclusterToBeRemoved].Shutdown || foundInStatus && subclusterstatus.Shutdown {
Copy link
Collaborator

Choose a reason for hiding this comment

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

minor

Suggested change
if oldSubclusterMap[subclusterToBeRemoved].Shutdown || foundInStatus && subclusterstatus.Shutdown {
if oldSubclusterMap[subclusterToBeRemoved].Shutdown || (foundInStatus && subclusterstatus.Shutdown) {

for _, subclusterToBeRemoved := range subclustersToBeRemoved {
subclusterstatus, foundInStatus := statusSubclusterMap[subclusterToBeRemoved]
if oldSubclusterMap[subclusterToBeRemoved].Shutdown || foundInStatus && subclusterstatus.Shutdown {
p := field.NewPath("spec").Child("subclusters")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Set this outside of the loop.

subclusterstatus, foundInStatus := statusSubclusterMap[subclusterToBeRemoved]
if oldSubclusterMap[subclusterToBeRemoved].Shutdown || foundInStatus && subclusterstatus.Shutdown {
p := field.NewPath("spec").Child("subclusters")
err := field.Invalid(p,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
err := field.Invalid(p,
err := field.Invalid(p.Index(...),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The subcluster is already removed. We cannot further drill down and give the index.

statusSclusterIndexMap := v.GenStatusSClusterIndexMap()
oldSclusterMap := oldObj.GenSubclusterMap()
newSclusterIndexMap := v.GenSubclusterIndexMap()
for subclusterName, sandboxName := range newSclusterSboxMap {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will not include subclusters in the main cluster. You should iterate over new.spec.subclusters, for each subcluster , check if size has changed, if true, check if shutdown(spec/status) is true, if true append an error, then check if it is part of a sandbox, if true check sandbox.Shutdown, if true, append an error

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

Successfully merging this pull request may close these issues.

3 participants