github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/alpha/rpkg/reject/command.go (about) 1 // Copyright 2022 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package reject 16 17 import ( 18 "context" 19 "fmt" 20 "strings" 21 22 "github.com/GoogleContainerTools/kpt/internal/docs/generated/rpkgdocs" 23 "github.com/GoogleContainerTools/kpt/internal/errors" 24 "github.com/GoogleContainerTools/kpt/internal/util/porch" 25 "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 26 "github.com/spf13/cobra" 27 "k8s.io/cli-runtime/pkg/genericclioptions" 28 "k8s.io/client-go/rest" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 ) 31 32 const ( 33 command = "cmdrpkgreject" 34 ) 35 36 func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command { 37 return newRunner(ctx, rcg).Command 38 } 39 40 func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner { 41 r := &runner{ 42 ctx: ctx, 43 cfg: rcg, 44 client: nil, 45 } 46 47 c := &cobra.Command{ 48 Use: "reject PACKAGE", 49 Short: rpkgdocs.RejectShort, 50 Long: rpkgdocs.RejectShort + "\n" + rpkgdocs.RejectLong, 51 Example: rpkgdocs.RejectExamples, 52 PreRunE: r.preRunE, 53 RunE: r.runE, 54 Hidden: porch.HidePorchCommands, 55 } 56 r.Command = c 57 58 return r 59 } 60 61 type runner struct { 62 ctx context.Context 63 cfg *genericclioptions.ConfigFlags 64 client rest.Interface 65 porchClient client.Client 66 Command *cobra.Command 67 68 // Flags 69 } 70 71 func (r *runner) preRunE(_ *cobra.Command, args []string) error { 72 const op errors.Op = command + ".preRunE" 73 74 if len(args) < 1 { 75 return errors.E(op, "PACKAGE_REVISION is a required positional argument") 76 } 77 78 client, err := porch.CreateRESTClient(r.cfg) 79 if err != nil { 80 return errors.E(op, err) 81 } 82 r.client = client 83 84 porchClient, err := porch.CreateClientWithFlags(r.cfg) 85 if err != nil { 86 return errors.E(op, err) 87 } 88 r.porchClient = porchClient 89 return nil 90 } 91 92 func (r *runner) runE(_ *cobra.Command, args []string) error { 93 const op errors.Op = command + ".runE" 94 var messages []string 95 96 namespace := *r.cfg.Namespace 97 98 for _, name := range args { 99 pr := &v1alpha1.PackageRevision{} 100 if err := r.porchClient.Get(r.ctx, client.ObjectKey{ 101 Namespace: namespace, 102 Name: name, 103 }, pr); err != nil { 104 return errors.E(op, err) 105 } 106 switch pr.Spec.Lifecycle { 107 case v1alpha1.PackageRevisionLifecycleProposed: 108 if err := porch.UpdatePackageRevisionApproval(r.ctx, r.client, client.ObjectKey{ 109 Namespace: namespace, 110 Name: name, 111 }, v1alpha1.PackageRevisionLifecycleDraft); err != nil { 112 messages = append(messages, err.Error()) 113 fmt.Fprintf(r.Command.ErrOrStderr(), "%s failed (%s)\n", name, err) 114 } else { 115 fmt.Fprintf(r.Command.OutOrStderr(), "%s rejected\n", name) 116 } 117 case v1alpha1.PackageRevisionLifecycleDeletionProposed: 118 pr.Spec.Lifecycle = v1alpha1.PackageRevisionLifecyclePublished 119 if err := r.porchClient.Update(r.ctx, pr); err != nil { 120 messages = append(messages, err.Error()) 121 fmt.Fprintf(r.Command.ErrOrStderr(), "%s failed (%s)\n", name, err) 122 } else { 123 fmt.Fprintf(r.Command.OutOrStderr(), "%s no longer proposed for deletion\n", name) 124 } 125 default: 126 msg := fmt.Sprintf("cannot reject %s with lifecycle '%s'", name, pr.Spec.Lifecycle) 127 messages = append(messages, msg) 128 fmt.Fprintln(r.Command.ErrOrStderr(), msg) 129 } 130 } 131 132 if len(messages) > 0 { 133 return errors.E(op, fmt.Errorf("errors:\n %s", strings.Join(messages, "\n "))) 134 } 135 136 return nil 137 }