mirror of
https://github.com/rancher/norman.git
synced 2025-09-06 01:30:20 +00:00
Move ObjectClient to different package
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/juju/ratelimit"
|
"github.com/juju/ratelimit"
|
||||||
errors2 "github.com/pkg/errors"
|
errors2 "github.com/pkg/errors"
|
||||||
"github.com/rancher/norman/clientbase"
|
"github.com/rancher/norman/objectclient"
|
||||||
"github.com/rancher/norman/types"
|
"github.com/rancher/norman/types"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
@@ -40,7 +40,7 @@ type GenericController interface {
|
|||||||
type Backend interface {
|
type Backend interface {
|
||||||
List(opts metav1.ListOptions) (runtime.Object, error)
|
List(opts metav1.ListOptions) (runtime.Object, error)
|
||||||
Watch(opts metav1.ListOptions) (watch.Interface, error)
|
Watch(opts metav1.ListOptions) (watch.Interface, error)
|
||||||
ObjectFactory() clientbase.ObjectFactory
|
ObjectFactory() objectclient.ObjectFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
type handlerDef struct {
|
type handlerDef struct {
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
{{.importPackage}}
|
{{.importPackage}}
|
||||||
"github.com/rancher/norman/clientbase"
|
"github.com/rancher/norman/objectclient"
|
||||||
"github.com/rancher/norman/controller"
|
"github.com/rancher/norman/controller"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -59,7 +59,7 @@ type {{.schema.CodeName}}Controller interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type {{.schema.CodeName}}Interface interface {
|
type {{.schema.CodeName}}Interface interface {
|
||||||
ObjectClient() *clientbase.ObjectClient
|
ObjectClient() *objectclient.ObjectClient
|
||||||
Create(*{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
Create(*{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
||||||
GetNamespaced(namespace, name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error)
|
GetNamespaced(namespace, name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error)
|
||||||
Get(name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error)
|
Get(name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error)
|
||||||
@@ -185,11 +185,11 @@ func (s *{{.schema.ID}}Client) Controller() {{.schema.CodeName}}Controller {
|
|||||||
type {{.schema.ID}}Client struct {
|
type {{.schema.ID}}Client struct {
|
||||||
client *Client
|
client *Client
|
||||||
ns string
|
ns string
|
||||||
objectClient *clientbase.ObjectClient
|
objectClient *objectclient.ObjectClient
|
||||||
controller {{.schema.CodeName}}Controller
|
controller {{.schema.CodeName}}Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *{{.schema.ID}}Client) ObjectClient() *clientbase.ObjectClient {
|
func (s *{{.schema.ID}}Client) ObjectClient() *objectclient.ObjectClient {
|
||||||
return s.objectClient
|
return s.objectClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/rancher/norman/clientbase"
|
"github.com/rancher/norman/objectclient"
|
||||||
"github.com/rancher/norman/controller"
|
"github.com/rancher/norman/controller"
|
||||||
"github.com/rancher/norman/restwatch"
|
"github.com/rancher/norman/restwatch"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
@@ -64,7 +64,7 @@ type {{.CodeNamePlural}}Getter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) {{.CodeNamePlural}}(namespace string) {{.CodeName}}Interface {
|
func (c *Client) {{.CodeNamePlural}}(namespace string) {{.CodeName}}Interface {
|
||||||
objectClient := clientbase.NewObjectClient(namespace, c.restClient, &{{.CodeName}}Resource, {{.CodeName}}GroupVersionKind, {{.ID}}Factory{})
|
objectClient := objectclient.NewObjectClient(namespace, c.restClient, &{{.CodeName}}Resource, {{.CodeName}}GroupVersionKind, {{.ID}}Factory{})
|
||||||
return &{{.ID}}Client{
|
return &{{.ID}}Client{
|
||||||
ns: namespace,
|
ns: namespace,
|
||||||
client: c,
|
client: c,
|
||||||
|
@@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/rancher/norman/clientbase"
|
"github.com/rancher/norman/objectclient"
|
||||||
"github.com/rancher/norman/types/slice"
|
"github.com/rancher/norman/types/slice"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -27,10 +27,10 @@ type objectLifecycleAdapter struct {
|
|||||||
name string
|
name string
|
||||||
clusterScoped bool
|
clusterScoped bool
|
||||||
lifecycle ObjectLifecycle
|
lifecycle ObjectLifecycle
|
||||||
objectClient *clientbase.ObjectClient
|
objectClient *objectclient.ObjectClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewObjectLifecycleAdapter(name string, clusterScoped bool, lifecycle ObjectLifecycle, objectClient *clientbase.ObjectClient) func(key string, obj runtime.Object) error {
|
func NewObjectLifecycleAdapter(name string, clusterScoped bool, lifecycle ObjectLifecycle, objectClient *objectclient.ObjectClient) func(key string, obj runtime.Object) error {
|
||||||
o := objectLifecycleAdapter{
|
o := objectLifecycleAdapter{
|
||||||
name: name,
|
name: name,
|
||||||
clusterScoped: clusterScoped,
|
clusterScoped: clusterScoped,
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
package clientbase
|
package objectclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
@@ -2,24 +2,20 @@ package types
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/rancher/norman/types/convert"
|
"github.com/rancher/norman/types/convert"
|
||||||
"github.com/rancher/norman/types/definition"
|
"github.com/rancher/norman/types/definition"
|
||||||
"github.com/rancher/norman/types/slice"
|
"github.com/rancher/norman/types/slice"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
namespacedType = reflect.TypeOf(Namespaced{})
|
namespacedType = reflect.TypeOf(Namespaced{})
|
||||||
resourceType = reflect.TypeOf(Resource{})
|
resourceType = reflect.TypeOf(Resource{})
|
||||||
typeType = reflect.TypeOf(metav1.TypeMeta{})
|
|
||||||
metaType = reflect.TypeOf(metav1.ObjectMeta{})
|
|
||||||
blacklistNames = map[string]bool{
|
blacklistNames = map[string]bool{
|
||||||
"links": true,
|
"links": true,
|
||||||
"actions": true,
|
"actions": true,
|
||||||
@@ -194,6 +190,16 @@ func jsonName(f reflect.StructField) string {
|
|||||||
return strings.SplitN(f.Tag.Get("json"), ",", 2)[0]
|
return strings.SplitN(f.Tag.Get("json"), ",", 2)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func k8sType(field reflect.StructField) bool {
|
||||||
|
return field.Type.Name() == "TypeMeta" &&
|
||||||
|
strings.HasSuffix(field.Type.PkgPath(), "k8s.io/apimachinery/pkg/apis/meta/v1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func k8sObject(field reflect.StructField) bool {
|
||||||
|
return field.Type.Name() == "ObjectMeta" &&
|
||||||
|
strings.HasSuffix(field.Type.PkgPath(), "k8s.io/apimachinery/pkg/apis/meta/v1")
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
|
func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
|
||||||
if t == resourceType {
|
if t == resourceType {
|
||||||
schema.CollectionMethods = []string{"GET", "POST"}
|
schema.CollectionMethods = []string{"GET", "POST"}
|
||||||
@@ -212,16 +218,15 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jsonName := jsonName(field)
|
jsonName := jsonName(field)
|
||||||
|
|
||||||
if jsonName == "-" {
|
if jsonName == "-" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.Anonymous && jsonName == "" && field.Type == typeType {
|
if field.Anonymous && jsonName == "" && k8sType(field) {
|
||||||
hasType = true
|
hasType = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.Anonymous && jsonName == "metadata" && field.Type == metaType {
|
if field.Anonymous && jsonName == "metadata" && k8sObject(field) {
|
||||||
hasMeta = true
|
hasMeta = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user