Skip to content

Commit

Permalink
Pass delete options in proxy store
Browse files Browse the repository at this point in the history
Problem:
There is no way to pass gracePeriodSeconds for force deletion

Solution:
Get deletion options in query params and pass it in proxy store
  • Loading branch information
gitlawr committed Jul 23, 2019
1 parent 043a1c9 commit f150d42
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
23 changes: 18 additions & 5 deletions store/proxy/proxy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 != "" {
Expand Down
32 changes: 32 additions & 0 deletions store/proxy/proxy_store_test.go
Original file line number Diff line number Diff line change
@@ -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'")

}

0 comments on commit f150d42

Please sign in to comment.