Pass DeleteOptions down to the Reactor

Co-authored-by: Mo Khan <theenjeru@gmail.com>
This commit is contained in:
Chun Chen 2021-06-17 18:06:47 +08:00
parent afff019fbc
commit 621970476f
5 changed files with 49 additions and 12 deletions

View File

@ -23,6 +23,7 @@ import (
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
core "k8s.io/client-go/testing"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
"k8s.io/kubernetes/pkg/apis/core/helper"
@ -34,6 +35,7 @@ func newTokenSecret(tokenID, tokenSecret string) *v1.Secret {
Namespace: metav1.NamespaceSystem,
Name: bootstrapapi.BootstrapTokenSecretPrefix + tokenID,
ResourceVersion: "1",
UID: types.UID("uid" + tokenID),
},
Type: bootstrapapi.SecretTypeBootstrapToken,
Data: map[string][]byte{

View File

@ -22,6 +22,7 @@ import (
"github.com/davecgh/go-spew/spew"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
@ -76,10 +77,13 @@ func TestCleanerExpired(t *testing.T) {
cleaner.evalSecret(secret)
expected := []core.Action{
core.NewDeleteAction(
core.NewDeleteActionWithOptions(
schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
api.NamespaceSystem,
secret.ObjectMeta.Name),
secret.ObjectMeta.Name,
metav1.DeleteOptions{
Preconditions: metav1.NewUIDPreconditions(string(secret.UID)),
}),
}
verifyActions(t, expected, cl.Actions())
@ -138,10 +142,13 @@ func TestCleanerExpiredAt(t *testing.T) {
// secret was eventually deleted
expected = []core.Action{
core.NewDeleteAction(
core.NewDeleteActionWithOptions(
schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
api.NamespaceSystem,
secret.ObjectMeta.Name),
secret.ObjectMeta.Name,
metav1.DeleteOptions{
Preconditions: metav1.NewUIDPreconditions(string(secret.UID)),
}),
}
verifyFunc()
}

View File

@ -409,7 +409,10 @@ func TestTokenCreation(t *testing.T) {
DeletedServiceAccount: serviceAccount(tokenSecretReferences()),
ExpectedActions: []core.Action{
core.NewDeleteAction(schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, metav1.NamespaceDefault, "token-secret-1"),
core.NewDeleteActionWithOptions(
schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
metav1.NamespaceDefault, "token-secret-1",
*metav1.NewPreconditionDeleteOptions("23456")),
},
},
@ -419,7 +422,10 @@ func TestTokenCreation(t *testing.T) {
AddedSecret: serviceAccountTokenSecret(),
ExpectedActions: []core.Action{
core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "serviceaccounts"}, metav1.NamespaceDefault, "default"),
core.NewDeleteAction(schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, metav1.NamespaceDefault, "token-secret-1"),
core.NewDeleteActionWithOptions(
schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
metav1.NamespaceDefault, "token-secret-1",
*metav1.NewPreconditionDeleteOptions("23456")),
},
},
"added secret with serviceaccount": {
@ -484,7 +490,10 @@ func TestTokenCreation(t *testing.T) {
UpdatedSecret: serviceAccountTokenSecret(),
ExpectedActions: []core.Action{
core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "serviceaccounts"}, metav1.NamespaceDefault, "default"),
core.NewDeleteAction(schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, metav1.NamespaceDefault, "token-secret-1"),
core.NewDeleteActionWithOptions(
schema.GroupVersionResource{Version: "v1", Resource: "secrets"},
metav1.NamespaceDefault, "token-secret-1",
*metav1.NewPreconditionDeleteOptions("23456")),
},
},
"updated secret with serviceaccount": {

View File

@ -222,10 +222,15 @@ func NewUpdateSubresourceAction(resource schema.GroupVersionResource, subresourc
}
func NewRootDeleteAction(resource schema.GroupVersionResource, name string) DeleteActionImpl {
return NewRootDeleteActionWithOptions(resource, name, metav1.DeleteOptions{})
}
func NewRootDeleteActionWithOptions(resource schema.GroupVersionResource, name string, opts metav1.DeleteOptions) DeleteActionImpl {
action := DeleteActionImpl{}
action.Verb = "delete"
action.Resource = resource
action.Name = name
action.DeleteOptions = opts
return action
}
@ -241,11 +246,16 @@ func NewRootDeleteSubresourceAction(resource schema.GroupVersionResource, subres
}
func NewDeleteAction(resource schema.GroupVersionResource, namespace, name string) DeleteActionImpl {
return NewDeleteActionWithOptions(resource, namespace, name, metav1.DeleteOptions{})
}
func NewDeleteActionWithOptions(resource schema.GroupVersionResource, namespace, name string, opts metav1.DeleteOptions) DeleteActionImpl {
action := DeleteActionImpl{}
action.Verb = "delete"
action.Resource = resource
action.Namespace = namespace
action.Name = name
action.DeleteOptions = opts
return action
}
@ -391,6 +401,7 @@ type UpdateAction interface {
type DeleteAction interface {
Action
GetName() string
GetDeleteOptions() metav1.DeleteOptions
}
type DeleteCollectionAction interface {
@ -583,17 +594,23 @@ func (a PatchActionImpl) DeepCopy() Action {
type DeleteActionImpl struct {
ActionImpl
Name string
Name string
DeleteOptions metav1.DeleteOptions
}
func (a DeleteActionImpl) GetName() string {
return a.Name
}
func (a DeleteActionImpl) GetDeleteOptions() metav1.DeleteOptions {
return a.DeleteOptions
}
func (a DeleteActionImpl) DeepCopy() Action {
return DeleteActionImpl{
ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
Name: a.Name,
ActionImpl: a.ActionImpl.DeepCopy().(ActionImpl),
Name: a.Name,
DeleteOptions: *a.DeleteOptions.DeepCopy(),
}
}

View File

@ -144,7 +144,9 @@ func (g *genFakeForType) GenerateType(c *generator.Context, t *types.Type, w io.
"NewRootGetAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootGetAction"}),
"NewGetAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewGetAction"}),
"NewRootDeleteAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootDeleteAction"}),
"NewRootDeleteActionWithOptions": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootDeleteActionWithOptions"}),
"NewDeleteAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewDeleteAction"}),
"NewDeleteActionWithOptions": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewDeleteActionWithOptions"}),
"NewRootDeleteCollectionAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootDeleteCollectionAction"}),
"NewDeleteCollectionAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewDeleteCollectionAction"}),
"NewRootUpdateAction": c.Universe.Function(types.Name{Package: pkgClientGoTesting, Name: "NewRootUpdateAction"}),
@ -412,8 +414,8 @@ var deleteTemplate = `
// Delete takes name of the $.type|private$ and deletes it. Returns an error if one occurs.
func (c *Fake$.type|publicPlural$) Delete(ctx context.Context, name string, opts $.DeleteOptions|raw$) error {
_, err := c.Fake.
$if .namespaced$Invokes($.NewDeleteAction|raw$($.type|allLowercasePlural$Resource, c.ns, name), &$.type|raw${})
$else$Invokes($.NewRootDeleteAction|raw$($.type|allLowercasePlural$Resource, name), &$.type|raw${})$end$
$if .namespaced$Invokes($.NewDeleteActionWithOptions|raw$($.type|allLowercasePlural$Resource, c.ns, name, opts), &$.type|raw${})
$else$Invokes($.NewRootDeleteActionWithOptions|raw$($.type|allLowercasePlural$Resource, name, opts), &$.type|raw${})$end$
return err
}
`