-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cluster and machine support TableConvertor
- Loading branch information
1 parent
a2d0b20
commit 36a5b87
Showing
13 changed files
with
1,078 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
Oops, something went wrong.