Skip to content

Commit

Permalink
Fix slow operator deletions
Browse files Browse the repository at this point in the history
Summary:
After the k8s API upgrades, we were seeing that K8s operator deletions were taking a very, very long time (up to 7 minutes!)
after doing some investigation and comparing our deletes with `kubectl`'s, it looks like we should no longer use `restClientAdapter`. I updated our code to match what they are doing instead, which seems to speed things up

Test Plan: Deploy operator with skaffold, delete Pixie CRD and make sure vizier resources get deleted in a timely manner

Reviewers: vihang, zasgar

Reviewed By: zasgar

Signed-off-by: Michelle Nguyen <michellenguyen@pixielabs.ai>

Differential Revision: https://phab.corp.pixielabs.ai/D11546

GitOrigin-RevId: 0965870
  • Loading branch information
aimichelle authored and copybaranaut committed Jun 7, 2022
1 parent 39d95d0 commit db92077
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/utils/shared/k8s/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ go_library(
"@io_k8s_cli_runtime//pkg/printers",
"@io_k8s_cli_runtime//pkg/resource",
"@io_k8s_client_go//discovery",
"@io_k8s_client_go//discovery/cached",
"@io_k8s_client_go//dynamic",
"@io_k8s_client_go//kubernetes",
"@io_k8s_client_go//plugin/pkg/client/auth",
Expand Down
55 changes: 8 additions & 47 deletions src/utils/shared/k8s/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,14 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/discovery"
memory "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/clientcmd"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
cmdwait "k8s.io/kubectl/pkg/cmd/wait"
)

type restClientAdapter struct {
clientset *kubernetes.Clientset
restConfig *rest.Config
}

func (r *restClientAdapter) ToRESTConfig() (*rest.Config, error) {
return r.restConfig, nil
}

func (r *restClientAdapter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
return memory.NewMemCacheClient(r.clientset.Discovery()), nil
}

func (r *restClientAdapter) ToRESTMapper() (meta.RESTMapper, error) {
discoveryClient := r.clientset.Discovery()
apiGroupResources, err := restmapper.GetAPIGroupResources(discoveryClient)
if err != nil {
return nil, err
}
return restmapper.NewDiscoveryRESTMapper(apiGroupResources), nil
}

func (r *restClientAdapter) ToRawKubeConfigLoader() clientcmd.ClientConfig {
log.Fatal("raw kubeconfig loader is not implemented.")
return nil
}
var defaultConfigFlags = genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag().WithDiscoveryBurst(300).WithDiscoveryQPS(50.0)

// ObjectDeleter has methods to delete K8s objects and wait for them. This code is adopted from `kubectl delete`.
type ObjectDeleter struct {
Expand All @@ -81,12 +52,9 @@ type ObjectDeleter struct {

// DeleteCustomObject is used to delete a custom object (instantiation of CRD).
func (o *ObjectDeleter) DeleteCustomObject(resourceName, resourceValue string) error {
rca := &restClientAdapter{
clientset: o.Clientset,
restConfig: o.RestConfig,
}
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(defaultConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

f := cmdutil.NewFactory(rca)
r := f.NewBuilder().
Unstructured().
ContinueOnError().
Expand All @@ -111,12 +79,9 @@ func (o *ObjectDeleter) DeleteCustomObject(resourceName, resourceValue string) e

// DeleteNamespace removes the namespace and all objects within it. Waits for deletion to complete.
func (o *ObjectDeleter) DeleteNamespace() error {
rca := &restClientAdapter{
clientset: o.Clientset,
restConfig: o.RestConfig,
}
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(defaultConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

f := cmdutil.NewFactory(rca)
r := f.NewBuilder().
Unstructured().
ContinueOnError().
Expand All @@ -141,16 +106,13 @@ func (o *ObjectDeleter) DeleteNamespace() error {

// DeleteByLabel delete objects that match the labels and specified by resourceKinds. Waits for deletion.
func (o *ObjectDeleter) DeleteByLabel(selector string, resourceKinds ...string) (int, error) {
rca := &restClientAdapter{
clientset: o.Clientset,
restConfig: o.RestConfig,
}
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(defaultConfigFlags)
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)

if len(resourceKinds) == 0 {
resourceKinds = []string{"all"}
}

f := cmdutil.NewFactory(rca)
r := f.NewBuilder().
Unstructured().
ContinueOnError().
Expand Down Expand Up @@ -185,10 +147,9 @@ func (o *ObjectDeleter) runDelete(r *resource.Result) (int, error) {
deletedInfos = append(deletedInfos, info)
found++

options := &metav1.DeleteOptions{}
options := metav1.NewDeleteOptions(0)
policy := metav1.DeletePropagationBackground
options.PropagationPolicy = &policy

response, err := o.deleteResource(info, options)
if err != nil {
return err
Expand Down

0 comments on commit db92077

Please sign in to comment.