From f150d42f529fb521938636838891c190f256838d Mon Sep 17 00:00:00 2001 From: gitlawr Date: Mon, 22 Jul 2019 15:47:54 +0800 Subject: [PATCH] Pass delete options in proxy store Problem: There is no way to pass gracePeriodSeconds for force deletion Solution: Get deletion options in query params and pass it in proxy store --- store/proxy/proxy_store.go | 23 ++++++++++++++++++----- store/proxy/proxy_store_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 store/proxy/proxy_store_test.go diff --git a/store/proxy/proxy_store.go b/store/proxy/proxy_store.go index 13927dca8..3b164bccb 100644 --- a/store/proxy/proxy_store.go +++ b/store/proxy/proxy_store.go @@ -20,6 +20,7 @@ import ( "golang.org/x/sync/errgroup" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" "k8s.io/apimachinery/pkg/api/errors" + metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -459,12 +460,12 @@ func (s *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id st } namespace, name := splitID(id) - - prop := metav1.DeletePropagationBackground + options, err := getDeleteOption(apiContext.Request) + if err != nil { + return nil, err + } req := s.common(namespace, k8sClient.Delete()). - Body(&metav1.DeleteOptions{ - PropagationPolicy: &prop, - }). + Body(options). Name(name) err = s.doAuthed(apiContext, req).Error() @@ -509,6 +510,18 @@ func splitID(id string) (string, string) { return namespace, id } +func getDeleteOption(req *http.Request) (*metav1.DeleteOptions, error) { + options := &metav1.DeleteOptions{} + if err := metainternalversion.ParameterCodec.DecodeParameters(req.URL.Query(), metainternalversion.SchemeGroupVersion, options); err != nil { + return nil, err + } + prop := metav1.DeletePropagationBackground + if options.PropagationPolicy == nil { + options.PropagationPolicy = &prop + } + return options, nil +} + func (s *Store) common(namespace string, req *rest.Request) *rest.Request { prefix := append([]string{}, s.prefix...) if s.group != "" { diff --git a/store/proxy/proxy_store_test.go b/store/proxy/proxy_store_test.go new file mode 100644 index 000000000..6ef15080e --- /dev/null +++ b/store/proxy/proxy_store_test.go @@ -0,0 +1,32 @@ +package proxy + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestGetDeletionOptions(t *testing.T) { + req, err := http.NewRequest("DELETE", "https://test.url/api", nil) + assert.Empty(t, err) + prop := metav1.DeletePropagationBackground + expected := &metav1.DeleteOptions{ + PropagationPolicy: &prop, + } + options, err := getDeleteOption(req) + assert.Empty(t, err) + assert.Equal(t, options, expected, "unexpected deletion options for empty query") + + req.URL.RawQuery = "gracePeriodSeconds=0" + period := int64(0) + expected = &metav1.DeleteOptions{ + PropagationPolicy: &prop, + GracePeriodSeconds: &period, + } + options, err = getDeleteOption(req) + assert.Empty(t, err) + assert.Equal(t, options, expected, "unexpected deletion options for query 'gracePeriodSeconds=0'") + +}