github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/deletecmd/delete_addon_istio.go (about)

     1  package deletecmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/create"
     7  
     8  	"github.com/jenkins-x/jx-logging/pkg/log"
     9  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    10  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    11  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    12  	"github.com/olli-ai/jx/v2/pkg/util"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    16  )
    17  
    18  var (
    19  	deleteAddonIstioLong = templates.LongDesc(`
    20  		Deletes the Istio addon
    21  `)
    22  
    23  	deleteAddonIstioExample = templates.Examples(`
    24  		# Deletes the Istio addon
    25  		jx delete addon istio
    26  	`)
    27  )
    28  
    29  // DeleteAddonIstioOptions the options for the create spring command
    30  type DeleteAddonIstioOptions struct {
    31  	DeleteAddonOptions
    32  
    33  	ReleaseName string
    34  	Namespace   string
    35  }
    36  
    37  // NewCmdDeleteAddonIstio defines the command
    38  func NewCmdDeleteAddonIstio(commonOpts *opts.CommonOptions) *cobra.Command {
    39  	options := &DeleteAddonIstioOptions{
    40  		DeleteAddonOptions: DeleteAddonOptions{
    41  			CommonOptions: commonOpts,
    42  		},
    43  	}
    44  
    45  	cmd := &cobra.Command{
    46  		Use:     "istio",
    47  		Short:   "Deletes the Istio addon",
    48  		Long:    deleteAddonIstioLong,
    49  		Example: deleteAddonIstioExample,
    50  		Run: func(cmd *cobra.Command, args []string) {
    51  			options.Cmd = cmd
    52  			options.Args = args
    53  			err := options.Run()
    54  			helper.CheckErr(err)
    55  		},
    56  	}
    57  	cmd.Flags().StringVarP(&options.ReleaseName, opts.OptionRelease, "r", "istio", "The chart release name")
    58  	cmd.Flags().StringVarP(&options.Namespace, opts.OptionNamespace, "n", create.DefaultIstioNamespace, "The Namespace to delete from")
    59  	options.addFlags(cmd)
    60  	return cmd
    61  }
    62  
    63  // Run implements the command
    64  func (o *DeleteAddonIstioOptions) Run() error {
    65  	if o.ReleaseName == "" {
    66  		return util.MissingOption(opts.OptionRelease)
    67  	}
    68  
    69  	// TODO the official way to delete Istio is to download the release again and run
    70  	// helm template install/kubernetes/helm/istio --name istio --namespace istio-system | kubectl delete -f -
    71  	// but that also means figuring out and downloading the same version that was installed
    72  	// we will just delete most of it using label selectors
    73  
    74  	// Delete from the 'istio-system' namespace, not from 'jx'
    75  	err := o.Helm().DeleteRelease(o.Namespace, o.ReleaseName, o.Purge)
    76  	if err != nil {
    77  		return errors.Wrap(err, "Failed to delete istio chart")
    78  	}
    79  	err = o.Helm().DeleteRelease(o.Namespace, o.ReleaseName+"-init", o.Purge)
    80  	if err != nil {
    81  		return errors.Wrap(err, "Failed to delete istio-init chart")
    82  	}
    83  
    84  	// delete CRDs
    85  	apisClient, err := o.ApiExtensionsClient()
    86  	if err != nil {
    87  		return err
    88  	}
    89  	selector := fmt.Sprintf("chart=%s,release=%s", o.ReleaseName, o.ReleaseName)
    90  	log.Logger().Infof("Removing Istio CRDs using selector: %s", util.ColorInfo(selector))
    91  	err = apisClient.ApiextensionsV1beta1().CustomResourceDefinitions().DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: selector})
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	return err
    97  }