-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
77 lines (67 loc) · 2.16 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright © 2019 Peter Kurfer peter.kurfer@googlemail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8s
import (
"context"
"fmt"
"github.com/baez90/psdoom-containers/internal/pkg/api/k8s/generated"
"github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"strconv"
)
func NewK8sAPIServer(pr PodRegistry) k8sApi.K8SDaemonServer {
client, err := GetKubeClient()
if err != nil {
panic(err)
}
return &k8sDaemonServer{
podRegistry: pr,
client: client,
}
}
type k8sDaemonServer struct {
podRegistry PodRegistry
client *kubernetes.Clientset
}
func (ds *k8sDaemonServer) GetPods(context.Context, *k8sApi.Empty) (*k8sApi.Pods, error) {
logrus.Info("Processing GetPods() request...")
pods := make([]*k8sApi.Pod, 0)
ds.podRegistry.ForEach(func(key string, pod v1.Pod) {
pods = append(pods, &k8sApi.Pod{
Name: pod.Name,
Namespace: pod.Namespace,
Id: key,
})
})
return &k8sApi.Pods{
Pods: pods,
}, nil
}
func (ds *k8sDaemonServer) KillPod(ctx context.Context, podDel *k8sApi.PodDeletion) (*k8sApi.Empty, error) {
logrus.Infof("Processing KillPod() request for ID %s", podDel.PodId)
pod, exists := ds.podRegistry.GetPod(strconv.Itoa(int(podDel.PodId)))
if !exists {
return &k8sApi.Empty{}, fmt.Errorf("no pod with given id %s/%s found", pod.Namespace, pod.Name)
}
go func() {
// run the deletion asynchronous to ensure that client is not blocked
err := ds.client.CoreV1().Pods(pod.Namespace).Delete(pod.Name, &v1meta.DeleteOptions{})
if err != nil {
logrus.Error("Error while deleting pod", err)
}
}()
return &k8sApi.Empty{}, nil
}