github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/package-examples/ghost/ghost-app/fn-config-validate-host.yaml (about)

     1  apiVersion: fn.kpt.dev/v1alpha1
     2  kind: StarlarkRun
     3  metadata:
     4    name: validate-blog-host
     5    annotations:
     6      config.kubernetes.io/local-config: "true"
     7    labels:
     8      app.kubernetes.io/name: ghost-app
     9  source: |-
    10    def validate_host(resources):
    11      placeHolderHostname = "example.com"
    12      for resource in resources:
    13        # this is an abstract package, so ignore host name validation
    14        if resource["kind"] == "ConfigMap" and resource["metadata"]["name"] == "kptfile.kpt.dev" and resource["data"]["name"] == "example":
    15          return
    16      host_in_ingress = ""
    17      host_in_deployment = ""
    18      for resource in ctx.resource_list["items"]:
    19        if resource["kind"] == "Ingress":
    20          host_in_ingress = resource["spec"]["rules"][0]["host"]
    21        if resource["kind"] == "Deployment":
    22          container = resource["spec"]["template"]["spec"]["containers"][0]
    23          for env in container["env"]:
    24            if env["name"] == "GHOST_HOST":
    25              host_in_deployment = env["value"]
    26      if host_in_ingress == placeHolderHostname:
    27        fail("Blog's hostname must be specified in ingress-ghost.yaml")
    28      if host_in_ingress != host_in_deployment:
    29        fail("Blog host name in deployment object does not match with the one in ingress object")
    30  
    31    validate_host(ctx.resource_list["items"])