Skip to content

Commit 48ba5e7

Browse files
Add TCP probe and fix default values (#18)
* Add TCP probe and fix default values Signed-off-by: Jacob Tomlinson <[email protected]> * Remove development tweaks Signed-off-by: Jacob Tomlinson <[email protected]> * Add TCPSocket probe docs Signed-off-by: Jacob Tomlinson <[email protected]>
1 parent 8996838 commit 48ba5e7

File tree

6 files changed

+105
-7
lines changed

6 files changed

+105
-7
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Container Canary is a tool for recording those requirements as a manifest that c
3232
- [Checks](#checks)
3333
- [Exec](#exec)
3434
- [HTTPGet](#httpget)
35+
- [TCPSocket](#tcpsocket)
3536
- [Delays, timeouts, periods and thresholds](#delays-timeouts-periods-and-thresholds)
3637
- [Contributing](#contributing)
3738
- [Maintaining](#maintaining)
@@ -253,6 +254,19 @@ checks:
253254
value: "*"
254255
```
255256

257+
#### TCPSocket
258+
259+
A TCP Socket check will ensure something is listening on a specific TCP port.
260+
261+
```yaml
262+
checks:
263+
- name: tcp
264+
description: Is listening via TCP on port 80
265+
probe:
266+
tcpSocket:
267+
port: 80
268+
```
269+
256270
#### Delays, timeouts, periods and thresholds
257271

258272
Checks also support the same delays, timeouts, periods and thresholds that Kubernetes probes do.

examples/dask-scheduler.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: container-canary.nvidia.com/v1
2+
kind: Validator
3+
name: dask-scheduler
4+
description: Dask Scheduler
5+
documentation:
6+
command:
7+
- dask-scheduler
8+
ports:
9+
- port: 8786
10+
protocol: TCP
11+
- port: 8787
12+
protocol: TCP
13+
checks:
14+
- name: dashboard
15+
description: 🌏 Exposes the Dashboard on port 8787
16+
probe:
17+
httpGet:
18+
path: /
19+
port: 8787
20+
failureThreshold: 30
21+
- name: comm
22+
description: ⛓ Exposes Dask comm on port 8786
23+
probe:
24+
tcpSocket:
25+
port: 8786
26+
failureThreshold: 30

internal/apis/v1/types.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,31 @@ type Probe struct {
7474

7575
FailureThreshold int `yaml:"failureThreshold"`
7676

77-
TerminationGracePeriodSeconds *int `yaml:"terminationGracePeriodSeconds"`
77+
TerminationGracePeriodSeconds int `yaml:"terminationGracePeriodSeconds"`
7878

7979
Exec *v1.ExecAction `yaml:"exec"`
8080

8181
HTTPGet *HTTPGetAction `yaml:"httpGet"`
82+
83+
TCPSocket *TCPSocketAction `yaml:"tcpSocket" `
84+
}
85+
86+
func (p *Probe) UnmarshalYAML(unmarshal func(interface{}) error) error {
87+
type rawProbe Probe
88+
raw := rawProbe{
89+
InitialDelaySeconds: 0,
90+
TimeoutSeconds: 30,
91+
PeriodSeconds: 1,
92+
SuccessThreshold: 1,
93+
FailureThreshold: 1,
94+
TerminationGracePeriodSeconds: 30,
95+
}
96+
if err := unmarshal(&raw); err != nil {
97+
return err
98+
}
99+
100+
*p = Probe(raw)
101+
return nil
82102
}
83103

84104
type HTTPGetAction struct {
@@ -88,10 +108,6 @@ type HTTPGetAction struct {
88108
// Number of the port to access on the container.
89109
// Number must be in the range 1 to 65535.
90110
Port int `json:"port"`
91-
// Host name to connect to, defaults to the pod IP. You probably want to set
92-
// "Host" in httpHeaders instead.
93-
// +optional
94-
Host string `json:"host,omitempty"`
95111
// Scheme to use for connecting to the host.
96112
// Defaults to HTTP.
97113
// +optional
@@ -104,6 +120,12 @@ type HTTPGetAction struct {
104120
ResponseHTTPHeaders []v1.HTTPHeader `json:"responseHttpHeaders,omitempty"`
105121
}
106122

123+
type TCPSocketAction struct {
124+
// Number or name of the port to access on the container.
125+
// Number must be in the range 1 to 65535.
126+
Port int `json:"port"`
127+
}
128+
107129
type Volume struct {
108130
// Path to mount in the container
109131
MountPath string `yaml:"mountPath,omitempty"`

internal/validator/httpget.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func HTTPGetCheck(c container.ContainerInterface, probe *canaryv1.Probe) (bool,
3131
client := &http.Client{}
3232
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d%s", action.Port, action.Path), nil)
3333
if err != nil {
34-
fmt.Println(err.Error())
3534
return false, nil
3635
}
3736
req.Close = true
@@ -41,7 +40,6 @@ func HTTPGetCheck(c container.ContainerInterface, probe *canaryv1.Probe) (bool,
4140
}
4241
resp, err := client.Do(req)
4342
if err != nil {
44-
fmt.Println(err.Error())
4543
return false, nil
4644
}
4745
for _, header := range action.ResponseHTTPHeaders {

internal/validator/tcp.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright (c) <2022> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package validator
19+
20+
import (
21+
"fmt"
22+
"net"
23+
24+
canaryv1 "github.com/nvidia/container-canary/internal/apis/v1"
25+
"github.com/nvidia/container-canary/internal/container"
26+
)
27+
28+
func TCPSocketCheck(c container.ContainerInterface, probe *canaryv1.Probe) (bool, error) {
29+
action := probe.TCPSocket
30+
address := fmt.Sprintf("localhost:%d", action.Port)
31+
_, err := net.Dial("tcp", address)
32+
if err != nil {
33+
return false, nil
34+
}
35+
return true, nil
36+
}

internal/validator/validator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func runCheck(results chan<- checkResult, c container.ContainerInterface, check
9393
p, err = executeCheck(ExecCheck, c, &check.Probe)
9494
} else if check.Probe.HTTPGet != nil {
9595
p, err = executeCheck(HTTPGetCheck, c, &check.Probe)
96+
} else if check.Probe.TCPSocket != nil {
97+
p, err = executeCheck(TCPSocketCheck, c, &check.Probe)
9698
} else {
9799
results <- checkResult{false, fmt.Errorf("check '%s' has no known probes", check.Name)}
98100
return

0 commit comments

Comments
 (0)