1
0
mirror of https://github.com/rancher/rke.git synced 2025-06-23 14:07:13 +00:00

Vendor Update

This commit is contained in:
galal-hussein 2017-11-28 19:44:55 +02:00
parent e21bf80950
commit c77d3b51be
20 changed files with 461 additions and 212 deletions

View File

@ -55,7 +55,7 @@ func IsNotFound(err error) bool {
return apiError.StatusCode == http.StatusNotFound
}
func newApiError(resp *http.Response, url string) *APIError {
func newAPIError(resp *http.Response, url string) *APIError {
contents, err := ioutil.ReadAll(resp.Body)
var body string
if err != nil {
@ -161,7 +161,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
defer resp.Body.Close()
if resp.StatusCode != 200 {
return result, newApiError(resp, opts.URL)
return result, newAPIError(resp, opts.URL)
}
schemasURLs := resp.Header.Get("X-API-Schemas")
@ -184,7 +184,7 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
defer resp.Body.Close()
if resp.StatusCode != 200 {
return result, newApiError(resp, opts.URL)
return result, newAPIError(resp, opts.URL)
}
}
@ -244,7 +244,7 @@ func (a *APIBaseClient) Post(url string, createObj interface{}, respObject inter
func (a *APIBaseClient) GetLink(resource types.Resource, link string, respObject interface{}) error {
url := resource.Links[link]
if url == "" {
return fmt.Errorf("Failed to find link: %s", link)
return fmt.Errorf("failed to find link: %s", link)
}
return a.Ops.DoGet(url, &types.ListOpts{}, respObject)
@ -270,12 +270,12 @@ func (a *APIBaseClient) Delete(existing *types.Resource) error {
}
func (a *APIBaseClient) Reload(existing *types.Resource, output interface{}) error {
selfUrl, ok := existing.Links[SELF]
selfURL, ok := existing.Links[SELF]
if !ok {
return errors.New(fmt.Sprintf("Failed to find self URL of [%v]", existing))
return fmt.Errorf("failed to find self URL of [%v]", existing)
}
return a.Ops.DoGet(selfUrl, NewListOpts(), output)
return a.Ops.DoGet(selfURL, NewListOpts(), output)
}
func (a *APIBaseClient) Action(schemaType string, action string,

View File

@ -35,6 +35,13 @@ func NewObjectClient(namespace string, restClient rest.Interface, apiResource *m
}
}
func (p *ObjectClient) getAPIPrefix() string {
if p.gvk.Group == "" {
return "api"
}
return "apis"
}
func (p *ObjectClient) Create(o runtime.Object) (runtime.Object, error) {
ns := p.ns
if obj, ok := o.(metav1.Object); ok && obj.GetNamespace() != "" {
@ -42,7 +49,7 @@ func (p *ObjectClient) Create(o runtime.Object) (runtime.Object, error) {
}
result := p.Factory.Object()
err := p.restClient.Post().
Prefix("apis", p.gvk.Group, p.gvk.Version).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
NamespaceIfScoped(ns, p.resource.Namespaced).
Resource(p.resource.Name).
Body(o).
@ -54,7 +61,7 @@ func (p *ObjectClient) Create(o runtime.Object) (runtime.Object, error) {
func (p *ObjectClient) Get(name string, opts metav1.GetOptions) (runtime.Object, error) {
result := p.Factory.Object()
err := p.restClient.Get().
Prefix("apis", p.gvk.Group, p.gvk.Version).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
NamespaceIfScoped(p.ns, p.resource.Namespaced).
Resource(p.resource.Name).
VersionedParams(&opts, dynamic.VersionedParameterEncoderWithV1Fallback).
@ -74,7 +81,7 @@ func (p *ObjectClient) Update(name string, o runtime.Object) (runtime.Object, er
return result, errors.New("object missing name")
}
err := p.restClient.Put().
Prefix("apis", p.gvk.Group, p.gvk.Version).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
NamespaceIfScoped(ns, p.resource.Namespaced).
Resource(p.resource.Name).
Name(name).
@ -86,7 +93,7 @@ func (p *ObjectClient) Update(name string, o runtime.Object) (runtime.Object, er
func (p *ObjectClient) Delete(name string, opts *metav1.DeleteOptions) error {
return p.restClient.Delete().
Prefix("apis", p.gvk.Group, p.gvk.Version).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
NamespaceIfScoped(p.ns, p.resource.Namespaced).
Resource(p.resource.Name).
Name(name).
@ -98,7 +105,7 @@ func (p *ObjectClient) Delete(name string, opts *metav1.DeleteOptions) error {
func (p *ObjectClient) List(opts metav1.ListOptions) (runtime.Object, error) {
result := p.Factory.List()
return result, p.restClient.Get().
Prefix("apis", p.gvk.Group, p.gvk.Version).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
NamespaceIfScoped(p.ns, p.resource.Namespaced).
Resource(p.resource.Name).
VersionedParams(&opts, dynamic.VersionedParameterEncoderWithV1Fallback).
@ -108,7 +115,7 @@ func (p *ObjectClient) List(opts metav1.ListOptions) (runtime.Object, error) {
func (p *ObjectClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
r, err := p.restClient.Get().
Prefix("apis", p.gvk.Group, p.gvk.Version).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
Prefix("watch").
Namespace(p.ns).
NamespaceIfScoped(p.ns, p.resource.Namespaced).
@ -127,7 +134,7 @@ func (p *ObjectClient) Watch(opts metav1.ListOptions) (watch.Interface, error) {
func (p *ObjectClient) DeleteCollection(deleteOptions *metav1.DeleteOptions, listOptions metav1.ListOptions) error {
return p.restClient.Delete().
Prefix("apis", p.gvk.Group, p.gvk.Version).
Prefix(p.getAPIPrefix(), p.gvk.Group, p.gvk.Version).
NamespaceIfScoped(p.ns, p.resource.Namespaced).
Resource(p.resource.Name).
VersionedParams(&listOptions, dynamic.VersionedParameterEncoderWithV1Fallback).

View File

@ -34,7 +34,7 @@ func (a *APIOperations) DoDelete(url string) error {
io.Copy(ioutil.Discard, resp.Body)
if resp.StatusCode >= 300 {
return newApiError(resp, url)
return newAPIError(resp, url)
}
return nil
@ -68,7 +68,7 @@ func (a *APIOperations) DoGet(url string, opts *types.ListOpts, respObject inter
defer resp.Body.Close()
if resp.StatusCode != 200 {
return newApiError(resp, url)
return newAPIError(resp, url)
}
byteContent, err := ioutil.ReadAll(resp.Body)
@ -97,16 +97,16 @@ func (a *APIOperations) DoList(schemaType string, opts *types.ListOpts, respObje
return errors.New("Resource type [" + schemaType + "] is not listable")
}
collectionUrl, ok := schema.Links[COLLECTION]
collectionURL, ok := schema.Links[COLLECTION]
if !ok {
return errors.New("Failed to find collection URL for [" + schemaType + "]")
}
return a.DoGet(collectionUrl, opts, respObject)
return a.DoGet(collectionURL, opts, respObject)
}
func (a *APIOperations) DoNext(nextUrl string, respObject interface{}) error {
return a.DoGet(nextUrl, nil, respObject)
func (a *APIOperations) DoNext(nextURL string, respObject interface{}) error {
return a.DoGet(nextURL, nil, respObject)
}
func (a *APIOperations) DoModify(method string, url string, createObj interface{}, respObject interface{}) error {
@ -136,7 +136,7 @@ func (a *APIOperations) DoModify(method string, url string, createObj interface{
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return newApiError(resp, url)
return newAPIError(resp, url)
}
byteContent, err := ioutil.ReadAll(resp.Body)
@ -170,16 +170,16 @@ func (a *APIOperations) DoCreate(schemaType string, createObj interface{}, respO
return errors.New("Resource type [" + schemaType + "] is not creatable")
}
var collectionUrl string
collectionUrl, ok = schema.Links[COLLECTION]
var collectionURL string
collectionURL, ok = schema.Links[COLLECTION]
if !ok {
// return errors.New("Failed to find collection URL for [" + schemaType + "]")
// This is a hack to address https://github.com/rancher/cattle/issues/254
re := regexp.MustCompile("schemas.*")
collectionUrl = re.ReplaceAllString(schema.Links[SELF], schema.PluralName)
collectionURL = re.ReplaceAllString(schema.Links[SELF], schema.PluralName)
}
return a.DoModify("POST", collectionUrl, createObj, respObject)
return a.DoModify("POST", collectionURL, createObj, respObject)
}
func (a *APIOperations) DoUpdate(schemaType string, existing *types.Resource, updates interface{}, respObject interface{}) error {
@ -187,9 +187,9 @@ func (a *APIOperations) DoUpdate(schemaType string, existing *types.Resource, up
return errors.New("Existing object is nil")
}
selfUrl, ok := existing.Links[SELF]
selfURL, ok := existing.Links[SELF]
if !ok {
return errors.New(fmt.Sprintf("Failed to find self URL of [%v]", existing))
return fmt.Errorf("failed to find self URL of [%v]", existing)
}
if updates == nil {
@ -209,7 +209,7 @@ func (a *APIOperations) DoUpdate(schemaType string, existing *types.Resource, up
return errors.New("Resource type [" + schemaType + "] is not updatable")
}
return a.DoModify("PUT", selfUrl, updates, respObject)
return a.DoModify("PUT", selfURL, updates, respObject)
}
func (a *APIOperations) DoByID(schemaType string, id string, respObject interface{}) error {
@ -222,12 +222,12 @@ func (a *APIOperations) DoByID(schemaType string, id string, respObject interfac
return errors.New("Resource type [" + schemaType + "] can not be looked up by ID")
}
collectionUrl, ok := schema.Links[COLLECTION]
collectionURL, ok := schema.Links[COLLECTION]
if !ok {
return errors.New("Failed to find collection URL for [" + schemaType + "]")
}
return a.DoGet(collectionUrl+"/"+id, nil, respObject)
return a.DoGet(collectionURL+"/"+id, nil, respObject)
}
func (a *APIOperations) DoResourceDelete(schemaType string, existing *types.Resource) error {
@ -240,12 +240,12 @@ func (a *APIOperations) DoResourceDelete(schemaType string, existing *types.Reso
return errors.New("Resource type [" + schemaType + "] can not be deleted")
}
selfUrl, ok := existing.Links[SELF]
selfURL, ok := existing.Links[SELF]
if !ok {
return errors.New(fmt.Sprintf("Failed to find self URL of [%v]", existing))
return fmt.Errorf("failed to find self URL of [%v]", existing)
}
return a.DoDelete(selfUrl)
return a.DoDelete(selfURL)
}
func (a *APIOperations) DoAction(schemaType string, action string,
@ -255,9 +255,9 @@ func (a *APIOperations) DoAction(schemaType string, action string,
return errors.New("Existing object is nil")
}
actionUrl, ok := existing.Actions[action]
actionURL, ok := existing.Actions[action]
if !ok {
return errors.New(fmt.Sprintf("Action [%v] not available on [%v]", action, existing))
return fmt.Errorf("action [%v] not available on [%v]", action, existing)
}
_, ok = a.Types[schemaType]
@ -278,7 +278,7 @@ func (a *APIOperations) DoAction(schemaType string, action string,
input = bytes.NewBuffer(bodyContent)
}
req, err := http.NewRequest("POST", actionUrl, input)
req, err := http.NewRequest("POST", actionURL, input)
if err != nil {
return err
}
@ -295,7 +295,7 @@ func (a *APIOperations) DoAction(schemaType string, action string,
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return newApiError(resp, actionUrl)
return newAPIError(resp, actionURL)
}
byteContent, err := ioutil.ReadAll(resp.Body)

View File

@ -25,7 +25,7 @@ type GenericController interface {
Informer() cache.SharedIndexInformer
AddHandler(handler HandlerFunc)
Enqueue(namespace, name string)
Start(threadiness int, ctx context.Context) error
Start(ctx context.Context, threadiness int) error
}
type genericController struct {
@ -69,12 +69,12 @@ func (g *genericController) AddHandler(handler HandlerFunc) {
g.handlers = append(g.handlers, handler)
}
func (g *genericController) Start(threadiness int, ctx context.Context) error {
func (g *genericController) Start(ctx context.Context, threadiness int) error {
g.Lock()
defer g.Unlock()
if !g.running {
go g.run(threadiness, ctx)
go g.run(ctx, threadiness)
}
g.running = true
@ -88,7 +88,7 @@ func (g *genericController) queueObject(obj interface{}) {
}
}
func (g *genericController) run(threadiness int, ctx context.Context) {
func (g *genericController) run(ctx context.Context, threadiness int) {
defer utilruntime.HandleCrash()
defer g.queue.ShutDown()

View File

@ -1,108 +1,108 @@
package types
var (
COND_EQ = QueryConditionType{"eq", 1}
COND_NE = QueryConditionType{"ne", 1}
COND_NULL = QueryConditionType{"null", 0}
COND_NOTNULL = QueryConditionType{"notnull", 0}
COND_IN = QueryConditionType{"in", -1}
COND_NOTIN = QueryConditionType{"notin", -1}
COND_OR = QueryConditionType{"or", 1}
COND_AND = QueryConditionType{"and", 1}
import (
"github.com/rancher/norman/types/convert"
)
mods = map[string]QueryConditionType{
COND_EQ.Name: COND_EQ,
COND_NE.Name: COND_NE,
COND_NULL.Name: COND_NULL,
COND_NOTNULL.Name: COND_NOTNULL,
COND_IN.Name: COND_IN,
COND_NOTIN.Name: COND_NOTIN,
COND_OR.Name: COND_OR,
COND_AND.Name: COND_AND,
var (
CondEQ = QueryConditionType{ModifierEQ, 1}
CondNE = QueryConditionType{ModifierNE, 1}
CondNull = QueryConditionType{ModifierNull, 0}
CondNotNull = QueryConditionType{ModifierNotNull, 0}
CondIn = QueryConditionType{ModifierIn, -1}
CondNotIn = QueryConditionType{ModifierNotIn, -1}
CondOr = QueryConditionType{ModifierType("or"), 1}
CondAnd = QueryConditionType{ModifierType("and"), 1}
mods = map[ModifierType]QueryConditionType{
CondEQ.Name: CondEQ,
CondNE.Name: CondNE,
CondNull.Name: CondNull,
CondNotNull.Name: CondNotNull,
CondIn.Name: CondIn,
CondNotIn.Name: CondNotIn,
CondOr.Name: CondOr,
CondAnd.Name: CondAnd,
}
)
type QueryConditionType struct {
Name string
Name ModifierType
Args int
}
type QueryCondition struct {
Field string
Values []interface{}
Value string
Values map[string]bool
conditionType QueryConditionType
left, right *QueryCondition
}
func (q *QueryCondition) Valid(data map[string]interface{}) bool {
switch q.conditionType {
case CondAnd:
if q.left == nil || q.right == nil {
return false
}
return q.left.Valid(data) && q.right.Valid(data)
case CondOr:
if q.left == nil || q.right == nil {
return false
}
return q.left.Valid(data) || q.right.Valid(data)
case CondEQ:
return q.Value == convert.ToString(data[q.Field])
case CondNE:
return q.Value != convert.ToString(data[q.Field])
case CondIn:
return q.Values[convert.ToString(data[q.Field])]
case CondNotIn:
return !q.Values[convert.ToString(data[q.Field])]
case CondNotNull:
return convert.ToString(data[q.Field]) != ""
case CondNull:
return convert.ToString(data[q.Field]) == ""
}
return false
}
func (q *QueryCondition) ToCondition() Condition {
cond := Condition{
Modifier: q.conditionType.Name,
}
if q.conditionType.Args == 1 && len(q.Values) > 0 {
cond.Value = q.Values[0]
if q.conditionType.Args == 1 {
cond.Value = q.Value
} else if q.conditionType.Args == -1 {
cond.Value = q.Values
stringValues := []string{}
for val := range q.Values {
stringValues = append(stringValues, val)
}
cond.Value = stringValues
}
return cond
}
func ValidMod(mod string) bool {
func ValidMod(mod ModifierType) bool {
_, ok := mods[mod]
return ok
}
func NewConditionFromString(field, mod string, values ...interface{}) *QueryCondition {
return &QueryCondition{
func NewConditionFromString(field string, mod ModifierType, values ...string) *QueryCondition {
q := &QueryCondition{
Field: field,
Values: values,
conditionType: mods[mod],
Values: map[string]bool{},
}
}
func NewCondition(mod QueryConditionType, values ...interface{}) *QueryCondition {
return &QueryCondition{
Values: values,
conditionType: mod,
for i, value := range values {
if i == 0 {
q.Value = value
}
q.Values[value] = true
}
}
func NE(value interface{}) *QueryCondition {
return NewCondition(COND_NE, value)
}
func EQ(value interface{}) *QueryCondition {
return NewCondition(COND_EQ, value)
}
func NULL(value interface{}) *QueryCondition {
return NewCondition(COND_NULL)
}
func NOTNULL(value interface{}) *QueryCondition {
return NewCondition(COND_NOTNULL)
}
func IN(values ...interface{}) *QueryCondition {
return NewCondition(COND_IN, values...)
}
func NOTIN(values ...interface{}) *QueryCondition {
return NewCondition(COND_NOTIN, values...)
}
func (c *QueryCondition) AND(right *QueryCondition) *QueryCondition {
return &QueryCondition{
conditionType: COND_AND,
left: c,
right: right,
}
}
func (c *QueryCondition) OR(right *QueryCondition) *QueryCondition {
return &QueryCondition{
conditionType: COND_OR,
left: c,
right: right,
}
return q
}

View File

@ -132,12 +132,12 @@ func ToStringSlice(data interface{}) []string {
return nil
}
func ToObj(data interface{}, obj interface{}) error {
func ToObj(data interface{}, into interface{}) error {
bytes, err := json.Marshal(data)
if err != nil {
return err
}
return json.Unmarshal(bytes, obj)
return json.Unmarshal(bytes, into)
}
func EncodeToMap(obj interface{}) (map[string]interface{}, error) {

11
vendor/github.com/rancher/norman/types/convert/ref.go generated vendored Normal file
View File

@ -0,0 +1,11 @@
package convert
import "fmt"
func ToReference(typeName string) string {
return fmt.Sprintf("reference[%s]", typeName)
}
func ToFullReference(path, typeName string) string {
return fmt.Sprintf("reference[%s/schemas/%s]", path, typeName)
}

View File

@ -13,7 +13,6 @@ func ToValuesSlice(value string) []string {
value = strings.TrimSpace(value)
if strings.HasPrefix(value, "(") && strings.HasSuffix(value, ")") {
return splitRegexp.Split(value[1:len(value)-1], -1)
} else {
return []string{value}
}
return []string{value}
}

View File

@ -1,7 +1,6 @@
package types
import (
"github.com/pkg/errors"
"github.com/rancher/norman/types/definition"
)
@ -11,14 +10,37 @@ type Mapper interface {
ModifySchema(schema *Schema, schemas *Schemas) error
}
type TypeMapper struct {
type Mappers []Mapper
func (m Mappers) FromInternal(data map[string]interface{}) {
for _, mapper := range m {
mapper.FromInternal(data)
}
}
func (m Mappers) ToInternal(data map[string]interface{}) {
for i := len(m) - 1; i >= 0; i-- {
m[i].ToInternal(data)
}
}
func (m Mappers) ModifySchema(schema *Schema, schemas *Schemas) error {
for _, mapper := range m {
if err := mapper.ModifySchema(schema, schemas); err != nil {
return err
}
}
return nil
}
type typeMapper struct {
Mappers []Mapper
typeName string
subSchemas map[string]*Schema
subArraySchemas map[string]*Schema
}
func (t *TypeMapper) FromInternal(data map[string]interface{}) {
func (t *typeMapper) FromInternal(data map[string]interface{}) {
for fieldName, schema := range t.subSchemas {
if schema.Mapper == nil {
continue
@ -38,19 +60,29 @@ func (t *TypeMapper) FromInternal(data map[string]interface{}) {
}
}
for _, mapper := range t.Mappers {
mapper.FromInternal(data)
}
Mappers(t.Mappers).FromInternal(data)
if data != nil {
data["type"] = t.typeName
if _, ok := data["type"]; !ok {
data["type"] = t.typeName
}
name, _ := data["name"].(string)
namespace, _ := data["namespace"].(string)
if _, ok := data["id"]; !ok {
if name != "" {
if namespace == "" {
data["id"] = name
} else {
data["id"] = namespace + ":" + name
}
}
}
}
}
func (t *TypeMapper) ToInternal(data map[string]interface{}) {
for i := len(t.Mappers) - 1; i >= 0; i-- {
t.Mappers[i].ToInternal(data)
}
func (t *typeMapper) ToInternal(data map[string]interface{}) {
Mappers(t.Mappers).ToInternal(data)
for fieldName, schema := range t.subArraySchemas {
if schema.Mapper == nil {
@ -71,7 +103,7 @@ func (t *TypeMapper) ToInternal(data map[string]interface{}) {
}
}
func (t *TypeMapper) ModifySchema(schema *Schema, schemas *Schemas) error {
func (t *typeMapper) ModifySchema(schema *Schema, schemas *Schemas) error {
t.subSchemas = map[string]*Schema{}
t.subArraySchemas = map[string]*Schema{}
t.typeName = schema.ID
@ -94,11 +126,5 @@ func (t *TypeMapper) ModifySchema(schema *Schema, schemas *Schemas) error {
}
}
for _, mapper := range t.Mappers {
if err := mapper.ModifySchema(schema, schemas); err != nil {
return errors.Wrapf(err, "mapping type %s", schema.ID)
}
}
return nil
return Mappers(t.Mappers).ModifySchema(schema, schemas)
}

View File

@ -6,13 +6,19 @@ import (
"strconv"
"strings"
"net/http"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/definition"
"github.com/rancher/norman/types/slice"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
namespacedType = reflect.TypeOf(Namespaced{})
resourceType = reflect.TypeOf(Resource{})
typeType = reflect.TypeOf(metav1.TypeMeta{})
metaType = reflect.TypeOf(metav1.ObjectMeta{})
blacklistNames = map[string]bool{
"links": true,
@ -20,10 +26,17 @@ var (
}
)
func (s *Schemas) AddMapperForType(version *APIVersion, obj interface{}, mapper Mapper) *Schemas {
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())
return s.AddMapper(version, typeName, mapper)
if len(mapper) == 1 {
return s.AddMapper(version, typeName, mapper[0])
}
return s.AddMapper(version, typeName, Mappers(mapper))
}
func (s *Schemas) MustImport(version *APIVersion, obj interface{}, externalOverrides ...interface{}) *Schemas {
@ -35,8 +48,13 @@ func (s *Schemas) MustImport(version *APIVersion, obj interface{}, externalOverr
return s
}
func (s *Schemas) MustImportAndCustomize(version *APIVersion, obj interface{}, f func(*Schema), externalOverrides ...interface{}) *Schemas {
return s.MustImport(version, obj, externalOverrides...).
MustCustomizeType(version, obj, f)
}
func (s *Schemas) Import(version *APIVersion, obj interface{}, externalOverrides ...interface{}) (*Schema, error) {
types := []reflect.Type{}
var types []reflect.Type
for _, override := range externalOverrides {
types = append(types, reflect.TypeOf(override))
}
@ -60,6 +78,54 @@ func (s *Schemas) newSchemaFromType(version *APIVersion, t reflect.Type, typeNam
return schema, nil
}
func (s *Schemas) setupFilters(schema *Schema) {
if !slice.ContainsString(schema.CollectionMethods, http.MethodGet) {
return
}
for fieldName, field := range schema.ResourceFields {
var mods []ModifierType
switch field.Type {
case "enum":
mods = []ModifierType{ModifierEQ, ModifierNE, ModifierIn, ModifierNotIn}
case "string":
mods = []ModifierType{ModifierEQ, ModifierNE, ModifierIn, ModifierNotIn}
case "int":
mods = []ModifierType{ModifierEQ, ModifierNE, ModifierIn, ModifierNotIn}
case "boolean":
mods = []ModifierType{ModifierEQ, ModifierNE}
default:
if definition.IsReferenceType(field.Type) {
mods = []ModifierType{ModifierEQ, ModifierNE, ModifierIn, ModifierNotIn}
}
}
if len(mods) > 0 {
if schema.CollectionFilters == nil {
schema.CollectionFilters = map[string]Filter{}
}
schema.CollectionFilters[fieldName] = Filter{
Modifiers: mods,
}
}
}
}
func (s *Schemas) MustCustomizeType(version *APIVersion, obj interface{}, f func(*Schema)) *Schemas {
name := convert.LowerTitle(reflect.TypeOf(obj).Name())
schema := s.Schema(version, name)
if schema == nil {
panic("Failed to find schema " + name)
}
f(schema)
if schema.SubContext != "" {
s.schemasBySubContext[schema.SubContext] = schema
}
return s
}
func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...reflect.Type) (*Schema, error) {
typeName := convert.LowerTitle(t.Name())
@ -75,8 +141,13 @@ func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...r
return nil, err
}
mapper := s.mapper(&schema.Version, schema.ID)
if mapper != nil {
mappers := s.mapper(&schema.Version, schema.ID)
if schema.CanList() {
mappers = append(s.DefaultMappers, mappers...)
}
mappers = append(mappers, s.DefaultPostMappers...)
if len(mappers) > 0 {
copy, err := s.newSchemaFromType(version, t, typeName)
if err != nil {
return nil, err
@ -90,14 +161,16 @@ func (s *Schemas) importType(version *APIVersion, t reflect.Type, overrides ...r
}
}
if mapper == nil {
mapper = &TypeMapper{}
mapper := &typeMapper{
Mappers: mappers,
}
if err := mapper.ModifySchema(schema, s); err != nil {
return nil, err
}
s.setupFilters(schema)
schema.Mapper = mapper
s.AddSchema(schema)
@ -114,6 +187,9 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
schema.ResourceMethods = []string{"GET", "PUT", "DELETE"}
}
hasType := false
hasMeta := false
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
@ -128,12 +204,23 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
continue
}
if field.Anonymous && jsonName == "" && field.Type == typeType {
hasType = true
}
if field.Anonymous && jsonName == "metadata" && field.Type == metaType {
hasMeta = true
}
if field.Anonymous && jsonName == "" {
t := field.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() == reflect.Struct {
if t == namespacedType {
schema.Scope = NamespaceScope
}
if err := s.readFields(schema, t); err != nil {
return err
}
@ -144,6 +231,9 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
fieldName := jsonName
if fieldName == "" {
fieldName = convert.LowerTitle(field.Name)
if strings.HasSuffix(fieldName, "ID") {
fieldName = strings.TrimSuffix(fieldName, "ID") + "Id"
}
}
if blacklistNames[fieldName] {
@ -156,6 +246,7 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
schemaField := Field{
Create: true,
Update: true,
Nullable: true,
CodeName: field.Name,
}
@ -177,15 +268,15 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
schemaField.Type = inferedType
}
if field.Type == metaType {
schema.CollectionMethods = []string{"GET", "POST"}
schema.ResourceMethods = []string{"GET", "PUT", "DELETE"}
}
logrus.Debugf("Setting field %s.%s: %#v", schema.ID, fieldName, schemaField)
schema.ResourceFields[fieldName] = schemaField
}
if hasType && hasMeta {
schema.CollectionMethods = []string{"GET", "POST"}
schema.ResourceMethods = []string{"GET", "PUT", "DELETE"}
}
return nil
}

26
vendor/github.com/rancher/norman/types/schema_funcs.go generated vendored Normal file
View File

@ -0,0 +1,26 @@
package types
import (
"net/http"
"github.com/rancher/norman/types/slice"
)
func (s *Schema) MustCustomizeField(name string, f func(f Field) Field) *Schema {
field, ok := s.ResourceFields[name]
if !ok {
panic("Failed to find field " + name + " on schema " + s.ID)
}
s.ResourceFields[name] = f(field)
return s
}
func (v *APIVersion) Equals(other *APIVersion) bool {
return v.Version == other.Version &&
v.Group == other.Group &&
v.Path == other.Path
}
func (s *Schema) CanList() bool {
return slice.ContainsString(s.CollectionMethods, http.MethodGet)
}

View File

@ -13,25 +13,43 @@ type SchemaCollection struct {
Data []Schema
}
type SchemaInitFunc func(*Schemas) *Schemas
type Schemas struct {
schemasByPath map[string]map[string]*Schema
mappers map[string]map[string]Mapper
versions []APIVersion
schemas []*Schema
errors []error
schemasByPath map[string]map[string]*Schema
schemasBySubContext map[string]*Schema
mappers map[string]map[string][]Mapper
DefaultMappers []Mapper
DefaultPostMappers []Mapper
versions []APIVersion
schemas []*Schema
errors []error
}
func NewSchemas() *Schemas {
return &Schemas{
schemasByPath: map[string]map[string]*Schema{},
mappers: map[string]map[string]Mapper{},
schemasByPath: map[string]map[string]*Schema{},
schemasBySubContext: map[string]*Schema{},
mappers: map[string]map[string][]Mapper{},
}
}
func (s *Schemas) Init(initFunc SchemaInitFunc) *Schemas {
return initFunc(s)
}
func (s *Schemas) Err() error {
return NewErrors(s.errors)
}
func (s *Schemas) SubContext(subContext string) *Schema {
return s.schemasBySubContext[subContext]
}
func (s *Schemas) SubContextSchemas() map[string]*Schema {
return s.schemasBySubContext
}
func (s *Schemas) AddSchemas(schema *Schemas) *Schemas {
for _, schema := range schema.Schemas() {
s.AddSchema(schema)
@ -40,7 +58,7 @@ func (s *Schemas) AddSchemas(schema *Schemas) *Schemas {
}
func (s *Schemas) AddSchema(schema *Schema) *Schemas {
schema.Type = "schema"
schema.Type = "/v1-meta/schemas/schema"
if schema.ID == "" {
s.errors = append(s.errors, fmt.Errorf("ID is not set on schema: %v", schema))
return s
@ -58,6 +76,9 @@ func (s *Schemas) AddSchema(schema *Schema) *Schemas {
if schema.CodeNamePlural == "" {
schema.CodeNamePlural = name.GuessPluralName(schema.CodeName)
}
if schema.BaseType == "" {
schema.BaseType = schema.ID
}
schemas, ok := s.schemasByPath[schema.Version.Path]
if !ok {
@ -71,20 +92,21 @@ func (s *Schemas) AddSchema(schema *Schema) *Schemas {
s.schemas = append(s.schemas, schema)
}
if schema.SubContext != "" {
s.schemasBySubContext[schema.SubContext] = schema
}
return s
}
func (s *Schemas) AddMapper(version *APIVersion, schemaID string, mapper Mapper) *Schemas {
mappers, ok := s.mappers[version.Path]
if !ok {
mappers = map[string]Mapper{}
mappers = map[string][]Mapper{}
s.mappers[version.Path] = mappers
}
if _, ok := mappers[schemaID]; !ok {
mappers[schemaID] = mapper
}
mappers[schemaID] = append(mappers[schemaID], mapper)
return s
}
@ -100,7 +122,7 @@ func (s *Schemas) Schemas() []*Schema {
return s.schemas
}
func (s *Schemas) mapper(version *APIVersion, name string) Mapper {
func (s *Schemas) mapper(version *APIVersion, name string) []Mapper {
var (
path string
)
@ -133,10 +155,10 @@ func (s *Schemas) Schema(version *APIVersion, name string) *Schema {
path string
)
if strings.Contains(name, "/") {
idx := strings.LastIndex(name, "/")
path = name[0:idx]
name = name[idx+1:]
if strings.Contains(name, "/schemas/") {
parts := strings.SplitN(name, "/schemas/", 2)
path = parts[0]
name = parts[1]
} else if version != nil {
path = version.Path
} else {

View File

@ -27,12 +27,14 @@ func (r *RawResource) MarshalJSON() ([]byte, error) {
if r.ID != "" {
data["id"] = r.ID
}
data["type"] = r.Type
data["baseType"] = r.Schema.BaseType
data["links"] = r.Links
if r.ActionLinks {
data["actionLinks"] = r.Actions
} else {
data["action"] = r.Actions
data["actions"] = r.Actions
}
return json.Marshal(data)
}
@ -41,12 +43,19 @@ type ActionHandler func(actionName string, action *Action, request *APIContext)
type RequestHandler func(request *APIContext) error
type QueryFilter func(opts QueryOptions, data []map[string]interface{}) []map[string]interface{}
type Validator func(request *APIContext, data map[string]interface{}) error
type Formatter func(request *APIContext, resource *RawResource)
type ErrorHandler func(request *APIContext, err error)
type SubContextAttributeProvider interface {
Query(apiContext *APIContext, schema *Schema) []*QueryCondition
Create(apiContext *APIContext, schema *Schema) map[string]interface{}
}
type ResponseWriter interface {
Write(apiContext *APIContext, code int, obj interface{})
}
@ -57,22 +66,24 @@ type AccessControl interface {
}
type APIContext struct {
Action string
ID string
Type string
Link string
Method string
Schema *Schema
Schemas *Schemas
Version *APIVersion
ResponseFormat string
ReferenceValidator ReferenceValidator
ResponseWriter ResponseWriter
QueryOptions *QueryOptions
Body map[string]interface{}
URLBuilder URLBuilder
AccessControl AccessControl
SubContext map[string]interface{}
Action string
ID string
Type string
Link string
Method string
Schema *Schema
Schemas *Schemas
Version *APIVersion
ResponseFormat string
ReferenceValidator ReferenceValidator
ResponseWriter ResponseWriter
QueryFilter QueryFilter
SubContextAttributeProvider SubContextAttributeProvider
//QueryOptions *QueryOptions
URLBuilder URLBuilder
AccessControl AccessControl
SubContext map[string]string
Attributes map[string]interface{}
Request *http.Request
Response http.ResponseWriter
@ -82,6 +93,30 @@ func (r *APIContext) WriteResponse(code int, obj interface{}) {
r.ResponseWriter.Write(r, code, obj)
}
func (r *APIContext) FilterList(opts QueryOptions, obj []map[string]interface{}) []map[string]interface{} {
return r.QueryFilter(opts, obj)
}
func (r *APIContext) FilterObject(opts QueryOptions, obj map[string]interface{}) map[string]interface{} {
opts.Pagination = nil
result := r.QueryFilter(opts, []map[string]interface{}{obj})
if len(result) == 0 {
return nil
}
return result[0]
}
func (r *APIContext) Filter(opts QueryOptions, obj interface{}) interface{} {
switch v := obj.(type) {
case []map[string]interface{}:
return r.FilterList(opts, v)
case map[string]interface{}:
return r.FilterObject(opts, v)
}
return nil
}
var (
ASC = SortOrder("asc")
DESC = SortOrder("desc")
@ -100,20 +135,21 @@ type ReferenceValidator interface {
type URLBuilder interface {
Current() string
Collection(schema *Schema) string
Collection(schema *Schema, versionOverride *APIVersion) string
SubContextCollection(subContext *Schema, contextName string, schema *Schema) string
SchemaLink(schema *Schema) string
ResourceLink(resource *RawResource) string
RelativeToRoot(path string) string
//Link(resource Resource, name string) string
//ReferenceLink(resource Resource) string
//ReferenceByIdLink(resourceType string, id string) string
Version(version string) string
Version(version APIVersion) string
Marker(marker string) string
ReverseSort(order SortOrder) string
Sort(field string) string
SetSubContext(subContext string)
}
type Store interface {
ByID(apiContext *APIContext, schema *Schema, id string) (map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt *QueryOptions) ([]map[string]interface{}, error)
List(apiContext *APIContext, schema *Schema, opt QueryOptions) ([]map[string]interface{}, error)
Create(apiContext *APIContext, schema *Schema, data map[string]interface{}) (map[string]interface{}, error)
Update(apiContext *APIContext, schema *Schema, data map[string]interface{}, id string) (map[string]interface{}, error)
Delete(apiContext *APIContext, schema *Schema, id string) error

View File

@ -0,0 +1,10 @@
package slice
func ContainsString(slice []string, item string) bool {
for _, j := range slice {
if j == item {
return true
}
}
return false
}

View File

@ -27,12 +27,23 @@ type Sort struct {
Name string `json:"name,omitempty"`
Order SortOrder `json:"order,omitempty"`
Reverse string `json:"reverse,omitempty"`
Links map[string]string `json:"sortLinks,omitempty"`
Links map[string]string `json:"links,omitempty"`
}
var (
ModifierEQ ModifierType = "eq"
ModifierNE ModifierType = "ne"
ModifierNull ModifierType = "null"
ModifierNotNull ModifierType = "notnull"
ModifierIn ModifierType = "in"
ModifierNotIn ModifierType = "notin"
)
type ModifierType string
type Condition struct {
Modifier string `json:"modifier,omitempty"`
Value interface{} `json:"value,omitempty"`
Modifier ModifierType `json:"modifier,omitempty"`
Value interface{} `json:"value,omitempty"`
}
type Pagination struct {
@ -40,6 +51,7 @@ type Pagination struct {
First string `json:"first,omitempty"`
Previous string `json:"previous,omitempty"`
Next string `json:"next,omitempty"`
Last string `json:"last,omitempty"`
Limit *int64 `json:"limit,omitempty"`
Total *int64 `json:"total,omitempty"`
Partial bool `json:"partial,omitempty"`
@ -59,12 +71,20 @@ type APIVersion struct {
SubContexts map[string]bool `json:"subContext,omitempty"`
}
type Namespaced struct{}
var NamespaceScope TypeScope = "namespace"
type TypeScope string
type Schema struct {
ID string `json:"id,omitempty"`
CodeName string `json:"-"`
CodeNamePlural string `json:"-"`
PkgName string `json:"-"`
Type string `json:"type,omitempty"`
BaseType string `json:"baseType,omitempty"`
SubContext string `json:"-,omitempty"`
Links map[string]string `json:"links"`
Version APIVersion `json:"version"`
PluralName string `json:"pluralName,omitempty"`
@ -75,6 +95,7 @@ type Schema struct {
CollectionFields map[string]Field `json:"collectionFields,omitempty"`
CollectionActions map[string]Action `json:"collectionActions,omitempty"`
CollectionFilters map[string]Filter `json:"collectionFilters,omitempty"`
Scope TypeScope `json:"-"`
InternalSchema *Schema `json:"-"`
Mapper Mapper `json:"-"`
@ -115,7 +136,7 @@ type Action struct {
}
type Filter struct {
Modifiers []string `json:"modifiers,omitempty"`
Modifiers []ModifierType `json:"modifiers,omitempty"`
}
type ListOpts struct {

View File

@ -107,7 +107,7 @@ type AzureKubernetesServiceConfig struct {
type RancherKubernetesEngineConfig struct {
// Kubernetes nodes
Hosts []RKEConfigHost `yaml:"hosts" json:"hosts,omitempty"`
Nodes []RKEConfigNode `yaml:"nodes" json:"nodes,omitempty"`
// Kubernetes components
Services RKEConfigServices `yaml:"services" json:"services,omitempty"`
// Network configuration used in the kubernetes cluster (flannel, calico)
@ -120,18 +120,18 @@ type RancherKubernetesEngineConfig struct {
SSHKeyPath string `yaml:"ssh_key_path" json:"sshKeyPath,omitempty"`
}
type RKEConfigHost struct {
// SSH IP address of the host
IP string `yaml:"ip" json:"ip,omitempty"`
// Advertised address that will be used for components communication
AdvertiseAddress string `yaml:"advertise_address" json:"advertiseAddress,omitempty"`
// Host role in kubernetes cluster (controlplane, worker, or etcd)
type RKEConfigNode struct {
// IP or FQDN that is fully resolvable and used for SSH communication
Address string `yaml:"address" json:"address,omitempty"`
// Optional - Internal address that will be used for components communication
InternalAddress string `yaml:"internal_address" json:"internalAddress,omitempty"`
// Node role in kubernetes cluster (controlplane, worker, or etcd)
Role []string `yaml:"role" json:"role,omitempty"`
// Hostname of the host
AdvertisedHostname string `yaml:"advertised_hostname" json:"advertisedHostname,omitempty"`
// Optional - Hostname of the node
HostnameOverride string `yaml:"hostname_override" json:"hostnameOverride,omitempty"`
// SSH usesr that will be used by RKE
User string `yaml:"user" json:"user,omitempty"`
// Docker socket on the host that will be used in tunneling
// Optional - Docker socket on the node that will be used in tunneling
DockerSocket string `yaml:"docker_socket" json:"dockerSocket,omitempty"`
}

View File

@ -38,7 +38,7 @@ type ClusterController interface {
Informer() cache.SharedIndexInformer
AddHandler(handler ClusterHandlerFunc)
Enqueue(namespace, name string)
Start(threadiness int, ctx context.Context) error
Start(ctx context.Context, threadiness int) error
}
type ClusterInterface interface {

View File

@ -38,7 +38,7 @@ type ClusterNodeController interface {
Informer() cache.SharedIndexInformer
AddHandler(handler ClusterNodeHandlerFunc)
Enqueue(namespace, name string)
Start(threadiness int, ctx context.Context) error
Start(ctx context.Context, threadiness int) error
}
type ClusterNodeInterface interface {

View File

@ -445,7 +445,7 @@ func (in *NetworkConfig) DeepCopy() *NetworkConfig {
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *RKEConfigHost) DeepCopyInto(out *RKEConfigHost) {
func (in *RKEConfigNode) DeepCopyInto(out *RKEConfigNode) {
*out = *in
if in.Role != nil {
in, out := &in.Role, &out.Role
@ -455,12 +455,12 @@ func (in *RKEConfigHost) DeepCopyInto(out *RKEConfigHost) {
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RKEConfigHost.
func (in *RKEConfigHost) DeepCopy() *RKEConfigHost {
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RKEConfigNode.
func (in *RKEConfigNode) DeepCopy() *RKEConfigNode {
if in == nil {
return nil
}
out := new(RKEConfigHost)
out := new(RKEConfigNode)
in.DeepCopyInto(out)
return out
}
@ -490,9 +490,9 @@ func (in *RKEConfigServices) DeepCopy() *RKEConfigServices {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *RancherKubernetesEngineConfig) DeepCopyInto(out *RancherKubernetesEngineConfig) {
*out = *in
if in.Hosts != nil {
in, out := &in.Hosts, &out.Hosts
*out = make([]RKEConfigHost, len(*in))
if in.Nodes != nil {
in, out := &in.Nodes, &out.Nodes
*out = make([]RKEConfigNode, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}

View File

@ -3,4 +3,4 @@ github.com/rancher/types
k8s.io/kubernetes v1.8.3 transitive=true,staging=true
bitbucket.org/ww/goautoneg a547fc61f48d567d5b4ec6f8aee5573d8efce11d https://github.com/rancher/goautoneg.git
github.com/rancher/norman 80024df69414f7cce0847ea72b0557f14edbc852
github.com/rancher/norman faa1fb2148211044253fc2f6403008958c72b1f0