By Application Name

When claiming a service, you can identify the target workloads for the binding by declaring the labels they should have. Primaza's Application Agent will project binding data just into all matching workloads.

To obtain this result, you need to declare in the ServiceClaim how to identify the workload using the application property.

Tutorial

  1. Configure the shell

    TENANT="primaza-mytenant"
    TENANT_CLUSTER_CONTEXT="kind-main"
    TARGET_CLUSTER_CONTEXT="kind-worker" # it would be main in Single-Cluster and Single-Namespace scenarios
    APPLICATION_NAMESPACE="applications" # it would be primaza-mytenant in Single-Namespace scenario
    
  2. Create a RegisteredService for the service to bind to our application

    cat << EOF | kubectl apply -f - \
        --context "$TENANT_CLUSTER_CONTEXT" \
        --namespace "$TENANT"
    apiVersion: primaza.io/v1alpha1
    kind: RegisteredService
    metadata:
      name: claim-by-labels
    spec:
      serviceClassIdentity:
      - name: type
        value: dummy
      - name: scope
        value: claim-by-labels
      serviceEndpointDefinition:
      - name: url
        value: https://my-app-by-labels-service.dev
      - name: password
        value: SomeoneThinksImAPassword
    EOF
    
  3. Create our target application

    kubectl create deployment my-app-labels --image bash:latest \
        --context "$TARGET_CLUSTER_CONTEXT" \
        --namespace "$APPLICATION_NAMESPACE" \
        -- sleep infinity
    
  4. Create the ServiceClaim in Primaza's Control Plane

    cat << EOF | kubectl apply -f - \
        --context "$TENANT_CLUSTER_CONTEXT" \
        --namespace "$TENANT"
    apiVersion: primaza.io/v1alpha1
    kind: ServiceClaim
    metadata:
      name: my-app-dummy-by-labels
    spec:
      serviceClassIdentity:
      - name: type
        value: dummy
      - name: scope
        value: claim-by-labels
      serviceEndpointDefinitionKeys:
      - url
      - password
      target:
        environmentTag: demo
      application:
        kind: Deployment
        apiVersion: apps/v1
        selector:
          matchLabels:
            app: my-app-labels
    EOF
    
  5. Wait for Service Binding to be created and Resolved

    until kubectl get servicebindings my-app-dummy-by-labels \
        --context "$TARGET_CLUSTER_CONTEXT" \
        --namespace "$APPLICATION_NAMESPACE"
    do sleep 2; done
    kubectl wait --for=condition=Bound servicebindings my-app-dummy-by-labels \
        --context "$TARGET_CLUSTER_CONTEXT" \
        --namespace "$APPLICATION_NAMESPACE"
    
  6. Read data injected in the workload

    until kubectl exec "pod/$(kubectl get pod \
            -l app=my-app-labels \
            -o jsonpath="{.items[0].metadata.name}" \
            --context "$TARGET_CLUSTER_CONTEXT" \
            --namespace "$APPLICATION_NAMESPACE" )" \
        --context "$TARGET_CLUSTER_CONTEXT" \
        --namespace "$APPLICATION_NAMESPACE" \
        -- bash -c '[ -d "/bindings/my-app-dummy-by-labels" ] && ( ls /bindings/my-app-dummy-by-labels | xargs -I@ bash -c '"'"'echo "$1: $(cat /bindings/my-app-dummy-by-labels/$1)"'"'"' -- @ )'
        do sleep 5; done