1
0
mirror of https://github.com/rancher/types.git synced 2025-09-25 12:21:24 +00:00

update norman

This commit is contained in:
Craig Jellick
2019-04-23 19:56:00 -07:00
parent d0700e6519
commit 3bbca006fb
15 changed files with 185 additions and 44 deletions

View File

@@ -1,9 +1,23 @@
---
pipeline:
build:
privileged: true
image: rancher/dapper:1.11.2
volumes:
- /var/run/docker.sock:/var/run/docker.sock
commands:
- dapper ci
kind: pipeline
name: default
platform:
os: linux
arch: amd64
steps:
- name: build
pull: default
image: rancher/dapper:1.11.2
commands:
- dapper ci
privileged: true
volumes:
- name: socket
path: /var/run/docker.sock
volumes:
- name: socket
host:
path: /var/run/docker.sock

View File

@@ -7,6 +7,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
@@ -216,7 +217,9 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
if err != nil {
return result, err
}
defer resp.Body.Close()
defer func(closer io.Closer) {
closer.Close()
}(resp.Body)
if resp.StatusCode != 200 {
return result, NewAPIError(resp, opts.URL)
@@ -242,7 +245,9 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) {
if err != nil {
return result, err
}
defer resp.Body.Close()
defer func(closer io.Closer) {
closer.Close()
}(resp.Body)
if resp.StatusCode != 200 {
return result, NewAPIError(resp, schemasURLs)

View File

@@ -120,6 +120,12 @@ func (a *APIOperations) DoNext(nextURL string, respObject interface{}) error {
}
func (a *APIOperations) DoModify(method string, url string, createObj interface{}, respObject interface{}) error {
if createObj == nil {
createObj = map[string]string{}
}
if respObject == nil {
respObject = &map[string]interface{}{}
}
bodyContent, err := json.Marshal(createObj)
if err != nil {
return err

View File

@@ -304,7 +304,7 @@ func filterConflictsError(err error) error {
var newErrors []error
for _, err := range errs.Errors {
if !ignoreError(err, true) {
newErrors = append(newErrors)
newErrors = append(newErrors, err)
}
}
return types.NewErrors(newErrors...)

View File

@@ -8,6 +8,7 @@ import (
{{.importPackage}}
"github.com/rancher/norman/objectclient"
"github.com/rancher/norman/controller"
"github.com/rancher/norman/resource"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@@ -34,8 +35,18 @@ var (
{{- end }}
Kind: {{.schema.CodeName}}GroupVersionKind.Kind,
}
{{.schema.CodeName}}GroupVersionResource = schema.GroupVersionResource{
Group: GroupName,
Version: Version,
Resource: "{{.schema.PluralName | toLower}}",
}
)
func init() {
resource.Put({{.schema.CodeName}}GroupVersionResource)
}
func New{{.schema.CodeName}}(namespace, name string, obj {{.prefix}}{{.schema.CodeName}}) *{{.prefix}}{{.schema.CodeName}} {
obj.APIVersion, obj.Kind = {{.schema.CodeName}}GroupVersionKind.ToAPIVersionAndKind()
obj.Name = name

View File

@@ -105,6 +105,14 @@ func IsAPIError(err error) bool {
return ok
}
func IsNotFound(err error) bool {
if apiError, ok := err.(*APIError); ok {
return apiError.Code.Status == 404
}
return false
}
func IsConflict(err error) bool {
if apiError, ok := err.(*APIError); ok {
return apiError.Code.Status == 409

View File

@@ -102,6 +102,13 @@ func (p *ObjectClient) Create(o runtime.Object) (runtime.Object, error) {
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string)
} else {
ls := make(map[string]string)
for k, v := range labels {
ls[k] = v
}
labels = ls
}
labels["cattle.io/creator"] = "norman"
obj.SetLabels(labels)

38
vendor/github.com/rancher/norman/resource/resource.go generated vendored Normal file
View File

@@ -0,0 +1,38 @@
package resource
import (
"sync"
"k8s.io/apimachinery/pkg/runtime/schema"
)
//rancherTypes is a set of all types generated by rancher
var (
rancherTypes = struct {
sync.RWMutex
m map[schema.GroupVersionResource]bool
}{m: make(map[schema.GroupVersionResource]bool)}
)
//Get returns a copy of the set of rancherTypes
func Get() map[schema.GroupVersionResource]bool {
rancherTypes.RLock()
defer rancherTypes.RUnlock()
targetMap := make(map[schema.GroupVersionResource]bool, len(rancherTypes.m))
for key, value := range rancherTypes.m {
targetMap[key] = value
}
return targetMap
}
//Put adds an object to the set and panic on collision
func Put(key schema.GroupVersionResource) {
rancherTypes.Lock()
defer rancherTypes.Unlock()
_, exists := rancherTypes.m[key]
if exists {
//only used in an init function
panic("key exists in rancherTypes")
}
rancherTypes.m[key] = true
}

View File

@@ -21,7 +21,7 @@ func UnversionedRESTClientFor(config *rest.Config) (rest.Interface, error) {
}
newConfig := *config
newConfig.Timeout = time.Hour
newConfig.Timeout = 30 * time.Minute
watchClient, err := rest.UnversionedRESTClientFor(&newConfig)
if err != nil {
return nil, err

View File

@@ -17,6 +17,7 @@ import (
"github.com/rancher/norman/types/convert/merge"
"github.com/rancher/norman/types/values"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -192,11 +193,41 @@ func (s *Store) Context() types.StorageContext {
}
func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) ([]map[string]interface{}, error) {
namespace := getNamespace(apiContext, opt)
var resultList unstructured.UnstructuredList
resultList, err := s.retryList(namespace, apiContext)
if err != nil {
return nil, err
// if there are no namespaces field in options, a single request is made
if opt == nil || opt.Namespaces == nil {
ns := getNamespace(apiContext, opt)
list, err := s.retryList(ns, apiContext)
if err != nil {
return nil, err
}
resultList = *list
} else {
var (
errGroup errgroup.Group
mux sync.Mutex
)
allNS := opt.Namespaces
for _, ns := range allNS {
nsCopy := ns
errGroup.Go(func() error {
list, err := s.retryList(nsCopy, apiContext)
if err != nil {
return err
}
mux.Lock()
resultList.Items = append(resultList.Items, list.Items...)
mux.Unlock()
return nil
})
}
if err := errGroup.Wait(); err != nil {
return nil, err
}
}
var result []map[string]interface{}
@@ -256,7 +287,7 @@ func (s *Store) realWatch(apiContext *types.APIContext, schema *types.Schema, op
k8sClient = watchClient.WatchClient()
}
timeout := int64(60 * 60)
timeout := int64(60 * 30)
req := s.common(namespace, k8sClient.Get())
req.VersionedParams(&metav1.ListOptions{
Watch: true,

View File

@@ -70,10 +70,11 @@ func (e *Embed) ModifySchema(schema *types.Schema, schemas *types.Schemas) error
}
deleteField := true
outer:
for name, field := range embeddedSchema.ResourceFields {
for _, ignore := range e.Ignore {
if ignore == name {
continue
continue outer
}
}

View File

@@ -108,10 +108,16 @@ func (s *Schemas) removeReferences(schema *Schema) {
func (s *Schemas) AddSchema(schema Schema) *Schemas {
s.Lock()
defer s.Unlock()
return s.doAddSchema(schema)
return s.doAddSchema(schema, false)
}
func (s *Schemas) doAddSchema(schema Schema) *Schemas {
func (s *Schemas) ForceAddSchema(schema Schema) *Schemas {
s.Lock()
defer s.Unlock()
return s.doAddSchema(schema, true)
}
func (s *Schemas) doAddSchema(schema Schema, replace bool) *Schemas {
s.setupDefaults(&schema)
if s.AddHook != nil {
@@ -125,9 +131,20 @@ func (s *Schemas) doAddSchema(schema Schema) *Schemas {
s.versions = append(s.versions, schema.Version)
}
if _, ok := schemas[schema.ID]; !ok {
if _, ok := schemas[schema.ID]; !ok ||
(replace && schema.DynamicSchemaVersion != schemas[schema.ID].DynamicSchemaVersion) {
schemas[schema.ID] = &schema
s.schemas = append(s.schemas, &schema)
if replace {
for i, candidate := range s.schemas {
if candidate.ID == schema.ID {
s.schemas[i] = &schema
break
}
}
} else {
s.schemas = append(s.schemas, &schema)
}
if !schema.Embed {
s.addReferences(&schema)
@@ -159,7 +176,7 @@ func (s *Schemas) removeEmbed(schema *Schema) {
}
s.doRemoveSchema(*target)
s.doAddSchema(newSchema)
s.doAddSchema(newSchema, false)
}
func (s *Schemas) embed(schema *Schema) {
@@ -184,7 +201,7 @@ func (s *Schemas) embed(schema *Schema) {
}
s.doRemoveSchema(*target)
s.doAddSchema(newSchema)
s.doAddSchema(newSchema, false)
}
func (s *Schemas) addReferences(schema *Schema) {

View File

@@ -184,6 +184,8 @@ type QueryOptions struct {
Pagination *Pagination
Conditions []*QueryCondition
Options map[string]string
// Set namespaces to an empty array will result in an empty response
Namespaces []string
}
type ReferenceValidator interface {

View File

@@ -94,25 +94,26 @@ var NamespaceScope TypeScope = "namespace"
type TypeScope string
type Schema struct {
ID string `json:"id,omitempty"`
Embed bool `json:"embed,omitempty"`
EmbedType string `json:"embedType,omitempty"`
CodeName string `json:"-"`
CodeNamePlural string `json:"-"`
PkgName string `json:"-"`
Type string `json:"type,omitempty"`
BaseType string `json:"baseType,omitempty"`
Links map[string]string `json:"links"`
Version APIVersion `json:"version"`
PluralName string `json:"pluralName,omitempty"`
ResourceMethods []string `json:"resourceMethods,omitempty"`
ResourceFields map[string]Field `json:"resourceFields"`
ResourceActions map[string]Action `json:"resourceActions,omitempty"`
CollectionMethods []string `json:"collectionMethods,omitempty"`
CollectionFields map[string]Field `json:"collectionFields,omitempty"`
CollectionActions map[string]Action `json:"collectionActions,omitempty"`
CollectionFilters map[string]Filter `json:"collectionFilters,omitempty"`
Scope TypeScope `json:"-"`
ID string `json:"id,omitempty"`
Embed bool `json:"embed,omitempty"`
EmbedType string `json:"embedType,omitempty"`
CodeName string `json:"-"`
CodeNamePlural string `json:"-"`
PkgName string `json:"-"`
Type string `json:"type,omitempty"`
BaseType string `json:"baseType,omitempty"`
Links map[string]string `json:"links"`
Version APIVersion `json:"version"`
PluralName string `json:"pluralName,omitempty"`
ResourceMethods []string `json:"resourceMethods,omitempty"`
ResourceFields map[string]Field `json:"resourceFields"`
ResourceActions map[string]Action `json:"resourceActions,omitempty"`
CollectionMethods []string `json:"collectionMethods,omitempty"`
CollectionFields map[string]Field `json:"collectionFields,omitempty"`
CollectionActions map[string]Action `json:"collectionActions,omitempty"`
CollectionFilters map[string]Filter `json:"collectionFilters,omitempty"`
DynamicSchemaVersion string `json:"dynamicSchemaVersion,omitempty"`
Scope TypeScope `json:"-"`
InternalSchema *Schema `json:"-"`
Mapper Mapper `json:"-"`