Skip to content
Merged
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
28 changes: 26 additions & 2 deletions pkg/sdk/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand Down Expand Up @@ -81,9 +82,32 @@ func KubeApply(object sdkTypes.Object) (err error) {
return nil
}

// KubeDelete deletes an object
// KubeDelete deletes an object if it still exists
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it still exists -> if exists?
seems more concise

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but I'm pretty sure the latter statement is grammatically incorrect. So probably have to keep it as it is.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on a second thought, i think you are right.

func KubeDelete(object sdkTypes.Object) (err error) {
panic("TODO")
defer func() {
if err != nil {
err = fmt.Errorf("kube-delete failed: %v", err)
}
}()

name, namespace, err := getNameAndNamespace(object)
if err != nil {
return err
}
gvk := object.GetObjectKind().GroupVersionKind()
apiVersion, kind := gvk.ToAPIVersionAndKind()
objectInfo := objectInfoString(kind, name, namespace)

resourceClient, _, err := k8sclient.GetResourceClient(apiVersion, kind, namespace)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious if GetResourceClient caches resource client based on type? if not , maybe we can cache client based on type in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the dynamic ClientPool implementation already does that.
https://github.com/kubernetes/client-go/blob/master/dynamic/client_pool.go#L93-L104

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

if err != nil {
return fmt.Errorf("failed to get resource client for object: %v", err)
}

err = resourceClient.Delete(name, &metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to delete object (%s): %v", objectInfo, err)
}
return nil
}

func getNameAndNamespace(object sdkTypes.Object) (string, string, error) {
Expand Down