@@ -10,10 +10,11 @@ import (
10
10
"strconv"
11
11
"time"
12
12
"strings"
13
+ "fmt"
13
14
)
14
15
15
16
type kubernetesProviderInterface interface {
16
- Create (podName string , nodeParams nodeParams ) error
17
+ Create (podName string , nodeParams nodeParams ) ( nodeAddress string , err error )
17
18
// idempotent operation
18
19
Destroy (podName string ) error
19
20
}
@@ -24,7 +25,7 @@ type kubDnsProvider struct {
24
25
clientFactory jsonwire.ClientFactoryInterface
25
26
}
26
27
27
- func (p * kubDnsProvider ) Create (podName string , nodeParams nodeParams ) error {
28
+ func (p * kubDnsProvider ) Create (podName string , nodeParams nodeParams ) ( nodeAddress string , err error ) {
28
29
pod := & apiV1.Pod {}
29
30
pod .ObjectMeta .Name = podName
30
31
pod .ObjectMeta .Labels = map [string ]string {"name" : podName }
@@ -34,50 +35,64 @@ func (p *kubDnsProvider) Create(podName string, nodeParams nodeParams) error {
34
35
container .Image = nodeParams .Image
35
36
port , err := strconv .Atoi (nodeParams .Port )
36
37
if err != nil {
37
- return errors .New ("convert to int nodeParams.Port, " + err .Error ())
38
+ return "" , errors .New ("convert to int nodeParams.Port, " + err .Error ())
38
39
}
39
40
container .Ports = []apiV1.ContainerPort {{ContainerPort : int32 (port )}}
40
41
pod .Spec .Containers = append (pod .Spec .Containers , container )
41
42
_ , err = p .clientset .CoreV1Client .Pods (p .namespace ).Create (pod )
42
43
if err != nil {
43
- return errors .New ("send command pod/create to k8s, " + err .Error ())
44
+ return "" , errors .New ("send command pod/create to k8s, " + err .Error ())
44
45
}
45
46
46
- service := & apiV1.Service {}
47
- service .ObjectMeta .Name = podName
48
- service .Spec .ClusterIP = "None"
49
- service .Spec .Ports = []apiV1.ServicePort {{Port : int32 (port )}}
50
- service .Spec .Selector = map [string ]string {"name" : podName }
51
- _ , err = p .clientset .CoreV1Client .Services (p .namespace ).Create (service )
52
- if err != nil {
53
- return errors .New ("send command service/create to k8s, " + err .Error ())
47
+ stopWaitIP := time .After (40 * time .Second )
48
+ log .Debugf ("start waiting pod ip" )
49
+ var createdPodIP string
50
+ LoopWaitIP:
51
+ for {
52
+ select {
53
+ case <- stopWaitIP :
54
+ return "" , fmt .Errorf ("wait podIP stopped by timeout, %v" , podName )
55
+ default :
56
+ time .Sleep (time .Second )
57
+ createdPod , err := p .clientset .CoreV1Client .Pods (p .namespace ).Get (podName )
58
+ if err != nil {
59
+ log .Debugf ("fail get created pod, %v, %v" ,podName , err )
60
+ continue
61
+ }
62
+ if createdPod .Status .PodIP == "" {
63
+ log .Debugf ("empty pod ip, %v" , podName )
64
+ continue
65
+ }
66
+ createdPodIP = createdPod .Status .PodIP
67
+ break LoopWaitIP
68
+ }
54
69
}
55
70
56
71
// todo: пока так ожидаем поднятие ноды, так как не понятно что конкретно означают статусы возвращаемые через апи
57
- client := p .clientFactory .Create (net .JoinHostPort (podName , nodeParams .Port ))
72
+ nodeAddress = net .JoinHostPort (createdPodIP , nodeParams .Port )
73
+ client := p .clientFactory .Create (nodeAddress )
58
74
stop := time .After (40 * time .Second )
59
- log .Debugln ("start waiting" )
60
- Loop :
75
+ log .Debugln ("start waiting selenium " )
76
+ LoopWaitSelenium :
61
77
for {
62
78
select {
63
79
case <- stop :
64
- return errors . New ("wait stopped by timeout" )
80
+ return "" , fmt . Errorf ("wait selenium stopped by timeout, %v" , podName )
65
81
default :
66
82
time .Sleep (time .Second )
67
- log .Debugln ("start request" )
68
83
message , err := client .Health ()
69
84
if err != nil {
70
85
log .Debugf ("fail request, %v" , err )
71
86
continue
72
87
}
73
88
log .Debugf ("done request, status: %v" , message .Status )
74
89
if message .Status == 0 {
75
- break Loop
90
+ break LoopWaitSelenium
76
91
}
77
92
}
78
93
}
79
94
80
- return nil
95
+ return nodeAddress , nil
81
96
}
82
97
83
98
//Destroy - destroy all pod data (idempotent operation)
@@ -90,12 +105,5 @@ func (p *kubDnsProvider) Destroy(podName string) error {
90
105
err = errors .New ("send command pod/delete to k8s, " + err .Error ())
91
106
return err
92
107
}
93
- err = p .clientset .CoreV1Client .Services (p .namespace ).Delete (podName , & apiV1.DeleteOptions {})
94
- switch {
95
- case err != nil && strings .Contains (err .Error (), "not found" ):
96
- // service already deleted
97
- case err != nil :
98
- return errors .New ("send command service/delete to k8s, " + err .Error ())
99
- }
100
108
return nil
101
109
}
0 commit comments