diff --git a/go.sum b/go.sum index 3731208..3f9c2f8 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/recipe/delete_all.go b/pkg/recipe/delete_all.go new file mode 100644 index 0000000..d228801 --- /dev/null +++ b/pkg/recipe/delete_all.go @@ -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 +} diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index 1dd5dfb..12e4e68 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -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", diff --git a/pkg/recipe/task.go b/pkg/recipe/task.go index 602a796..5be4803 100644 --- a/pkg/recipe/task.go +++ b/pkg/recipe/task.go @@ -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() @@ -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 { diff --git a/types/recipe/delete_all.go b/types/recipe/delete_all.go new file mode 100644 index 0000000..14aa6dc --- /dev/null +++ b/types/recipe/delete_all.go @@ -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"` +} diff --git a/types/recipe/task.go b/types/recipe/task.go index 7a10dd4..21bf9c6 100644 --- a/types/recipe/task.go +++ b/types/recipe/task.go @@ -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"`