Skip to content

Commit

Permalink
Merge pull request #35 from yue9944882/printer-example
Browse files Browse the repository at this point in the history
Adding example for custom printers
  • Loading branch information
k8s-ci-robot authored Feb 22, 2021
2 parents e20867a + ef474c0 commit 7df5711
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/example/v1alpha1/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package v1alpha1

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/apiserver-runtime/pkg/builder/resource/resourcestrategy"
)

type ExampleResource struct {
Expand Down Expand Up @@ -67,3 +70,38 @@ func (e *ExampleResourceList) DeepCopyObject() runtime.Object {
// implemented by code generation
return e
}

var _ resourcestrategy.TableConverter = &ExampleResource{}
var _ resourcestrategy.TableConverter = &ExampleResourceList{}

var (
definitions = []metav1.TableColumnDefinition{
{Name: "Name", Type: "string", Format: "name", Description: "the name of the cluster"},
}
)

func (in *ExampleResource) ConvertToTable(ctx context.Context, tableOptions runtime.Object) (*metav1.Table, error) {
return &metav1.Table{
ColumnDefinitions: definitions,
Rows: []metav1.TableRow{printResource(in)},
}, nil
}

func (in *ExampleResourceList) ConvertToTable(ctx context.Context, tableOptions runtime.Object) (*metav1.Table, error) {
t := &metav1.Table{
ColumnDefinitions: definitions,
}
for _, c := range in.Items {
t.Rows = append(t.Rows, printResource(&c))
}
return t, nil
}

func printResource(c *ExampleResource) metav1.TableRow {
name := c.Name
row := metav1.TableRow{
Object: runtime.RawExtension{Object: c},
}
row.Cells = append(row.Cells, name)
return row
}

0 comments on commit 7df5711

Please sign in to comment.