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

feat(recipe): delete all resources belonging to a gvk from a particular namespace #101

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
Expand Down
49 changes: 49 additions & 0 deletions pkg/recipe/delete_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package recipe

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
types "mayadata.io/d-operators/types/recipe"
"openebs.io/metac/dynamic/clientset"
)

func (r *TaskRunner) deleteAll() (*types.TaskStatus, error) {
var message = fmt.Sprintf(
"DeleteAll: Resource %s %s: GVK %s",
r.Task.DeleteAll.State.GetNamespace(),
r.Task.DeleteAll.State.GetAPIVersion(),
r.Task.DeleteAll.State.GetKind(),
)
var client *clientset.ResourceClient
var err error
err = r.Retry.Waitf(
func() (bool, error) {
client, err = r.GetClientForAPIVersionAndKind(
r.Task.DeleteAll.State.GetAPIVersion(),
r.Task.DeleteAll.State.GetKind(),
)
if err != nil {
return r.IsFailFastOnDiscoveryError(), err
}
return true, nil
},
message,
)
if err != nil {
return nil, err
}
err = client.
Namespace(r.Task.DeleteAll.State.GetNamespace()).
DeleteCollection(
&metav1.DeleteOptions{},
metav1.ListOptions{},
)
if err != nil {
return nil, err
}
return &types.TaskStatus{
Phase: types.TaskStatusPassed,
Message: message,
}, nil
}
4 changes: 4 additions & 0 deletions pkg/recipe/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func (r *Runner) eval(task types.Task) error {
action++
state = task.Create.State
}
if task.DeleteAll != nil {
action++
state = task.DeleteAll.State
}
if action == 0 {
return errors.Errorf(
"Invalid task %q: Task needs one action",
Expand Down
10 changes: 10 additions & 0 deletions pkg/recipe/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ func (r *TaskRunner) tryRunDelete() (*types.TaskStatus, bool, error) {

}

func (r *TaskRunner) tryRunDeleteAll() (*types.TaskStatus, bool, error) {
if r.Task.DeleteAll == nil || r.Task.DeleteAll.State == nil {
return nil, false, nil
}
// delete from Delete action
got, err := r.deleteAll()
return got, true, err
}

func (r *TaskRunner) tryRunApply() (*types.TaskStatus, bool, error) {
// check if this is delete from Apply action
isDel, err := r.isDeleteFromApply()
Expand Down Expand Up @@ -255,6 +264,7 @@ func (r *TaskRunner) Run() (types.TaskStatus, error) {
r.tryRunCreate,
r.tryRunAssert,
r.tryRunDelete,
r.tryRunDeleteAll,
r.tryRunApply,
}
for _, fn := range probables {
Expand Down
27 changes: 27 additions & 0 deletions types/recipe/delete_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2020 The MayaData Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// DeleteAll deletes all the resouces present in the given namespace
type DeleteAll struct {
// Desired state that needs to be deleted
State *unstructured.Unstructured `json:"state"`
}
1 change: 1 addition & 0 deletions types/recipe/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Task struct {
Assert *Assert `json:"assert,omitempty"`
Apply *Apply `json:"apply,omitempty"`
Delete *Delete `json:"delete,omitempty"`
DeleteAll *DeleteAll `json:"deleteAll,omitempty"`
Create *Create `json:"create,omitempty"`
IgnoreErrorRule IgnoreErrorRule `json:"ignoreError,omitempty"`
FailFast *FailFast `json:"failFast,omitempty"`
Expand Down