fix vela app delete

This commit is contained in:
天元
2020-08-26 16:10:04 +08:00
parent 3498e5d68a
commit eb693ed74a
3 changed files with 48 additions and 31 deletions
+8
View File
@@ -68,6 +68,14 @@ func Load(envName, appName string) (*Application, error) {
return LoadFromFile(filepath.Join(appDir, appName+".yaml"))
}
func Delete(envName, appName string) error {
appDir, err := system.GetApplicationDir(envName)
if err != nil {
return fmt.Errorf("get app dir from env %s err %v", envName, err)
}
return os.Remove(filepath.Join(appDir, appName+".yaml"))
}
func List(envName string) ([]*Application, error) {
appDir, err := system.GetApplicationDir(envName)
if err != nil {
+36 -27
View File
@@ -4,19 +4,24 @@ import (
"context"
"errors"
"fmt"
"os"
corev1alpha2 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/cloud-native-application/rudrx/pkg/application"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/cloud-native-application/rudrx/api/types"
cmdutil "github.com/cloud-native-application/rudrx/pkg/cmd/util"
corev1alpha2 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type deleteOptions struct {
Component corev1alpha2.Component
AppConfig corev1alpha2.ApplicationConfiguration
client client.Client
appName string
client client.Client
cmdutil.IOStreams
Env *types.EnvMeta
}
@@ -53,39 +58,43 @@ func NewDeleteCommand(c types.Args, ioStreams cmdutil.IOStreams) *cobra.Command
if err != nil {
return err
}
if err := o.Complete(cmd, args); err != nil {
return err
if len(args) < 1 {
return errors.New("must specify name for the app")
}
o.appName = args[0]
return o.Delete()
}
return cmd
}
func (o *deleteOptions) Complete(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("must specify name for the app")
}
namespace := o.Env.Namespace
o.Component.Name = args[0]
o.Component.Namespace = namespace
o.AppConfig.Name = args[0]
o.AppConfig.Namespace = namespace
return nil
}
func (o *deleteOptions) Delete() error {
o.Infof("Deleting AppConfig \"%s\"\n", o.AppConfig.Name)
err := o.client.Delete(context.Background(), &o.AppConfig)
o.Infof("Deleting AppConfig \"%s\"\n", o.appName)
if err := application.Delete(o.Env.Name, o.appName); err != nil && !os.IsNotExist(err) {
return err
}
ctx := context.Background()
var appConfig corev1alpha2.ApplicationConfiguration
err := o.client.Get(ctx, client.ObjectKey{Name: o.appName, Namespace: o.Env.Namespace}, &appConfig)
if err != nil {
if apierrors.IsNotFound(err) {
o.Info("Already deleted")
return nil
}
return fmt.Errorf("delete appconfig err %s", err)
}
err = o.client.Delete(context.Background(), &o.Component)
if err != nil {
return fmt.Errorf("delete component err: %s", err)
for _, comp := range appConfig.Status.Workloads {
var c corev1alpha2.Component
c.Name = comp.ComponentName
c.Namespace = o.Env.Namespace
err = o.client.Delete(context.Background(), &c)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete component err: %s", err)
}
}
err = o.client.Delete(context.Background(), &appConfig)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete appconfig err %s", err)
}
o.Info("DELETE SUCCEED")
return nil
+4 -4
View File
@@ -154,17 +154,17 @@ func showComponent(cmd *cobra.Command, env *types.EnvMeta, compName, appName str
}
cmd.Printf("%s\n\n", table.String())
traits, err := app.GetTraits(compName)
if err != nil || len(traits) == 0 {
if err != nil {
cmd.PrintErr(err)
continue
}
cmd.Println()
cmd.Printf("Traits:\n\n")
for k, v := range traits {
cmd.Printf(" Type:\t%s\n", k)
cmd.Printf(" Arguments:\n\n")
cmd.Printf(" %s:\n", k)
table = uitable.New()
for kk, vv := range v {
table.AddRow(fmt.Sprintf(" %s:", kk), vv)
table.AddRow(fmt.Sprintf(" %s:", kk), vv)
}
cmd.Printf("%s\n\n", table.String())
}