{"id":16744004,"url":"https://github.com/goccy/kubejob","last_synced_at":"2025-05-08T19:47:38.858Z","repository":{"id":38108701,"uuid":"274916700","full_name":"goccy/kubejob","owner":"goccy","description":"A library for managing Kubernetes Job in Go","archived":false,"fork":false,"pushed_at":"2024-07-22T20:30:04.000Z","size":416,"stargazers_count":46,"open_issues_count":8,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-09T19:42:04.268Z","etag":null,"topics":["go","go-cli","go-library","golang","kubernetes","kubernetes-job"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/goccy.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":"2020-06-25T12:58:21.000Z","updated_at":"2024-08-16T16:45:24.000Z","dependencies_parsed_at":"2024-02-14T08:24:44.148Z","dependency_job_id":"e1d19f26-bb5f-4d3b-a83b-c133f15587d0","html_url":"https://github.com/goccy/kubejob","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccy%2Fkubejob","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccy%2Fkubejob/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccy%2Fkubejob/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/goccy%2Fkubejob/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/goccy","download_url":"https://codeload.github.com/goccy/kubejob/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225110575,"owners_count":17422411,"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":["go","go-cli","go-library","golang","kubernetes","kubernetes-job"],"created_at":"2024-10-13T01:42:13.141Z","updated_at":"2024-11-18T01:04:56.146Z","avatar_url":"https://github.com/goccy.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kubejob\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/goccy/kubejob)](https://pkg.go.dev/github.com/goccy/kubejob)\n![Go](https://github.com/goccy/kubejob/workflows/test/badge.svg)\n[![codecov](https://codecov.io/gh/goccy/kubejob/branch/master/graph/badge.svg)](https://codecov.io/gh/goccy/kubejob)\n\nA library for managing Kubernetes Job in Go\n\n# Features\n\n- Creates a Kubernetes Job\n- Run and wait for Kubernetes Job\n- Capture logs of Kubernetes Job\n- Can use `context.Context` to run Kubernetes Job\n- Delayed execution of Kubernetes Job ( also support Sidecar pattern )\n- Can control execution order ( and timing ) of command for multiple containers at Job\n- Copy any files or directory between local file system and container in Job\n- Can insert any process before the process of the init container\n- Automatically clean up Kubernetes Job\n\n# Installation\n\n```bash\n$ go get github.com/goccy/kubejob\n```\n\n# Synopsis\n\n## Simply create and run and wait Kubernetes Job\n\n```go\n\nimport (\n  \"github.com/goccy/kubejob\"\n  batchv1 \"k8s.io/api/batch/v1\"\n  corev1 \"k8s.io/api/core/v1\"\n  metav1 \"k8s.io/apimachinery/pkg/apis/meta/v1\"\n  \"k8s.io/client-go/rest\"\n)\n\nvar cfg *rest.Config // = rest.InClusterConfig() assign *rest.Config value to cfg\n\njob, err := kubejob.NewJobBuilder(cfg, \"default\").BuildWithJob(\u0026batchv1.Job{\n  ObjectMeta: metav1.ObjectMeta{\n    GenerateName: \"kubejob-\",\n  },\n  Spec: batchv1.JobSpec{\n    Template: corev1.PodTemplateSpec{\n      Spec: corev1.PodSpec{\n        Containers: []corev1.Container{\n          {\n            Name:    \"test\",\n            Image:   \"golang:1.22.0-bookworm\",\n            Command: []string{\"echo\", \"hello\"},\n          },\n        },\n      },\n    },\n  },\n})\nif err != nil {\n  panic(err)\n}\n\n// Run KubernetesJob and output log to stdout.\n// If Job doesn't exit status by `0` , `Run()` returns error.\nif err := job.Run(context.Background()); err != nil {\n\tpanic(err)\n}\n```\n\n## Manage execution timing\n\nIf you don't want to execute the Job immediately after the Pod is `Running` state, you can delay the execution timing.\nAlso, this feature can be used if the Job has multiple containers and you want to control their execution order.\n\n```go\nctx := context.Background()\nif err := job.RunWithExecutionHandler(ctx, func(executors []*kubejob.JobExecutor) error {\n  // callback when the Pod is in Running status.\n  // `executors` corresponds to the list of `Containers` specified in `JobSpec`\n  for _, exec := range executors { \n    // `Exec()` executes the command\n    out, err := exec.Exec()\n    if err != nil {\n      panic(err)\n    }\n    fmt.Println(string(out), err)\n  }\n  return nil\n})\n```\n\n## Manage execution with Sidecar\n\n```go\njob, err := kubejob.NewJobBuilder(cfg, \"default\").BuildWithJob(\u0026batchv1.Job{\n  ObjectMeta: metav1.ObjectMeta{\n    GenerateName: \"kubejob-\",\n  },\n  Spec: batchv1.JobSpec{\n    Template: corev1.PodTemplateSpec{\n      Spec: corev1.PodSpec{\n        Containers: []corev1.Container{\n          {\n            Name:    \"main\",\n            Image:   \"golang:1.22.0-bookworm\",\n            Command: []string{\"echo\", \"hello\"},\n          },\n          {\n            Name:    \"sidecar\",\n            Image:   \"nginx:latest\",\n            Command: []string{\"nginx\"},\n          },\n        },\n      },\n    },\n  },\n})\nif err != nil {\n  panic(err)\n}\nif err := job.RunWithExecutionHandler(context.Background(), func(executors []*kubejob.JobExecutor) error {\n  for _, exec := range executors {\n    if exec.Container.Name == \"sidecar\" {\n      // `ExecAsync()` executes the command and doesn't wait finished.\n      exec.ExecAsync()\n    } else {\n      out, err := exec.Exec()\n      if err != nil {\n        panic(err)\n      }\n      fmt.Pritln(string(out))\n    }\n  }\n  return nil\n})\n```\n\n## Execution with kubejob-agent\n\nNormally, copying and executing commands using JobExecutor is performed using the shell or tar command in the container image and using the Kubernetes API ( `pods/exec` ).\nHowever, if you need to copy large files or run a large number of commands and don't want to overload the Kubernetes API, or if you don't have a shell or tar command in your container image, you can use the `kubejob-agent` method.\n\nkubejob-agent is a CLI tool that can be installed by the following methods.\n\n```console\n$ go install github.com/goccy/kubejob/cmd/kubejob-agent\n```\n\nInclude this tool in your container image, create `kubejob.AgentConfig` with the path where `kubejob-agent` is located, and give it to `kubejob.Job` as follows:\n\n```go\nagentConfig, err := kubejob.NewAgentConfig(map[string]string{\n  \"container-name\": filepath.Join(\"/\", \"bin\", \"kubejob-agent\"),\n})\nif err != nil {\n  return err\n}\njob.UseAgent(agentConfig)\n```\n\nThis will switch from communication using the Kubernetes API to gRPC-based communication with kubejob-agent.\nCommunication with `kubejob-agent` is performed using JWT issued using the RSA Key issued each time Kubernetes Job is started, so requests cannot be sent directly to the container from other processes.\n\n\u003cimg width=\"700px\" src=\"https://user-images.githubusercontent.com/209884/172190425-26f17151-20e9-4e6b-857f-8b9ae230927a.png\"/\u003e\n\n# Requirements\n\n## Role\n\n```yaml\nkind: Role\napiVersion: rbac.authorization.k8s.io/v1\nmetadata:\n  name: kubejob\nrules:\n  - apiGroups:\n      - batch\n    resources:\n      - jobs\n    verbs:\n      - create\n      - delete\n  - apiGroups:\n      - \"\"\n    resources:\n      - pods\n    verbs:\n      - get\n      - list\n      - watch\n      - delete\n  - apiGroups:\n      - \"\"\n    resources:\n      - pods/log\n    verbs:\n      - get\n      - watch\n  - apiGroups:\n      - \"\"\n    resources:\n      - pods/exec\n    verbs:\n      - create # required when using kubejob.JobExecutor without kubejob-agent\n```\n\n# Tools\n\n## cmd/kubejob\n\n### Installation\n\n```bash\ngo install github.com/goccy/kubejob/cmd/kubejob\n```\n\n```console\nUsage:\n  kubejob [OPTIONS]\n\nApplication Options:\n  -n, --namespace= specify namespace (default: default)\n  -f, --file=      specify yaml or json file for written job definition\n  -i, --image=     specify container image\n\nHelp Options:\n  -h, --help       Show this help message\n```\n\n### Example\n\n```bash\n$ kubejob --image golang:1.22.0-bookworm -- go version\ngo version\ngo version go1.22.0 linux/amd64\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoccy%2Fkubejob","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoccy%2Fkubejob","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoccy%2Fkubejob/lists"}