-
Notifications
You must be signed in to change notification settings - Fork 13
/
runtimeclass.go
82 lines (64 loc) · 2.42 KB
/
runtimeclass.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
78
79
80
81
82
package globalhelper
import (
"context"
"fmt"
"time"
"github.com/golang/glog"
. "github.com/onsi/gomega"
nodev1 "k8s.io/api/node/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func CreateRunTimeClass(rtc *nodev1.RuntimeClass) error {
return createRunTimeClass(GetAPIClient().K8sClient, rtc)
}
func createRunTimeClass(client kubernetes.Interface, rtc *nodev1.RuntimeClass) error {
rtc, err := client.NodeV1().RuntimeClasses().Create(context.TODO(), rtc, metav1.CreateOptions{})
if k8serrors.IsAlreadyExists(err) {
glog.V(5).Info(fmt.Sprintf("runtimeclass %s already created", rtc.Name))
} else if err != nil {
return fmt.Errorf("failed to create runtimeclass %q (ns %s): %w", rtc.Name, rtc.Namespace, err)
}
Eventually(func() bool {
rtcCreated, err := isRtcCreated(client, rtc)
if err != nil {
glog.V(5).Info(fmt.Sprintf("rtc %s was not created, retry in %d seconds", rtc.Name, retryInterval))
return false
}
return rtcCreated
}, retryInterval*time.Minute, retryInterval*time.Second).Should(Equal(true), "rtc was not created")
return nil
}
func isRtcCreated(client kubernetes.Interface, rtc *nodev1.RuntimeClass) (bool, error) {
rtc, err := client.NodeV1().RuntimeClasses().Get(context.TODO(), rtc.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
return len(rtc.UID) != 0, nil
}
func DeleteRunTimeClass(rtc *nodev1.RuntimeClass) error {
return deleteRunTimeClass(GetAPIClient().K8sClient, rtc)
}
func deleteRunTimeClass(client kubernetes.Interface, rtc *nodev1.RuntimeClass) error {
err := client.NodeV1().RuntimeClasses().Delete(context.TODO(), rtc.Name, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete runtimeclass %q (ns %s): %w", rtc.Name, rtc.Namespace, err)
}
Eventually(func() bool {
rtcDeleted, err := isRtcDeleted(client, rtc)
if err != nil {
glog.V(5).Info(fmt.Sprintf("rtc %s was not deleted, retry in %d seconds", rtc.Name, retryInterval))
return false
}
return rtcDeleted
}, retryInterval*time.Minute, retryInterval*time.Second).Should(Equal(true), "rtc was not deleted")
return nil
}
func isRtcDeleted(client kubernetes.Interface, rtc *nodev1.RuntimeClass) (bool, error) {
_, err := client.NodeV1().RuntimeClasses().Get(context.TODO(), rtc.Name, metav1.GetOptions{})
if err != nil && k8serrors.IsNotFound(err) {
return true, nil
}
return false, err
}