From 7c220dddc3e4227e24da8fe2e0a6b86e6ffbc358 Mon Sep 17 00:00:00 2001 From: huccshen <1171593960@qq.com> Date: Wed, 2 Nov 2022 15:45:49 +0800 Subject: [PATCH] feat(application): update application table (#2028) Co-authored-by: ginoczhu --- api/application/v1/generated.proto | 2 + api/application/v1/types.go | 14 +++-- .../v1/types_swagger_doc_generated.go | 4 +- api/openapi/zz_generated.openapi.go | 14 +++-- .../registry/application/storage/storage.go | 4 +- .../registry/application/storage/table.go | 62 +++++++++++++++++++ 6 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 pkg/application/registry/application/storage/table.go diff --git a/api/application/v1/generated.proto b/api/application/v1/generated.proto index e03cb0c99..141defb56 100644 --- a/api/application/v1/generated.proto +++ b/api/application/v1/generated.proto @@ -191,8 +191,10 @@ message Chart { optional string chartGroupName = 2; + // ChartName is the name of the chart. optional string chartName = 3; + // ChartVersion is the version of the chart. optional string chartVersion = 4; optional string repoURL = 5; diff --git a/api/application/v1/types.go b/api/application/v1/types.go index 553d0f9c1..c1922f519 100644 --- a/api/application/v1/types.go +++ b/api/application/v1/types.go @@ -74,12 +74,14 @@ type AppSpec struct { type Chart struct { TenantID string `json:"tenantID" protobuf:"bytes,1,opt,name=tenantID"` ChartGroupName string `json:"chartGroupName" protobuf:"bytes,2,opt,name=chartGroupName"` - ChartName string `json:"chartName" protobuf:"bytes,3,opt,name=chartName"` - ChartVersion string `json:"chartVersion" protobuf:"bytes,4,opt,name=chartVersion"` - RepoURL string `json:"repoURL" protobuf:"bytes,5,opt,name=repoURL"` - RepoUsername string `json:"repoUsername" protobuf:"bytes,6,opt,name=repoUsername"` - RepoPassword string `json:"repoPassword" protobuf:"bytes,7,opt,name=repoPassword"` - ImportedRepo bool `json:"importedRepo" protobuf:"bytes,8,opt,name=importedRepo"` + // ChartName is the name of the chart. + ChartName string `json:"chartName" protobuf:"bytes,3,opt,name=chartName"` + // ChartVersion is the version of the chart. + ChartVersion string `json:"chartVersion" protobuf:"bytes,4,opt,name=chartVersion"` + RepoURL string `json:"repoURL" protobuf:"bytes,5,opt,name=repoURL"` + RepoUsername string `json:"repoUsername" protobuf:"bytes,6,opt,name=repoUsername"` + RepoPassword string `json:"repoPassword" protobuf:"bytes,7,opt,name=repoPassword"` + ImportedRepo bool `json:"importedRepo" protobuf:"bytes,8,opt,name=importedRepo"` } // AppStatus represents information about the status of a bootstrap. diff --git a/api/application/v1/types_swagger_doc_generated.go b/api/application/v1/types_swagger_doc_generated.go index 41ca3bfbc..ed00262e0 100644 --- a/api/application/v1/types_swagger_doc_generated.go +++ b/api/application/v1/types_swagger_doc_generated.go @@ -117,7 +117,9 @@ func (AppValues) SwaggerDoc() map[string]string { } var map_Chart = map[string]string{ - "": "Chart is a description of a chart.", + "": "Chart is a description of a chart.", + "chartName": "ChartName is the name of the chart.", + "chartVersion": "ChartVersion is the version of the chart.", } func (Chart) SwaggerDoc() map[string]string { diff --git a/api/openapi/zz_generated.openapi.go b/api/openapi/zz_generated.openapi.go index 329f40232..e986247d3 100644 --- a/api/openapi/zz_generated.openapi.go +++ b/api/openapi/zz_generated.openapi.go @@ -41609,16 +41609,18 @@ func schema_tke_api_application_v1_Chart(ref common.ReferenceCallback) common.Op }, "chartName": { SchemaProps: spec.SchemaProps{ - Default: "", - Type: []string{"string"}, - Format: "", + Description: "ChartName is the name of the chart.", + Default: "", + Type: []string{"string"}, + Format: "", }, }, "chartVersion": { SchemaProps: spec.SchemaProps{ - Default: "", - Type: []string{"string"}, - Format: "", + Description: "ChartVersion is the version of the chart.", + Default: "", + Type: []string{"string"}, + Format: "", }, }, "repoURL": { diff --git a/pkg/application/registry/application/storage/storage.go b/pkg/application/registry/application/storage/storage.go index 396316bb6..049f27209 100644 --- a/pkg/application/registry/application/storage/storage.go +++ b/pkg/application/registry/application/storage/storage.go @@ -41,6 +41,8 @@ import ( applicationutil "tkestack.io/tke/pkg/application/util" platformfilter "tkestack.io/tke/pkg/platform/apiserver/filter" "tkestack.io/tke/pkg/util/log" + "tkestack.io/tke/pkg/util/printers" + printerstorage "tkestack.io/tke/pkg/util/printers/storage" ) // Storage includes storage for application and all sub resources. @@ -65,7 +67,7 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter, UpdateStrategy: strategy, DeleteStrategy: strategy, } - store.TableConvertor = rest.NewDefaultTableConvertor(store.DefaultQualifiedResource) + store.TableConvertor = printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(AddHandlers)} options := &genericregistry.StoreOptions{ RESTOptions: optsGetter, AttrFunc: applicationtrategy.GetAttrs, diff --git a/pkg/application/registry/application/storage/table.go b/pkg/application/registry/application/storage/table.go new file mode 100644 index 000000000..b2ad6c61f --- /dev/null +++ b/pkg/application/registry/application/storage/table.go @@ -0,0 +1,62 @@ +/* + * Tencent is pleased to support the open source community by making TKEStack + * available. + * + * Copyright (C) 2012-2019 Tencent. All Rights Reserved. + * + * 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 + * + * https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package storage + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1" + "k8s.io/apimachinery/pkg/runtime" + "tkestack.io/tke/api/application" + applicationv1 "tkestack.io/tke/api/application/v1" + "tkestack.io/tke/pkg/util/printers" +) + +// AddHandlers adds print handlers for default TKE types dealing with internal versions. +// Refer kubernetes/pkg/printers/internalversion/printers.go:78 +func AddHandlers(h printers.PrintHandler) { + appColumnDefinitions := []metav1beta1.TableColumnDefinition{ + {Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]}, + {Name: "ChartName", Type: "string", Description: applicationv1.AppSpec{}.Chart.SwaggerDoc()["chartName"]}, + {Name: "ChartVersion", Type: "string", Description: applicationv1.AppSpec{}.Chart.SwaggerDoc()["chartVersion"]}, + {Name: "Status", Type: "string", Description: applicationv1.AppStatus{}.SwaggerDoc()["phase"]}, + {Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]}, + } + h.TableHandler(appColumnDefinitions, printAppList) + h.TableHandler(appColumnDefinitions, printApp) +} + +func printAppList(appList *application.AppList, options printers.PrintOptions) ([]metav1.TableRow, error) { + rows := make([]metav1.TableRow, 0, len(appList.Items)) + for i := range appList.Items { + r, err := printApp(&appList.Items[i], options) + if err != nil { + return nil, err + } + rows = append(rows, r...) + } + return rows, nil +} + +func printApp(app *application.App, options printers.PrintOptions) ([]metav1.TableRow, error) { + row := metav1.TableRow{ + Object: runtime.RawExtension{Object: app}, + } + row.Cells = append(row.Cells, app.Name, app.Spec.Chart.ChartName, app.Spec.Chart.ChartVersion, app.Status.Phase, printers.TranslateTimestampSince(app.CreationTimestamp)) + return []metav1beta1.TableRow{row}, nil +}