Skip to content

Instantly share code, notes, and snippets.

@mmontes11
Last active February 22, 2023 21:59
Show Gist options
  • Save mmontes11/9760469f41432b7ac2e46b8ea1793138 to your computer and use it in GitHub Desktop.
Save mmontes11/9760469f41432b7ac2e46b8ea1793138 to your computer and use it in GitHub Desktop.
Worker controller resource
import (
echo "github.com/mmontes11/echoperator/pkg/echo"
echov1alpha1 "github.com/mmontes11/echoperator/pkg/echo/v1alpha1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func createJob(newEcho *echov1alpha1.Echo, namespace string) *batchv1.Job {
return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: newEcho.ObjectMeta.Name,
Namespace: namespace,
Labels: make(map[string]string),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(
newEcho,
echov1alpha1.SchemeGroupVersion.WithKind(echo.EchoKind),
),
},
},
Spec: createJobSpec(newEcho.Name, namespace, newEcho.Spec.Message),
}
}
func createJobSpec(name, namespace, msg string) batchv1.JobSpec {
return batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
GenerateName: name + "-",
Namespace: namespace,
Labels: make(map[string]string),
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: name,
Image: "busybox:1.33.1",
Command: []string{"echo", msg},
ImagePullPolicy: "IfNotPresent",
},
},
RestartPolicy: corev1.RestartPolicyNever,
},
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment