Add Stop function and WaitForState to Job. Reove kubernetesConfig as a function.
This commit is contained in:
+37
-15
@@ -1,6 +1,8 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/client"
|
||||
|
||||
@@ -10,20 +12,8 @@ import (
|
||||
type Job struct {
|
||||
Host string
|
||||
Pod *api.Pod
|
||||
}
|
||||
|
||||
func (t Job) kubernetesClient() (*client.Client, error) {
|
||||
client, err := client.New(
|
||||
&client.Config{
|
||||
Host: t.Host,
|
||||
Version: "v1",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return client, err
|
||||
}
|
||||
|
||||
return client, err
|
||||
client *client.Client
|
||||
}
|
||||
|
||||
func (t Job) String() string {
|
||||
@@ -32,20 +22,52 @@ func (t Job) String() string {
|
||||
|
||||
func (t Job) Start() error {
|
||||
log.Print("Executing job: ", t)
|
||||
client, err := t.kubernetesClient()
|
||||
|
||||
client, err := client.New(
|
||||
&client.Config{
|
||||
Host: t.Host,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.client = client
|
||||
|
||||
log.Print("Creating pod...")
|
||||
pod, err := client.Pods(t.Pod.ObjectMeta.Namespace).Create(t.Pod)
|
||||
pod, err := t.client.Pods(t.Pod.ObjectMeta.Namespace).Create(t.Pod)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.Pod = pod
|
||||
|
||||
log.Print("Successfully scheduled job on cluster with name: ", pod.ObjectMeta.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Job) Stop() error {
|
||||
return t.client.Pods(t.Pod.ObjectMeta.Namespace).Delete(t.Pod.ObjectMeta.Name, nil)
|
||||
}
|
||||
|
||||
func (t Job) WaitForState(targetState api.PodPhase) error {
|
||||
Loop:
|
||||
for {
|
||||
pod, err := t.client.Pods(t.Pod.ObjectMeta.Namespace).Get(t.Pod.ObjectMeta.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Switch:
|
||||
switch pod.Status.Phase {
|
||||
case targetState:
|
||||
break Loop
|
||||
default:
|
||||
break Switch
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user