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 6 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
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
16 changes: 15 additions & 1 deletion modules/web/src/typings/root.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ export interface CRD extends Resource {
established: string;
}

export type CRDObject = Resource;
export interface AdditionalPrinterColumnValue {
[key: string]: string;
}

export interface CRDObject extends Resource {
additionalPrinterColumns: AdditionalPrinterColumnValue[];
}

export interface DaemonSet extends Resource {
podInfo: PodInfo;
Expand Down Expand Up @@ -1002,10 +1008,18 @@ export interface CRDNames {
categories?: string[];
}

export interface AdditionalPrinterColumn {
name: string;
type: string;
priority: number;
jsonPath: string;
}

export interface CRDVersion {
name: string;
served: boolean;
storage: boolean;
additionalPrinterColumns: AdditionalPrinterColumn[];
}

export interface PodMetrics {
Expand Down