1
0
mirror of https://github.com/rancher/types.git synced 2025-09-01 05:09:10 +00:00

Update vendor

This commit is contained in:
Darren Shepherd
2017-12-22 23:20:57 -07:00
parent e18b4a9cae
commit 5b7563a713
24 changed files with 293 additions and 32 deletions

View File

@@ -9,6 +9,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
@@ -208,6 +209,27 @@ func (p *ObjectClient) DeleteCollection(deleteOptions *metav1.DeleteOptions, lis
Error()
}
func (p *ObjectClient) Patch(name string, o runtime.Object, data []byte, subresources ...string) (runtime.Object, error) {
ns := p.ns
if obj, ok := o.(metav1.Object); ok && obj.GetNamespace() != "" {
ns = obj.GetNamespace()
}
result := p.Factory.Object()
if len(name) == 0 {
return result, errors.New("object missing name")
}
err := p.restClient.Patch(types.StrategicMergePatchType).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
NamespaceIfScoped(ns, p.resource.Namespaced).
Resource(p.resource.Name).
SubResource(subresources...).
Name(name).
Body(data).
Do().
Into(result)
return result, err
}
type dynamicDecoder struct {
factory ObjectFactory
dec *json.Decoder

View File

@@ -10,7 +10,7 @@ type Client struct {
clientbase.APIBaseClient
{{range .schemas}}
{{- if . | hasGet }}{{.ID | capitalize}} {{.ID | capitalize}}Operations
{{- if . | hasGet }}{{.CodeName}} {{.CodeName}}Operations
{{end}}{{end}}}
func NewClient(opts *clientbase.ClientOpts) (*Client, error) {
@@ -24,7 +24,7 @@ func NewClient(opts *clientbase.ClientOpts) (*Client, error) {
}
{{range .schemas}}
{{- if . | hasGet }}client.{{.ID | capitalize}} = new{{.ID | capitalize}}Client(client)
{{- if . | hasGet }}client.{{.CodeName}} = new{{.CodeName}}Client(client)
{{end}}{{end}}
return client, nil
}

View File

@@ -198,6 +198,12 @@ func (s *{{.schema.ID}}Client) Watch(opts metav1.ListOptions) (watch.Interface,
return s.objectClient.Watch(opts)
}
// Patch applies the patch and returns the patched deployment.
func (s *{{.schema.ID}}Client) Patch(o *{{.prefix}}{{.schema.CodeName}}, data []byte, subresources ...string) (*{{.prefix}}{{.schema.CodeName}}, error) {
obj, err := s.objectClient.Patch(o.Name, o, data, subresources...)
return obj.(*{{.prefix}}{{.schema.CodeName}}), err
}
func (s *{{.schema.ID}}Client) DeleteCollection(deleteOpts *metav1.DeleteOptions, listOpts metav1.ListOptions) error {
return s.objectClient.DeleteCollection(deleteOpts, listOpts)
}

View File

@@ -52,6 +52,8 @@ func getTypeString(nullable bool, typeName string, schema *types.Schema, schemas
name := ""
switch typeName {
case "base64":
return "string"
case "json":
return "interface{}"
case "boolean":

View File

@@ -37,6 +37,7 @@ func (m Mappers) ModifySchema(schema *Schema, schemas *Schemas) error {
type typeMapper struct {
Mappers []Mapper
root bool
typeName string
subSchemas map[string]*Schema
subArraySchemas map[string]*Schema
@@ -64,10 +65,11 @@ func (t *typeMapper) FromInternal(data map[string]interface{}) {
Mappers(t.Mappers).FromInternal(data)
if data != nil {
if _, ok := data["type"]; !ok {
data["type"] = t.typeName
}
if _, ok := data["type"]; !ok && data != nil {
data["type"] = t.typeName
}
if data != nil && t.root {
name, _ := data["name"].(string)
namespace, _ := data["namespaceId"].(string)

View File

@@ -18,7 +18,7 @@ func (e Access) ToInternal(data map[string]interface{}) {
func (e Access) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
for name, access := range e.Fields {
if err := validateField(name, schema); err != nil {
if err := ValidateField(name, schema); err != nil {
return err
}

View File

@@ -9,8 +9,9 @@ import (
)
type AnnotationField struct {
Field string
Object bool
Field string
Object bool
IgnoreDefinition bool
}
func (e AnnotationField) FromInternal(data map[string]interface{}) {
@@ -41,5 +42,8 @@ func (e AnnotationField) ToInternal(data map[string]interface{}) {
}
func (e AnnotationField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
return validateField(e.Field, schema)
if e.IgnoreDefinition {
return nil
}
return ValidateField(e.Field, schema)
}

View File

@@ -0,0 +1,61 @@
package mapper
import (
"encoding/base64"
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
)
type Base64 struct {
Field string
IgnoreDefinition bool
Separator string
}
func (m Base64) FromInternal(data map[string]interface{}) {
if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok {
str := convert.ToString(v)
if str == "" {
return
}
newData, err := base64.StdEncoding.DecodeString(str)
if err != nil {
log.Errorf("failed to base64 decode data")
}
values.PutValue(data, string(newData), strings.Split(m.Field, m.getSep())...)
}
}
func (m Base64) ToInternal(data map[string]interface{}) {
if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok {
str := convert.ToString(v)
if str == "" {
return
}
newData := base64.StdEncoding.EncodeToString([]byte(str))
values.PutValue(data, newData, strings.Split(m.Field, m.getSep())...)
}
}
func (m Base64) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
if !m.IgnoreDefinition {
if err := ValidateField(m.Field, s); err != nil {
return err
}
}
return nil
}
func (m Base64) getSep() string {
if m.Separator == "" {
return "/"
}
return m.Separator
}

View File

@@ -6,7 +6,7 @@ import (
"github.com/rancher/norman/types"
)
func validateField(field string, schema *types.Schema) error {
func ValidateField(field string, schema *types.Schema) error {
if _, ok := schema.ResourceFields[field]; !ok {
return fmt.Errorf("field %s missing on schema %s", field, schema.ID)
}

View File

@@ -0,0 +1,27 @@
package mapper
import (
"github.com/rancher/norman/types"
)
type Condition struct {
Field string
Value interface{}
Mapper types.Mapper
}
func (m Condition) FromInternal(data map[string]interface{}) {
if data[m.Field] == m.Value {
m.Mapper.FromInternal(data)
}
}
func (m Condition) ToInternal(data map[string]interface{}) {
if data[m.Field] == m.Value {
m.Mapper.ToInternal(data)
}
}
func (m Condition) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
return m.Mapper.ModifySchema(s, schemas)
}

View File

@@ -43,7 +43,7 @@ func (e *Embed) ToInternal(data map[string]interface{}) {
}
func (e *Embed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
err := validateField(e.Field, schema)
err := ValidateField(e.Field, schema)
if err != nil {
if e.Optional {
return nil

View File

@@ -44,5 +44,5 @@ func (e Enum) ToInternal(data map[string]interface{}) {
}
func (e Enum) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
return validateField(e.Field, schema)
return ValidateField(e.Field, schema)
}

View File

@@ -0,0 +1,51 @@
package mapper
import (
"strings"
"encoding/json"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
)
type JSONEncode struct {
Field string
IgnoreDefinition bool
Separator string
}
func (m JSONEncode) FromInternal(data map[string]interface{}) {
if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok {
obj := map[string]interface{}{}
if err := json.Unmarshal([]byte(convert.ToString(v)), &obj); err == nil {
values.PutValue(data, obj, strings.Split(m.Field, m.getSep())...)
} else {
log.Errorf("Failed to unmarshal json field: %v", err)
}
}
}
func (m JSONEncode) ToInternal(data map[string]interface{}) {
if v, ok := values.RemoveValue(data, strings.Split(m.Field, m.getSep())...); ok && v != nil {
if bytes, err := json.Marshal(v); err == nil {
values.PutValue(data, string(bytes), strings.Split(m.Field, m.getSep())...)
}
}
}
func (m JSONEncode) getSep() string {
if m.Separator == "" {
return "/"
}
return m.Separator
}
func (m JSONEncode) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
if m.IgnoreDefinition {
return nil
}
return ValidateField(m.Field, s)
}

View File

@@ -24,5 +24,5 @@ func (e LabelField) ToInternal(data map[string]interface{}) {
}
func (e LabelField) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
return validateField(e.Field, schema)
return ValidateField(e.Field, schema)
}

7
vendor/github.com/rancher/norman/types/mapper/log.go generated vendored Normal file
View File

@@ -0,0 +1,7 @@
package mapper
import "github.com/sirupsen/logrus"
var (
log = logrus.WithField("component", "norman/mapper")
)

View File

@@ -7,7 +7,9 @@ import (
func NewMetadataMapper() types.Mapper {
return types.Mappers{
Drop{"generateName"},
Move{From: "selfLink", To: "resourcePath"},
//Move{From: "selfLink", To: "resourcePath"},
Drop{"selfLink"},
//Drop{"ownerReferences"},
Move{From: "uid", To: "uuid"},
Drop{"resourceVersion"},
Drop{"generation"},

View File

@@ -25,7 +25,7 @@ func (r ReadOnly) ModifySchema(schema *types.Schema, schemas *types.Schemas) err
return nil
}
if err := validateField(r.Field, schema); err != nil {
if err := ValidateField(r.Field, schema); err != nil {
if r.Optional {
return nil
}

View File

@@ -10,40 +10,59 @@ import (
)
type SetValue struct {
From, To string
Value interface{}
IfEq interface{}
Field, To string
Value interface{}
IfEq interface{}
IgnoreDefinition bool
}
func (s SetValue) FromInternal(data map[string]interface{}) {
v, ok := values.GetValue(data, strings.Split(s.From, "/")...)
if s.IfEq == nil {
values.PutValue(data, s.Value, strings.Split(s.getTo(), "/")...)
return
}
v, ok := values.GetValue(data, strings.Split(s.Field, "/")...)
if !ok {
return
}
if v == s.IfEq {
values.PutValue(data, s.Value, strings.Split(s.To, "/")...)
values.PutValue(data, s.Value, strings.Split(s.getTo(), "/")...)
}
}
func (s SetValue) getTo() string {
if s.To == "" {
return s.Field
}
return s.To
}
func (s SetValue) ToInternal(data map[string]interface{}) {
v, ok := values.GetValue(data, strings.Split(s.To, "/")...)
v, ok := values.GetValue(data, strings.Split(s.getTo(), "/")...)
if !ok {
return
}
if v == s.Value {
values.PutValue(data, s.IfEq, strings.Split(s.From, "/")...)
if s.IfEq == nil {
values.RemoveValue(data, strings.Split(s.Field, "/")...)
} else if v == s.Value {
values.PutValue(data, s.IfEq, strings.Split(s.Field, "/")...)
}
}
func (s SetValue) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
_, _, _, ok, err := getField(schema, schemas, s.To)
if s.IgnoreDefinition {
return nil
}
_, _, _, ok, err := getField(schema, schemas, s.getTo())
if err != nil {
return err
}
if !ok {
return fmt.Errorf("failed to find defined field for %s on schemas %s", s.To, schema.ID)
return fmt.Errorf("failed to find defined field for %s on schemas %s", s.getTo(), schema.ID)
}
return nil

View File

@@ -47,7 +47,7 @@ func (s SliceToMap) ToInternal(data map[string]interface{}) {
}
func (s SliceToMap) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
err := validateField(s.Field, schema)
err := ValidateField(s.Field, schema)
if err != nil {
return err
}

View File

@@ -0,0 +1,36 @@
package mapper
import (
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/values"
)
type UntypedMove struct {
From, To string
Separator string
}
func (m UntypedMove) FromInternal(data map[string]interface{}) {
if v, ok := values.RemoveValue(data, strings.Split(m.From, m.getSep())...); ok {
values.PutValue(data, v, strings.Split(m.To, m.getSep())...)
}
}
func (m UntypedMove) ToInternal(data map[string]interface{}) {
if v, ok := values.RemoveValue(data, strings.Split(m.To, m.getSep())...); ok {
values.PutValue(data, v, strings.Split(m.From, m.getSep())...)
}
}
func (m UntypedMove) getSep() string {
if m.Separator == "" {
return "/"
}
return m.Separator
}
func (m UntypedMove) ModifySchema(s *types.Schema, schemas *types.Schemas) error {
return nil
}

View File

@@ -26,13 +26,25 @@ var (
}
)
func (s *Schemas) TypeName(name string, obj interface{}) *Schemas {
s.typeNames[reflect.TypeOf(obj)] = name
return s
}
func (s *Schemas) getTypeName(t reflect.Type) string {
if name, ok := s.typeNames[t]; ok {
return name
}
return convert.LowerTitle(t.Name())
}
func (s *Schemas) AddMapperForType(version *APIVersion, obj interface{}, mapper ...Mapper) *Schemas {
if len(mapper) == 0 {
return s
}
t := reflect.TypeOf(obj)
typeName := convert.LowerTitle(t.Name())
typeName := s.getTypeName(t)
if len(mapper) == 1 {
return s.AddMapper(version, typeName, mapper[0])
}
@@ -111,7 +123,7 @@ func (s *Schemas) setupFilters(schema *Schema) {
}
func (s *Schemas) MustCustomizeType(version *APIVersion, obj interface{}, f func(*Schema)) *Schemas {
name := convert.LowerTitle(reflect.TypeOf(obj).Name())
name := s.getTypeName(reflect.TypeOf(obj))
schema := s.Schema(version, name)
if schema == nil {
panic("Failed to find schema " + name)
@@ -127,7 +139,7 @@ func (s *Schemas) MustCustomizeType(version *APIVersion, obj interface{}, f func
}
func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...reflect.Type) (*Schema, error) {
typeName := convert.LowerTitle(t.Name())
typeName := s.getTypeName(t)
existing := s.Schema(version, typeName)
if existing != nil {
@@ -167,6 +179,7 @@ func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...r
mapper := &typeMapper{
Mappers: mappers,
root: schema.CanList(),
}
if err := mapper.ModifySchema(schema, s); err != nil {
@@ -376,6 +389,8 @@ func getKeyValue(input string) (string, string) {
func (s *Schemas) determineSchemaType(version *APIVersion, t reflect.Type) (string, error) {
switch t.Kind() {
case reflect.Uint8:
return "byte", nil
case reflect.Bool:
return "boolean", nil
case reflect.Int:
@@ -397,6 +412,9 @@ func (s *Schemas) determineSchemaType(version *APIVersion, t reflect.Type) (stri
if err != nil {
return "", err
}
if subType == "byte" {
return "base64", nil
}
return fmt.Sprintf("array[%s]", subType), nil
case reflect.String:
return "string", nil

View File

@@ -3,8 +3,8 @@ package types
import (
"bytes"
"fmt"
"reflect"
"strings"
"sync"
"github.com/rancher/norman/name"
@@ -29,6 +29,7 @@ type BackReference struct {
type Schemas struct {
sync.Mutex
typeNames map[reflect.Type]string
schemasByPath map[string]map[string]*Schema
schemasBySubContext map[string]*Schema
mappers map[string]map[string][]Mapper
@@ -44,6 +45,7 @@ type Schemas struct {
func NewSchemas() *Schemas {
return &Schemas{
typeNames: map[reflect.Type]string{},
schemasByPath: map[string]map[string]*Schema{},
schemasBySubContext: map[string]*Schema{},
mappers: map[string]map[string][]Mapper{},

View File

@@ -3,6 +3,7 @@ package types
import (
"encoding/json"
"net/http"
"net/url"
)
type ValuesMap struct {
@@ -79,6 +80,7 @@ type APIContext struct {
Schema *Schema
Schemas *Schemas
Version *APIVersion
Query url.Values
ResponseFormat string
ReferenceValidator ReferenceValidator
ResponseWriter ResponseWriter