datadogpulumipulumi-python

pulumi yaml file to set config in values.yaml


I´m qite new at pulumi (I´m more a terraform guy). I´m working in a legacy code which deploys helm charts into kubernetes. (splunk, Datadog, etc). They are using python for this. In code I see that they have a pulumi.[env].chart_name to pass some values I believe. Like follows:

config:
  kubernetes:context: my-aks-cluster
  toolbase:baseStack: dev.sandbox
  toolbase:cluster-name: aks-foo-123
  toolbase:cluster-tags: aks-foo-123
  toolbase:datadog-logLevel: DEBUG
  tooling:some other value.....

in some other python file I see they use a values option to override parameters using for example: ""logLevel": config.require('datadog-logLevel')," and they override that with the yaml file that I previous showed.

values={
            "nameOverride": "",
            "fullnameOverride": "",
            "targetSystem": "linux",
            "datadog": {
                "apiKey": datadog_creds.data.get('api-key'),
                "logLevel": config.require('datadog-logLevel'),
                "securityContext": {},

My question is How this actually works? I want to now use the yaml file to pass some key:value pairs for some pod labels in the values.yaml file but I´m not sure how to do this with an object type. for exxample:

toolbase:podlabels: {label1:foo, label2:bar, labelx:some othervalue}

is this possible?

thank you


Solution

  • Yep -- take a look at the docs on structured configuration. You're most likely looking for either get_object() or require_object().

    The following Python program, for example, fetches an object from the selected stack's configuration file and uses it to apply some tags to an S3 bucket:

    import pulumi
    from pulumi_aws import s3
    
    config = pulumi.Config()
    bucket_tags = config.require_object("bucket_tags")
    
    pulumi.log.info(bucket_tags["tag_1"])
    pulumi.log.info(bucket_tags["tag_2"])
    
    bucket = s3.Bucket("my-bucket", s3.BucketArgs(
        tags=bucket_tags,
    ))
    
    pulumi.export("bucket_name", bucket.id)
    

    To set the object's data, you have a couple of options. With the CLI, you can use pulumi config set with the --path option to define the object's structure and data:

    pulumi config set --path "bucket_tags.tag_1" "value1"
    pulumi config set --path "bucket_tags.tag_2" "value2"
    

    This'll give you a Pulumi.<stack-name>.yaml file that looks something like this:

    config:
      aws:region: us-west-2
      my-project:bucket_tags:
        tag_1: value1
        tag_2: value2
    

    Alternatively, you could write these values directly into the YAML file yourself. This is fine, too, so long as what you write is valid YAML object syntax.

    The result, after pulumi up:

    $ pulumi up
    
    Updating (dev)
    
         Type                 Name             Status           Info
     +   pulumi:pulumi:Stack  my-project-dev   created (3s)     2 messages
     +   └─ aws:s3:Bucket     my-bucket        created (2s)     
    
    Diagnostics:
      pulumi:pulumi:Stack (my-project-dev):
        value1
        value2
    
    Outputs:
        bucket_name: "my-bucket-bd67797"
    
    Resources:
        + 2 created
    
    Duration: 4s
    

    The tags applied to the s3

    Hope that helps!