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

Feature Request: Selector Support (equivalent to ResourceSourceSelector functionality) in Sveltos #473

Open
kahirokunn opened this issue Feb 6, 2025 · 2 comments

Comments

@kahirokunn
Copy link
Contributor

Currently, templateResourceRefs in both addon-controller and event-manager only support ObjectReference, as implemented in the following example:

https://github.com/projectsveltos/addon-controller/blob/d6fbb74b53d512cd2ebeeb1dbc914f379b689ebe/api/v1beta1/spec.go#L518-L532

Meanwhile, Crossplane's function-extra-resources supports two methods of resource referencing:

  1. ObjectReference
    Similar to the existing implementation, this method references resources by specifying type: Reference

  2. ResourceSourceSelector
    This method enables selector-based resource selection that cannot be handled by ObjectReference
    (For details, please refer to https://github.com/crossplane-contrib/function-extra-resources/blob/7a4ef280ea9aea3e37b3f0d324188537e4a6b4fe/input/v1beta1/resource_select.go#L69-L94)

Here's an example YAML using ResourceSourceSelector:

extraResources:
  - kind: XCluster
    into: XCluster
    apiVersion: example.crossplane.io/v1
    type: Selector
    selector:
      maxMatch: 2
      minMatch: 1
      matchLabels:
        - key: type
          type: Value
          value: cluster

As a practical example, extraResources can be pulled within a Composition and used for template processing:

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: function-environment-configs
spec:
  compositeTypeRef:
    apiVersion: example.crossplane.io/v1
    kind: XR
  mode: Pipeline
  pipeline:
  - step: pull-extra-resources
    functionRef:
      name: function-extra-resources
    input:
      apiVersion: extra-resources.fn.crossplane.io/v1beta1
      kind: Input
      spec:
        extraResources:
          - kind: XCluster
            into: XCluster
            apiVersion: example.crossplane.io/v1
            type: Selector
            selector:
              minMatch: 0
              matchLabels:
                - key: type
                  type: Value
                  value: cluster
  - step: go-templating
    functionRef:
      name: function-go-templating
    input:
      apiVersion: gotemplating.fn.crossplane.io/v1beta1
      kind: GoTemplate
      source: Inline
      inline:
        template: |
            {{- $XClusters := index (index .context "apiextensions.crossplane.io/extra-resources") "XCluster" }}
            {{- range $i, $A := $XClusters }}
            ---
            apiVersion: vault.upbound.io/v1beta1
            kind: VaultRole
            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: {{index (index $A "metadata") "name"}}
            spec:
              forProvider:
            {{- end}}

We believe implementing selector support (preferably with a mechanism similar to ResourceSourceSelector) in Sveltos would offer the following benefits:

  1. Reference resources with dynamic names
    While ObjectReference requires fixed names, using selectors enables flexible referencing of dynamically generated resources.

  2. Generate resources based on multiple resource contents
    This enables flexible operations that consider the states and attributes of multiple resources when generating final resources. While aggregatedSelection is supported in EventSource, we would like to see similar functionality in EventTrigger and ClusterProfile as well.

  3. Enable recursive processing
    This enables recursive processing, such as referencing resources generated within the same ClusterProfile or EventTrigger, which previously required combining multiple ClusterProfiles.

For these reasons, we would like to request your consideration for implementing selector support (functionality equivalent to ResourceSourceSelector) in Sveltos. We believe this would expand the range of use cases by allowing users to reference and utilize resources more flexibly.

Thank you for your consideration.

@gianlucam76
Copy link
Member

Hi @kahirokunn the approach is Sveltos is different. If you need to collect N resources via label selector for instance, you would create an EventReport with:

  1. EventSource that selects all resources you need (via label selector)
  2. (for instance) create a ConfigMap with one entry per resource and deploy a ClusterProfile that fetches via TemplateResourceRefs this configMap

After point 2, the ClusterProfile has access to all its need. But the ClusterProfile remain simple (just one resource fetched and an identifier associated to it making it easy to use it inside the template).

If you are OK we can close this. Or better if you have any such concrete example, we can write a tutorial on how to use EventTrigger + ClusterProfile to solve this use case.

@kahirokunn
Copy link
Contributor Author

kahirokunn commented Feb 7, 2025

step1: Create an EventSource and an empty EventTrigger, then generate an EventReport.
step2: Directly reference the EventReport from the ClusterProfile's templateResourceRefs, base64 decode the spec.resources and loop through them in a for loop.
Is that correct?
I feel that if there was documentation here, Sveltos would be more helpful.
Thank you. 🙏

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

No branches or pull requests

2 participants