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

UPSTREAM: <carry>: SQUASH: with shard prefix support – simplify #98

Merged
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
67 changes: 25 additions & 42 deletions staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,49 @@ limitations under the License.
package etcd3

import (
"regexp"
"strings"

"github.com/kcp-dev/logicalcluster/v2"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/klog/v2"
)

var (
// matches cluster/remainder, capturing cluster.
//
// Example: root:org:ws/some-namespace/some-name
// Example: root:org:ws/some-name
wildcardClusterNameRegex = regexp.MustCompile(`^([^/]+)\/.+`)

// matches shard/cluster/remainder, capturing shard and cluster.
//
// Example: amber/root:org:ws/some-namespace/some-name
// Example: amber/root:org:ws/some-name
wildcardShardClusterNameRegex = regexp.MustCompile(`^([^\/]+)\/([^\/]+)\/.+`)

// matches identity-or-customresources/cluster/remainder, capturing cluster
//
// Example: customresources/root:org:ws/some-namespace/some-name
// Example: customresources/root:org:ws/some-name
// Example: 2699f4d273d342adccdc8a32663408226ecf66de7d191113ed3d4dc9bccec2f2/root:org:ws/some-namespace/some-name
// Example: 2699f4d273d342adccdc8a32663408226ecf66de7d191113ed3d4dc9bccec2f2/root:org:ws/some-name
crdWildcardPartialMetadataClusterNameRegex = regexp.MustCompile(`^[^/]+\/([^/]+)\/.+`)
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
)

// adjustClusterNameIfWildcard determines the logical cluster name. If this is not a wildcard list/watch request,
// the cluster name is returned unmodified. Otherwise, the cluster name is extracted from the key based on whether
// it is a CR partial metadata request (prefix/identity/clusterName/remainder) or not (prefix/clusterName/remainder).
// adjustClusterNameIfWildcard determines the logical cluster name. If this is not a cluster-wildcard list/watch request,
// the cluster name is returned unmodified. Otherwise, the cluster name is extracted from the key based on whether it is
// - a shard-wildcard request: <prefix>/shardName/clusterName/<remainder>
// - CR partial metadata request: <prefix>/identity/clusterName/<remainder>
// - any other request: <prefix>/clusterName/<remainder>.
func adjustClusterNameIfWildcard(shard genericapirequest.Shard, cluster *genericapirequest.Cluster, crdRequest bool, keyPrefix, key string) logicalcluster.Name {
if cluster.Name != logicalcluster.Wildcard {
if cluster.Name != logicalcluster.Wildcard && !cluster.Wildcard { // TODO: fix this duplicity, as well
return cluster.Name
}

keyWithoutPrefix := strings.TrimPrefix(key, keyPrefix)
parts := strings.SplitN(keyWithoutPrefix, "/", 3)

if !shard.Wildcard() {
var regex *regexp.Regexp
if cluster.PartialMetadataRequest && crdRequest {
regex = crdWildcardPartialMetadataClusterNameRegex
} else {
regex = wildcardClusterNameRegex
}
matches := regex.FindStringSubmatch(keyWithoutPrefix)
if len(matches) >= 2 {
return logicalcluster.New(matches[1])
extract := func(minLen, i int) logicalcluster.Name {
if len(parts) < minLen {
klog.Warningf("shard=%s cluster=%s invalid key=%s had %d parts, not %d", shard, cluster, keyWithoutPrefix, len(parts), minLen)
return logicalcluster.Name{}
}
return logicalcluster.Name{}
return logicalcluster.New(parts[i])
}

matches := wildcardShardClusterNameRegex.FindStringSubmatch(keyWithoutPrefix)
if len(matches) >= 3 {
return logicalcluster.New(matches[2])
switch {
case shard.Wildcard():
// expecting shardName/clusterName/<remainder>
return extract(3, 1)
case cluster.PartialMetadataRequest && crdRequest:
// expecting 2699f4d273d342adccdc8a32663408226ecf66de7d191113ed3d4dc9bccec2f2/root:org:ws/<remainder>
// OR customresources/root:org:ws/<remainder>
return extract(3, 1)
default:
// expecting root:org:ws/<remainder>
return extract(2, 0)
}
return logicalcluster.Name{}
}

// setClusterNameOnDecodedObject applies clusterName to obj. This is necessary because we don't store the cluster
Expand Down