Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider refactoring adjustClusterNameIfWildcard method #1875

Closed
p0lyn0mial opened this issue Sep 1, 2022 · 0 comments · Fixed by kcp-dev/kubernetes#98
Closed

consider refactoring adjustClusterNameIfWildcard method #1875

p0lyn0mial opened this issue Sep 1, 2022 · 0 comments · Fixed by kcp-dev/kubernetes#98
Assignees

Comments

@p0lyn0mial
Copy link
Contributor

The adjustClusterNameIfWildcard method extracts a cluster name from an etcd key under an object is stored.

At the moment the method uses regular expressions for extracting names.
A suggestion to change it to the following code snippet has been suggested by @stevekuznetsov.
We should consider changing it to the proposed code or something similar if it improves the readability of the code.

 func adjustClusterNameIfWildcard(shard genericapirequest.Shard, cluster *genericapirequest.Cluster, crdRequest bool, keyPrefix, key string) logicalcluster.Name {
    if cluster.Name != logicalcluster.Wildcard || !cluster.Wildcard { // TODO: fix this duplicity, as well
        return cluster.Name
    }
    
    keyWithoutPrefix := strings.TrimPrefix(key, keyPrefix)
    
    var extractCluster func([]string) logicalcluster.Name
    extractor := func(numParts, clusterPart int) func([]string) logicalcluster.Name {
        return func([]string) logicalcluster.Name {
            if len(parts) < numParts {
                klog.Warnf("shard=%s cluster=%s invalid key=%s had %d parts, not %d", shard, cluster, keyWIthoutPrefix, len(parts), numParts)
                return logicalcluster.Name{}
            }
            return logicalcluster.New(parts[clusterPart])
        }
    }
    
    switch {
    case shard.Wildcard():
        // expecting shardName/clusterName/<remainder>
        extractCluster = extractor(3,1)
    case cluster.PartialMetadataRequest && crdRequest:
        // expecting 2699f4d273d342adccdc8a32663408226ecf66de7d191113ed3d4dc9bccec2f2/root:org:ws/<remainder>
        // OR customresources/root:org:ws/<remainder>
        extractCluster = extractor(3,1)
    case cluster.Wildcard():
        // expecting root:org:ws/<remainder>
        extractCluster = extractor(2,0)
    default:
        panic("bad programmer")
    }
    
    parts := strings.Split(keyWithoutPrefix, "/")
    return extractCluster(parts)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant