kubernetes-sigs / kubebuilder-declarative-pattern

A toolkit for building declarative operators with kubebuilder
Apache License 2.0
254 stars 84 forks source link

Would like to create a fanout of resources from a slice #262

Closed keithmcneill closed 1 year ago

keithmcneill commented 1 year ago

Would like to create a fanout of resources from a slice:

For example lets say the incoming declarativeObject has a list of animals

spec:
   animal:
      - cat
      - dog
      - cow

I have a resource template that I would like to expand:

apiVersion: animal.farm/v1beta1
kind: FarmAnimal
metadata:
  name: '{{ animal }}'
spec:
    name: '{{ animal }}'

And create one for each animal.

One route I see is to do something like

{{range $index, $animal := .Spec.animal }}
apiVersion: animal.farm/v1beta1
kind: FarmAnimal
metadata:
  name: '{{ $animal }}'
spec:
    name: '{{ $animal }}'
{{ end }}

Using this with WithRawManifestOperation and a function that does some template processing.

I'm able to expand this.

Another route is with using WithObjectTransform and manually expanding the list of *manifest.Objects.

What is the preferred route to do something like this?

Thanks!

justinsb commented 1 year ago

RawManifest operations are string operations and thus ultimately powerful, but also a lot easier to accidentally produce invalid yaml etc.

So personally I would add an ObjectTransform that creates or duplicates objects. One way would be to mark a "template" object with an well-known annotation and then loop over your spec values and duplicate the object, removing the "template" object and adding the cloned instances to Items. In this case I would have something like:

apiVersion: animal.farm/v1beta1
kind: FarmAnimal
metadata:
  annotations:
    example.com/template: animal

And then you code would add metdata.name and spec.name.

If the object is fairly simple, you could also just generate it directly in the transform code. It's likely slightly simpler if the object is indeed as simple as your example, but it has the downside that it means that any changes to the object would need a code change (instead of just a manifest change).

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-ci-robot commented 1 year ago

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to [this](https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern/issues/262#issuecomment-1529144298): >The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs. > >This bot triages issues according to the following rules: >- After 90d of inactivity, `lifecycle/stale` is applied >- After 30d of inactivity since `lifecycle/stale` was applied, `lifecycle/rotten` is applied >- After 30d of inactivity since `lifecycle/rotten` was applied, the issue is closed > >You can: >- Reopen this issue with `/reopen` >- Mark this issue as fresh with `/remove-lifecycle rotten` >- Offer to help out with [Issue Triage][1] > >Please send feedback to sig-contributor-experience at [kubernetes/community](https://github.com/kubernetes/community). > >/close not-planned > >[1]: https://www.kubernetes.dev/docs/guide/issue-triage/ Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.