Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes #53

Merged
merged 6 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/access/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func ByID(context *types.APIContext, version *types.APIVersion, typeName string,
return err
}

if into == nil {
return nil
}

return convert.ToObj(item, into)
}

Expand Down
8 changes: 6 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"sync"

"github.com/rancher/norman/api/access"
"github.com/rancher/norman/api/builtin"
"github.com/rancher/norman/api/handler"
"github.com/rancher/norman/api/writer"
Expand Down Expand Up @@ -220,8 +221,11 @@ func (s *Server) handle(rw http.ResponseWriter, req *http.Request) (*types.APICo
return apiRequest, nil
}

func handleAction(action *types.Action, request *types.APIContext) error {
return request.Schema.ActionHandler(request.Action, action, request)
func handleAction(action *types.Action, context *types.APIContext) error {
if err := access.ByID(context, context.Version, context.Type, context.ID, nil); err != nil {
return err
}
return context.Schema.ActionHandler(context.Action, action, context)
}

func (s *Server) handleError(apiRequest *types.APIContext, err error) {
Expand Down
18 changes: 12 additions & 6 deletions condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ func (c Cond) ReasonAndMessageFromError(obj runtime.Object, err error) {
return
}
cond := findOrCreateCond(obj, string(c))
getFieldValue(cond, "Message").SetString(err.Error())
setValue(cond, "Message", err.Error())
if ce, ok := err.(*conditionError); ok {
getFieldValue(cond, "Reason").SetString(ce.reason)
setValue(cond, "Reason", ce.reason)
} else {
getFieldValue(cond, "Reason").SetString("Error")
setValue(cond, "Reason", "Error")
}
touchTS(cond)
}

func (c Cond) GetReason(obj runtime.Object) string {
Expand Down Expand Up @@ -151,8 +150,15 @@ func getStatus(obj interface{}, condName string) string {

func setStatus(obj interface{}, condName, status string) {
cond := findOrCreateCond(obj, condName)
getFieldValue(cond, "Status").SetString(status)
touchTS(cond)
setValue(cond, "Status", status)
}

func setValue(cond reflect.Value, fieldName, newValue string) {
value := getFieldValue(cond, fieldName)
if value.String() != newValue {
value.SetString(newValue)
touchTS(cond)
}
}

func findOrCreateCond(obj interface{}, condName string) reflect.Value {
Expand Down
29 changes: 13 additions & 16 deletions lifecycle/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (o *objectLifecycleAdapter) sync(key string, obj runtime.Object) error {
return err
}

obj = obj.DeepCopyObject()
newObj, err := o.lifecycle.Updated(obj)
copyObj := obj.DeepCopyObject()
newObj, err := o.lifecycle.Updated(copyObj)
o.update(metadata.GetName(), obj, newObj)
return err
}
Expand All @@ -80,19 +80,19 @@ func (o *objectLifecycleAdapter) finalize(metadata metav1.Object, obj runtime.Ob
return false, nil
}

obj = obj.DeepCopyObject()
if newObj, err := o.lifecycle.Finalize(obj); err != nil {
copyObj := obj.DeepCopyObject()
if newObj, err := o.lifecycle.Finalize(copyObj); err != nil {
o.update(metadata.GetName(), obj, newObj)
return false, err
} else if newObj != nil {
obj = newObj
copyObj = newObj
}

if err := removeFinalizer(o.constructFinalizerKey(), obj); err != nil {
if err := removeFinalizer(o.constructFinalizerKey(), copyObj); err != nil {
return false, err
}

_, err := o.objectClient.Update(metadata.GetName(), obj)
_, err := o.objectClient.Update(metadata.GetName(), copyObj)
return false, err
}

Expand Down Expand Up @@ -130,21 +130,20 @@ func (o *objectLifecycleAdapter) create(metadata metav1.Object, obj runtime.Obje
return true, nil
}

// addFinalizer will always return a DeepCopy
obj, err := o.addFinalizer(obj)
copyObj := obj.DeepCopyObject()
copyObj, err := o.addFinalizer(copyObj)
if err != nil {
return false, err
}

orig := obj.DeepCopyObject()
if newObj, err := o.lifecycle.Create(obj); err != nil {
o.update(metadata.GetName(), orig, newObj)
if newObj, err := o.lifecycle.Create(copyObj); err != nil {
o.update(metadata.GetName(), obj, newObj)
return false, err
} else if newObj != nil {
obj = newObj
copyObj = newObj
}

return false, o.setInitialized(obj)
return false, o.setInitialized(copyObj)
}

func (o *objectLifecycleAdapter) isInitialized(metadata metav1.Object) bool {
Expand All @@ -170,8 +169,6 @@ func (o *objectLifecycleAdapter) setInitialized(obj runtime.Object) error {
}

func (o *objectLifecycleAdapter) addFinalizer(obj runtime.Object) (runtime.Object, error) {
obj = obj.DeepCopyObject()

metadata, err := meta.Accessor(obj)
if err != nil {
return nil, err
Expand Down
12 changes: 7 additions & 5 deletions types/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ func (s *Schemas) Import(version *APIVersion, obj interface{}, externalOverrides

func (s *Schemas) newSchemaFromType(version *APIVersion, t reflect.Type, typeName string) (*Schema, error) {
schema := &Schema{
ID: typeName,
Version: *version,
CodeName: t.Name(),
PkgName: t.PkgPath(),
ResourceFields: map[string]Field{},
ID: typeName,
Version: *version,
CodeName: t.Name(),
PkgName: t.PkgPath(),
ResourceFields: map[string]Field{},
ResourceActions: map[string]Action{},
CollectionActions: map[string]Action{},
}

if err := s.readFields(schema, t); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions types/server_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type RawResource struct {
ActionLinks bool `json:"-"`
}

func (r *RawResource) AddAction(apiContext *APIContext, name string) {
r.Actions[name] = apiContext.URLBuilder.Action(name, r)
}

func (r *RawResource) MarshalJSON() ([]byte, error) {
data := map[string]interface{}{}
for k, v := range r.Values {
Expand Down