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 object relationships #2

Open
Frankkkkk opened this issue Jul 28, 2020 · 6 comments
Open

Add object relationships #2

Frankkkkk opened this issue Jul 28, 2020 · 6 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Frankkkkk
Copy link
Owner

We should add a Relationship field whose role is to reference another resource.

However, how to deal with references to other namespaces ? We could support:

  • ClusterResource → ClusterResource
  • NamespacedResource(ns n) → NamespacedResource(ns n)
  • NamespacedResource(ns n) → Cluster resource
@Frankkkkk Frankkkkk added the enhancement New feature or request label Jul 28, 2020
@Frankkkkk Frankkkkk added the help wanted Extra attention is needed label Oct 22, 2020
@AnyISalIn
Copy link
Contributor

We need to deal with Relationship Resource CRUD operations.

I have two ideas.

  1. association based on ownerReferences of Kubernetes.
  2. association based on structured annotations

If we adopt the first, Kubernetes will help us deal with garbage collection. but cross-namespace owner references are disallowed.

@Frankkkkk
Copy link
Owner Author

Maybe we could have both ?
Indeed, ownerReferences are useful, but sometimes soft dependencies (that don't imply parent-child, unlike ownerRef) are also useful. I'm imagining something like:

@pykorm.k8s_custom_object
class Server(pykorm.NamespacedModel):
  pass

@pykorm.k8s_custom_object
class User(pykorm.NamespacedModel):
  # Many-to-one or one-to-one
  server = pykorm.fields.Relationship(field=pykorm.fields.Spec('server'), relationship=Server, lookup_field='name')

In this case, behind the scenes, the user CRD would have their .spec.server field equal to the name of the corresponding CRD in the same namespace.

This is easy when having references in the same namespace (for a Namespaced resource) or for the whole cluster (cluster resource). However we could, at first, disallow inter-namespace references for the sake of clarity.

What do you think ?

@Roni1993
Copy link

Hey @Frankkkkk, i'm currently looking into using pykorm as i've been writing way to much boilerplate for my liking with pykube.
this feature would be something i'd be using a lot. My use-case is as following:

  • my Kubernetes operator listens for CRD creations
  • on creation it'll generate child resources like pods, pvcs, svc etc.
  • later on specific changes of the CRD it would be usefull to easily request all children of the original CRD

as of now i'm just requesting the k8s api & then filtering in my code but it would be nice to use the relationships estabilished in ownerReferences.

i'd like to help with this feature if you can give me some guidance on how to contribute

@Frankkkkk
Copy link
Owner Author

Maybe we should decide whether we want lazy loading or not. I'd think that its not a bad idea to load on the fly so we have less "circular" checking to do.

With lazy loading, we could handle the relationship loading on the model's __getattr__. It shouldn't be really hard as we'd have the class, so we can do some querying and return the found (or not) object

On #2 (comment) I thought that sqlalchemy-like relationships sounded neat, but if you've got another idea I'm all for it !

Maybe we could start with many-to-one (or one-to-one as it's the same from the "father" object) relationships ?

Would you like to do a POC MR @Roni1993 and then we can improve on it ?

Cheers

@Roni1993
Copy link

I think lazy loading sounds like a good idea.
As in my use case I'm often requesting the CRDs but require the ownerReference relationships only in a few cases.
I think i can try to create a MR for these relationships.

Regarding the custom relationships I'm thinking that it might be a good idea to build on a similar approach like the labels are used with service selectors

Labels allow more efficient requests as the k8s API already provides special filters & it would also align with the standard approach. What do you think about this?

@danielloader
Copy link

Maybe we could have both ? Indeed, ownerReferences are useful, but sometimes soft dependencies (that don't imply parent-child, unlike ownerRef) are also useful. I'm imagining something like:

@pykorm.k8s_custom_object
class Server(pykorm.NamespacedModel):
  pass

@pykorm.k8s_custom_object
class User(pykorm.NamespacedModel):
  # Many-to-one or one-to-one
  server = pykorm.fields.Relationship(field=pykorm.fields.Spec('server'), relationship=Server, lookup_field='name')

In this case, behind the scenes, the user CRD would have their .spec.server field equal to the name of the corresponding CRD in the same namespace.

This is easy when having references in the same namespace (for a Namespaced resource) or for the whole cluster (cluster resource). However we could, at first, disallow inter-namespace references for the sake of clarity.

What do you think ?

I know it's been a while but this soft dependency pattern has a lot of benefits though I appreciate it doesn't solve all the issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants