mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-18 03:56:36 +00:00
+1
-72
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user