github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/alpha/rpkg/copy/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 copy 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/GoogleContainerTools/kpt/internal/docs/generated/rpkgdocs" 22 "github.com/GoogleContainerTools/kpt/internal/errors" 23 "github.com/GoogleContainerTools/kpt/internal/util/porch" 24 porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 25 "github.com/spf13/cobra" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/types" 28 "k8s.io/cli-runtime/pkg/genericclioptions" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 ) 31 32 const ( 33 command = "cmdrpkgcopy" 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 } 45 r.Command = &cobra.Command{ 46 Use: "copy SOURCE_PACKAGE NAME", 47 Aliases: []string{"edit"}, 48 Short: rpkgdocs.CopyShort, 49 Long: rpkgdocs.CopyShort + "\n" + rpkgdocs.CopyLong, 50 Example: rpkgdocs.CopyExamples, 51 PreRunE: r.preRunE, 52 RunE: r.runE, 53 Hidden: porch.HidePorchCommands, 54 } 55 r.Command.Flags().StringVar(&r.workspace, "workspace", "", "Workspace name of the copy of the package.") 56 r.Command.Flags().BoolVar(&r.replayStrategy, "replay-strategy", false, "Use replay strategy for creating new package revision.") 57 return r 58 } 59 60 type runner struct { 61 ctx context.Context 62 cfg *genericclioptions.ConfigFlags 63 client client.Client 64 Command *cobra.Command 65 66 copy porchapi.PackageEditTaskSpec 67 68 workspace string // Target package revision workspaceName 69 replayStrategy bool 70 } 71 72 func (r *runner) preRunE(_ *cobra.Command, args []string) error { 73 const op errors.Op = command + ".preRunE" 74 client, err := porch.CreateClientWithFlags(r.cfg) 75 if err != nil { 76 return errors.E(op, err) 77 } 78 r.client = client 79 80 if len(args) < 1 { 81 return errors.E(op, fmt.Errorf("SOURCE_PACKAGE is a required positional argument")) 82 } 83 if len(args) > 1 { 84 return errors.E(op, fmt.Errorf("too many arguments; SOURCE_PACKAGE is the only accepted positional arguments")) 85 } 86 87 r.copy.Source = &porchapi.PackageRevisionRef{ 88 Name: args[0], 89 } 90 return nil 91 } 92 93 func (r *runner) runE(cmd *cobra.Command, _ []string) error { 94 const op errors.Op = command + ".runE" 95 96 revisionSpec, err := r.getPackageRevisionSpec() 97 if err != nil { 98 return errors.E(op, err) 99 } 100 101 pr := &porchapi.PackageRevision{ 102 TypeMeta: metav1.TypeMeta{ 103 Kind: "PackageRevision", 104 APIVersion: porchapi.SchemeGroupVersion.Identifier(), 105 }, 106 ObjectMeta: metav1.ObjectMeta{ 107 Namespace: *r.cfg.Namespace, 108 }, 109 Spec: *revisionSpec, 110 } 111 if err := r.client.Create(r.ctx, pr); err != nil { 112 return errors.E(op, err) 113 } 114 fmt.Fprintf(cmd.OutOrStdout(), "%s created\n", pr.Name) 115 return nil 116 } 117 118 func (r *runner) getPackageRevisionSpec() (*porchapi.PackageRevisionSpec, error) { 119 packageRevision := porchapi.PackageRevision{} 120 err := r.client.Get(r.ctx, types.NamespacedName{ 121 Name: r.copy.Source.Name, 122 Namespace: *r.cfg.Namespace, 123 }, &packageRevision) 124 if err != nil { 125 return nil, err 126 } 127 128 if r.workspace == "" { 129 return nil, fmt.Errorf("--workspace is required to specify workspace name") 130 } 131 132 spec := &porchapi.PackageRevisionSpec{ 133 PackageName: packageRevision.Spec.PackageName, 134 WorkspaceName: porchapi.WorkspaceName(r.workspace), 135 RepositoryName: packageRevision.Spec.RepositoryName, 136 } 137 138 if len(packageRevision.Spec.Tasks) == 0 || !r.replayStrategy { 139 spec.Tasks = []porchapi.Task{ 140 { 141 Type: porchapi.TaskTypeEdit, 142 Edit: &porchapi.PackageEditTaskSpec{ 143 Source: &porchapi.PackageRevisionRef{ 144 Name: packageRevision.Name, 145 }, 146 }, 147 }, 148 } 149 } else { 150 spec.Tasks = packageRevision.Spec.Tasks 151 } 152 return spec, nil 153 }