{"id":22930726,"url":"https://github.com/kubewarden/k8s-objects","last_synced_at":"2025-08-12T15:31:54.285Z","repository":{"id":39749227,"uuid":"492171063","full_name":"kubewarden/k8s-objects","owner":"kubewarden","description":"Experimental: Kubernetes Go types that can be used with TinyGo","archived":false,"fork":false,"pushed_at":"2024-06-03T07:00:32.000Z","size":4394,"stargazers_count":4,"open_issues_count":1,"forks_count":2,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-06-21T06:40:55.268Z","etag":null,"topics":["hacktoberfest","kubernetes","kubernetes-security","policy-as-code","webassembly"],"latest_commit_sha":null,"homepage":"https://github.com/kubewarden/","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kubewarden.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-14T09:35:30.000Z","updated_at":"2024-06-03T07:01:46.000Z","dependencies_parsed_at":"2024-01-29T15:27:02.598Z","dependency_job_id":null,"html_url":"https://github.com/kubewarden/k8s-objects","commit_stats":null,"previous_names":[],"tags_count":128,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fk8s-objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fk8s-objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fk8s-objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fk8s-objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kubewarden","download_url":"https://codeload.github.com/kubewarden/k8s-objects/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229694588,"owners_count":18108931,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","kubernetes","kubernetes-security","policy-as-code","webassembly"],"created_at":"2024-12-14T10:29:30.921Z","updated_at":"2024-12-14T10:31:29.721Z","avatar_url":"https://github.com/kubewarden.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Stable](https://img.shields.io/badge/status-stable-brightgreen?style=for-the-badge)](https://github.com/kubewarden/community/blob/main/REPOSITORIES.md#stable)\n\nKubernetes Go types that can be used with [TinyGo](tinygo.org/) to build WebAssembly\nmodules meant to be run outside of the browser.\n\n\u003e These files are generated automatically via\n\u003e [`k8s-objects-generator`](https://github.com/kubewarden/k8s-objects-generator).\n\n## Repository structure\n\nThis repository provides Kubernetes Go types for different Kubernetes releases.\n\nThe code is organized in this way:\n\n * Git branches: each Kubernetes release has its own `release-\u003ckube major\u003e.\u003ckube-minor\u003e` branch\n * Git Tags: each Kubernetes release has its own `v\u003ckube major\u003e.\u003ckube minor\u003e.\u003ckube patch\u003e-kw\u003crev number\u003e`\n\nFor example:\n\n| Kubernetes version | Branch | Tag | Notes |\n|--------------------|--------|-----|-------|\n| `1.14.0`             | `release-1.14` | `v1.14.0-kw1` | First version of the Kubernetes `1.14.0` objects for TinyGo |\n| `1.14.1`             | `release-1.14` | `v1.14.1-kw1` | First version of the Kubernetes `1.14.1` objects for TinyGo |\n| `1.23.1`             | `release-1.23` | `v1.23.1-kw1` | First version of the Kubernetes `1.23.1` objects for TinyGo |\n| `1.23.1`             | `release-1.23` | `v1.23.1-kw2` | Second version of the Kubernetes `1.23.1` objects for TinyGo, hypothetically there was a bug in the automatically generated code of `1.23.1+kw1` |\n\n\u003e **Note:** Go [module versioning](https://go.dev/doc/modules/version-numbers)\ndoesn't fully implement [semver](https://semver.org/) rules.\n\u003e\n\u003e Go module are not aware of \"build\" metadata. For example, the `v1.23.1+kw1`\n\u003e version is not recognized by Go and is automatically translated to `v1.23.1`.\n\u003e\n\u003e Because of that, all our tags have to use the \"pre-release\" notation, hence\n\u003e instead of having `v1.23.1+kw1` we must have `v1.23.1-kw1`.\n\u003e\n\u003e Unfortunately, this is still happing with Go 1.18.\n\n## Marshall a Kubernetes object to JSON\n\nCreate a new Go project using Go modules:\n\n```console\nmkdir demo\ncd demo\ngo mod init demo\n```\n\n**Important:** run the following command to ensure a TinyGo compatible version of\nthe `github.com/go-openapi/strfmt` module is being used:\n\n```console\ngo mod edit -replace github.com/go-openapi/strfmt=github.com/kubewarden/strfmt@v0.1.0\n```\n\nNow create a `main.go` file with the following contents:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tcorev1 \"github.com/kubewarden/k8s-objects/api/core/v1\"\n\tmetav1 \"github.com/kubewarden/k8s-objects/apimachinery/pkg/apis/meta/v1\"\n)\n\nfunc main() {\n\tcontainerName := \"nginx\"\n\tvar httpPort int32 = 80\n\n\tpod := corev1.Pod{\n\t\tMetadata: metav1.ObjectMeta{\n\t\t\tName: \"nginx\",\n\t\t},\n\t\tSpec: \u0026corev1.PodSpec{\n\t\t\tContainers: []*corev1.Container{\n\t\t\t\t{\n\t\t\t\t\tName:  \u0026containerName,\n\t\t\t\t\tImage: \"nginx:latest\",\n\t\t\t\t\tPorts: []*corev1.ContainerPort{\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tContainerPort: \u0026httpPort,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t}\n\n\tb, err := pod.MarshalJSON()\n\tif err != nil {\n\t\tfmt.Println(\"Error\", err)\n\t} else {\n\t\tfmt.Printf(\"%s\\n\", string(b))\n\t}\n}\n```\n\nNext, run `go mod tidy` and, if you want, vendor all the dependencies:\n\n```console\ngo mod tidy\ngo mod vendor\n```\n\nThe latest version of the Kubernetes objects available is going to be used.\nIf you want to pick a different version of the library, you can run the\nsomething like the following command:\n\n```console\n# Download the v1.14.0+kw1 release\ngo get github.com/kubewarden/k8s-objects@v1.14.0+kw1\n```\n\nFinally, build the code using TinyGo to a WebAssembly module targeting the\n[WASI interface](https://wasi.dev/):\n\n```console\ndocker run --rm -v `pwd`:/src -w /src tinygo/tinygo:0.23.0 tinygo build -o demo.wasm -target=wasi -no-debug .\n```\n\nNow, you can run the code using [Wasmtime](https://wasmtime.dev/):\n\n```console\nwasmtime run demo.wasm\n```\n\nIf you pipe the output through something like the `jq` utility, you will\nobtain something like that:\n\n```json\n{\n  \"metadata\": {\n    \"creationTimestamp\": \"0001-01-01T00:00:00.000Z\",\n    \"deletionTimestamp\": \"0001-01-01T00:00:00.000Z\",\n    \"finalizers\": null,\n    \"managedFields\": null,\n    \"name\": \"nginx\",\n    \"ownerReferences\": null\n  },\n  \"spec\": {\n    \"containers\": [\n      {\n        \"args\": null,\n        \"command\": null,\n        \"env\": null,\n        \"envFrom\": null,\n        \"image\": \"nginx:latest\",\n        \"name\": \"nginx\",\n        \"ports\": [\n          {\n            \"containerPort\": 80\n          }\n        ],\n        \"volumeDevices\": null,\n        \"volumeMounts\": null\n      }\n    ],\n    \"ephemeralContainers\": null,\n    \"hostAliases\": null,\n    \"imagePullSecrets\": null,\n    \"initContainers\": null,\n    \"readinessGates\": null,\n    \"tolerations\": null,\n    \"topologySpreadConstraints\": null,\n    \"volumes\": null\n  }\n}\n```\n\n## Unmarshal from JSON to a Kubernetes object\n\nCreate a new Go project using Go modules:\n\n```console\nmkdir demo\ncd demo\ngo mod init demo\n```\n\n**Important:** run the following command to ensure a TinyGo compatible version of\nthe `github.com/go-openapi/strfmt` module is being used:\n\n```console\ngo mod edit -replace github.com/go-openapi/strfmt=github.com/kubewarden/strfmt@v0.1.0\n```\n\nNow create a `main.go` file with the following contents:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\n\tnetworkingv1 \"github.com/kubewarden/k8s-objects/api/networking/v1\"\n\t\"github.com/mailru/easyjson\"\n)\n\nfunc main() {\n\tif len(os.Args) \u003c 2 {\n\t\tfmt.Fprintln(os.Stderr, \"Usage: demo \u003cpath to file with json ingress\u003e\")\n\t\tos.Exit(1)\n\t}\n\n\tfilename := os.Args[1]\n\tdata, err := os.ReadFile(filename)\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"error reading file %s: %v\\n\", filename, err)\n\t\tos.Exit(1)\n\t}\n\n\tingress := \u0026networkingv1.Ingress{}\n\tif err := easyjson.Unmarshal(data, ingress); err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"cannot decode contents of file %s into an Ingress object: %v\\n\", filename, err)\n\t\tos.Exit(1)\n\t}\n\n\tingress.Metadata.Name = \"DEMO\"\n\n\tb, err := ingress.MarshalJSON()\n\tif err != nil {\n\t\tfmt.Println(\"Error\", err)\n\t} else {\n\t\tfmt.Printf(\"%s\\n\", string(b))\n\t}\n}\n```\n\nThis program loads an Ingress definition from a JSON file, changes its name to\nbe `DEMO` and then prints the object back to the standard output using\nthe JSON format.\n\nLet's create a file named `ingress.json`, which contains the definition of a\nsimple Ingress object:\n\n```json\n{\n  \"apiVersion\": \"networking.k8s.io/v1\",\n  \"kind\": \"Ingress\",\n  \"metadata\": {\n    \"name\": \"minimal-ingress\",\n    \"annotations\": {\n      \"nginx.ingress.kubernetes.io/rewrite-target\": \"/\"\n    }\n  },\n  \"spec\": {\n    \"ingressClassName\": \"nginx-example\",\n    \"rules\": [\n      {\n        \"http\": {\n          \"paths\": [\n            {\n              \"path\": \"/testpath\",\n              \"pathType\": \"Prefix\",\n              \"backend\": {\n                \"service\": {\n                  \"name\": \"test\",\n                  \"port\": {\n                    \"number\": 80\n                  }\n                }\n              }\n            }\n          ]\n        }\n      }\n    ]\n  }\n}\n```\n\nNext, run `go mod tidy` and, if you want, vendor all the dependencies:\n\n```console\ngo mod tidy\ngo mod vendor\n```\n\nFinally, build the code using TinyGo to a WebAssembly module targeting the\n[WASI interface](https://wasi.dev/):\n\n```console\ndocker run --rm -v `pwd`:/src -w /src tinygo/tinygo:0.23.0 tinygo build -o demo.wasm -target=wasi -no-debug .\n```\n\nNow, you can run the code using [Wasmtime](https://wasmtime.dev/). This time\nhowever we have to provide some extra runtime flags to allow the Wasm module\nto read the `ingress.json` file we previously created. This is needed because\nall the Wasm modules are run inside of a dedicated sandbox that, by default,\ndoes not grant access to the host file system to the running code.\n\n```console\nwasmtime run \\\n  --mapdir /demo::`pwd` \\\n  demo.wasm /demo/ingress.json\n```\n\nThe command maps the current directory on the host (`pwd`) into the guest\nunder the `/demo` path. Because of that, the guest code will find the\n`ingress.json` file at the `/demo/ingress.json` location.\n\n\u003e **Tip:** the `mapdir` statement works exactly like sharing a Volume between\n\u003e the host system and a container.\n\nIf you pipe the output through something like the `jq` utility, you will\nobtain something like that:\n\n```json\n{\n  \"apiVersion\": \"networking.k8s.io/v1\",\n  \"kind\": \"Ingress\",\n  \"metadata\": {\n    \"annotations\": {\n      \"nginx.ingress.kubernetes.io/rewrite-target\": \"/\"\n    },\n    \"creationTimestamp\": \"0001-01-01T00:00:00.000Z\",\n    \"deletionTimestamp\": \"0001-01-01T00:00:00.000Z\",\n    \"finalizers\": null,\n    \"managedFields\": null,\n    \"name\": \"DEMO\",\n    \"ownerReferences\": null\n  },\n  \"spec\": {\n    \"ingressClassName\": \"nginx-example\",\n    \"rules\": [\n      {\n        \"http\": {\n          \"paths\": [\n            {\n              \"backend\": {\n                \"resource\": {\n                  \"kind\": null,\n                  \"name\": null\n                },\n                \"service\": {\n                  \"name\": \"test\",\n                  \"port\": {\n                    \"number\": 80\n                  }\n                }\n              },\n              \"path\": \"/testpath\",\n              \"pathType\": \"Prefix\"\n            }\n          ]\n        }\n      }\n    ],\n    \"tls\": null\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubewarden%2Fk8s-objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkubewarden%2Fk8s-objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubewarden%2Fk8s-objects/lists"}