diff --git a/pkg/stores/proxy/proxy_store.go b/pkg/stores/proxy/proxy_store.go index b2bb31b7..da275e75 100644 --- a/pkg/stores/proxy/proxy_store.go +++ b/pkg/stores/proxy/proxy_store.go @@ -446,15 +446,7 @@ func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, params } gvk := attributes.GVK(schema) - apiVersion, kind := gvk.ToAPIVersionAndKind() - - if value, found := input["apiVersion"]; !found || value == "" { - input["apiVersion"] = apiVersion - } - - if value, found := input["kind"]; !found || value == "" { - input["kind"] = kind - } + input["apiVersion"], input["kind"] = gvk.ToAPIVersionAndKind() buffer := WarningBuffer{} k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, namespace, &buffer)) @@ -528,15 +520,7 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, params } gvk := attributes.GVK(schema) - apiVersion, kind := gvk.ToAPIVersionAndKind() - - if value, found := input["apiVersion"]; !found || value == "" { - input["apiVersion"] = apiVersion - } - - if value, found := input["kind"]; !found || value == "" { - input["kind"] = kind - } + input["apiVersion"], input["kind"] = gvk.ToAPIVersionAndKind() opts := metav1.UpdateOptions{} if err := decodeParams(apiOp, &opts); err != nil { diff --git a/pkg/stores/proxy/proxy_store_test.go b/pkg/stores/proxy/proxy_store_test.go index 5fe97dfb..47fe2b37 100644 --- a/pkg/stores/proxy/proxy_store_test.go +++ b/pkg/stores/proxy/proxy_store_test.go @@ -521,6 +521,7 @@ func TestUpdate(t *testing.T) { Schema: &schemas.Schema{ ID: "testing", Attributes: map[string]interface{}{ + "kind": "Secret", "version": "v1", "namespaced": true, }, @@ -570,13 +571,15 @@ func TestUpdate(t *testing.T) { Schema: &schemas.Schema{ ID: "testing", Attributes: map[string]interface{}{ + "version": "v2", + "kind": "Secret", "namespaced": true, }, }, }, params: types.APIObject{ Object: map[string]interface{}{ - "apiVersion": "v2", + "apiVersion": "v1", "kind": "Secret", "metadata": map[string]interface{}{ "name": "testing-secret", @@ -646,114 +649,6 @@ func TestUpdate(t *testing.T) { err: fmt.Errorf(errResourceVersionRequired), }, }, - { - name: "update - missing apiVersion (should copy from schema)", - updateCallbackFunc: func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) { - return false, ret, nil - }, - createInput: &sampleCreateInput, - updateInput: input{ - apiOp: &types.APIRequest{ - Request: &http.Request{ - URL: &url.URL{}, - Method: http.MethodPut, - }, - Schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - }, - }, - Method: http.MethodPut, - }, - - schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - Attributes: map[string]interface{}{ - "version": "v2", - "namespaced": true, - }, - }, - }, - params: types.APIObject{ - Object: map[string]interface{}{ - "kind": "Secret", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }, - }, - }, - expected: expected{ - value: &unstructured.Unstructured{Object: map[string]interface{}{ - "apiVersion": "v2", - "kind": "Secret", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }}, - warning: []types.Warning{}, - err: nil, - }, - }, - { - name: "update - missing kind (should copy from schema)", - updateCallbackFunc: func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) { - return false, ret, nil - }, - createInput: &sampleCreateInput, - updateInput: input{ - apiOp: &types.APIRequest{ - Request: &http.Request{ - URL: &url.URL{}, - Method: http.MethodPut, - }, - Schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - }, - }, - Method: http.MethodPut, - }, - - schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - Attributes: map[string]interface{}{ - "kind": "Secret", - "namespaced": true, - }, - }, - }, - params: types.APIObject{ - Object: map[string]interface{}{ - "apiVersion": "v2", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }, - }, - }, - expected: expected{ - value: &unstructured.Unstructured{Object: map[string]interface{}{ - "apiVersion": "v2", - "kind": "Secret", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }}, - warning: []types.Warning{}, - err: nil, - }, - }, { name: "update - error request", updateCallbackFunc: func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) { diff --git a/pkg/stores/sqlproxy/proxy_store.go b/pkg/stores/sqlproxy/proxy_store.go index 62132e45..64f3fd2c 100644 --- a/pkg/stores/sqlproxy/proxy_store.go +++ b/pkg/stores/sqlproxy/proxy_store.go @@ -529,15 +529,7 @@ func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, params } gvk := attributes.GVK(schema) - apiVersion, kind := gvk.ToAPIVersionAndKind() - - if value, found := input["apiVersion"]; !found || value == "" { - input["apiVersion"] = apiVersion - } - - if value, found := input["kind"]; !found || value == "" { - input["kind"] = kind - } + input["apiVersion"], input["kind"] = gvk.ToAPIVersionAndKind() buffer := WarningBuffer{} k8sClient, err := metricsStore.Wrap(s.clientGetter.TableClient(apiOp, schema, namespace, &buffer)) @@ -611,15 +603,7 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, params } gvk := attributes.GVK(schema) - apiVersion, kind := gvk.ToAPIVersionAndKind() - - if value, found := input["apiVersion"]; !found || value == "" { - input["apiVersion"] = apiVersion - } - - if value, found := input["kind"]; !found || value == "" { - input["kind"] = kind - } + input["apiVersion"], input["kind"] = gvk.ToAPIVersionAndKind() opts := metav1.UpdateOptions{} if err := decodeParams(apiOp, &opts); err != nil { diff --git a/pkg/stores/sqlproxy/proxy_store_test.go b/pkg/stores/sqlproxy/proxy_store_test.go index 336d2f8e..8e61deca 100644 --- a/pkg/stores/sqlproxy/proxy_store_test.go +++ b/pkg/stores/sqlproxy/proxy_store_test.go @@ -1136,6 +1136,7 @@ func TestUpdate(t *testing.T) { Schema: &schemas.Schema{ ID: "testing", Attributes: map[string]interface{}{ + "kind": "Secret", "version": "v1", "namespaced": true, }, @@ -1185,13 +1186,15 @@ func TestUpdate(t *testing.T) { Schema: &schemas.Schema{ ID: "testing", Attributes: map[string]interface{}{ + "version": "v2", + "kind": "Secret", "namespaced": true, }, }, }, params: types.APIObject{ Object: map[string]interface{}{ - "apiVersion": "v2", + "apiVersion": "v1", "kind": "Secret", "metadata": map[string]interface{}{ "name": "testing-secret", @@ -1261,114 +1264,6 @@ func TestUpdate(t *testing.T) { err: fmt.Errorf(errResourceVersionRequired), }, }, - { - name: "update - missing apiVersion (should copy from schema)", - updateCallbackFunc: func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) { - return false, ret, nil - }, - createInput: &sampleCreateInput, - updateInput: input{ - apiOp: &types.APIRequest{ - Request: &http.Request{ - URL: &url.URL{}, - Method: http.MethodPut, - }, - Schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - }, - }, - Method: http.MethodPut, - }, - - schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - Attributes: map[string]interface{}{ - "version": "v2", - "namespaced": true, - }, - }, - }, - params: types.APIObject{ - Object: map[string]interface{}{ - "kind": "Secret", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }, - }, - }, - expected: expected{ - value: &unstructured.Unstructured{Object: map[string]interface{}{ - "apiVersion": "v2", - "kind": "Secret", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }}, - warning: []types.Warning{}, - err: nil, - }, - }, - { - name: "update - missing kind (should copy from schema)", - updateCallbackFunc: func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) { - return false, ret, nil - }, - createInput: &sampleCreateInput, - updateInput: input{ - apiOp: &types.APIRequest{ - Request: &http.Request{ - URL: &url.URL{}, - Method: http.MethodPut, - }, - Schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - }, - }, - Method: http.MethodPut, - }, - - schema: &types.APISchema{ - Schema: &schemas.Schema{ - ID: "testing", - Attributes: map[string]interface{}{ - "kind": "Secret", - "namespaced": true, - }, - }, - }, - params: types.APIObject{ - Object: map[string]interface{}{ - "apiVersion": "v2", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }, - }, - }, - expected: expected{ - value: &unstructured.Unstructured{Object: map[string]interface{}{ - "apiVersion": "v2", - "kind": "Secret", - "metadata": map[string]interface{}{ - "name": "testing-secret", - "namespace": "testing-ns", - "resourceVersion": "1", - }, - }}, - warning: []types.Warning{}, - err: nil, - }, - }, { name: "update - error request", updateCallbackFunc: func(action clientgotesting.Action) (handled bool, ret runtime.Object, err error) {