-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new command 'kpt alpha rpkg delete' (#3745)
- Loading branch information
1 parent
e710f1b
commit 6b7f4a6
Showing
7 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package proposedelete | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/GoogleContainerTools/kpt/internal/errors" | ||
"github.com/GoogleContainerTools/kpt/internal/util/porch" | ||
"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
command = "cmdrpkgpropose-delete" | ||
) | ||
|
||
func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner { | ||
r := &runner{ | ||
ctx: ctx, | ||
cfg: rcg, | ||
} | ||
c := &cobra.Command{ | ||
Use: "propose-delete PACKAGE", | ||
Aliases: []string{"propose-del"}, | ||
SuggestFor: []string{}, | ||
PreRunE: r.preRunE, | ||
RunE: r.runE, | ||
Hidden: porch.HidePorchCommands, | ||
} | ||
r.Command = c | ||
|
||
// Create flags | ||
|
||
return r | ||
} | ||
|
||
func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command { | ||
return newRunner(ctx, rcg).Command | ||
} | ||
|
||
type runner struct { | ||
ctx context.Context | ||
cfg *genericclioptions.ConfigFlags | ||
client client.Client | ||
Command *cobra.Command | ||
} | ||
|
||
func (r *runner) preRunE(cmd *cobra.Command, args []string) error { | ||
const op errors.Op = command + ".preRunE" | ||
|
||
client, err := porch.CreateClientWithFlags(r.cfg) | ||
if err != nil { | ||
return errors.E(op, err) | ||
} | ||
r.client = client | ||
return nil | ||
} | ||
|
||
func (r *runner) runE(_ *cobra.Command, args []string) error { | ||
const op errors.Op = command + ".runE" | ||
var messages []string | ||
namespace := *r.cfg.Namespace | ||
|
||
for _, name := range args { | ||
pr := &v1alpha1.PackageRevision{} | ||
if err := r.client.Get(r.ctx, client.ObjectKey{ | ||
Namespace: namespace, | ||
Name: name, | ||
}, pr); err != nil { | ||
return errors.E(op, err) | ||
} | ||
|
||
switch pr.Spec.Lifecycle { | ||
case v1alpha1.PackageRevisionLifecyclePublished: | ||
// ok | ||
case v1alpha1.PackageRevisionLifecycleDeletionProposed: | ||
fmt.Fprintf(r.Command.OutOrStderr(), "%s is already proposed for deletion\n", name) | ||
continue | ||
default: | ||
msg := fmt.Sprintf("can only propose published packages for deletion; package %s is not published", name) | ||
messages = append(messages, msg) | ||
fmt.Fprintln(r.Command.ErrOrStderr(), msg) | ||
continue | ||
} | ||
|
||
pr.Spec.Lifecycle = v1alpha1.PackageRevisionLifecycleDeletionProposed | ||
if err := r.client.Update(r.ctx, pr); err != nil { | ||
messages = append(messages, err.Error()) | ||
fmt.Fprintf(r.Command.ErrOrStderr(), "%s failed (%s)\n", name, err) | ||
} else { | ||
fmt.Fprintf(r.Command.OutOrStderr(), "%s proposed for deletion\n", name) | ||
} | ||
} | ||
|
||
if len(messages) > 0 { | ||
return errors.E(op, fmt.Errorf("errors:\n %s", strings.Join(messages, "\n "))) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters