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 changes #37

Merged
merged 4 commits into from
Dec 30, 2017
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: 0 additions & 4 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ func (s *Server) handle(rw http.ResponseWriter, req *http.Request) (*types.APICo
return apiRequest, err
}

if err := addCommonResponseHeader(apiRequest); err != nil {
return apiRequest, err
}

action, err := ValidateAction(apiRequest)
if err != nil {
return apiRequest, err
Expand Down
4 changes: 2 additions & 2 deletions api/headers.go → api/writer/headers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package api
package writer

import (
"github.com/rancher/norman/api/builtin"
"github.com/rancher/norman/types"
)

func addCommonResponseHeader(apiContext *types.APIContext) error {
func AddCommonResponseHeader(apiContext *types.APIContext) error {
addExpires(apiContext)
return addSchemasHeader(apiContext)
}
Expand Down
1 change: 1 addition & 0 deletions api/writer/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type HTMLResponseWriter struct {
}

func (h *HTMLResponseWriter) start(apiContext *types.APIContext, code int, obj interface{}) {
AddCommonResponseHeader(apiContext)
apiContext.Response.Header().Set("content-type", "text/html")
apiContext.Response.WriteHeader(code)
}
Expand Down
4 changes: 2 additions & 2 deletions api/writer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package writer
import (
"encoding/json"
"fmt"
"net/http"

"io"
"net/http"

"github.com/rancher/norman/parse"
"github.com/rancher/norman/parse/builder"
Expand All @@ -18,6 +17,7 @@ type JSONResponseWriter struct {
}

func (j *JSONResponseWriter) start(apiContext *types.APIContext, code int, obj interface{}) {
AddCommonResponseHeader(apiContext)
apiContext.Response.Header().Set("content-type", "application/json")
apiContext.Response.WriteHeader(code)
}
Expand Down
2 changes: 1 addition & 1 deletion store/crd/crd_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *Store) AddSchemas(ctx context.Context, schemas ...*types.Schema) error
}

for schema, crd := range schemaStatus {
c.schemaStores[key(schema)] = proxy.NewProxyStore(c.k8sClient,
c.schemaStores[key(schema)] = proxy.NewProxyStoreForCRD(c.k8sClient,
[]string{"apis"},
crd.Spec.Group,
crd.Spec.Version,
Expand Down
56 changes: 51 additions & 5 deletions store/proxy/proxy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,33 @@ type Store struct {
kind string
resourcePlural string
authContext map[string]string
supportPatch bool
}

func NewProxyStore(k8sClient rest.Interface,
prefix []string, group, version, kind, resourcePlural string) types.Store {
return &errorStore{
Store: &Store{
supportPatch: true,
k8sClient: k8sClient,
prefix: prefix,
group: group,
version: version,
kind: kind,
resourcePlural: resourcePlural,
authContext: map[string]string{
"apiGroup": group,
"resource": resourcePlural,
},
},
}
}

func NewProxyStoreForCRD(k8sClient rest.Interface,
prefix []string, group, version, kind, resourcePlural string) types.Store {
return &errorStore{
Store: &Store{
supportPatch: false,
k8sClient: k8sClient,
prefix: prefix,
group: group,
Expand Down Expand Up @@ -217,15 +238,40 @@ func (p *Store) toInternal(mapper types.Mapper, data map[string]interface{}) {
}

func (p *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
p.toInternal(schema.Mapper, data)
if p.supportPatch {
p.toInternal(schema.Mapper, data)
namespace, id := splitID(id)

req := p.common(namespace, p.k8sClient.Patch(patchtype.StrategicMergePatchType)).
Body(&unstructured.Unstructured{
Object: data,
}).
Name(id).
SetHeader("Content-Type", string(patchtype.StrategicMergePatchType))

_, result, err := p.singleResult(apiContext, schema, req)
return result, err
}

resourceVersion, existing, err := p.byID(apiContext, schema, id)
if err != nil {
return data, nil
}

for k, v := range data {
existing[k] = v
}

p.toInternal(schema.Mapper, existing)
namespace, id := splitID(id)

req := p.common(namespace, p.k8sClient.Patch(patchtype.StrategicMergePatchType)).
values.PutValue(existing, resourceVersion, "metadata", "resourceVersion")

req := p.common(namespace, p.k8sClient.Put()).
Body(&unstructured.Unstructured{
Object: data,
Object: existing,
}).
Name(id).
SetHeader("Content-Type", string(patchtype.StrategicMergePatchType))
Name(id)

_, result, err := p.singleResult(apiContext, schema, req)
return result, err
Expand Down
7 changes: 6 additions & 1 deletion types/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func (t *typeMapper) FromInternal(data map[string]interface{}) {
name, _ := data["name"].(string)
namespace, _ := data["namespaceId"].(string)

if _, ok := data["id"]; !ok {
if _, ok := data["id"]; ok {
if namespace != "" {
id, _ := data["id"].(string)
data["id"] = namespace + ":" + id
}
} else {
if name != "" {
if namespace == "" {
data["id"] = name
Expand Down
2 changes: 0 additions & 2 deletions types/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ func (s *Schemas) AddMapperForType(version *APIVersion, obj interface{}, mapper
}

func (s *Schemas) MustImport(version *APIVersion, obj interface{}, externalOverrides ...interface{}) *Schemas {
//TODO: remove
logrus.SetLevel(logrus.DebugLevel)
if _, err := s.Import(version, obj, externalOverrides...); err != nil {
panic(err)
}
Expand Down