From a6e730d7bdf2ec3b9a1d11b7063d6bdf7efbba9b Mon Sep 17 00:00:00 2001 From: zzxwill Date: Fri, 24 Jul 2020 21:47:11 +0800 Subject: [PATCH] Refactor `rudr apps` Move some basic functions to cmd.util.helpers.go and decompose some of them for further calling, like `rudr unbind` --- pkg/cmd/apps.go | 73 +---------------------------------- pkg/cmd/util/helpers.go | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 72 deletions(-) diff --git a/pkg/cmd/apps.go b/pkg/cmd/apps.go index 652f37dcf..ed8cce2b0 100644 --- a/pkg/cmd/apps.go +++ b/pkg/cmd/apps.go @@ -2,12 +2,10 @@ package cmd import ( "context" - "encoding/json" "fmt" "strings" cmdutil "github.com/cloud-native-application/rudrx/pkg/cmd/util" - corev1alpha2 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2" "github.com/gosuri/uitable" "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/client" @@ -33,7 +31,7 @@ func NewAppsCommand(f cmdutil.Factory, c client.Client, ioStreams cmdutil.IOStre } func printApplicationList(ctx context.Context, c client.Client, appName string, namespace string) { - applicationMetaList, err := RetrieveApplicationsByApplicationName(ctx, c, appName, namespace) + applicationMetaList, err := cmdutil.RetrieveApplicationsByApplicationName(ctx, c, appName, namespace) table := uitable.New() table.MaxColWidth = 60 @@ -49,72 +47,3 @@ func printApplicationList(ctx context.Context, c client.Client, appName string, } fmt.Print(table.String()) } - -type ApplicationMeta struct { - Name string `json:"name"` - Workload string `json:"workload,omitempty"` - Traits []string `json:"traits,omitempty"` - Status string `json:"status,omitempty"` - CreatedTime string `json:"created,omitempty"` -} - -/* - Get application list by optional filter `applicationName` - Application name is equal to Component name as currently rudrx only supports one component exists in one application -*/ -func RetrieveApplicationsByApplicationName(ctx context.Context, c client.Client, workloadName string, namespace string) ([]ApplicationMeta, error) { - var applicationMetaList []ApplicationMeta - - if namespace == "" { - namespace = "default" - } - - var applicationList corev1alpha2.ApplicationConfigurationList - - if workloadName != "" { - var application corev1alpha2.ApplicationConfiguration - err := c.Get(ctx, client.ObjectKey{Name: workloadName, Namespace: namespace}, &application) - - if err != nil { - return applicationMetaList, err - } - - applicationList.Items = append(applicationList.Items, application) - } else { - err := c.List(ctx, &applicationList) - if err != nil { - return applicationMetaList, err - } - } - - for _, a := range applicationList.Items { - componentName := a.Spec.Components[0].ComponentName - - var component corev1alpha2.Component - err := c.Get(ctx, client.ObjectKey{Name: componentName, Namespace: namespace}, &component) - if err != nil { - return applicationMetaList, err - } - - var workload corev1alpha2.WorkloadDefinition - json.Unmarshal(component.Spec.Workload.Raw, &workload) - workloadName := workload.TypeMeta.Kind - - var traitNames []string - for _, t := range a.Spec.Components[0].Traits { - var trait corev1alpha2.TraitDefinition - json.Unmarshal(t.Trait.Raw, &trait) - traitNames = append(traitNames, trait.Kind) - } - - applicationMetaList = append(applicationMetaList, ApplicationMeta{ - Name: a.Name, - Workload: workloadName, - Traits: traitNames, - Status: string(a.Status.Conditions[0].Status), - CreatedTime: a.ObjectMeta.CreationTimestamp.String(), - }) - } - - return applicationMetaList, nil -} diff --git a/pkg/cmd/util/helpers.go b/pkg/cmd/util/helpers.go index eda79deac..d5bd1bf0a 100644 --- a/pkg/cmd/util/helpers.go +++ b/pkg/cmd/util/helpers.go @@ -5,6 +5,12 @@ import ( "os" "strings" + corev1alpha2 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2" + "sigs.k8s.io/controller-runtime/pkg/client" + + "context" + "encoding/json" + "k8s.io/klog" ) @@ -49,3 +55,82 @@ func CheckErr(err error) { } fatal(msg, DefaultErrorExitCode) } + +type ApplicationMeta struct { + Name string `json:"name"` + Workload string `json:"workload,omitempty"` + Traits []string `json:"traits,omitempty"` + Status string `json:"status,omitempty"` + CreatedTime string `json:"created,omitempty"` +} + +func GetComponent(ctx context.Context, c client.Client, componentName string, namespace string) (corev1alpha2.Component, error) { + var component corev1alpha2.Component + err := c.Get(ctx, client.ObjectKey{Name: componentName, Namespace: namespace}, &component) + return component, err +} + +func GetTraitNames(app corev1alpha2.ApplicationConfiguration) []string { + var traitNames []string + for _, t := range app.Spec.Components[0].Traits { + var trait corev1alpha2.TraitDefinition + json.Unmarshal(t.Trait.Raw, &trait) + traitNames = append(traitNames, trait.Kind) + } + return traitNames +} + +/* + Get application list by optional filter `applicationName` + Application name is equal to Component name as currently rudrx only supports one component exists in one application +*/ +func RetrieveApplicationsByApplicationName(ctx context.Context, c client.Client, applicationName string, namespace string) ([]ApplicationMeta, error) { + var applicationMetaList []ApplicationMeta + + if namespace == "" { + namespace = "default" + } + + var applicationList corev1alpha2.ApplicationConfigurationList + + if applicationName != "" { + var application corev1alpha2.ApplicationConfiguration + err := c.Get(ctx, client.ObjectKey{Name: applicationName, Namespace: namespace}, &application) + + if err != nil { + return applicationMetaList, err + } + + applicationList.Items = append(applicationList.Items, application) + } else { + err := c.List(ctx, &applicationList) + if err != nil { + return applicationMetaList, err + } + } + + for _, a := range applicationList.Items { + componentName := a.Spec.Components[0].ComponentName + + component, err := GetComponent(ctx, c, componentName, namespace) + if err != nil { + return applicationMetaList, err + } + + var workload corev1alpha2.WorkloadDefinition + json.Unmarshal(component.Spec.Workload.Raw, &workload) + workloadName := workload.TypeMeta.Kind + + traitNames := GetTraitNames(a) + + applicationMetaList = append(applicationMetaList, ApplicationMeta{ + Name: a.Name, + Workload: workloadName, + Traits: traitNames, + Status: string(a.Status.Conditions[0].Status), + CreatedTime: a.ObjectMeta.CreationTimestamp.String(), + }) + } + + return applicationMetaList, nil +}