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

Implementing the AggregateStatus hook #1253

Merged
merged 1 commit into from
Jan 19, 2022

Conversation

iawia002
Copy link
Member

Signed-off-by: Xinzhao Xu [email protected]

What type of PR is this?

/kind feature

What this PR does / why we need it:

Part of #1237

This patch implements the AggregateStatus hook, check resource interpreter webhook proposal for more info.

The original code to modify the status of core resources such as Deployment and Job has been changed to use default interpreters. After the update, the status collection of the sample deployment is the same as before:

$ kubectl get deploy

NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/2     2            2           56s
status:
  availableReplicas: 2
  observedGeneration: 1
  readyReplicas: 2
  replicas: 2
  updatedReplicas: 2

for custom resources, users only need to implement the corresponding webhook and return the patch data:

func (e *workloadInterpreter) responseWithExploreAggregateStatus(workload *workloadv1alpha1.Workload, req interpreter.Request) interpreter.Response {
	wantedWorkload := workload.DeepCopy()
	var readyReplicas int32
	for _, item := range req.AggregatedStatus {
		// NOTE: this is just an example, you need to change the method of updating status according to the actual situation
		if !item.Applied {
			continue
		}
		readyReplicas += 1
	}
	wantedWorkload.Status.ReadyReplicas = readyReplicas
	marshaledBytes, err := json.Marshal(wantedWorkload)
	if err != nil {
		return interpreter.Errored(http.StatusInternalServerError, err)
	}
	return interpreter.PatchResponseFromRaw(req.Object.Raw, marshaledBytes)
}
status:
  readyReplicas: 2

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

Implementing the AggregateStatus hook.

@karmada-bot karmada-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Jan 13, 2022
@karmada-bot karmada-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jan 13, 2022
@RainbowMango
Copy link
Member

cc @XiShanYongYe-Chang
/assign

Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Nice job! Thanks 👍

pkg/detector/detector.go Outdated Show resolved Hide resolved
pkg/detector/detector.go Show resolved Hide resolved
@iawia002 iawia002 force-pushed the aggregate-status-hook branch 2 times, most recently from 76858ff to 474021b Compare January 13, 2022 14:39
@iawia002
Copy link
Member Author

All comments addressed, PTAL @RainbowMango @XiShanYongYe-Chang 56cd871

/hold
NOTE: I will squash the commits into one after you finished the review.

@karmada-bot karmada-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 17, 2022
@iawia002 iawia002 force-pushed the aggregate-status-hook branch from 9e85728 to a051e0d Compare January 17, 2022 08:25
@iawia002 iawia002 force-pushed the aggregate-status-hook branch from a051e0d to fb463b2 Compare January 17, 2022 08:49
Copy link
Member

@XiShanYongYe-Chang XiShanYongYe-Chang left a comment

Choose a reason for hiding this comment

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

Thanks, LGTM

/cc @RainbowMango

@iawia002
Copy link
Member Author

/hold cancel

@karmada-bot karmada-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jan 17, 2022
Comment on lines 1054 to 1061
newStatus, _, err := unstructured.NestedFieldNoCopy(newObj.Object, "status")
if err != nil {
return err
}
oldStatus, _, err := unstructured.NestedFieldNoCopy(obj.Object, "status")
if err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to parse the .status, we can just compare the obj and newObj, since the Interpreter won't change anything but .status, so if obj != newObj means the status has been aggregated, then we do update operation.

In addition, we seem can't assume there is a .status field. See InterpretStatus.

Copy link
Member

Choose a reason for hiding this comment

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

Something like this:

	if reflect.DeepEqual(obj, newObj) {
		klog.V(3).Infof("ignore update resource(%s/%s/%s) status as up to date", resource.Kind, resource.Namespace, resource.Name)
		return nil
	}

Maybe we can't use retry.RetryOnConflict for this case,

Copy link
Member Author

@iawia002 iawia002 Jan 18, 2022

Choose a reason for hiding this comment

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

Maybe we can't use retry.RetryOnConflict for this case

What is this for? I can compare two objects directly, but it doesn't seem to affect the use of RetryOnConflict, is it because the newObj may also not have status?

Copy link
Member

Choose a reason for hiding this comment

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

but it doesn't seem to affect the use of RetryOnConflict, is it because the newObj may also not have status?

As after we get a new copy of the object, we have to set the status, do you mean to call the webhook again in retry?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, Is there a problem with the current retry method?

Copy link
Member

@RainbowMango RainbowMango Jan 18, 2022

Choose a reason for hiding this comment

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

The thing is

if err = unstructured.SetNestedField(obj.Object, newStatus, "status"); err != nil {
			return err
		}

Personally, I'm hesitant to assume there is a .status filed(Maybe no problem for 99.9% cases).

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 am not so familiar with SetNestedField, but by looking at the source code, if the original object does not have a status field, it should not report an error when setting for it, it will only report an error if the value is not compliant.

The workload object initially had no status field, but SetNestedField added it, as expected.

Copy link
Member

Choose a reason for hiding this comment

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

I mean we can't assume all resource's status represent by .status, that might be .status-xxx or something else. That's why we design InterpretStatus operation for.

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 get your point now, it seems we can't use retry in this case, let's return the error directly to trigger its resync.

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

Good Job! @iawia002
Generally looks good to me. Just two nits.

Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/lgtm

Just the nolint need to be clarified.

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 18, 2022
@iawia002 iawia002 force-pushed the aggregate-status-hook branch from b83d53e to 2f55e6c Compare January 18, 2022 12:35
@karmada-bot karmada-bot removed the lgtm Indicates that a PR is ready to be merged. label Jan 18, 2022
Copy link
Member

@RainbowMango RainbowMango left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@karmada-bot karmada-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 19, 2022
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: RainbowMango

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@karmada-bot karmada-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 19, 2022
@karmada-bot karmada-bot merged commit 0d98cc3 into karmada-io:master Jan 19, 2022
@iawia002 iawia002 deleted the aggregate-status-hook branch January 19, 2022 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants