Search code examples
kuberneteskubernetes-ingressingress-controller

Can someone explain this K8s ingress file


I have encountered this ingress file and struggling to understand http (& , *) meaning in this ingress file- Can some one please explain * and & meaning here?

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt
  name: my-example
spec:
  rules:
  - host: example.com
    http: &exampleBackendService
      paths:
      - backend:
          serviceName: my-example
          servicePort: 80
        path: /
  - host: example-dev.com
    http: *exampleBackendService

Solution

  • It's all yaml grammer, nothing with k8s. see Anchors and Aliases, or a simpler tutorial.
    The purpose is reuse, shorter and maintainability.

    You can use python(among many choices) to expand:

    import yaml
    
    
    class ExplicitDumper(yaml.SafeDumper):
        def ignore_aliases(self, data):
            return True
    
    
    d = yaml.safe_load('''your very long yaml string''')
    print(yaml.dump(d, Dumper=ExplicitDumper))
    

    output

    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      annotations:
        cert-manager.io/cluster-issuer: letsencrypt
      name: my-example
    spec:
      rules:
      - host: example.com
        http:
          paths:
          - backend:
              serviceName: my-example
              servicePort: 80
            path: /
      - host: example-dev.com
        http:
          paths:
          - backend:
              serviceName: my-example
              servicePort: 80
            path: /