// Complete turns a partially defined PruneDeploymentsOptions into a solvent structure // which can be validated and used for pruning deployments. func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string, out io.Writer) error { if len(args) > 0 { return kcmdutil.UsageError(cmd, "no arguments are allowed to this command") } o.Out = out osClient, kClient, err := f.Clients() if err != nil { return err } o.Client = kClient deploymentConfigList, err := osClient.DeploymentConfigs(kapi.NamespaceAll).List(kapi.ListOptions{}) if err != nil { return err } deploymentConfigs := []*deployapi.DeploymentConfig{} for i := range deploymentConfigList.Items { deploymentConfigs = append(deploymentConfigs, &deploymentConfigList.Items[i]) } deploymentList, err := kClient.ReplicationControllers(kapi.NamespaceAll).List(kapi.ListOptions{}) if err != nil { return err } deployments := []*kapi.ReplicationController{} for i := range deploymentList.Items { deployments = append(deployments, &deploymentList.Items[i]) } options := prune.PrunerOptions{ KeepYoungerThan: o.KeepYoungerThan, Orphans: o.Orphans, KeepComplete: o.KeepComplete, KeepFailed: o.KeepFailed, DeploymentConfigs: deploymentConfigs, Deployments: deployments, } o.Pruner = prune.NewPruner(options) return nil }
// Run contains all the necessary functionality for the OpenShift cli prune deployments command. func (o PruneDeploymentsOptions) Run() error { deploymentConfigList, err := o.OSClient.DeploymentConfigs(o.Namespace).List(kapi.ListOptions{}) if err != nil { return err } deploymentConfigs := []*deployapi.DeploymentConfig{} for i := range deploymentConfigList.Items { deploymentConfigs = append(deploymentConfigs, &deploymentConfigList.Items[i]) } deploymentList, err := o.KClient.Core().ReplicationControllers(o.Namespace).List(kapi.ListOptions{}) if err != nil { return err } deployments := []*kapi.ReplicationController{} for i := range deploymentList.Items { deployments = append(deployments, &deploymentList.Items[i]) } options := prune.PrunerOptions{ KeepYoungerThan: o.KeepYoungerThan, Orphans: o.Orphans, KeepComplete: o.KeepComplete, KeepFailed: o.KeepFailed, DeploymentConfigs: deploymentConfigs, Deployments: deployments, } pruner := prune.NewPruner(options) w := tabwriter.NewWriter(o.Out, 10, 4, 3, ' ', 0) defer w.Flush() deploymentDeleter := &describingDeploymentDeleter{w: w} if o.Confirm { deploymentDeleter.delegate = prune.NewDeploymentDeleter(o.KClient.Core(), o.KClient.Core()) } else { fmt.Fprintln(os.Stderr, "Dry run enabled - no modifications will be made. Add --confirm to remove deployments") } return pruner.Prune(deploymentDeleter) }