From ea1322041367041d77d745ad699519e9f27e7383 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Fri, 20 Dec 2024 18:59:00 -0800 Subject: [PATCH] Add new values to the k8s.node resource - Fetch the creation date, the port, and the overall OS config. Signed-off-by: Tim Smith --- providers/k8s/resources/k8s.lr | 6 ++++ providers/k8s/resources/k8s.lr.go | 36 ++++++++++++++++++++ providers/k8s/resources/k8s.lr.manifest.yaml | 6 ++++ providers/k8s/resources/node.go | 19 ++++++++--- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/providers/k8s/resources/k8s.lr b/providers/k8s/resources/k8s.lr index ed78102f7e..178b0e9f0b 100644 --- a/providers/k8s/resources/k8s.lr +++ b/providers/k8s/resources/k8s.lr @@ -114,6 +114,12 @@ private k8s.node @defaults("name labels['kubernetes.io/arch'] labels['kubernetes name string // Kubernetes object type kind string + // Kubernetes object creation timestamp + created time + // Node configuration information + nodeInfo dict + // Kubelet port + kubeletPort int } // Kubernetes Pod diff --git a/providers/k8s/resources/k8s.lr.go b/providers/k8s/resources/k8s.lr.go index 667689571a..d6601fe171 100644 --- a/providers/k8s/resources/k8s.lr.go +++ b/providers/k8s/resources/k8s.lr.go @@ -364,6 +364,15 @@ var getDataFields = map[string]func(r plugin.Resource) *plugin.DataRes{ "k8s.node.kind": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlK8sNode).GetKind()).ToDataRes(types.String) }, + "k8s.node.created": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlK8sNode).GetCreated()).ToDataRes(types.Time) + }, + "k8s.node.nodeInfo": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlK8sNode).GetNodeInfo()).ToDataRes(types.Dict) + }, + "k8s.node.kubeletPort": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlK8sNode).GetKubeletPort()).ToDataRes(types.Int) + }, "k8s.pod.id": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlK8sPod).GetId()).ToDataRes(types.String) }, @@ -1500,6 +1509,18 @@ var setDataFields = map[string]func(r plugin.Resource, v *llx.RawData) bool { r.(*mqlK8sNode).Kind, ok = plugin.RawToTValue[string](v.Value, v.Error) return }, + "k8s.node.created": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlK8sNode).Created, ok = plugin.RawToTValue[*time.Time](v.Value, v.Error) + return + }, + "k8s.node.nodeInfo": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlK8sNode).NodeInfo, ok = plugin.RawToTValue[interface{}](v.Value, v.Error) + return + }, + "k8s.node.kubeletPort": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlK8sNode).KubeletPort, ok = plugin.RawToTValue[int64](v.Value, v.Error) + return + }, "k8s.pod.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) { r.(*mqlK8sPod).__id, ok = v.Value.(string) return @@ -3486,6 +3507,9 @@ type mqlK8sNode struct { ResourceVersion plugin.TValue[string] Name plugin.TValue[string] Kind plugin.TValue[string] + Created plugin.TValue[*time.Time] + NodeInfo plugin.TValue[interface{}] + KubeletPort plugin.TValue[int64] } // createK8sNode creates a new instance of this resource @@ -3557,6 +3581,18 @@ func (c *mqlK8sNode) GetKind() *plugin.TValue[string] { return &c.Kind } +func (c *mqlK8sNode) GetCreated() *plugin.TValue[*time.Time] { + return &c.Created +} + +func (c *mqlK8sNode) GetNodeInfo() *plugin.TValue[interface{}] { + return &c.NodeInfo +} + +func (c *mqlK8sNode) GetKubeletPort() *plugin.TValue[int64] { + return &c.KubeletPort +} + // mqlK8sPod for the k8s.pod resource type mqlK8sPod struct { MqlRuntime *plugin.Runtime diff --git a/providers/k8s/resources/k8s.lr.manifest.yaml b/providers/k8s/resources/k8s.lr.manifest.yaml index 94dfb58bc3..db1f36f432 100755 --- a/providers/k8s/resources/k8s.lr.manifest.yaml +++ b/providers/k8s/resources/k8s.lr.manifest.yaml @@ -488,12 +488,18 @@ resources: fields: annotations: min_mondoo_version: 5.29.2 + created: + min_mondoo_version: 9.0.0 id: min_mondoo_version: 6.10.0 kind: {} + kubeletPort: + min_mondoo_version: 9.0.0 labels: min_mondoo_version: 5.29.2 name: {} + nodeInfo: + min_mondoo_version: 9.0.0 resourceVersion: min_mondoo_version: 5.29.2 uid: {} diff --git a/providers/k8s/resources/node.go b/providers/k8s/resources/node.go index dae0153e07..47528dd548 100644 --- a/providers/k8s/resources/node.go +++ b/providers/k8s/resources/node.go @@ -54,21 +54,32 @@ func initK8sNode(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[str func (k *mqlK8s) nodes() ([]interface{}, error) { k.mqlK8sInternal.nodesByName = make(map[string]*mqlK8sNode) return k8sResourceToMql(k.MqlRuntime, gvkString(corev1.SchemeGroupVersion.WithKind("nodes")), func(kind string, resource runtime.Object, obj metav1.Object, objT metav1.Type) (interface{}, error) { + ts := obj.GetCreationTimestamp() + + n, ok := obj.(*corev1.Node) + if !ok { + return nil, errors.New("not a k8s node") + } + + nodeInfo, err := convert.JsonToDict(n.Status.NodeInfo) + if err != nil { + return nil, err + } + r, err := CreateResource(k.MqlRuntime, "k8s.node", map[string]*llx.RawData{ "id": llx.StringData(objIdFromK8sObj(obj, objT)), "uid": llx.StringData(string(obj.GetUID())), "resourceVersion": llx.StringData(obj.GetResourceVersion()), "name": llx.StringData(obj.GetName()), "kind": llx.StringData(objT.GetKind()), + "created": llx.TimeData(ts.Time), + "nodeInfo": llx.DictData(nodeInfo), + "kubeletPort": llx.IntData(n.Status.DaemonEndpoints.KubeletEndpoint.Port), }) if err != nil { return nil, err } - n, ok := resource.(*corev1.Node) - if !ok { - return nil, errors.New("not a k8s node") - } r.(*mqlK8sNode).obj = n k.mqlK8sInternal.nodesByName[obj.GetName()] = r.(*mqlK8sNode)