Skip to content

Commit

Permalink
karmada-search: support field selector for corev1 resources
Browse files Browse the repository at this point in the history
Signed-off-by: SataQiu <[email protected]>
  • Loading branch information
SataQiu committed Nov 11, 2024
1 parent d8b58c4 commit 774d133
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/search/proxy/framework/plugins/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (c *Cache) Connect(_ context.Context, request framework.ProxyRequest) (http
ClusterScoped: mapping.Scope.Name() == meta.RESTScopeNameRoot,
},
Serializer: scheme.Codecs.WithoutConversion(),
Convertor: runtime.NewScheme(),
Convertor: cacheScheme,
Subresource: requestInfo.Subresource,
MetaGroupVersion: metav1.SchemeGroupVersion,
TableConvertor: r.tableConvertor,
Expand Down
136 changes: 136 additions & 0 deletions pkg/search/proxy/framework/plugins/cache/scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2024 The Karmada 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 cache

import (
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// cacheScheme is the runtime.Scheme for the cache plugin.
var cacheScheme = runtime.NewScheme()

func init() {
// This is to ensure that the cache plugin can handle the field selectors for these resources.
_ = cacheScheme.AddFieldLabelConversionFunc(corev1.SchemeGroupVersion.WithKind("Pod"),
func(label, value string) (string, string, error) {
switch label {
case "metadata.name",
"metadata.namespace",
"spec.nodeName",
"spec.restartPolicy",
"spec.schedulerName",
"spec.serviceAccountName",
"spec.hostNetwork",
"status.phase",
"status.podIP",
"status.podIPs",
"status.nominatedNodeName":
return label, value, nil
// This is for backwards compatibility with old v1 clients which send spec.host
case "spec.host":
return "spec.nodeName", value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
},
)
_ = cacheScheme.AddFieldLabelConversionFunc(corev1.SchemeGroupVersion.WithKind("Node"),
func(label, value string) (string, string, error) {
switch label {
case "metadata.name":
return label, value, nil
case "spec.unschedulable":
return label, value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
},
)
_ = cacheScheme.AddFieldLabelConversionFunc(corev1.SchemeGroupVersion.WithKind("ReplicationController"),
func(label, value string) (string, string, error) {
switch label {
case "metadata.name",
"metadata.namespace",
"status.replicas":
return label, value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
},
)
_ = cacheScheme.AddFieldLabelConversionFunc(corev1.SchemeGroupVersion.WithKind("Event"),
func(label, value string) (string, string, error) {
switch label {
case "involvedObject.kind",
"involvedObject.namespace",
"involvedObject.name",
"involvedObject.uid",
"involvedObject.apiVersion",
"involvedObject.resourceVersion",
"involvedObject.fieldPath",
"reason",
"reportingComponent",
"source",
"type",
"metadata.namespace",
"metadata.name":
return label, value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
},
)
_ = cacheScheme.AddFieldLabelConversionFunc(corev1.SchemeGroupVersion.WithKind("Namespace"),
func(label, value string) (string, string, error) {
switch label {
case "status.phase",
"metadata.name":
return label, value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
},
)
_ = cacheScheme.AddFieldLabelConversionFunc(corev1.SchemeGroupVersion.WithKind("Secret"),
func(label, value string) (string, string, error) {
switch label {
case "type",
"metadata.namespace",
"metadata.name":
return label, value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
},
)
_ = cacheScheme.AddFieldLabelConversionFunc(corev1.SchemeGroupVersion.WithKind("Service"),
func(label, value string) (string, string, error) {
switch label {
case "metadata.namespace",
"metadata.name",
"spec.clusterIP",
"spec.type":
return label, value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
},
)
}

0 comments on commit 774d133

Please sign in to comment.