This is an automated email from the ASF dual-hosted git repository.
zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git
The following commit(s) were added to refs/heads/master by this push:
new e47a0d22 [operator] gvk client logic supplement
e47a0d22 is described below
commit e47a0d22275726a1b6610d0225755493d0c94e98
Author: mfordjody <[email protected]>
AuthorDate: Wed Dec 18 12:34:25 2024 +0800
[operator] gvk client logic supplement
---
operator/pkg/config/protogvk.go | 9 +++++++++
operator/pkg/schema/schema.go | 36 ++++++++++++++++++++++++++++++++++
pkg/kube/client.go | 26 ++++++++++++++++++++++--
pkg/kube/collections/collection.gen.go | 28 ++++++++++++++++++++++++++
4 files changed, 97 insertions(+), 2 deletions(-)
diff --git a/operator/pkg/config/protogvk.go b/operator/pkg/config/protogvk.go
index 8da1f4a8..40c92b43 100644
--- a/operator/pkg/config/protogvk.go
+++ b/operator/pkg/config/protogvk.go
@@ -8,6 +8,7 @@ import (
gogoproto "github.com/gogo/protobuf/proto"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
+ "k8s.io/apimachinery/pkg/runtime/schema"
)
type GroupVersionKind struct {
@@ -33,6 +34,14 @@ func CanoncalGroup(group string) string {
return "core"
}
+func FromK8sGVK(gvk schema.GroupVersionKind) GroupVersionKind {
+ return GroupVersionKind{
+ Group: gvk.Group,
+ Version: gvk.Version,
+ Kind: gvk.Kind,
+ }
+}
+
type Spec any
func ToJSON(s Spec) ([]byte, error) {
diff --git a/operator/pkg/schema/schema.go b/operator/pkg/schema/schema.go
index cc349f6b..cbd758cc 100644
--- a/operator/pkg/schema/schema.go
+++ b/operator/pkg/schema/schema.go
@@ -3,7 +3,11 @@ package schema
import (
"fmt"
"github.com/apache/dubbo-kubernetes/operator/pkg/config"
+ "github.com/hashicorp/go-multierror"
+ "google.golang.org/protobuf/reflect/protoreflect"
+ "google.golang.org/protobuf/reflect/protoregistry"
"k8s.io/apimachinery/pkg/runtime/schema"
+ "reflect"
)
type schemaImpl struct {
@@ -13,6 +17,9 @@ type schemaImpl struct {
goPkg string
proto string
versionAliases []string
+ apiVersion string
+ reflectType reflect.Type
+ statusType reflect.Type
}
func (s *schemaImpl) GroupVersionAliasKinds() []config.GroupVersionKind {
@@ -66,10 +73,14 @@ func (s *schemaImpl) GroupVersionResource()
schema.GroupVersionResource {
}
func (s *schemaImpl) Validate() (err error) {
+ if s.reflectType == nil && getProtoMessageType(s.proto) == nil {
+ err = multierror.Append(err, fmt.Errorf("proto message or
reflect type not found: %v", s.proto))
+ }
return
}
type Builder struct {
+ Identifier string
Plural string
ClusterScoped bool
ProtoPkg string
@@ -77,6 +88,10 @@ type Builder struct {
Kind string
Group string
Version string
+ ReflectType reflect.Type
+ StatusType reflect.Type
+ Builtin bool
+ Synthetic bool
}
func (b Builder) BuildNoValidate() Schema {
@@ -90,6 +105,9 @@ func (b Builder) BuildNoValidate() Schema {
clusterScoped: b.ClusterScoped,
goPkg: b.ProtoPkg,
proto: b.Proto,
+ apiVersion: b.Group + "/" + b.Version,
+ reflectType: b.ReflectType,
+ statusType: b.StatusType,
}
}
@@ -101,6 +119,14 @@ func (b Builder) Build() (Schema, error) {
return s, nil
}
+func (b Builder) MustBuild() Schema {
+ s, err := b.Build()
+ if err != nil {
+ panic(fmt.Sprintf("MustBuild: %v", err))
+ }
+ return s
+}
+
type Schema interface {
fmt.Stringer
GroupVersionResource() schema.GroupVersionResource
@@ -109,3 +135,13 @@ type Schema interface {
Validate() error
IsClusterScoped() bool
}
+
+var protoMessageType = protoregistry.GlobalTypes.FindMessageByName
+
+func getProtoMessageType(protoMessageName string) reflect.Type {
+ t, err := protoMessageType(protoreflect.FullName(protoMessageName))
+ if err != nil || t == nil {
+ return nil
+ }
+ return reflect.TypeOf(t.Zero().Interface())
+}
diff --git a/pkg/kube/client.go b/pkg/kube/client.go
index 8f41ea79..780b6ed0 100644
--- a/pkg/kube/client.go
+++ b/pkg/kube/client.go
@@ -1,6 +1,9 @@
package kube
import (
+ "fmt"
+ "github.com/apache/dubbo-kubernetes/operator/pkg/config"
+ "github.com/apache/dubbo-kubernetes/pkg/kube/collections"
apiextensionsv1
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
@@ -61,13 +64,32 @@ func (c *client) Dynamic() dynamic.Interface {
}
func (c *client) DynamicClientFor(gvk schema.GroupVersionKind, obj
*unstructured.Unstructured, namespace string) (dynamic.ResourceInterface,
error) {
- // TODO
+ gvr, namespaced := c.bestEffortToGVR(gvk, obj, namespace)
var dr dynamic.ResourceInterface
+ if namespaced {
+ ns := ""
+ if obj != nil {
+ ns = obj.GetNamespace()
+ }
+ if ns == "" {
+ ns = namespace
+ } else if namespace != "" && ns != namespace {
+ return nil, fmt.Errorf("object %v/%v provided namespace
%q but apply called with %q", gvk, obj.GetName(), ns, namespace)
+ }
+ dr = c.dynamic.Resource(gvr).Namespace(ns)
+ } else {
+ dr = c.dynamic.Resource(gvr)
+ }
return dr, nil
}
func (c *client) bestEffortToGVR(gvk schema.GroupVersionKind, obj
*unstructured.Unstructured, namespace string) (schema.GroupVersionResource,
bool) {
- // TODO
+ if s, f :=
collections.All.FindByGroupVersionAliasesKind(config.FromK8sGVK(gvk)); f {
+ gvr := s.GroupVersionResource()
+ gvr.Version = gvk.Version
+ return gvr, !s.IsClusterScoped()
+ }
+
return schema.GroupVersionResource{}, false
}
diff --git a/pkg/kube/collections/collection.gen.go
b/pkg/kube/collections/collection.gen.go
new file mode 100644
index 00000000..d7a4b7ac
--- /dev/null
+++ b/pkg/kube/collections/collection.gen.go
@@ -0,0 +1,28 @@
+package collections
+
+import (
+ "github.com/apache/dubbo-kubernetes/operator/pkg/schema"
+ "github.com/apache/dubbo-kubernetes/pkg/kube/collection"
+ k8sioapiextensionsapiserverpkgapisapiextensionsv1
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
+ "reflect"
+)
+
+var (
+ CustomResourceDefinition = schema.Builder{
+ Identifier: "CustomResourceDefinition",
+ Group: "apiextensions.k8s.io",
+ Kind: "CustomResourceDefinition",
+ Plural: "customresourcedefinitions",
+ Version: "v1",
+ Proto:
"k8s.io.apiextensions_apiserver.pkg.apis.apiextensions.v1.CustomResourceDefinition",
+ ReflectType:
reflect.TypeOf(&k8sioapiextensionsapiserverpkgapisapiextensionsv1.CustomResourceDefinition{}).Elem(),
+ ProtoPkg:
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1",
+ ClusterScoped: true,
+ Synthetic: false,
+ Builtin: true,
+ }.MustBuild()
+
+ All = collection.NewSchemasBuilder().
+ MustAdd(CustomResourceDefinition).
+ Build()
+)