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 13, 2024
1 parent d8b58c4 commit 056daa5
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 1 deletion.
163 changes: 163 additions & 0 deletions pkg/search/proxy/framework/plugins/cache/apis/core/v1/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
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 v1

import (
"fmt"

"k8s.io/apimachinery/pkg/runtime"
)

// addConversionFuncs ensures that the cache plugin can handle the field selectors for corev1 resources.
// It is copied from "k8s.io/kubernetes/pkg/apis/core/v1/conversion.go".
func addConversionFuncs(scheme *runtime.Scheme) error {
// Add field conversion funcs.
err := scheme.AddFieldLabelConversionFunc(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)
}
},
)
if err != nil {
return err
}
err = scheme.AddFieldLabelConversionFunc(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)
}
},
)
if err != nil {
return err
}
err = scheme.AddFieldLabelConversionFunc(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)
}
})
if err != nil {
return err
}
if err := AddFieldLabelConversionsForEvent(scheme); err != nil {
return err
}
if err := AddFieldLabelConversionsForNamespace(scheme); err != nil {
return err
}
if err := AddFieldLabelConversionsForSecret(scheme); err != nil {
return err
}
if err := AddFieldLabelConversionsForService(scheme); err != nil {
return err
}
return nil
}

func AddFieldLabelConversionsForEvent(scheme *runtime.Scheme) error {

Check failure on line 99 in pkg/search/proxy/framework/plugins/cache/apis/core/v1/conversion.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function AddFieldLabelConversionsForEvent should have comment or be unexported (revive)
return scheme.AddFieldLabelConversionFunc(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)
}
})
}

func AddFieldLabelConversionsForNamespace(scheme *runtime.Scheme) error {

Check failure on line 123 in pkg/search/proxy/framework/plugins/cache/apis/core/v1/conversion.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function AddFieldLabelConversionsForNamespace should have comment or be unexported (revive)
return scheme.AddFieldLabelConversionFunc(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)
}
})
}

func AddFieldLabelConversionsForSecret(scheme *runtime.Scheme) error {

Check failure on line 136 in pkg/search/proxy/framework/plugins/cache/apis/core/v1/conversion.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function AddFieldLabelConversionsForSecret should have comment or be unexported (revive)
return scheme.AddFieldLabelConversionFunc(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)
}
})
}

func AddFieldLabelConversionsForService(scheme *runtime.Scheme) error {

Check failure on line 150 in pkg/search/proxy/framework/plugins/cache/apis/core/v1/conversion.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function AddFieldLabelConversionsForService should have comment or be unexported (revive)
return scheme.AddFieldLabelConversionFunc(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)
}
})
}
29 changes: 29 additions & 0 deletions pkg/search/proxy/framework/plugins/cache/apis/core/v1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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 v1

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

var SchemeGroupVersion = corev1.SchemeGroupVersion

Check failure on line 24 in pkg/search/proxy/framework/plugins/cache/apis/core/v1/register.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported var SchemeGroupVersion should have comment or be unexported (revive)

var (
SchemeBuilder = runtime.NewSchemeBuilder(addConversionFuncs)

Check failure on line 27 in pkg/search/proxy/framework/plugins/cache/apis/core/v1/register.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported var SchemeBuilder should have comment or be unexported (revive)
AddToScheme = SchemeBuilder.AddToScheme
)
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
34 changes: 34 additions & 0 deletions pkg/search/proxy/framework/plugins/cache/scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
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 (
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

corev1 "github.com/karmada-io/karmada/pkg/search/proxy/framework/plugins/cache/apis/core/v1"
)

var cacheScheme = runtime.NewScheme()

func init() {
AddToScheme(cacheScheme)
}

func AddToScheme(scheme *runtime.Scheme) {

Check failure on line 32 in pkg/search/proxy/framework/plugins/cache/scheme.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function AddToScheme should have comment or be unexported (revive)
utilruntime.Must(corev1.AddToScheme(scheme))
}

0 comments on commit 056daa5

Please sign in to comment.