github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/cmd/helm/delete.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"k8s.io/helm/pkg/helm"
    27  )
    28  
    29  const deleteDesc = `
    30  This command takes a release name, and then deletes the release from Kubernetes.
    31  It removes all of the resources associated with the last release of the chart.
    32  
    33  Use the '--dry-run' flag to see which releases will be deleted without actually
    34  deleting them.
    35  `
    36  
    37  type deleteCmd struct {
    38  	name         string
    39  	dryRun       bool
    40  	disableHooks bool
    41  	purge        bool
    42  	timeout      int64
    43  	description  string
    44  
    45  	out    io.Writer
    46  	client helm.Interface
    47  }
    48  
    49  func newDeleteCmd(c helm.Interface, out io.Writer) *cobra.Command {
    50  	del := &deleteCmd{
    51  		out:    out,
    52  		client: c,
    53  	}
    54  
    55  	cmd := &cobra.Command{
    56  		Use:        "delete [flags] RELEASE_NAME [...]",
    57  		Aliases:    []string{"del"},
    58  		SuggestFor: []string{"remove", "rm"},
    59  		Short:      "Given a release name, delete the release from Kubernetes",
    60  		Long:       deleteDesc,
    61  		PreRunE:    func(_ *cobra.Command, _ []string) error { return setupConnection() },
    62  		RunE: func(cmd *cobra.Command, args []string) error {
    63  			if len(args) == 0 {
    64  				return errors.New("command 'delete' requires a release name")
    65  			}
    66  			del.client = ensureHelmClient(del.client)
    67  
    68  			for i := 0; i < len(args); i++ {
    69  				del.name = args[i]
    70  				if err := del.run(); err != nil {
    71  					return err
    72  				}
    73  
    74  				fmt.Fprintf(out, "release \"%s\" deleted\n", del.name)
    75  			}
    76  			return nil
    77  		},
    78  	}
    79  
    80  	f := cmd.Flags()
    81  	settings.AddFlagsTLS(f)
    82  	f.BoolVar(&del.dryRun, "dry-run", false, "Simulate a delete")
    83  	f.BoolVar(&del.disableHooks, "no-hooks", false, "Prevent hooks from running during deletion")
    84  	f.BoolVar(&del.purge, "purge", false, "Remove the release from the store and make its name free for later use")
    85  	f.Int64Var(&del.timeout, "timeout", 300, "Time in seconds to wait for any individual Kubernetes operation (like Jobs for hooks)")
    86  	f.StringVar(&del.description, "description", "", "Specify a description for the release")
    87  
    88  	// set defaults from environment
    89  	settings.InitTLS(f)
    90  
    91  	return cmd
    92  }
    93  
    94  func (d *deleteCmd) run() error {
    95  	opts := []helm.DeleteOption{
    96  		helm.DeleteDryRun(d.dryRun),
    97  		helm.DeleteDisableHooks(d.disableHooks),
    98  		helm.DeletePurge(d.purge),
    99  		helm.DeleteTimeout(d.timeout),
   100  		helm.DeleteDescription(d.description),
   101  	}
   102  	res, err := d.client.DeleteRelease(d.name, opts...)
   103  	if res != nil && res.Info != "" {
   104  		fmt.Fprintln(d.out, res.Info)
   105  	}
   106  
   107  	return prettyError(err)
   108  }