https://pulumi.com logo
t

tall-photographer-1935

01/06/2022, 2:34 AM
Has anyone created a YAML transformation in Pulumi (specifically Python)? I am trying to update the ip address name in an ingress object dynamically based on a generated IP address. I see from the source code that the transformation function must only take two parameters..but I'd like to do something like this:
Copy code
def add_ingress_ip_address(obj, opts):
  if obj['kind'] == 'Ingress':
    try:
      t = obj['metadata']['annotations']['<http://kubernetes.io/ingress.global-static-ip-name|kubernetes.io/ingress.global-static-ip-name>'] = **DYNAMIC BASED ON CREATED IP ADDRESS NAME**
Is that possible?
k

kind-mechanic-53546

01/06/2022, 2:39 AM
I don't speak python 🙂 but that should be possible This is what I do in typescript to set an annotation for AKS
Copy code
transformations: [
    (obj: any) => {
      if (obj.kind === "Service" && obj.metadata.name === "ingress-nginx-controller") {
        obj.metadata.annotations['<http://service.beta.kubernetes.io/azure-dns-label-name|service.beta.kubernetes.io/azure-dns-label-name>'] = 'my-dns-label'
      }
    },
  ],
b

billowy-army-68599

01/06/2022, 3:09 AM
where would you retrieve the IP from? you can pass as many function arguments as you need
t

tall-photographer-1935

01/06/2022, 4:53 AM
@billowy-army-68599 essentially, creating an IP address in my Pulumi code and then passing it into the transformation function. So, the method signature would be
add_ingress_ip_address(obj, opts, ip_address_name)
The full code might be:
Copy code
load_balancer_ip = pulumi_gcp.compute.GlobalAddress("default")
def add_ingress_ip_address(obj, opts):
  if obj['kind'] == 'Ingress':
    try:
      t = obj['metadata']['annotations']['<http://kubernetes.io/ingress.global-static-ip-name|kubernetes.io/ingress.global-static-ip-name>'] = opts.ip_name
    except KeyError:
      pass

# How to pass the IP name into here?
console_app_yaml = k8s.yaml.ConfigFile('console', file='sample.yaml', transformations=[add_ingress_ip_address])
@kind-mechanic-53546 interesting. So that value can be set dynamically? Not just static within the function?
right now if I call
k

kind-mechanic-53546

01/06/2022, 10:55 PM
@tall-photographer-1935, I don't know exactly how the scoping works in python but at least in typescript, everything currently in scope is available in the function passed to transformations Where is your opts.ip_name coming from? As a rough guess based on my limited python understanding, if you define your ip_name before your function, then it should be available without passing as an arg and then so long as it is set before the function is run, the value should be correct To ensure that, you might have to use the depends_on arg to ensure it's resolved before your configFile resource is created
4 Views