-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwok.go
55 lines (45 loc) · 1.51 KB
/
kwok.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
package kwok
import (
"context"
"errors"
"fmt"
"github.com/dbschenker/node-undertaker/pkg/nodeundertaker/config"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"regexp"
)
type KwokCloudProvider struct {
K8sClient kubernetes.Interface
}
func CreateCloudProvider(ctx context.Context, cfg *config.Config) (KwokCloudProvider, error) {
log.Warnf("Kwok cloud provider should be used only for development and testing. This provider is not intended for production use.")
ret := KwokCloudProvider{}
ret.K8sClient = cfg.K8sClient
var err error = nil
return ret, err
}
func (p KwokCloudProvider) ValidateConfig() error {
return nil
}
func (p KwokCloudProvider) TerminateNode(ctx context.Context, cloudProviderNodeId string) (string, error) {
re, err := regexp.Compile("^kwok://(.+)$")
if err != nil {
return "InstanceTerminationFailed", err
}
matches := re.FindStringSubmatch(cloudProviderNodeId)
if len(matches) != 2 {
return "InstanceTerminationFailed", fmt.Errorf("couldn't parse providerId: %s", cloudProviderNodeId)
}
if p.K8sClient == nil {
return "Instance Termination Failed", errors.New("K8sclient is nil")
}
err = p.K8sClient.CoreV1().Nodes().Delete(ctx, matches[1], metav1.DeleteOptions{})
if err != nil {
return "Instance Termination Failed", err
}
return "Instance Terminated", nil
}
func (p KwokCloudProvider) PrepareTermination(ctx context.Context, cloudProviderNodeId string) (string, error) {
return "No preparation required", nil
}