Skip to content

Commit

Permalink
Add support for kind Job (#370)
Browse files Browse the repository at this point in the history
Signed-off-by: Kazuki Suda <kazuki.suda@gmail.com>
  • Loading branch information
superbrothers committed Nov 1, 2021
1 parent 0ef3005 commit 6cc7a75
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/k8sinternal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func GetAllResources(clientset kubernetes.Interface, options ClientOptions) []k8
resources = append(resources, GetServiceAccounts(clientset, options)...)
resources = append(resources, GetNamespaces(clientset, options)...)
resources = append(resources, GetServices(clientset, options)...)
resources = append(resources, GetJobs(clientset, options)...)

resources = excludeGenerated(resources)

Expand Down Expand Up @@ -345,6 +346,26 @@ func GetServices(clientset kubernetes.Interface, option ClientOptions) []k8s.Res
return services
}

// GetJobs gets all Job resources from the cluster
func GetJobs(clientset kubernetes.Interface, options ClientOptions) []k8s.Resource {
jobClient := clientset.BatchV1().Jobs(options.Namespace)
jobList, err := jobClient.List(context.Background(), k8s.ListOptionsV1{})

if err != nil {
log.Error(err)
return nil
}

jobs := make([]k8s.Resource, 0, len(jobList.Items))
for _, job := range jobList.Items {
// For some reason the kubernetes SDK doesn't populate the type meta so we populate it manually
job.TypeMeta = k8s.NewJob().TypeMeta
jobs = append(jobs, job.DeepCopyObject())
}

return jobs
}

// GetKubernetesVersion returns the kubernetes client version
func GetKubernetesVersion(clientset kubernetes.Interface) (*version.Info, error) {
discoveryClient := clientset.Discovery()
Expand Down
1 change: 1 addition & 0 deletions internal/k8sinternal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestGetAllResources(t *testing.T) {
k8s.NewCronJob(),
k8s.NewServiceAccount(),
k8s.NewService(),
k8s.NewJob(),
}
namespaces := []string{"foo", "bar"}

Expand Down
21 changes: 21 additions & 0 deletions internal/test/fixtures/all_resources/job.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
kind: Namespace
metadata:
name: job

---
apiVersion: batch/v1
kind: Job
metadata:
name: job
namespace: job
spec:
template:
spec:
restartPolicy: Never
hostPID: true
hostIPC: true
hostNetwork: true
containers:
- name: container
image: scratch
4 changes: 4 additions & 0 deletions pkg/k8s/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func GetObjectMeta(resource Resource) *ObjectMetaV1 {
return &kubeType.ObjectMeta
case *DeploymentV1Beta2:
return &kubeType.ObjectMeta
case *JobV1:
return &kubeType.ObjectMeta
case *PodTemplateV1:
return &kubeType.ObjectMeta
case *ReplicationControllerV1:
Expand Down Expand Up @@ -137,6 +139,8 @@ func GetPodTemplateSpec(resource Resource) *PodTemplateSpecV1 {
return &kubeType.Spec.Template
case *DeploymentV1Beta2:
return &kubeType.Spec.Template
case *JobV1:
return &kubeType.Spec.Template
case *PodTemplateV1:
return &kubeType.Template
case *ReplicationControllerV1:
Expand Down
14 changes: 14 additions & 0 deletions pkg/k8s/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,17 @@ func NewService() *ServiceV1 {
ObjectMeta: ObjectMetaV1{},
}
}

// NewJob creates a new Job resource
func NewJob() *JobV1 {
return &JobV1{
TypeMeta: TypeMetaV1{
Kind: "Job",
APIVersion: "batch/v1",
},
ObjectMeta: ObjectMetaV1{},
Spec: JobSpecV1{
Template: podTemplateSpec,
},
}
}
4 changes: 4 additions & 0 deletions pkg/k8s/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ type JobTemplateSpecV1Beta1 = batchv1beta1.JobTemplateSpec
// JobSpecV1 is a type alias for the v1 version of the k8s batch API.
type JobSpecV1 = batchv1.JobSpec

// JobV1 is a type alias for the v1 version of the k8s batch API.
type JobV1 = batchv1.Job

// ListOptionsV1 is a type alias for the v1 version of the k8s meta API.
type ListOptionsV1 = metav1.ListOptions

Expand Down Expand Up @@ -136,6 +139,7 @@ func IsSupportedResourceType(obj Resource) bool {
case *CronJobV1Beta1,
*DaemonSetV1, *DaemonSetV1Beta1, *DaemonSetV1Beta2,
*DeploymentExtensionsV1Beta1, *DeploymentV1, *DeploymentV1Beta1, *DeploymentV1Beta2,
*JobV1,
*NamespaceV1,
*NetworkPolicyV1,
*PodV1,
Expand Down

0 comments on commit 6cc7a75

Please sign in to comment.