How to know the Ambassador service prefix at runtime in my microservice in Kubernetes

7/9/2019

Is there a way to learn the Ambassador service prefix at runtime in my microservice in Kubernetes?

Taking this config example:

---
apiVersion: ambassador/v1
kind: Mapping
name: myservice_get_mapping
prefix: /myprefix/
service: myservice

From within my docker container, I would like to get the '/myprefix/'. Either via some env variable to the deployment or programmatically if cannot be done using env variable.

Thanks.

-- ewolfman
ambassador
kubernetes

1 Answer

7/10/2019

Assuming the fact that Ambassador Mapping resource associates REST resources with Kubernetes services, you can fetch annotation metadata via JSONPath and then parse prefix: field, if I understand your question correctly.

Example for k8s service from Ambassador documentation:

kind: Service
metadata:
  name: httpbin
  annotations:
    getambassador.io/config: |
      ---
      apiVersion: ambassador/v1
      kind:  Mapping
      name:  tour-ui_mapping
      prefix: /test/
      service: http://tour
spec:
  ports:
  - name: httpbin
    port: 80

Command line to fetch prefix: field value:

$ kubectl get svc httpbin -o jsonpath='{.metadata.annotations}'| grep -w "prefix:"| awk '{print $2}'

/test/

Update:

Alternatively, you might also consider to retrieve the same result through the direct call to REST API through Bearer token authentication method:

curl -k -XGET  -H "Authorization : Bearer $MY_TOKEN" 'https://<API-server_IP>/api/v1/namespaces/default/services/httpbin' -H "Accept: application/yaml"| grep -w "prefix:"| awk '{print $2}

$MY_TOKEN variable has to be supplied with appropriate token which is entitled to perform above query against REST API as I've already pointed out in my former answer.

-- mk_sta
Source: StackOverflow