Integrate the Application with Notification Client Sidecar

Overview

This section provides an example of kubernetes deployment of the sidecar and a simulated application. The user application must support querying and handling responses for either of the API definitions available at:

See:

https://docs.starlingx.io/api-ref/ptp-notification-armada-app/api_ptp_notifications_definition_v2.html

The integration between the application is done with the use of a Sidecar. The Sidecar runs as a container along with the application in the same pod. The application and the Sidecar communicate via a REST API. See the figure below.

To use the v2 API, deploy starlingx/notificationclient-base:stx.14.0-v2.7.0 as the sidecar container.

Prerequisites

The following prerequisites are required before the integration:

  • The cloud is configured with a node that supports the Subordinate mode (Secondary mode).

  • The cloud is labeled with ptp-registration=true, and ptp-notification=true.

  • The ptp-notification-armada-app application is installed successfully.

  • The application supports the PTP Notifications API.

The following example deploys the notification client sidecar alongside a minimal stand-in application. The sidecar runs as a non-root user with all capabilities dropped. Its command starts the notification client API server inline. A suitable user-provided application container must run in the same pod and be configured to make API calls to the notificationclient-base container.

The second container in the example is a minimal stand-in for the user application. It reuses the notificationclient-base image and listens on port 9090, which is the EndpointUri port used in the subscription examples below. The container logs every notification it receives. These logs can be viewed using the kubectl logs <pod name> -c ptp-notification-demo-v2-app -n ptpexample command.

The curl commands shown in the example can be run from within either container, for instance, by using kubectl exec.

Note

The following example is for ptp-notification v2 API.

Note

If the rabbitmq username and password values were modified as per “Change default rabbitmq credentials”, then the values for REGISTRATION_USER, REGISTRATION_PASS, NOTIFICATIONSERVICE_USER and NOTIFICATIONSERVICE_PASS below need to be changed to match when integrating with ptp-notification.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ptp-notification-example
  namespace: ptpexample
  labels:
    release: RELEASE-NAME
    app: ptp-notification-example
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ptp-notification-example
      release: RELEASE-NAME
  template:
    metadata:
      namespace: ptpexample
      labels:
        app: ptp-notification-example
        release: RELEASE-NAME
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: ptp-notification
                operator: In
                values:
                - "true"
      containers:
        - name: ptp-notification-demo-v2-sidecar
          image: "starlingx/notificationclient-base:stx.14.0-v2.7.0"
          imagePullPolicy: IfNotPresent
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
            runAsGroup: 1000
            allowPrivilegeEscalation: false
            capabilities:
              drop:
              - ALL
          tty: true
          stdin: true
          env:
            - name: THIS_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: THIS_POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: REGISTRATION_HOST
              value: "registration.notification.svc.cluster.local"
            - name: REGISTRATION_USER
              value: "admin"
            - name: REGISTRATION_PASS
              value: "admin"
            - name: REGISTRATION_PORT
              value: "5672"
            - name: NOTIFICATIONSERVICE_USER
              value: "admin"
            - name: NOTIFICATIONSERVICE_PASS
              value: "admin"
            - name: NOTIFICATIONSERVICE_PORT
              value: "5672"
            - name: SIDECAR_API_PORT
              value: "8080"
            - name: DATASTORE_PATH
              value: "/opt/datastore"
            - name: LOGGING_LEVEL
              value: "INFO"
          command:
            - python3
            - -c
            - |
              import os
              from pecan.deploy import deploy
              from waitress import serve
              port = os.environ.get("SIDECAR_API_PORT", "8080")
              app = deploy("/opt/notificationclient/config.py")
              serve(app, listen=f"0.0.0.0:{port} [::]:{port}")
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /health
              port: 8080
              scheme: HTTP
            initialDelaySeconds: 60
            periodSeconds: 3
            successThreshold: 1
            timeoutSeconds: 3
          volumeMounts:
            - name: data-volume
              mountPath: /opt/datastore
          resources:
            requests:
              cpu: 50m
              memory: 128Mi
            limits:
              cpu: 300m
              memory: 256Mi
        - name: ptp-notification-demo-v2-app
          image: "starlingx/notificationclient-base:stx.14.0-v2.7.0"
          imagePullPolicy: IfNotPresent
          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
            runAsGroup: 1000
            allowPrivilegeEscalation: false
            capabilities:
              drop:
              - ALL
          command:
            - python3
            - -c
            - |
              import http.server
              class H(http.server.BaseHTTPRequestHandler):
                  def do_POST(self):
                      n = int(self.headers.get("Content-Length", 0))
                      print(self.path, self.rfile.read(n).decode(), flush=True)
                      self.send_response(204)
                      self.end_headers()
              http.server.ThreadingHTTPServer(("", 9090), H).serve_forever()
          resources:
            requests:
              cpu: 10m
              memory: 32Mi
            limits:
              cpu: 100m
              memory: 64Mi
      volumes:
        - name: data-volume
          emptyDir: {}

Example API Calls

Below are examples of using curl to interact with the ptp-notification API. These can be used to validate the functionality of the application deployment.

Note

Some versions of curl have been seen to automatically normalize URLs which results in malformed requests to the server by removing parts of the URL containing ‘/././’ or ‘/./’

If the version of curl being used supports the flag ‘–path-as-is’ then it should be included in the command to avoid this behavior.

v2 Examples

Some commands can target the status of specific node names or of specific ptp4l instances. The names in the format <name> will vary depending on the user’s environment.

The “| python -m json.tool” portion of the command is just for output formatting and is not required for operation.

Requests with the “/././” path with be automatically directed to the ptp-notification server on the local node, while providing “/./<node name>” will route to the specified node

## pull

# overall

curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/././sync/sync-status/sync-state/CurrentState |
python -m json.tool
curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/./<node name>/sync/sync-status/sync-state/CurrentState |
python -m json.tool

# ptp state

curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/././sync/ptp-status/lock-state/CurrentState |
python -m json.tool
curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/./<node name>/sync/ptp-status/lock-state/CurrentState |
python -m json.tool
curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/./<node name>/<ptp instance name>/sync/ptp-status/lock-state/CurrentState |
python -m json.tool

# ptp class

curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/././sync/ptp-status/clock-class/CurrentState |
python -m json.tool
curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/./<node name>/sync/ptp-status/clock-class/CurrentState |
python -m json.tool
curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/./<node name>/<ptp instance name>/sync/ptp-status/clock-class/CurrentState |
python -m json.tool

# phc2sys / os clock state

curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/././sync/sync-status/os-clock-sync-state/CurrentState |
python -m json.tool
curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/./<node name>/sync/sync-status/os-clock-sync-state/CurrentState |
python -m json.tool

# gnss

curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/././sync/gnss-status/gnss-sync-status/CurrentState |
python -m json.tool
curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:8080/ocloudNotifications/v2/./<node name>/sync/gnss-status/gnss-sync-status/CurrentState |
python -m json.tool

## subscribe

# subscribe overall

curl --path-as-is -v -d '{"ResourceAddress": "/././sync/sync-status/sync-state", "EndpointUri":
"http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type: application/json' -X POST
http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool
curl --path-as-is -v -d '{"ResourceAddress": "/./<node name>/sync/sync-status/sync-state",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type:
application/json' -X POST http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool

# subscribe PTP lock state

curl --path-as-is -v -d '{"ResourceAddress": "/././sync/ptp-status/lock-state",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type: application/json'
-X POST http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool
curl --path-as-is -v -d '{"ResourceAddress": "/./<node name>/sync/ptp-status/lock-state",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type: application/json'
-X POST http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool

# subscribe PTP clock class

curl --path-as-is -v -d '{"ResourceAddress": "/././sync/ptp-status/clock-class",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}'
-H 'Content-Type: application/json' -X POST
http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool
curl --path-as-is -v -d '{"ResourceAddress": "/./<node name>/sync/ptp-status/clock-class",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type: application/json'
-X POST http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool

# subscribe Os clock

curl --path-as-is -v -d '{"ResourceAddress": "/././sync/sync-status/os-clock-sync-state",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type:
application/json' -X POST http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool
curl --path-as-is -v -d '{"ResourceAddress": "/./<node name>/sync/sync-status/os-clock-sync-state",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type: application/json'
-X POST http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool

# subscribe gnss

curl --path-as-is -v -d '{"ResourceAddress": "/././sync/gnss-status/gnss-sync-status",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type: application/json'
-X POST http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool
curl --path-as-is -v -d '{"ResourceAddress": "/./<node name>/sync/gnss-status/gnss-sync-status",
"EndpointUri": "http://127.0.0.1:9090/v2/resource_status/ptp"}' -H 'Content-Type:
application/json' -X POST
http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool

## List subscriptions

curl --path-as-is -v -H 'Content-Type: application/json'
http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions |
python -m json.tool

# unsubscribe

curl --path-as-is -X DELETE -v -H 'Content-Type: application/json'
http://127.0.0.1:${SIDECAR_API_PORT}/ocloudNotifications/v2/subscriptions/b3862aa2-3499-11ed-a5b5-522422c3cf7d
../../_images/cak1614112389132.png