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

Patching a resource from a value obtained from function-go-templating #146

Open
ashishkurian opened this issue Sep 13, 2024 · 2 comments
Open

Comments

@ashishkurian
Copy link

Hi Folks,

I want to patch a resource inside the function-patch-and-transform function using a value that I obtain from the function-go-templating.

I see the other way where we get data from the go template function into the p&t function here

I am attaching my sample composition below.

---
# Removed for brevity
  pipeline:
    - step: castai-ekscluster
      functionRef:
        name: function-go-templating
      input:
        apiVersion: gotemplating.fn.crossplane.io/v1beta1
        kind: GoTemplate
        source: Inline
        inline:
          template: |
            {{- $environmentConfigs := (index .context "apiextensions.crossplane.io/environment") }}
            {{- $accountIdstr := printf "%q" (index $environmentConfigs "accountId") }}
            {{- $eksClusterName := printf "%q" (index $environmentConfigs "eksClusterName") }}
            {{- $region := (index $environmentConfigs "region") }}
            {{- $deletionPolicy := .observed.composite.resource.spec.crossplaneParameters.deletionPolicy }}
            {{- $providerConfigRef := "default" }}
            ---
            apiVersion: castai.upbound.io/v1alpha1
            kind: EksClusterId
            metadata:
              annotations:
                gotemplating.fn.crossplane.io/composition-resource-name: EksClusterId
              labels:
                cast-ai-cluster: {{$eksClusterName}}
            spec:
              deletionPolicy: {{$deletionPolicy}}
              providerConfigRef:
                name: {{$providerConfigRef}}
              forProvider:
                region: {{$region}}
                accountId: {{$accountIdstr}}
                clusterName: {{$eksClusterName}}
    
    
    - step: aws-auth-patch
      functionRef:
        name: function-patch-and-transform
      input:
        apiVersion: pt.fn.crossplane.io/v1beta1
        kind: Resources
        resources:
          - name: awsauthconfigmap
            base:
              apiVersion: v1
              kind: ConfigMap
              metadata:
                name: ashish-test
                namespace: castai-agent
            patches:
              - type: FromCompositeFieldPath
                fromFieldPath: HERE I want the value from the function-go-templating
                toFieldPath: "data.mapRoles"
                transforms:
                  - type: string
                    string:
                      type: Format
                      fmt: '[{"rolearn": "%s", "username": "system:node:{{EC2PrivateDNSName}}", "groups": ["system:masters"]}]'

tagging @ytsarev as you might already be knowing solution.

@phisco
Copy link
Collaborator

phisco commented Sep 14, 2024

You mean some value from the status of the EksClusterId resource created from function-go-templating?

Unfortunately f-go-templating can't write to the environment, so you'd have to define an empty resource in f-p-and-t, patch from that to the environment and then from the environment to your configmap. But why not just access the observed resource in function-go-templating?

@ashishkurian
Copy link
Author

@phisco Can please you show that here in my example on how to access the observed resource value into the p&t function?

When I used .observed.... I got some erros. I am not at my laptop and thus could not share the exact error.

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