Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions examples/logicalclusters/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# LogicalCluster to workspace hierarchy Example

This example demonstrates how to resolve a `LogicalCluster` to a `Workspace` using the KCP API and
This example demonstrates how to resolve a `LogicalCluster` to a `Workspace` using the kcp API and
walk up the workspace hierarchy tree, displaying parent workspace information. This might be useful
if one stores metadata in parent workspace and wants to access it.
if one stores metadata in a parent workspace and wants to access it.

But one needs to be careful when doing this, as walking up the hierarchy can be expensive if done
frequently, and workspaces are located on different shards, so each step up the hierarchy might
require a network call.
require a network call. In addition to fetching the workspace object (namespace in this example), `kcpkubernetesClient`
needs to point to the front-proxy so cross-shard calls can be made.

Consider the architecture, where a resolution controller, based on the example here, runs one instance per shard,
and resolves parent workspace data to get metadata. If this is done often and on each reconcile loop - this
will soon become a bottleneck for this reconciler. So while possible, this type of architecture needs to be implemented with caution.

## Setup

Expand All @@ -20,15 +25,15 @@ kubectl ws create provider --enter

```yaml
kubectl apply -f - <<EOF
apiVersion: apis.kcp.io/v1alpha1
apiVersion: apis.kcp.io/v1alpha2
kind: APIExport
metadata:
name: logicalcluster.workspaces.kcp.dev
spec:
permissionClaims:
- all: true
group: core.kcp.io
- group: core.kcp.io
resource: logicalclusters
verbs: ["*"]
EOF
```

Expand Down Expand Up @@ -64,5 +69,5 @@ go run . --server=$(kubectl get apiexportendpointslice logicalcluster.workspaces
The example controller:
- Watches for `LogicalCluster` resources
- Extracts the workspace path from the LogicalCluster annotation
- Walks up the workspace hierarchy to collect parent workspace information
- Walks up the workspace hierarchy to collect parent workspace information. Assuming one can talk to the front proxy.
- Displays the hierarchy in a tree format showing workspace paths, namespace names, and UUIDs
2 changes: 2 additions & 0 deletions examples/logicalclusters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func main() {
cfgRoot.Host = strings.Split(cfgRoot.Host, "/clusters/")[0]

// Assume that our restConfig we use for this example can access all the workspaces:
// IMPORTANT: This should be shard-aware client, meaning it should go via front-proxy to be able to resolve
// objects across shards.
kcpkubernetesClient, err := kcpkubernetesclient.NewForConfig(cfgRoot)
if err != nil {
entryLog.Error(err, "failed to create kcp kubernetes client")
Expand Down