Skip to content

Commit

Permalink
feat: cluster and machine support TableConvertor
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChenglong committed Dec 18, 2019
1 parent a2d0b20 commit 36a5b87
Show file tree
Hide file tree
Showing 13 changed files with 1,078 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
github.com/kr/fs v0.1.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/leodido/go-urn v1.1.0 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de
github.com/moul/http2curl v1.0.0 // indirect
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/lib/pq v0.0.0-20181016162627-9eb73efc1fcc/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/lightstep/lightstep-tracer-go v0.15.6/go.mod h1:6AMpwZpsyCFwSovxzM78e+AsYxE8sGwiM6C3TytaWeI=
github.com/lovoo/gcloud-opentracing v0.3.0/go.mod h1:ZFqk2y38kMDDikZPAK7ynTTGuyt17nSPdS3K5e+ZTBY=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
Expand Down
6 changes: 5 additions & 1 deletion pkg/platform/registry/cluster/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"context"
"fmt"
"sync"
"tkestack.io/tke/pkg/apiserver/authentication"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
Expand All @@ -36,10 +35,13 @@ import (
"k8s.io/apiserver/pkg/util/dryrun"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/api/platform"
"tkestack.io/tke/pkg/apiserver/authentication"
apiserverutil "tkestack.io/tke/pkg/apiserver/util"
clusterstrategy "tkestack.io/tke/pkg/platform/registry/cluster"
"tkestack.io/tke/pkg/platform/util"
"tkestack.io/tke/pkg/util/log"
"tkestack.io/tke/pkg/util/printers"
printerstorage "tkestack.io/tke/pkg/util/printers/storage"
)

// Storage includes storage for clusters and all sub resources.
Expand Down Expand Up @@ -79,6 +81,8 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter, clusterProviders *
UpdateStrategy: strategy,
DeleteStrategy: strategy,
ExportStrategy: strategy,

TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(AddHandlers)},
}
options := &genericregistry.StoreOptions{
RESTOptions: optsGetter,
Expand Down
62 changes: 62 additions & 0 deletions pkg/platform/registry/cluster/storage/table.go
Original file line number Diff line number Diff line change
@@ -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/platform"
platformv1 "tkestack.io/tke/api/platform/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) {
clusterColumnDefinitions := []metav1beta1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Type", Type: "string", Description: platformv1.ClusterSpec{}.SwaggerDoc()["type"]},
{Name: "Version", Type: "string", Description: platformv1.ClusterStatus{}.SwaggerDoc()["version"]},
{Name: "Status", Type: "string", Description: platformv1.ClusterStatus{}.SwaggerDoc()["phase"]},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
}
h.TableHandler(clusterColumnDefinitions, printClusterList)
h.TableHandler(clusterColumnDefinitions, printCluster)
}

func printClusterList(clusterList *platform.ClusterList, options printers.PrintOptions) ([]metav1.TableRow, error) {
rows := make([]metav1.TableRow, 0, len(clusterList.Items))
for i := range clusterList.Items {
r, err := printCluster(&clusterList.Items[i], options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return rows, nil
}

func printCluster(cluster *platform.Cluster, options printers.PrintOptions) ([]metav1.TableRow, error) {
row := metav1.TableRow{
Object: runtime.RawExtension{Object: cluster},
}
row.Cells = append(row.Cells, cluster.Name, cluster.Spec.Type, cluster.Status.Version, cluster.Status.Phase, printers.TranslateTimestampSince(cluster.CreationTimestamp))
return []metav1beta1.TableRow{row}, nil
}
7 changes: 6 additions & 1 deletion pkg/platform/registry/machine/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package storage
import (
"context"
"fmt"
"sync"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -31,14 +33,15 @@ import (
"k8s.io/apiserver/pkg/storage"
storageerr "k8s.io/apiserver/pkg/storage/errors"
"k8s.io/apiserver/pkg/util/dryrun"
"sync"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/api/platform"
"tkestack.io/tke/pkg/apiserver/authentication"
apiserverutil "tkestack.io/tke/pkg/apiserver/util"
"tkestack.io/tke/pkg/platform/registry/machine"
"tkestack.io/tke/pkg/platform/util"
"tkestack.io/tke/pkg/util/log"
"tkestack.io/tke/pkg/util/printers"
printerstorage "tkestack.io/tke/pkg/util/printers/storage"
)

// Storage includes storage for machines and all sub resources.
Expand All @@ -62,6 +65,8 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter, machineProviders *
UpdateStrategy: strategy,
DeleteStrategy: strategy,
ExportStrategy: strategy,

TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(AddHandlers)},
}
options := &genericregistry.StoreOptions{
RESTOptions: optsGetter,
Expand Down
62 changes: 62 additions & 0 deletions pkg/platform/registry/machine/storage/table.go
Original file line number Diff line number Diff line change
@@ -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/platform"
platformv1 "tkestack.io/tke/api/platform/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) {
machineColumnDefinitions := []metav1beta1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Type", Type: "string", Description: platformv1.MachineSpec{}.SwaggerDoc()["type"]},
{Name: "IP", Type: "string", Description: platformv1.MachineSpec{}.SwaggerDoc()["ip"]},
{Name: "Status", Type: "string", Description: platformv1.MachineStatus{}.SwaggerDoc()["phase"]},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
}
h.TableHandler(machineColumnDefinitions, printMachineList)
h.TableHandler(machineColumnDefinitions, printMachine)
}

func printMachineList(machineList *platform.MachineList, options printers.PrintOptions) ([]metav1.TableRow, error) {
rows := make([]metav1.TableRow, 0, len(machineList.Items))
for i := range machineList.Items {
r, err := printMachine(&machineList.Items[i], options)
if err != nil {
return nil, err
}
rows = append(rows, r...)
}
return rows, nil
}

func printMachine(machine *platform.Machine, options printers.PrintOptions) ([]metav1.TableRow, error) {
row := metav1.TableRow{
Object: runtime.RawExtension{Object: machine},
}
row.Cells = append(row.Cells, machine.Name, machine.Spec.Type, machine.Spec.IP, machine.Status.Phase, printers.TranslateTimestampSince(machine.CreationTimestamp))
return []metav1beta1.TableRow{row}, nil
}
46 changes: 46 additions & 0 deletions pkg/util/printers/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 printers

import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
)

// TranslateTimestampSince returns the elapsed time since timestamp in
// human-readable approximation.
func TranslateTimestampSince(timestamp metav1.Time) string {
if timestamp.IsZero() {
return "<unknown>"
}

return duration.HumanDuration(time.Since(timestamp.Time))
}

// TranslateTimestampUntil returns the elapsed time until timestamp in
// human-readable approximation.
func TranslateTimestampUntil(timestamp metav1.Time) string {
if timestamp.IsZero() {
return "<unknown>"
}

return duration.HumanDuration(time.Until(timestamp.Time))
}
61 changes: 61 additions & 0 deletions pkg/util/printers/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 printers

import (
"io"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// ResourcePrinter is an interface that knows how to print runtime objects.
type ResourcePrinter interface {
// Print receives a runtime object, formats it and prints it to a writer.
PrintObj(runtime.Object, io.Writer) error
}

// ResourcePrinterFunc is a function that can print objects
type ResourcePrinterFunc func(runtime.Object, io.Writer) error

// PrintObj implements ResourcePrinter
func (fn ResourcePrinterFunc) PrintObj(obj runtime.Object, w io.Writer) error {
return fn(obj, w)
}

// PrintOptions struct defines a struct for various print options
type PrintOptions struct {
// supported Format types can be found in pkg/printers/printers.go
OutputFormatType string
OutputFormatArgument string

NoHeaders bool
WithNamespace bool
WithKind bool
Wide bool
ShowLabels bool
AbsoluteTimestamps bool
Kind schema.GroupKind
ColumnLabels []string

SortBy string

// indicates if it is OK to ignore missing keys for rendering an output template.
AllowMissingKeys bool
}
49 changes: 49 additions & 0 deletions pkg/util/printers/storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 (
"context"
"fmt"

metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"tkestack.io/tke/pkg/util/printers"
)

// TableConvertor struct - converts objects to metav1beta1.Table using printers.TableGenerator
type TableConvertor struct {
printers.TableGenerator
}

// ConvertToTable method - converts objects to metav1beta1.Table objects using TableGenerator
func (c TableConvertor) ConvertToTable(ctx context.Context, obj runtime.Object, tableOptions runtime.Object) (*metav1beta1.Table, error) {
noHeaders := false
if tableOptions != nil {
switch t := tableOptions.(type) {
case *metav1beta1.TableOptions:
if t != nil {
noHeaders = t.NoHeaders
}
default:
return nil, fmt.Errorf("unrecognized type %T for table options, can't display tabular output", tableOptions)
}
}
return c.TableGenerator.GenerateTable(obj, printers.PrintOptions{Wide: true, NoHeaders: noHeaders})
}
Loading

0 comments on commit 36a5b87

Please sign in to comment.