| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 1 | package events |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| giolekva | ede6d2b | 2020-05-05 22:14:16 +0400 | [diff] [blame^] | 6 | "time" |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 7 | |
| 8 | apiv1 "k8s.io/api/core/v1" |
| 9 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | corev1 "k8s.io/client-go/kubernetes/typed/core/v1" |
| 11 | |
| 12 | "github.com/golang/glog" |
| 13 | // "github.com/itaysk/regogo" |
| 14 | ) |
| 15 | |
| 16 | type Processor interface { |
| 17 | Start() |
| 18 | } |
| 19 | |
| 20 | // Implements processor |
| 21 | type singleEventAtATimeProcessor struct { |
| giolekva | ede6d2b | 2020-05-05 22:14:16 +0400 | [diff] [blame^] | 22 | store EventStore |
| 23 | pods corev1.PodInterface |
| 24 | pcloudApi string |
| 25 | // TODO(giolekva): Nodes themselves should be associated with object store |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 26 | objectStoreApi string |
| 27 | } |
| 28 | |
| 29 | func NewSingleEventAtATimeProcessor( |
| 30 | store EventStore, |
| 31 | pods corev1.PodInterface, |
| 32 | pcloudApi, objectStoreApi string) Processor { |
| 33 | return &singleEventAtATimeProcessor{store, pods, pcloudApi, objectStoreApi} |
| 34 | } |
| 35 | |
| 36 | func (p *singleEventAtATimeProcessor) Start() { |
| 37 | for { |
| giolekva | ede6d2b | 2020-05-05 22:14:16 +0400 | [diff] [blame^] | 38 | select { |
| 39 | case <-time.After(30 * time.Second): |
| 40 | events, err := p.store.GetEventsInState(EventStateNew) |
| 41 | if err != nil { |
| 42 | glog.Error(err) |
| 43 | continue |
| 44 | } |
| 45 | if len(events) == 0 { |
| 46 | continue |
| 47 | } |
| 48 | event := events[0] |
| 49 | pod := createPod(event.NodeId, p.pcloudApi, p.objectStoreApi) |
| 50 | glog.Info("Creating pod...") |
| 51 | resp, err := p.pods.Create(context.TODO(), pod, metav1.CreateOptions{}) |
| 52 | if err != nil { |
| 53 | glog.Error(resp) |
| 54 | continue |
| 55 | } |
| 56 | glog.Infof("Pod created: %s", resp) |
| 57 | // TODO(giolekva): do not ignore error |
| 58 | _ = monitorPod(resp, p.pods) |
| 59 | p.store.MarkEventDone(event) |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 60 | } |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | func isInTerminalState(pod *apiv1.Pod) bool { |
| 65 | return pod.Status.Phase == apiv1.PodSucceeded || |
| 66 | pod.Status.Phase == apiv1.PodFailed |
| 67 | } |
| 68 | |
| 69 | func monitorPod(pod *apiv1.Pod, pods corev1.PodInterface) error { |
| 70 | w, err := pods.Watch(context.TODO(), metav1.SingleObject(pod.ObjectMeta)) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | for { |
| 75 | select { |
| 76 | case events, ok := <-w.ResultChan(): |
| 77 | if !ok { |
| 78 | return nil |
| 79 | } |
| 80 | p := events.Object.(*apiv1.Pod) |
| giolekva | ede6d2b | 2020-05-05 22:14:16 +0400 | [diff] [blame^] | 81 | glog.Infof("Pod status: %s", pod.Status.Phase) |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 82 | if isInTerminalState(p) { |
| giolekva | ede6d2b | 2020-05-05 22:14:16 +0400 | [diff] [blame^] | 83 | glog.Info("Pod is DONE") |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 84 | w.Stop() |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | func createPod(id string, pcloudApi string, objectStoreApi string) *apiv1.Pod { |
| 92 | return &apiv1.Pod{ |
| 93 | ObjectMeta: metav1.ObjectMeta{ |
| 94 | Name: fmt.Sprintf("event-%s", id)}, |
| 95 | Spec: apiv1.PodSpec{ |
| giolekva | 1443803 | 2020-05-05 18:23:13 +0400 | [diff] [blame] | 96 | RestartPolicy: apiv1.RestartPolicyNever, |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 97 | Containers: []apiv1.Container{{ |
| 98 | Name: "event", |
| 99 | Image: "giolekva/face-detector:latest", |
| giolekva | 1443803 | 2020-05-05 18:23:13 +0400 | [diff] [blame] | 100 | ImagePullPolicy: apiv1.PullAlways, |
| giolekva | ede6d2b | 2020-05-05 22:14:16 +0400 | [diff] [blame^] | 101 | Command: []string{"python3", "main.py"}, |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 102 | Args: []string{pcloudApi, objectStoreApi, id}}}}} |
| giolekva | f89e046 | 2020-05-02 17:47:06 +0400 | [diff] [blame] | 103 | } |