Skip to content

Additional printer columns for crd #8583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions modules/api/pkg/resource/customresourcedefinition/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package types
import (
"encoding/json"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"k8s.io/dashboard/api/pkg/api"
"k8s.io/dashboard/api/pkg/resource/common"
Expand Down Expand Up @@ -59,31 +59,48 @@ type CustomResourceDefinitionDetail struct {
Errors []error `json:"errors"`
}

type AdditionalPrinterColumn struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Priority int32 `json:"priority,omitempty"`
JSONPath string `json:"jsonPath"`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add a description field as shown here that can pop up when you hover over the column name?

}

type CustomResourceDefinitionVersion struct {
Name string `json:"name"`
Served bool `json:"served"`
Storage bool `json:"storage"`
Name string `json:"name"`
Served bool `json:"served"`
Storage bool `json:"storage"`
AdditionalPrinterColumns []AdditionalPrinterColumn `json:"additionalPrinterColumns"`
}

// CustomResourceObject represents a custom resource object.
type CustomResourceObject struct {
TypeMeta api.TypeMeta `json:"typeMeta"`
ObjectMeta api.ObjectMeta `json:"objectMeta"`
AdditionalPrinterColumns map[string]interface{} `json:"additionalPrinterColumns,omitempty"`
RawObject unstructured.Unstructured `json:"-"` // the raw object as map[string]interface{} to grep the value for AdditionalPrinterColumnWithValue which is present on the CRD Details
TypeMeta api.TypeMeta `json:"typeMeta"`
ObjectMeta api.ObjectMeta `json:"objectMeta"`
}

func (r *CustomResourceObject) UnmarshalJSON(data []byte) error {
tempStruct := &struct {
metav1.TypeMeta `json:",inline"`
ObjectMeta metav1.ObjectMeta `json:"metadata,omitempty"`
}{}
tempUnstruct := &unstructured.Unstructured{}

err := json.Unmarshal(data, &tempStruct)
if err != nil {
return err
}

err = tempUnstruct.UnmarshalJSON(data)
if err != nil {
return err
}

r.TypeMeta = api.NewTypeMeta(api.ResourceKind(tempStruct.TypeMeta.Kind))
r.ObjectMeta = api.NewObjectMeta(tempStruct.ObjectMeta)
r.RawObject = *tempUnstruct
return nil
}

Expand Down
16 changes: 13 additions & 3 deletions modules/api/pkg/resource/customresourcedefinition/v1/detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,20 @@ func getCRDVersions(crd *apiextensions.CustomResourceDefinition) []types.CustomR
crdVersions := make([]types.CustomResourceDefinitionVersion, 0, len(crd.Spec.Versions))
if len(crd.Spec.Versions) > 0 {
for _, version := range crd.Spec.Versions {
tempColumns := make([]types.AdditionalPrinterColumn, 0)
for _, column := range version.AdditionalPrinterColumns {
tempColumns = append(tempColumns, types.AdditionalPrinterColumn{
Name: column.Name,
Type: column.Type,
Priority: column.Priority,
JSONPath: column.JSONPath,
})
}
crdVersions = append(crdVersions, types.CustomResourceDefinitionVersion{
Name: version.Name,
Served: version.Served,
Storage: version.Storage,
Name: version.Name,
Served: version.Served,
Storage: version.Storage,
AdditionalPrinterColumns: tempColumns,
})
}
}
Expand Down
187 changes: 187 additions & 0 deletions modules/api/pkg/resource/customresourcedefinition/v1/detail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright 2017 The Kubernetes Authors.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2017 The Kubernetes Authors.
// Copyright 2023 The Kubernetes Authors.

//
// 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
//
// http://www.apache.org/licenses/LICENSE-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 OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1

import (
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/dashboard/api/pkg/resource/customresourcedefinition/types"
"reflect"
"strings"
"testing"

metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/dashboard/api/pkg/resource/dataselect"
)

type SampleCustomResourceSpec struct {
Field1 string `json:"field1"`
Field2 int32 `json:"field2"`
}

type SampleCustomResourceStatus struct{}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type SampleCustomResource struct {
metaV1.TypeMeta `json:",inline"`
metaV1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

Spec SampleCustomResourceSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

Status SampleCustomResourceStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

func (s SampleCustomResource) GetObjectKind() schema.ObjectKind {
return schema.EmptyObjectKind
}

func (s SampleCustomResource) DeepCopyObject() runtime.Object {
return s
}

func createCustomResourceDefinition(fqName string) *apiextensions.CustomResourceDefinition {
splitName := strings.Split(fqName, ".")
name := splitName[0]
group := splitName[1]
return &apiextensions.CustomResourceDefinition{
TypeMeta: metaV1.TypeMeta{
Kind: "CustomResourceDefinition",
APIVersion: "apiextensions.k8s.io/v1",
},
ObjectMeta: metaV1.ObjectMeta{
Name: fqName,
},
Spec: apiextensions.CustomResourceDefinitionSpec{
Group: group,
Names: apiextensions.CustomResourceDefinitionNames{
Kind: "SampleCustomResource",
Plural: name,
ShortNames: []string{"scr"},
},
Scope: apiextensions.NamespaceScoped,
Versions: []apiextensions.CustomResourceDefinitionVersion{
{
Name: "v1alpha1",
Served: true,
Storage: true,
Schema: &apiextensions.CustomResourceValidation{
OpenAPIV3Schema: &apiextensions.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensions.JSONSchemaProps{
"field1": {
Type: "string",
},
"field2": {
Type: "integer",
},
},
},
},
AdditionalPrinterColumns: []apiextensions.CustomResourceColumnDefinition{
{
Name: "Field1",
JSONPath: ".spec.field1",
Priority: 2,
},
{
Name: "Field2",
JSONPath: ".spec.field2",
Type: "integer",
},
},
},
},
},
Status: apiextensions.CustomResourceDefinitionStatus{},
}
}

func createCustomResourceObject(name, namespace, field1 string, field2 int32) *SampleCustomResource {
return &SampleCustomResource{
ObjectMeta: metaV1.ObjectMeta{
Name: name,
Namespace: namespace,
},
TypeMeta: metaV1.TypeMeta{
Kind: "SampleCustomResource",
APIVersion: "sample-controller.k8s.io/v1alpha1",
},
Spec: SampleCustomResourceSpec{
Field1: field1,
Field2: field2,
},
}
}

func TestGetCustomResourceDefinitionDetail(t *testing.T) {
crd := createCustomResourceDefinition("sample-controller.k8s.io")
crdObject := createCustomResourceObject("ns-1", "scr-1", "string-field-1", 100)

gv := schema.GroupVersion{
Group: "sample-controller.k8s.io",
Version: "v1alpha1",
}

// Register the type and GroupVersion with the scheme.
metaV1.AddToGroupVersion(scheme.Scheme, gv)
scheme.Scheme.AddKnownTypes(gv, crd) // Register your custom resource type

cases := []struct {
namespace, name string
expectedActions []string
crdObject *SampleCustomResource
expected *types.CustomResourceDefinitionDetail
}{
{
"ns-1", "scr1",
[]string{"get"},
crdObject,
&types.CustomResourceDefinitionDetail{
CustomResourceDefinition: types.CustomResourceDefinition{},
},
},
}

for _, c := range cases {
fakeClient := fake.NewSimpleClientset(crd, c.crdObject)
fakeRestConfig := &rest.Config{}
dataselect.DefaultDataSelectWithMetrics.MetricQuery = dataselect.NoMetrics
actual, _ := GetCustomResourceDefinitionDetail(fakeClient, fakeRestConfig, c.name)

actions := fakeClient.Actions()
if len(actions) != len(c.expectedActions) {
t.Errorf("Unexpected actions: %v, expected %d actions got %d", actions,
len(c.expectedActions), len(actions))
continue
}

for i, verb := range c.expectedActions {
if actions[i].GetVerb() != verb {
t.Errorf("Unexpected action: %+v, expected %s", actions[i], verb)
}
}

if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("GetCustomResourceDefinitionDetail(client, config, name) == \ngot: %#v, \nexpected %#v",
actual, c.expected)
}
}
}
17 changes: 17 additions & 0 deletions modules/api/pkg/resource/customresourcedefinition/v1/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"

"k8s.io/dashboard/api/pkg/api"
Expand Down Expand Up @@ -128,4 +130,19 @@ func toCRDObject(object *types.CustomResourceObject, crd *apiextensionsv1.Custom
object.TypeMeta.Kind = api.ResourceKind(crd.Name)
crdSubresources := crd.Spec.Versions[0].Subresources
object.TypeMeta.Scalable = crdSubresources != nil && crdSubresources.Scale != nil
object.AdditionalPrinterColumns = make(map[string]interface{})
for _, col := range crd.Spec.Versions[0].AdditionalPrinterColumns {
val, _, _ := unstructured.NestedString(object.RawObject.Object, splitJsonPath(col.JSONPath)...)
object.AdditionalPrinterColumns[col.Name] = val
}
}

func splitJsonPath(path string) []string {
var paths []string
for _, p := range strings.Split(path, ".") {
if p != "" {
paths = append(paths, p)
}
}
return paths
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
export class CRDObjectListComponent extends ResourceListBase<CRDObjectList, CRDObject> {
@Input() endpoint: string;
@Input() namespaced = false;
@Input() additionalDisplayColumns: string[] = [];

constructor(
private readonly crdObject_: NamespacedResourceService<CRDObjectList>,
Expand Down Expand Up @@ -62,7 +63,12 @@ export class CRDObjectListComponent extends ResourceListBase<CRDObjectList, CRDO
}

getDisplayColumns(): string[] {
return ['name', 'namespace', 'created'];
return ['name', 'namespace', ...this.getAdditionalDisplayColumns(), 'created'];
}

getAdditionalDisplayColumns(): string[] {
const defaultColumns = ['name', 'namespace', 'created'];
return this.additionalDisplayColumns.filter(v => !defaultColumns.includes(v));
}

areMultipleNamespacesSelected(): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<mat-cell *matCellDef="let object">{{ object.objectMeta.namespace }}</mat-cell>
</ng-container>

<ng-container [matColumnDef]="getColumns()[2]">
<ng-container [matColumnDef]="getColumns()[getColumns().length - 2]">
<mat-header-cell *matHeaderCellDef
i18n>Created</mat-header-cell>
<mat-cell *matCellDef="let object">
Expand All @@ -62,6 +62,16 @@
</mat-cell>
</ng-container>

<ng-container *ngFor="let additionalColumn of getAdditionalDisplayColumns()"
[matColumnDef]="additionalColumn">
<mat-header-cell *matHeaderCellDef
i18n>{{ additionalColumn }}
</mat-header-cell>
<mat-cell *matCellDef="let object">
{{ object.additionalPrinterColumns[additionalColumn] }}
</mat-cell>
</ng-container>

<ng-container *ngFor="let col of getActionColumns()"
[matColumnDef]="col.name">
<mat-header-cell *matHeaderCellDef></mat-header-cell>
Expand Down
6 changes: 6 additions & 0 deletions modules/web/src/crd/detail/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@ export class CRDDetailComponent implements OnInit, OnDestroy {
isNamespaced(): boolean {
return this.crd && this.crd.scope === 'Namespaced';
}

getAdditionalDisplayColumns(): string[] {
return this.crd.versions[0]?.additionalPrinterColumns
.sort((a, b) => (a.priority < b.priority ? -1 : 1))
.map(c => c.name);
}
}
4 changes: 3 additions & 1 deletion modules/web/src/crd/detail/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@
</div>
</kd-card>

<kd-crd-object-list [namespaced]="isNamespaced()"></kd-crd-object-list>
<kd-crd-object-list *ngIf="crd?.versions"
[namespaced]="isNamespaced()"
[additionalDisplayColumns]="getAdditionalDisplayColumns()"></kd-crd-object-list>

<kd-crd-versions-list *ngIf="crd?.versions"
[versions]="crd?.versions"
Expand Down
Loading