github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/commands/alpha/repo/unreg/command.go (about) 1 // Copyright 2022 Google LLC 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 unreg 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/GoogleContainerTools/kpt/internal/docs/generated/repodocs" 22 "github.com/GoogleContainerTools/kpt/internal/errors" 23 "github.com/GoogleContainerTools/kpt/internal/util/porch" 24 configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1" 25 "github.com/spf13/cobra" 26 coreapi "k8s.io/api/core/v1" 27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/cli-runtime/pkg/genericclioptions" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 ) 31 32 const ( 33 command = "cmdrepounreg" 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 c := &cobra.Command{ 46 Use: "unreg REPOSITORY [flags]", 47 Aliases: []string{"unregister"}, 48 Short: repodocs.UnregShort, 49 Long: repodocs.UnregShort + "\n" + repodocs.UnregLong, 50 Example: repodocs.UnregExamples, 51 PreRunE: r.preRunE, 52 RunE: r.runE, 53 Hidden: porch.HidePorchCommands, 54 } 55 r.Command = c 56 57 c.Flags().BoolVar(&r.keepSecret, "keep-auth-secret", false, "Keep the auth secret associated with the repository registration, if any") 58 59 return r 60 } 61 62 type runner struct { 63 ctx context.Context 64 cfg *genericclioptions.ConfigFlags 65 client client.Client 66 Command *cobra.Command 67 68 // Flags 69 keepSecret bool 70 } 71 72 func (r *runner) preRunE(cmd *cobra.Command, args []string) error { 73 const op errors.Op = command + ".preRunE" 74 client, err := porch.CreateClient(r.cfg) 75 if err != nil { 76 return errors.E(op, err) 77 } 78 r.client = client 79 return nil 80 } 81 82 func (r *runner) runE(cmd *cobra.Command, args []string) error { 83 const op errors.Op = command + ".runE" 84 85 if len(args) == 0 { 86 return errors.E(op, fmt.Errorf("REPOSITORY is a required positional argument")) 87 } 88 89 repository := args[0] 90 91 var repo configapi.Repository 92 if err := r.client.Get(r.ctx, client.ObjectKey{ 93 Namespace: *r.cfg.Namespace, 94 Name: repository, 95 }, &repo); err != nil { 96 return errors.E(op, err) 97 } 98 if err := r.client.Delete(r.ctx, &configapi.Repository{ 99 TypeMeta: metav1.TypeMeta{ 100 Kind: "Repository", 101 APIVersion: configapi.GroupVersion.Identifier(), 102 }, 103 ObjectMeta: metav1.ObjectMeta{ 104 Name: repo.Name, 105 Namespace: repo.Namespace, 106 }, 107 }); err != nil { 108 return errors.E(op, err) 109 } 110 111 if r.keepSecret { 112 return nil 113 } 114 115 secret := getSecretName(&repo) 116 if secret == "" { 117 return nil 118 } 119 120 if err := r.client.Delete(r.ctx, &coreapi.Secret{ 121 TypeMeta: metav1.TypeMeta{ 122 Kind: "Secret", 123 APIVersion: coreapi.SchemeGroupVersion.Identifier(), 124 }, 125 ObjectMeta: metav1.ObjectMeta{ 126 Name: secret, 127 Namespace: repo.Namespace, 128 }, 129 }); err != nil { 130 return errors.E(op, fmt.Errorf("failed to delete Secret %s: %w", secret, err)) 131 } 132 133 return nil 134 } 135 136 func getSecretName(repo *configapi.Repository) string { 137 if repo.Spec.Git != nil { 138 return repo.Spec.Git.SecretRef.Name 139 } 140 if repo.Spec.Oci != nil { 141 return repo.Spec.Oci.SecretRef.Name 142 } 143 return "" 144 }