projectsveltos / addon-controller

Sveltos Kubernetes add-on controller programmatically deploys add-ons and applications in tens of clusters. Support for ClusterAPI powered clusters, Helm charts, kustomize ,YAMLs. Sveltos has built-in support for multi-tenancy.
https://projectsveltos.github.io/sveltos/
Apache License 2.0
222 stars 14 forks source link

BUG: substitutevalues:5: function "instance" not defined #565

Closed rbjorklin closed 1 month ago

rbjorklin commented 1 month ago

Problem Description

I'm trying to provision Prometheus but unfortunately I'm met with:

❯ k describe clustersummaries prometheus-capi-management
Status:
  Dependencies:  All dependencies deployed
  Feature Summaries:
    Failure Message:    failed to instantiate resource with substitute values: template: prometheus-capi-management-substitutevalues:5: function "instance" not defined: template: prometheus-capi-management-substitutevalues:5: function "instance" not defined
    Feature ID:         Kustomize
    Hash:               /alDzQ3OupAvIbPgZTTZwElQL4vp6Y4M/znsJXxzATk=
    Last Applied Time:  2024-05-28T03:44:30Z
    Status:             Provisioning
Events:                 <none>

My Cluster Profile looks like this:

apiVersion: config.projectsveltos.io/v1alpha1
kind: ClusterProfile
metadata:
  name: prometheus
  namespace: projectsveltos
spec:
  dependsOn:
    - hcloud-mvp
  clusterSelector: environment=hcloud
  syncMode: ContinuousWithDriftDetection
  kustomizationRefs:
    - kind: GitRepository
      name: cluster-repo
      namespace: projectsveltos
      path: "manifests/overlays/{{ .Cluster.metadata.name }}/prometheus"

My Kustomization file looks like this:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ../../../base/prometheus/59.0.0-1

And the contents of the folder referenced via the kustomization.yaml file was generated by executing:

❯ helm template \
        --include-crds \
        --values values.yaml \
        --version 59.0.0 \
        --namespace prometheus \
        prometheus \
        prometheus-community/kube-prometheus-stack \
        | kubectl slice --output-dir=../base/prometheus/59.0.0-1

System Information

CLUSTERAPI VERSION: v1.7.2 SVELTOS VERSION: 0.30.0 KUBERNETES VERSION: v1.30.1

Logs

rbjorklin commented 1 month ago

After looking at the section on templating and also the kustomize specific section I tried disabling templating by patching all of my configmaps with this addition to my kustomization.yaml:

patches:
  - target:
      version: v1
      kind: ConfigMap
    patch: |-
      apiVersion: v1
      kind: Configmap
      metadata:
        name: ignored # required but not used
        annotations:
          projectsveltos.io/template: "false"

Unfortunately it doesn't seem to have helped.

gianlucam76 commented 1 month ago

Thank you @rbjorklin

This is a use case I have not considered. Looking at the kustomize output, I see for instance

data:
  alertmanager-overview.json: |-
  ... legendFormat":"{{instance}}","refId":"A"}],...

that needs no instantiation.

I believe in your case you want to express path as a template

path: "manifests/overlays/{{ .Cluster.metadata.name }}/prometheus"

but then what is found inside that path requires no instantiation. So I will change Sveltos logic.

Do not assume the content of a Kustomize output is a template unless there are

// Values is a map[string]string type that allows to define a set of key-value pairs.
    // These key-value pairs can optionally leverage Go templates for further processing.
    // With Sveltos, you can define key-value pairs where the values can be Go templates.
    // These templates have access to management cluster information during deployment. This allows
    // to do more than just replace placeholders. Variables can be used to dynamically
    // construct values based on other resources or variables within the Kustomize output.
    // For example, imagine you have a Region key with a template value like:
    // '{{ index .Cluster.metadata.labels "region" }}'.
    // This template retrieves the region label from the cluster instance metadata.
    // Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
    // The output itself can also contain templates, like:
    // region: '{{ default "west" .Region }}'.
    // This way, the final output from Kustomize will have the region set dynamically based on
    // the actual region retrieved earlier.
    // +optional
    Values map[string]string `json:"values,omitempty"`

    // ValuesFrom can reference ConfigMap/Secret instances. Within the ConfigMap or Secret data,
    // it is possible to define key-value pairs. These key-value pairs can optionally leverage
    // Go templates for further processing.
    // With Sveltos, you can define key-value pairs where the values can be Go templates.
    // These templates have access to management cluster information during deployment. This allows
    // to do more than just replace placeholders. Variables can be used to dynamically
    // construct values based on other resources or variables within the Kustomize output.
    // For example, imagine you have a Region key with a template value like:
    // '{{ index .Cluster.metadata.labels "region" }}'.
    // This template retrieves the region label from the cluster instance metadata.
    // Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
    // The output itself can also contain templates, like:
    // region: '{{ default "west" .Region }}'.
    // This way, the final output from Kustomize will have the region set dynamically based on
    // the actual region retrieved earlier.
    // +optional
    ValuesFrom []ValueFrom `json:"valuesFrom,omitempty"`
gianlucam76 commented 1 month ago

Resolved. Fix will be in v0.31.0