/* Copyright 2022 The kcp Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package index import ( "context" "fmt" "maps" "net/url" "strings" "sync" "time" "k8s.io/apimachinery/pkg/api/equality" "github.com/kcp-dev/logicalcluster/v3" corev1alpha1 "github.com/kcp-dev/sdk/apis/core/v1alpha1" tenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1" "github.com/kcp-dev/kcp/pkg/contextmanager" ) // defaultMigrationGracePeriod is the period during which a cluster is considered "recently migrated". // Watches with a non-zero RV (so clients trying to reestablish watch) get a 410 returned while a // cluster is considered recently migrated to force the client to do a full relist. // // This is set to 90s since client-go reflectors, the most likely clients, may have started to backoff // trying to reconnect during the migration. The backoff ceiling is 60s, so 90s should be enough of a // window for all clients to receive a 410 and start a new watch. const defaultMigrationGracePeriod = 90 * time.Second // Index implements a mapping from logical cluster to (shard) URL. type Index interface { Lookup(path logicalcluster.Path) (Result, bool) LookupURL(logicalCluster logicalcluster.Path) (Result, bool) } // Result is the result of a lookup. type Result struct { URL string Shard string // Cluster canonical path Cluster logicalcluster.Name // Type is not set for mounted workspaces. For all others this value is the // fully-qualified name of the type, e.g. "root:universal". Type logicalcluster.Path // ErrorCode is the HTTP error code to return for the request. // If this is set, the URL and Shard fields are ignored. ErrorCode int } // PathRewriter can rewrite a logical cluster path before the actual mapping through // the index data. type PathRewriter func(segments []string) []string // State is a runtime graph of shards, workspaces, logicalclusters and mounts. // It usually gets fed by a controller that watches these resources on each // shard. The state then organizes the graph and offers a workspace path lookup // that resolves a workspace path to its physical location on a shard and logical // cluster. type State struct { rewriters []PathRewriter lock sync.RWMutex clusterShards map[logicalcluster.Name]string // logical cluster -> shard name shardClusterWorkspaceNameCluster map[string]map[logicalcluster.Name]map[string]logicalcluster.Name // (shard name, logical cluster, workspace name) -> logical cluster shardClusterWorkspaceName map[string]map[logicalcluster.Name]string // (shard name, logical cluster) -> workspace name shardClusterWorkspaceType map[string]map[logicalcluster.Name]logicalcluster.Path // (shard name, logical cluster) -> workspace type shardClusterParentCluster map[string]map[logicalcluster.Name]logicalcluster.Name // (shard name, logical cluster) -> parent logical cluster shardBaseURLs map[string]string // shard name -> base URL // Experimental feature: allow mounts to be used with Workspaces shardClusterWorkspaceMount map[string]map[logicalcluster.Name]map[string]tenancyv1alpha1.WorkspaceSpec // (shard name, logical cluster, workspace name) -> WorkspaceSpec shardClusterWorkspaceNameErrorCode map[string]map[logicalcluster.Name]map[string]int // (shard name, logical cluster, workspace name) -> error code // now is overridable for testing. now func() time.Time // migrated at maps logicalcluster.Name to the timestamp it was migrated. // The timestamp is cleared after the grace period on the next request. migratedAt sync.Map // migrationGracePeriod is the grace period for migratedAt. migrationGracePeriod time.Duration // clusterContexts allows deriving contexts from both a connection and a shared per-cluster context. clusterContexts *contextmanager.Manager[logicalcluster.Name] } func New(rewriters []PathRewriter) *State { return &State{ rewriters: rewriters, clusterShards: map[logicalcluster.Name]string{}, shardClusterWorkspaceNameCluster: map[string]map[logicalcluster.Name]map[string]logicalcluster.Name{}, shardClusterWorkspaceName: map[string]map[logicalcluster.Name]string{}, shardClusterWorkspaceType: map[string]map[logicalcluster.Name]logicalcluster.Path{}, shardClusterParentCluster: map[string]map[logicalcluster.Name]logicalcluster.Name{}, shardBaseURLs: map[string]string{}, // Experimental feature: allow mounts to be used with Workspaces // structure: (shard, logical cluster, workspace name) -> string serialized mount objects // This should be simplified once we promote this to workspace structure. shardClusterWorkspaceMount: map[string]map[logicalcluster.Name]map[string]tenancyv1alpha1.WorkspaceSpec{}, // shardClusterWorkspaceNameErrorCode is a map of shar,logical cluster, workspace to error code when we want to return an error code // instead of a URL. shardClusterWorkspaceNameErrorCode: map[string]map[logicalcluster.Name]map[string]int{}, migrationGracePeriod: defaultMigrationGracePeriod, now: time.Now, clusterContexts: contextmanager.New[logicalcluster.Name](context.Background()), } } func (c *State) UpsertWorkspace(shard string, ws *tenancyv1alpha1.Workspace) { if ws.Status.Phase == corev1alpha1.LogicalClusterPhaseScheduling { return } clusterName := logicalcluster.From(ws) c.lock.Lock() defer c.lock.Unlock() // If the workspace is unavailable, we set custom error code for it. And add it to the index as normal. // TODO(mjudeikis): Once we have one more case - move to a separate function. if ws.Status.Phase == corev1alpha1.LogicalClusterPhaseUnavailable { if c.shardClusterWorkspaceNameErrorCode[shard] == nil { c.shardClusterWorkspaceNameErrorCode[shard] = map[logicalcluster.Name]map[string]int{} } if c.shardClusterWorkspaceNameErrorCode[shard][clusterName] == nil { c.shardClusterWorkspaceNameErrorCode[shard][clusterName] = map[string]int{} } // Unavailable workspaces should return 503 c.shardClusterWorkspaceNameErrorCode[shard][clusterName][ws.Name] = 503 } else { delete(c.shardClusterWorkspaceNameErrorCode[shard][clusterName], ws.Name) if len(c.shardClusterWorkspaceNameErrorCode[shard][clusterName]) == 0 { delete(c.shardClusterWorkspaceNameErrorCode[shard], clusterName) } } if cluster := c.shardClusterWorkspaceNameCluster[shard][clusterName][ws.Name]; cluster.String() != ws.Spec.Cluster { if c.shardClusterWorkspaceNameCluster[shard] == nil { c.shardClusterWorkspaceNameCluster[shard] = map[logicalcluster.Name]map[string]logicalcluster.Name{} } if c.shardClusterWorkspaceName[shard] == nil { c.shardClusterWorkspaceName[shard] = map[logicalcluster.Name]string{} } if c.shardClusterParentCluster[shard] == nil { c.shardClusterParentCluster[shard] = map[logicalcluster.Name]logicalcluster.Name{} } if c.shardClusterWorkspaceNameCluster[shard][clusterName] == nil { c.shardClusterWorkspaceNameCluster[shard][clusterName] = map[string]logicalcluster.Name{} } c.shardClusterWorkspaceNameCluster[shard][clusterName][ws.Name] = logicalcluster.Name(ws.Spec.Cluster) c.shardClusterWorkspaceName[shard][logicalcluster.Name(ws.Spec.Cluster)] = ws.Name c.shardClusterParentCluster[shard][logicalcluster.Name(ws.Spec.Cluster)] = clusterName } if ws.Spec.Mount != nil { if wsSpec := c.shardClusterWorkspaceMount[shard][clusterName][ws.Name]; !equality.Semantic.DeepEqual(wsSpec, ws.Spec) { if c.shardClusterWorkspaceMount[shard] == nil { c.shardClusterWorkspaceMount[shard] = map[logicalcluster.Name]map[string]tenancyv1alpha1.WorkspaceSpec{} } if c.shardClusterWorkspaceMount[shard][clusterName] == nil { c.shardClusterWorkspaceMount[shard][clusterName] = map[string]tenancyv1alpha1.WorkspaceSpec{} } c.shardClusterWorkspaceMount[shard][clusterName][ws.Name] = ws.Spec } } clustersOnShard.WithLabelValues(shard).Set(float64(len(c.shardClusterWorkspaceName[shard]))) } func (c *State) DeleteWorkspace(shard string, ws *tenancyv1alpha1.Workspace) { clusterName := logicalcluster.From(ws) c.lock.RLock() _, foundCluster := c.shardClusterWorkspaceNameCluster[shard][clusterName][ws.Name] _, foundMount := c.shardClusterWorkspaceMount[shard][clusterName][ws.Name] c.lock.RUnlock() if !foundCluster && !foundMount { return } c.lock.Lock() defer c.lock.Unlock() if _, foundCluster = c.shardClusterWorkspaceNameCluster[shard][clusterName][ws.Name]; foundCluster { delete(c.shardClusterWorkspaceNameCluster[shard][clusterName], ws.Name) if len(c.shardClusterWorkspaceNameCluster[shard][clusterName]) == 0 { delete(c.shardClusterWorkspaceNameCluster[shard], clusterName) } if len(c.shardClusterWorkspaceNameCluster[shard]) == 0 { delete(c.shardClusterWorkspaceNameCluster, shard) } cluster := logicalcluster.Name(ws.Spec.Cluster) delete(c.shardClusterWorkspaceName[shard], cluster) if len(c.shardClusterWorkspaceName[shard]) == 0 { delete(c.shardClusterWorkspaceName, shard) } delete(c.shardClusterParentCluster[shard], cluster) if len(c.shardClusterParentCluster[shard]) == 0 { delete(c.shardClusterParentCluster, shard) } } if _, foundMount = c.shardClusterWorkspaceMount[shard][clusterName][ws.Name]; foundMount { delete(c.shardClusterWorkspaceMount[shard][clusterName], ws.Name) if len(c.shardClusterWorkspaceMount[shard][clusterName]) == 0 { delete(c.shardClusterWorkspaceMount[shard], clusterName) if len(c.shardClusterWorkspaceMount[shard]) == 0 { delete(c.shardClusterWorkspaceMount, shard) } } } delete(c.shardClusterWorkspaceNameErrorCode[shard][clusterName], ws.Name) if len(c.shardClusterWorkspaceNameErrorCode[shard][clusterName]) == 0 { delete(c.shardClusterWorkspaceNameErrorCode[shard], clusterName) if len(c.shardClusterWorkspaceNameErrorCode[shard]) == 0 { delete(c.shardClusterWorkspaceNameErrorCode, shard) } } clustersOnShard.WithLabelValues(shard).Set(float64(len(c.shardClusterWorkspaceName[shard]))) } func (c *State) UpsertLogicalCluster(shard string, logicalCluster *corev1alpha1.LogicalCluster) { clusterName := logicalcluster.From(logicalCluster) c.lock.RLock() got := c.clusterShards[clusterName] c.lock.RUnlock() if got == shard { return } c.lock.Lock() defer c.lock.Unlock() // Re-read under write lock, a concurrent upsert may have won the race. got = c.clusterShards[clusterName] if got == shard { return } // If the migrating annotation is set do not handle the update at all. // The delete event from the origin shard is processed in DeleteLogicalCluster. // The update event from the destination shard must only be // processed (as in, cause the entries in the front-proxies maps to // be updated) after the migration has finished and the annotation // has been removed from the LC. // TODO(ntnn): Make the migrating annotation part of the API. if logicalCluster.Annotations["internal.kcp.io/migrating"] != "" { return } // If got is not empty then the logical cluster was migrated from shard `got` to shard `shard`. // Record the timestamp and delete the context from the manager. // The timestamp is recorded so clients with a watch are getting a 410 sent back to trigger a full relist. // The context is cancelled to force close watches, which will then cause them to get the aforementioned 410s to relist. // The relist is important because the RV on the destination shard will be different, leading to erroneous watch results if no relist is done. if got != "" { c.migratedAt.Store(clusterName, c.now()) c.clusterContexts.Delete(clusterName, fmt.Errorf("logical cluster %s migrated from shard %s to shard %s", clusterName, got, shard)) } c.clusterShards[clusterName] = shard // LogicalClusters are annotated with "path:name" of their workspace's type. typeIdent := logicalcluster.NewPath(logicalCluster.Annotations[tenancyv1alpha1.LogicalClusterTypeAnnotationKey]) if c.shardClusterWorkspaceType[shard] == nil { c.shardClusterWorkspaceType[shard] = map[logicalcluster.Name]logicalcluster.Path{} } c.shardClusterWorkspaceType[shard][clusterName] = typeIdent // Use the LC owner to fill the rest of the maps owner := logicalCluster.Spec.Owner if owner == nil || owner.Resource != "workspaces" || owner.Name == "" || owner.Cluster == "" { return } parentCluster := logicalcluster.Name(owner.Cluster) // The edges are keyed by the shard the parent's Workspace lives on. // If the parent is not known yet the Workspace event will fill the edges. parentShard, found := c.clusterShards[parentCluster] if !found { return } if _, found := c.shardClusterWorkspaceNameCluster[parentShard][parentCluster][owner.Name]; !found { if c.shardClusterWorkspaceNameCluster[parentShard] == nil { c.shardClusterWorkspaceNameCluster[parentShard] = map[logicalcluster.Name]map[string]logicalcluster.Name{} } if c.shardClusterWorkspaceNameCluster[parentShard][parentCluster] == nil { c.shardClusterWorkspaceNameCluster[parentShard][parentCluster] = map[string]logicalcluster.Name{} } c.shardClusterWorkspaceNameCluster[parentShard][parentCluster][owner.Name] = clusterName } if _, found := c.shardClusterWorkspaceName[parentShard][clusterName]; !found { if c.shardClusterWorkspaceName[parentShard] == nil { c.shardClusterWorkspaceName[parentShard] = map[logicalcluster.Name]string{} } c.shardClusterWorkspaceName[parentShard][clusterName] = owner.Name } if _, found := c.shardClusterParentCluster[parentShard][clusterName]; !found { if c.shardClusterParentCluster[parentShard] == nil { c.shardClusterParentCluster[parentShard] = map[logicalcluster.Name]logicalcluster.Name{} } c.shardClusterParentCluster[parentShard][clusterName] = parentCluster } } func (c *State) DeleteLogicalCluster(shard string, logicalCluster *corev1alpha1.LogicalCluster) { clusterName := logicalcluster.From(logicalCluster) c.lock.RLock() got := c.clusterShards[clusterName] c.lock.RUnlock() if got != shard { return } c.lock.Lock() defer c.lock.Unlock() // Re-read under write lock, a concurrent upsert may have won the race. got = c.clusterShards[clusterName] if got != shard { // The shard in the mapping changed between the read- and // write-locked read, all related changes in the other maps are // already updated. return } // delete LC from shard->LC map delete(c.clusterShards, clusterName) c.clusterContexts.Delete(clusterName, fmt.Errorf("logical cluster %s deleted from shard %s", clusterName, shard)) // This LC keyed as the cluster being addressed. delete(c.shardClusterWorkspaceType[shard], clusterName) if len(c.shardClusterWorkspaceType[shard]) == 0 { delete(c.shardClusterWorkspaceType, shard) } // This LC keyed as the child in a parent→child relationship. Normally // cleaned by DeleteWorkspace via the parent's Workspace event, but if // the LogicalCluster delete races ahead of the Workspace delete (or the // Workspace event is missed) the entries leak. Capture the parent and // workspace name before deleting the reverse edges so we can also scrub // the forward edge under the parent below. parentCluster, hasParent := c.shardClusterParentCluster[shard][clusterName] workspaceName, hasWorkspaceName := c.shardClusterWorkspaceName[shard][clusterName] delete(c.shardClusterWorkspaceName[shard], clusterName) if len(c.shardClusterWorkspaceName[shard]) == 0 { delete(c.shardClusterWorkspaceName, shard) } delete(c.shardClusterParentCluster[shard], clusterName) if len(c.shardClusterParentCluster[shard]) == 0 { delete(c.shardClusterParentCluster, shard) } // This LC keyed as the parent of sub-workspaces. Same race rationale: // child Workspace deletes normally scrub these per-entry, but the LC // going away first means any stragglers are unreachable and should be // dropped wholesale. delete(c.shardClusterWorkspaceNameCluster[shard], clusterName) if len(c.shardClusterWorkspaceNameCluster[shard]) == 0 { delete(c.shardClusterWorkspaceNameCluster, shard) } delete(c.shardClusterWorkspaceMount[shard], clusterName) if len(c.shardClusterWorkspaceMount[shard]) == 0 { delete(c.shardClusterWorkspaceMount, shard) } delete(c.shardClusterWorkspaceNameErrorCode[shard], clusterName) if len(c.shardClusterWorkspaceNameErrorCode[shard]) == 0 { delete(c.shardClusterWorkspaceNameErrorCode, shard) } // Scrub the forward edge in the parent's per-workspace maps. Without // this, deleting the child LC before its Workspace event arrives leaves // shardClusterWorkspaceNameCluster[shard][parent][wsName] pointing at a // gone cluster — and the parent map keeps growing across churn. if hasParent && hasWorkspaceName { if m := c.shardClusterWorkspaceNameCluster[shard][parentCluster]; m != nil { delete(m, workspaceName) if len(m) == 0 { delete(c.shardClusterWorkspaceNameCluster[shard], parentCluster) if len(c.shardClusterWorkspaceNameCluster[shard]) == 0 { delete(c.shardClusterWorkspaceNameCluster, shard) } } } if m := c.shardClusterWorkspaceMount[shard][parentCluster]; m != nil { delete(m, workspaceName) if len(m) == 0 { delete(c.shardClusterWorkspaceMount[shard], parentCluster) if len(c.shardClusterWorkspaceMount[shard]) == 0 { delete(c.shardClusterWorkspaceMount, shard) } } } if m := c.shardClusterWorkspaceNameErrorCode[shard][parentCluster]; m != nil { delete(m, workspaceName) if len(m) == 0 { delete(c.shardClusterWorkspaceNameErrorCode[shard], parentCluster) if len(c.shardClusterWorkspaceNameErrorCode[shard]) == 0 { delete(c.shardClusterWorkspaceNameErrorCode, shard) } } } } clustersOnShard.WithLabelValues(shard).Set(float64(len(c.shardClusterWorkspaceName[shard]))) } func (c *State) UpsertShard(shardName, baseURL string) { c.lock.RLock() got := c.shardBaseURLs[shardName] c.lock.RUnlock() if got != baseURL { c.lock.Lock() defer c.lock.Unlock() // Re-read under write lock, a concurrent upsert may have won the race. if c.shardBaseURLs[shardName] == baseURL { return } c.shardBaseURLs[shardName] = baseURL } } func (c *State) DeleteShard(shardName string) { c.lock.Lock() defer c.lock.Unlock() maps.DeleteFunc(c.clusterShards, func(_ logicalcluster.Name, shard string) bool { return shardName == shard }) delete(c.shardClusterWorkspaceNameCluster, shardName) delete(c.shardBaseURLs, shardName) delete(c.shardClusterWorkspaceName, shardName) delete(c.shardClusterWorkspaceType, shardName) delete(c.shardClusterParentCluster, shardName) delete(c.shardClusterWorkspaceMount, shardName) delete(c.shardClusterWorkspaceNameErrorCode, shardName) clustersOnShard.DeleteLabelValues(shardName) } func (c *State) Lookup(path logicalcluster.Path) (Result, bool) { segments := strings.Split(path.String(), ":") for _, rewriter := range c.rewriters { segments = rewriter(segments) } c.lock.RLock() defer c.lock.RUnlock() var ( shard string cluster logicalcluster.Name wsType logicalcluster.Path errorCode int ) // walk through index graph to find the final logical cluster and shard for i, s := range segments { if i == 0 { var found bool shard, found = c.clusterShards[logicalcluster.Name(s)] if !found { return Result{}, false } cluster = logicalcluster.Name(s) continue } if ec, found := c.shardClusterWorkspaceNameErrorCode[shard][cluster][s]; found { errorCode = ec } var found bool originalCluster := cluster cluster, found = c.shardClusterWorkspaceNameCluster[shard][cluster][s] if !found { // We not gonna find the cluster if we using mounts and spec.cluster was never set. // Lets check if we have a mount for this workspace, and if we do, we can return the URL from the mount. // Else we get back to default behavior. wsSpec, foundMount := c.shardClusterWorkspaceMount[shard][originalCluster][s] // experimental feature if foundMount { if wsSpec.Mount != nil && wsSpec.URL != "" { u, err := url.Parse(wsSpec.URL) if err == nil { return Result{URL: u.String(), ErrorCode: errorCode}, true } } } return Result{}, false } shard, found = c.clusterShards[cluster] if !found { return Result{}, false } } wsType = c.shardClusterWorkspaceType[shard][cluster] return Result{ Shard: shard, Cluster: cluster, Type: wsType, ErrorCode: errorCode, }, true } func (c *State) LookupURL(path logicalcluster.Path) (Result, bool) { result, found := c.Lookup(path) if !found { return Result{}, false } if result.ErrorCode != 0 { return result, true } if result.URL != "" && result.Shard == "" && result.Cluster == "" { return result, true } baseURL, found := c.shardBaseURLs[result.Shard] if !found { return Result{}, false } return Result{ Shard: result.Shard, Cluster: result.Cluster, Type: result.Type, URL: strings.TrimSuffix(baseURL, "/") + result.Cluster.Path().RequestPath(), }, true } // RecentlyMigrated returns true if the given cluster was recently migrated. func (c *State) RecentlyMigrated(cluster logicalcluster.Name) bool { loaded, stored := c.migratedAt.Load(cluster) if !stored { return false } t := loaded.(time.Time) if c.now().Sub(t) < c.migrationGracePeriod { return true } // Grace period is over, drop it c.migratedAt.Delete(cluster) return false } // ClusterContext returns a context derived from the parent that will be // cancelled when the cluster is deleted or migrated. func (c *State) ClusterContext(parent context.Context, cluster logicalcluster.Name) (context.Context, context.CancelFunc) { return c.clusterContexts.Context(parent, cluster) }