|
| 1 | +/* |
| 2 | +Copyright 2022 The KCP Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package workspacemounts |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 25 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 26 | + |
| 27 | + "github.com/kcp-dev/logicalcluster/v3" |
| 28 | + |
| 29 | + tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1" |
| 30 | +) |
| 31 | + |
| 32 | +const workspaceByURL = "WorkspaceByURL" |
| 33 | + |
| 34 | +// indexWorkspaceByURL is an index for workspaces by their URL. |
| 35 | +func indexWorkspaceByURL(obj interface{}) ([]string, error) { |
| 36 | + ws, ok := obj.(*tenancyv1alpha1.Workspace) |
| 37 | + if !ok { |
| 38 | + return nil, fmt.Errorf("obj %T is not an Workspace", obj) |
| 39 | + } |
| 40 | + |
| 41 | + return []string{ws.Spec.URL}, nil |
| 42 | +} |
| 43 | + |
| 44 | +const workspaceMountsReferenceIndex = "WorkspacesByMountReference" |
| 45 | + |
| 46 | +type workspaceMountsReferenceKey struct { |
| 47 | + ClusterName string `json:"clusterName"` |
| 48 | + Group string `json:"group"` |
| 49 | + Resource string `json:"resource"` |
| 50 | + Name string `json:"name"` |
| 51 | + Namespace string `json:"namespace,omitempty"` |
| 52 | +} |
| 53 | + |
| 54 | +func indexWorkspaceByMountObject(obj interface{}) ([]string, error) { |
| 55 | + ws, ok := obj.(*tenancyv1alpha1.Workspace) |
| 56 | + if !ok { |
| 57 | + return []string{}, fmt.Errorf("obj is supposed to be a Workspace, but is %T", obj) |
| 58 | + } |
| 59 | + |
| 60 | + if ws.Spec.Mount == nil { |
| 61 | + return nil, nil |
| 62 | + } |
| 63 | + |
| 64 | + key := workspaceMountsReferenceKey{ |
| 65 | + ClusterName: logicalcluster.From(ws).String(), |
| 66 | + // TODO(sttts): do proper REST mapping |
| 67 | + Resource: strings.ToLower(ws.Spec.Mount.Reference.Kind) + "s", |
| 68 | + Name: ws.Spec.Mount.Reference.Name, |
| 69 | + Namespace: ws.Spec.Mount.Reference.Namespace, |
| 70 | + } |
| 71 | + cs := strings.SplitN(ws.Spec.Mount.Reference.APIVersion, "/", 2) |
| 72 | + if len(cs) == 2 { |
| 73 | + key.Group = cs[0] |
| 74 | + } |
| 75 | + bs, err := json.Marshal(key) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("unable to marshal mount reference: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + return []string{string(bs)}, nil |
| 81 | +} |
| 82 | + |
| 83 | +func indexWorkspaceByMountObjectValue(gvr schema.GroupVersionResource, obj *unstructured.Unstructured) (string, error) { |
| 84 | + key := workspaceMountsReferenceKey{ |
| 85 | + ClusterName: logicalcluster.From(obj).String(), |
| 86 | + Group: gvr.Group, |
| 87 | + Resource: gvr.Resource, |
| 88 | + Name: obj.GetName(), |
| 89 | + Namespace: obj.GetNamespace(), |
| 90 | + } |
| 91 | + bs, err := json.Marshal(key) |
| 92 | + if err != nil { |
| 93 | + return "", fmt.Errorf("unable to marshal mount reference: %w", err) |
| 94 | + } |
| 95 | + return string(bs), nil |
| 96 | +} |
0 commit comments