Skip to content

Commit

Permalink
Small changes in existing schema, add camelCase in taxonomy
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Dymiński <dyminski@gmail.com>
  • Loading branch information
mateuszdyminski committed Jun 30, 2023
1 parent 45d40af commit 72d0bd1
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 81 deletions.
28 changes: 14 additions & 14 deletions cmd/cyclonexdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
},
}

if kbom.Cluster.Location.Location != "" && kbom.Cluster.Location.Location != "unknown" {
if kbom.Cluster.Location.Name != "" && kbom.Cluster.Location.Name != "unknown" {
clusterProperties = append(clusterProperties, cyclonedx.Property{
Name: KSOCPrefix + "k8s:cluster:location:location",
Value: kbom.Cluster.Location.Location,
Name: KSOCPrefix + "k8s:cluster:location:name",
Value: kbom.Cluster.Location.Name,
})
}

Expand Down Expand Up @@ -110,7 +110,7 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
Value: n.Name,
},
{
Name: KSOCPrefix + "k8s:node:osimage",
Name: KSOCPrefix + "k8s:node:osImage",
Value: n.OsImage,
},
{
Expand All @@ -122,35 +122,35 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
Value: n.KernelVersion,
},
{
Name: KSOCPrefix + "k8s:node:bootid",
Name: KSOCPrefix + "k8s:node:bootId",
Value: n.BootID,
},
{
Name: KSOCPrefix + "k8s:node:type",
Value: n.Type,
},
{
Name: KSOCPrefix + "k8s:node:operatingsystem",
Name: KSOCPrefix + "k8s:node:operatingSystem",
Value: n.OperatingSystem,
},
{
Name: KSOCPrefix + "k8s:node:machineid",
Name: KSOCPrefix + "k8s:node:machineId",
Value: n.MachineID,
},
{
Name: KSOCPrefix + "k8s:node:hostname",
Value: n.Hostname,
},
{
Name: KSOCPrefix + "k8s:node:containerruntimeversion",
Name: KSOCPrefix + "k8s:node:containerRuntimeVersion",
Value: n.ContainerRuntimeVersion,
},
{
Name: KSOCPrefix + "k8s:node:kubeletversion",
Name: KSOCPrefix + "k8s:node:kubeletVersion",
Value: n.KubeletVersion,
},
{
Name: KSOCPrefix + "k8s:node:kubeproxyversion",
Name: KSOCPrefix + "k8s:node:kubeProxyVersion",
Value: n.KubeProxyVersion,
},
{
Expand All @@ -166,14 +166,14 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
Value: n.Capacity.Pods,
},
{
Name: KSOCPrefix + "k8s:node:capacity:ephemeralstorage",
Name: KSOCPrefix + "k8s:node:capacity:ephemeralStorage",
Value: n.Capacity.EphemeralStorage,
},
},
})
}

for _, img := range kbom.Cluster.Resources.Images {
for _, img := range kbom.Cluster.Components.Images {
container := cyclonedx.Component{
BOMRef: img.PkgID(),
Type: cyclonedx.ComponentTypeContainer,
Expand Down Expand Up @@ -211,7 +211,7 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
components = append(components, container)
}

for _, resList := range kbom.Cluster.Resources.Resources {
for _, resList := range kbom.Cluster.Components.Resources {
for _, res := range resList.Resources {
properties := []cyclonedx.Property{
{
Expand All @@ -223,7 +223,7 @@ func transformToCycloneDXBOM(kbom *model.KBOM) *cyclonedx.BOM { //nolint:funlen
Value: res.Name,
},
{
Name: KSOCPrefix + "k8s:component:apiversion",
Name: KSOCPrefix + "k8s:component:apiVersion",
Value: resList.APIVersion,
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
KSOCCompany = "KSOC Labs"
BOMFormat = "ksoc"
SpecVersion = "0.1"
SpecVersion = "0.2"

StdOutput = "stdout"
FileOutput = "file"
Expand Down Expand Up @@ -123,7 +123,7 @@ func generateKBOM(k8sClient kube.K8sClient) error {
CACertDigest: caCertDigest,
NodesCount: len(nodes),
Nodes: nodes,
Resources: model.Resources{
Components: model.Components{
Images: allImages,
Resources: resources,
},
Expand Down
16 changes: 8 additions & 8 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ func TestGenerateKBOM(t *testing.T) {
},
location: func(context.Context) (*model.Location, error) {
return &model.Location{
Location: "aws",
Region: "us-east-1",
Zone: "us-east-1a",
Name: "aws",
Region: "us-east-1",
Zone: "us-east-1a",
}, nil
},
allNodes: func(context.Context, bool) ([]model.Node, error) {
Expand Down Expand Up @@ -337,7 +337,7 @@ var mockCACert = "1234567890"
var expectedOutJSON = `{
"id": "00000001",
"bom_format": "ksoc",
"spec_version": "0.1",
"spec_version": "0.2",
"generated_at": "2023-04-26T10:00:00Z",
"generated_by": {
"vendor": "KSOC Labs",
Expand All @@ -352,7 +352,7 @@ var expectedOutJSON = `{
"ca_cert_digest": "1.25.1",
"k8s_version": "012345678",
"location": {
"location": "aws",
"name": "aws",
"region": "us-east-1",
"zone": "us-east-1a"
},
Expand Down Expand Up @@ -419,7 +419,7 @@ var expectedOutJSON = `{
"os_image": "Bottlerocket OS 1.11.1 (aws-k8s-1.24)"
}
],
"resources": {
"components": {
"images": [
{
"full_name": "nginx:1.17.1",
Expand Down Expand Up @@ -456,7 +456,7 @@ var expectedOutJSON = `{
`
var expectedOutYAML = `id: "00000001"
bomformat: ksoc
specversion: "0.1"
specversion: "0.2"
generatedat: 2023-04-26T10:00:00Z
generatedby:
vendor: KSOC Labs
Expand All @@ -473,7 +473,7 @@ cluster:
location: null
nodescount: 0
nodes: []
resources:
components:
images: []
resources: {}
`
54 changes: 27 additions & 27 deletions cmd/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ var expectedSchema = `{
},
"type": "array"
},
"resources": {
"$ref": "#/$defs/Resources"
"components": {
"$ref": "#/$defs/Components"
}
},
"additionalProperties": false,
Expand All @@ -97,6 +97,29 @@ var expectedSchema = `{
"location",
"nodes_count",
"nodes",
"components"
]
},
"Components": {
"properties": {
"images": {
"items": {
"$ref": "#/$defs/Image"
},
"type": "array"
},
"resources": {
"patternProperties": {
".*": {
"$ref": "#/$defs/ResourceList"
}
},
"type": "object"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"resources"
]
},
Expand Down Expand Up @@ -159,7 +182,7 @@ var expectedSchema = `{
},
"Location": {
"properties": {
"location": {
"name": {
"type": "string"
},
"region": {
Expand All @@ -172,7 +195,7 @@ var expectedSchema = `{
"additionalProperties": false,
"type": "object",
"required": [
"location",
"name",
"region",
"zone"
]
Expand Down Expand Up @@ -306,29 +329,6 @@ var expectedSchema = `{
"count"
]
},
"Resources": {
"properties": {
"images": {
"items": {
"$ref": "#/$defs/Image"
},
"type": "array"
},
"resources": {
"patternProperties": {
".*": {
"$ref": "#/$defs/ResourceList"
}
},
"type": "object"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"resources"
]
},
"Tool": {
"properties": {
"vendor": {
Expand Down
20 changes: 10 additions & 10 deletions docs/taxonomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ Following Taxonomy is used by the `KBOM` tool as extension to: [https://github.c

| Namespace | Description |
| ------------------------------------ | ----------------------------------------------------------------- |
| `ksoc:kbom:k8s:component:apiversion` | API Version of the Kubernetes component. |
| `ksoc:kbom:k8s:component:apiVersion` | API Version of the Kubernetes component. |
| `ksoc:kbom:k8s:component:namespace` | Namespace of the Kubernetes component. |

## `ksoc:kbom:k8s:cluster` Namespace Taxonomy

| Property | Description |
| ----------------------------------------- | ------------------------------ |
| `ksoc:kbom:k8s:cluster:location:location` | Location of the cluster. |
| `ksoc:kbom:k8s:cluster:location:name` | Name of the location. |
| `ksoc:kbom:k8s:cluster:location:region` | Region of the cluster. |
| `ksoc:kbom:k8s:cluster:location:zone` | Zone where cluster is located. |

## `ksoc:kbom:k8s:node` Namespace Taxonomy

| Property | Description |
| ---------------------------------------------- | --------------------------------- |
| `ksoc:kbom:k8s:node:osimage` | Node's operating system image |
| `ksoc:kbom:k8s:node:osImage` | Node's operating system image |
| `ksoc:kbom:k8s:node:arch` | Node's architecture |
| `ksoc:kbom:k8s:node:kernel` | Node's kernel version |
| `ksoc:kbom:k8s:node:bootid` | Node's Boot identifier |
| `ksoc:kbom:k8s:node:bootId` | Node's Boot identifier |
| `ksoc:kbom:k8s:node:type` | Node's type |
| `ksoc:kbom:k8s:node:operatingsystem` | Node's operating system |
| `ksoc:kbom:k8s:node:machineid` | Node's machine identifier |
| `ksoc:kbom:k8s:node:operatingSystem` | Node's operating system |
| `ksoc:kbom:k8s:node:machineId` | Node's machine identifier |
| `ksoc:kbom:k8s:node:hostname` | Node's hostname |
| `ksoc:kbom:k8s:node:containerruntimeversion` | Node's container runtime version |
| `ksoc:kbom:k8s:node:kubeletversion` | Node's kubelet version |
| `ksoc:kbom:k8s:node:kubeproxyversion` | Node's kube proxy version |
| `ksoc:kbom:k8s:node:containerRuntimeVersion` | Node's container runtime version |
| `ksoc:kbom:k8s:node:kubeletVersion` | Node's kubelet version |
| `ksoc:kbom:k8s:node:kubeProxyVersion` | Node's kube proxy version |
| `ksoc:kbom:k8s:node:capacity:cpu` | Node's CPU capacity |
| `ksoc:kbom:k8s:node:capacity:memory` | Node's Memory capacity |
| `ksoc:kbom:k8s:node:capacity:pods` | Node's Pods capacity |
| `ksoc:kbom:k8s:node:capacity:ephemeralstorage` | Node's ephemeral storage capacity |
| `ksoc:kbom:k8s:node:capacity:ephemeralStorage` | Node's ephemeral storage capacity |

## `ksoc:kbom:pkg` Namespace Taxonomy

Expand Down
13 changes: 6 additions & 7 deletions internal/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func (k *k8sDB) Location(ctx context.Context) (*model.Location, error) {

// get location from node labels
return &model.Location{
Location: getCloudLocation(node.Items[0].Labels),
Region: getLabelValue(node.Items[0].Labels, "topology.kubernetes.io/region"),
Zone: getLabelValue(node.Items[0].Labels, "topology.kubernetes.io/zone"),
Name: getCloudName(node.Items[0].Labels),
Region: getLabelValue(node.Items[0].Labels, "topology.kubernetes.io/region"),
Zone: getLabelValue(node.Items[0].Labels, "topology.kubernetes.io/zone"),
}, nil
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func (k *k8sDB) AllNodes(ctx context.Context, full bool) ([]model.Node, error) {
MachineID: nodes.Items[i].Status.NodeInfo.MachineID,
Architecture: nodes.Items[i].Status.NodeInfo.Architecture,
KernelVersion: nodes.Items[i].Status.NodeInfo.KernelVersion,
ContainerRuntimeVersion: strings.TrimPrefix(nodes.Items[i].Status.NodeInfo.ContainerRuntimeVersion, "containerd://"),
ContainerRuntimeVersion: nodes.Items[i].Status.NodeInfo.ContainerRuntimeVersion,
BootID: nodes.Items[i].Status.NodeInfo.BootID,
KubeProxyVersion: nodes.Items[i].Status.NodeInfo.KubeProxyVersion,
KubeletVersion: nodes.Items[i].Status.NodeInfo.KubeletVersion,
Expand Down Expand Up @@ -233,8 +233,7 @@ func containerToImage(img, imgName string, statuses []v1.ContainerStatus) (*mode
}
if strings.Contains(statuses[i].ImageID, "@") {
res.Digest = strings.Split(statuses[i].ImageID, "@")[1]
}
if strings.HasPrefix(statuses[i].ImageID, "sha256:") {
} else if strings.HasPrefix(statuses[i].ImageID, "sha256:") {
res.Digest = statuses[i].ImageID
}
break
Expand Down Expand Up @@ -365,7 +364,7 @@ func getLabelValue(labels map[string]string, key string) string {
return ""
}

func getCloudLocation(labels map[string]string) string {
func getCloudName(labels map[string]string) string {
if labels == nil {
return "unknown"
}
Expand Down
25 changes: 12 additions & 13 deletions internal/model/kbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ type Tool struct {
}

type Cluster struct {
Name string `json:"name"`
CACertDigest string `json:"ca_cert_digest"`
K8sVersion string `json:"k8s_version"`
CNIVersion string `json:"cni_version,omitempty"`
Location *Location `json:"location"`
NodesCount int `json:"nodes_count"`
Nodes []Node `json:"nodes"`

Resources Resources `json:"resources"`
Name string `json:"name"`
CACertDigest string `json:"ca_cert_digest"`
K8sVersion string `json:"k8s_version"`
CNIVersion string `json:"cni_version,omitempty"`
Location *Location `json:"location"`
NodesCount int `json:"nodes_count"`
Nodes []Node `json:"nodes"`
Components Components `json:"components"`
}

type Resources struct {
type Components struct {
Images []Image `json:"images,omitempty"`
Resources map[string]ResourceList `json:"resources"`
}
Expand All @@ -57,9 +56,9 @@ type ResourceList struct {
}

type Location struct {
Location string `json:"location"`
Region string `json:"region"`
Zone string `json:"zone"`
Name string `json:"name"`
Region string `json:"region"`
Zone string `json:"zone"`
}

type Node struct {
Expand Down

0 comments on commit 72d0bd1

Please sign in to comment.