Skip to content

Commit

Permalink
Add support for additionalPrinterColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
RafikFarhad committed Dec 29, 2023
1 parent a2baa5c commit 1b06436
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions modules/api/pkg/resource/customresourcedefinition/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"

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

"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"

Expand Down Expand Up @@ -67,23 +68,31 @@ type CustomResourceDefinitionVersion struct {

// CustomResourceObject represents a custom resource object.
type CustomResourceObject struct {
TypeMeta api.TypeMeta `json:"typeMeta"`
ObjectMeta api.ObjectMeta `json:"objectMeta"`
AdditionalPrinterColumns unstructured.Unstructured `json:"additionalPrinterColumns,omitempty"`
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.AdditionalPrinterColumns.Object = (*tempUnstruct).Object
return nil
}

Expand Down
18 changes: 18 additions & 0 deletions modules/api/pkg/resource/customresourcedefinition/v1/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import (
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"
"k8s.io/dashboard/api/pkg/errors"
"k8s.io/dashboard/api/pkg/resource/common"
"k8s.io/dashboard/api/pkg/resource/customresourcedefinition/types"
"k8s.io/dashboard/api/pkg/resource/dataselect"
"strings"
)

// GetCustomResourceObjectList gets objects for a CR.
Expand Down Expand Up @@ -128,4 +130,20 @@ 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
tempMap := map[string]interface{}{}
for _, col := range crd.Spec.Versions[0].AdditionalPrinterColumns {
val, _, _ := unstructured.NestedString(object.AdditionalPrinterColumns.Object, splitJsonPath(col.JSONPath)...)
tempMap[col.Name] = val
}
object.AdditionalPrinterColumns.Object = tempMap
}

func splitJsonPath(path string) []string {
var paths []string
for _, p := range strings.Split(path, ".") {
if p != "" {
paths = append(paths, p)
}
}
return paths
}

0 comments on commit 1b06436

Please sign in to comment.