Skip to content

Add convenient model extensions #405

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

Merged
merged 7 commits into from
May 18, 2020

Conversation

admilazz
Copy link
Contributor

@admilazz admilazz commented Apr 8, 2020

This change adds a many useful model extension methods that make the Kubernetes client smoother and more concise to use. This change helps address issue #390 .

  • Metadata is inlined, so instead of o.Metadata.X, you can use o.X(). Examples:

    o.Metadata?.Name -> o.Name()
    o.Metadata?.NamespaceProperty -> o.Namespace()
    ... etc ...
    
  • Managing labels and annotations. Examples for labels:

    string value = null;
    if(o.Metadata.Labels != null) o.Metadata.Labels.TryGetValue(key, out value)
    

    becomes

    o.GetLabel(key)
    

    Similarly,

    if(o.Metadata == null) o.Metadata = new V1ObjectMeta();
    if(o.Metadata.Labels == null) o.Metadata.Labels = new Dictionary<string,string>();
    o.Metadata.Labels[key] = value;
    

    becomes

    o.SetLabel(key, value)
    
  • Managing owner and object references

    • o.AddOwnerReference(ownerRef) // adds an owner reference
    • o.CreateOwnerReference(...) // create an owner reference to o
    • o.FindOwnerReference(owner) // finds and returns the reference to 'owner', if any
    • o.GetController() // gets the reference to the owner that controls the resource
    • o.GetObjectReference() // creates an object reference to o
    • ownerRef.Matches(o) // does ownerRef refer to o?
    • objRef.Matches(o) // does objRef refer to o?
    • ...
  • And some other small things.

The pull request depends on (and includes) the IKubernetesObject<T> PR I submitted earlier: #404 The only changes in this PR specifically belong to the ModelExtensions.cs file. (I'm not sure how best to represent that in a PR...)

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 8, 2020
@k8s-ci-robot k8s-ci-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Apr 8, 2020
@admilazz admilazz changed the title Add convenietn model extensions Add convenient model extensions Apr 8, 2020
@admilazz
Copy link
Contributor Author

admilazz commented Apr 8, 2020

/assign @krabhishek8260

@brendandburns
Copy link
Contributor

I'll wait for this PR to be rebased on the other one when it merges.

@admilazz
Copy link
Contributor Author

admilazz commented Apr 9, 2020

I'll wait for this PR to be rebased on the other one when it merges.

Okay, I think that's done.

@admilazz
Copy link
Contributor Author

I'm pretty sure that test failure is unrelated to my changes. Any way to rerun them?

@macsux
Copy link
Contributor

macsux commented Apr 17, 2020

I'm pretty sure that test failure is unrelated to my changes. Any way to rerun them?
Just add another commit or do what I do: reword the commit and force push it, it will trigger rebuild

@tg123
Copy link
Member

tg123 commented Apr 17, 2020

I'm pretty sure that test failure is unrelated to my changes. Any way to rerun them?
Just add another commit or do what I do: reword the commit and force push it, it will trigger rebuild

re-triggered

@admilazz
Copy link
Contributor Author

re-triggered

Magic. But now it needs a rebase anyway. :-P

@tg123
Copy link
Member

tg123 commented Apr 17, 2020

re-triggered

Magic. But now it needs a rebase anyway. :-P

there is a re-run button on top-right of console output
https://github.com/kubernetes-client/csharp/pull/405/checks?check_run_id=596493449

@macsux
Copy link
Contributor

macsux commented Apr 20, 2020

there is a re-run button on top-right of console output

I don't have this button. Maybe you have more permissions then I do

@brendandburns
Copy link
Contributor

Couple more comments.

@admilazz
Copy link
Contributor Author

Couple more comments.

Addressed.

@macsux
Copy link
Contributor

macsux commented Apr 25, 2020

Tried using this as part of controller codebase. Very convenient - hope it gets merged in soon. One method I recommend you add that I found wanting: AddOwnerReference(IKubernetesObject)

@admilazz
Copy link
Contributor Author

admilazz commented Apr 25, 2020

Tried using this as part of controller codebase. Very convenient - hope it gets merged in soon. One method I recommend you add that I found wanting: AddOwnerReference(IKubernetesObject)

I had methods for creating object and owner references but removed them because my implementation relied on the KubernetesScheme class which was part of the fluent API PR. (I could easily implement them a different way here, but then I wouldn't be able to have the KubernetesScheme-based versions in the fluent API).

Anyway, AFAIK think this PR is ready to merge if people want it...

@macsux
Copy link
Contributor

macsux commented Apr 25, 2020 via email

@brendandburns
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 18, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: admilazz, brendandburns

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 18, 2020
@k8s-ci-robot k8s-ci-robot merged commit a4ff002 into kubernetes-client:master May 18, 2020
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", 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.

6 participants