func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, timeout time.Duration, gracePeriod int, shortOutput bool, mapper meta.RESTMapper) error { found := 0 if ignoreNotFound { r = r.IgnoreErrors(errors.IsNotFound) } err := r.Visit(func(info *resource.Info) error { found++ reaper, err := f.Reaper(info.Mapping) if err != nil { // If there is no reaper for this resources and the user didn't explicitly ask for stop. if kubectl.IsNoSuchReaperError(err) && isDefaultDelete { return deleteResource(info, out, shortOutput, mapper) } return cmdutil.AddSourceToErr("reaping", info.Source, err) } var options *api.DeleteOptions if gracePeriod >= 0 { options = api.NewDeleteOptions(int64(gracePeriod)) } if _, err := reaper.Stop(info.Namespace, info.Name, timeout, options); err != nil { return cmdutil.AddSourceToErr("stopping", info.Source, err) } cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, "deleted") return nil }) if err != nil { return err } if found == 0 { fmt.Fprintf(out, "No resources found\n") } return nil }
func (f *ring1Factory) Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) { mappingVersion := mapping.GroupVersionKind.GroupVersion() clientset, clientsetErr := f.clientAccessFactory.ClientSetForVersion(&mappingVersion) reaper, reaperErr := kubectl.ReaperFor(mapping.GroupVersionKind.GroupKind(), clientset) if kubectl.IsNoSuchReaperError(reaperErr) { return nil, reaperErr } if clientsetErr != nil { return nil, clientsetErr } return reaper, reaperErr }
func (c *Context) deleteResourcesBySelect(fn selectFunc) error { factory, err := c.Factory() if err != nil { return err } namespace, err := c.Namespace() if err != nil { return err } mapper, typer := factory.Object() clientMapper := factory.ClientMapperForCommand() r := fn( resource. NewBuilder(mapper, typer, clientMapper). ContinueOnError(). NamespaceParam(namespace).DefaultNamespace(). FilenameParam(true). ResourceTypeOrNameArgs(false, "template,buildconfig,build,imagestream,deploymentconfig,replicationcontroller,pod,service,route,pvc"). RequireObject(false)). Flatten(). Do() if err = r.Err(); err != nil { return err } return r.Visit(func(info *resource.Info, err error) error { if err != nil { return err } reaper, err := factory.Reaper(info.Mapping) if err != nil { if kubectl.IsNoSuchReaperError(err) { return resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name) } return err } _, err = reaper.Stop(info.Namespace, info.Name, 5*time.Second, api.NewDeleteOptions(int64(0))) return err }) }
// Delete deletes kubernetes resources from an io.reader // // Namespace will set the namespace func (c *Client) Delete(namespace string, reader io.Reader) error { return perform(c, namespace, reader, func(info *resource.Info) error { log.Printf("Starting delete for %s %s", info.Name, info.Mapping.GroupVersionKind.Kind) reaper, err := c.Reaper(info.Mapping) if err != nil { // If there is no reaper for this resources, delete it. if kubectl.IsNoSuchReaperError(err) { err := resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name) return skipIfNotFound(err) } return err } log.Printf("Using reaper for deleting %s", info.Name) err = reaper.Stop(info.Namespace, info.Name, 0, nil) return skipIfNotFound(err) }) }