Workspace Examples

Workspaces provide a way to segment Kong entities. Entities in a workspace are isolated from those in other workspaces. That said, entities such as Services and Routes have “routing rules”, which are pieces of info attached to Services or Routes—such as HTTP method, URI, or host—that allow a given proxy-side request to be routed to its corresponding upstream service.

Admins configuring Services (or Routes) in their workspaces do not want traffic directed to their Services or Routes to be swallowed by Services or Routes in other workspaces; Kong allows them to prevent such undesired behavior—as long as certain measures are taken. Below we outline the conflict detection algorithm used by Kong to determine if a conflict occurs.

  • At Service or Route creation or modification time, Kong runs its internal router:
    • If no Services or Routes are found with matching routing rules, the creation or modification proceeds
    • If Services or Routes with matching routing rules are found within the same workspace, proceed
    • If Services or Routes are found in a different workspace:
      • If the matching Service or Route does not have an associated host value409 Conflict
      • If the matching Service or Route’s host is a wildcard
        • If they are the same, a conflict is reported—409 Conflict
        • If they are not equal, proceed
      • If the matching Service or Route’s host is an absolute value, a conflict is reported—409 Conflict

The default workspace

Kong starts with a default workspace named default. This workspace groups all existing entities in Kong:

  • Entities that were created in operation in previous versions & in case one is migrating from an older Kong version;
  • Entities that Kong creates at migration time—e.g., RBAC credentials, which are provisioned at migration time as a convenience

It will also hold entities that are created without being explicitly assigned to a specific workspace.

That said, it’s worth noting that the default workspace is a workspace as any other, the only difference being that it’s created by Kong, at migration time.

Using the API in workspaces

Any requests that don’t specify a workspace target the default workspace.

To target a different workspace, prefix any endpoint with the workspace name or ID:

  1. http://<HOSTNAME>:8001/<WORKSPACE_NAME|ID>/<ENDPOINT>

For example, if you don’t specify a workspace, this request retrieves a list of services from the default workspace:

  1. curl -i -X GET http://localhost:8001/services

While this request retrieves all services from the workspace SRE:

  1. curl -i -X GET http://localhost:8001/SRE/services

Listing workspaces and its entities

In a fresh Kong Gateway install, submit the following request:

  1. curl -i -X GET http://localhost:8001/workspaces

Response:

  1. {
  2. "total": 1,
  3. "data": [
  4. {
  5. "created_at": 1529627841000,
  6. "id": "a43fc3f9-98e4-43b0-b703-c3b1004980d5",
  7. "name": "default"
  8. }
  9. ]
  10. }

Creating a workspace

A more interesting example would be segmenting entities by teams; for the sake of example, let’s say they are teamA and teamB.

Each of these teams has its own set of entities—say, upstream services and routes—and want to segregate their configurations and traffic; they can achieve that with workspaces.

  1. Create teamA:

    1. curl -i -X POST http://localhost:8001/workspaces \
    2. --data name=teamA

    Response:

    1. {
    2. "created_at": 1528843468000,
    3. "id": "735af96e-206f-43f7-88f0-b930d5fd4b7e",
    4. "name": "teamA"
    5. }
  2. Create teamB:

    1. curl -i -X POST http://localhost:8001/workspaces \
    2. --data name=teamB

    Response:

    1. {
    2. "name": "teamB",
    3. "created_at": 1529628574000,
    4. "id": "a25728ac-6036-497c-82ee-524d4c22fcae"
    5. }
  3. At this point, if you list workspaces, you get a total of three: the default workspace and the two team workspaces.

    1. {
    2. "data": [
    3. {
    4. "created_at": 1529627841000,
    5. "id": "a43fc3f9-98e4-43b0-b703-c3b1004980d5",
    6. "name": "default"
    7. },
    8. {
    9. "created_at": 1529628818000,
    10. "id": "5ed1c043-78cc-4fe2-924e-40b17ecd97bc",
    11. "name": "teamA"
    12. },
    13. {
    14. "created_at": 1529628574000,
    15. "id": "a25728ac-6036-497c-82ee-524d4c22fcae",
    16. "name": "teamB"
    17. }
    18. ]
    19. "total": 3,
    20. }

Entities in different workspaces can have the same name!

Different teams—belonging to different workspaces—are allowed to give any name to their entities. To provide an example of that, let’s say that Teams A and B want a particular consumer named guest—a different consumer for each team, sharing the same username.

  1. Create a consumer named guest in teamA:

    1. curl -i -X POST http://localhost:8001/teamA/consumers \
    2. --data username=guest

    Response:

    1. {
    2. "created_at": 1529703386000,
    3. "id": "2e230275-2a4a-41fd-b06b-bae37008aed2",
    4. "type": 0,
    5. "username": "guest"
    6. }
  2. Create a consumer named guest in teamB:

    1. curl -i -X POST http://localhost:8001/teamB/consumers \
    2. --data username=guest

    Response:

    1. {
    2. "created_at": 1529703390000,
    3. "id": "8533e404-8d56-4481-a919-0ee35b8a768c",
    4. "type": 0,
    5. "username": "guest"
    6. }

With this, Teams A and B will have the freedom to operate their guest consumer independently, choosing authentication plugins or doing any other operation that is allowed in the non-workspaced Kong world.

See also