Skip to content

Commit

Permalink
Added liveness and kill-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
kainlite committed Jan 19, 2020
1 parent 3244c01 commit 2a14273
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ deploy: manifests
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl apply -f -

kill-deploy: manifests
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl delete -f -

# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ spec:
host: 10.244.0.8
port: 8000
protocol: tcp
liveness_probe: true
```

Then just do the port-forward:
Expand Down Expand Up @@ -82,7 +83,6 @@ Then type something and hit enter, it should show up in the first nc.
* Add/fix tests.
* Add proper error if the protocol doesn't match the supported ones.
* Review RBAC permissions, we need minimal permissions anyway.
* Add health check with the values of the remote endpoint and port.
* Add possibility to use a different port to listen.
* Another possible method in the future will be with SAML2 or OIDC to provide an authentication method, but this is looking way further into the future.
* Turn all of this bullet points into github issues.
Expand Down
3 changes: 3 additions & 0 deletions api/v1beta1/map_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type MapSpec struct {

// Host
Host string `json:"host,omitempty"`

// LivenessProbe
LivenessProbe bool `json:"liveness_probe"`
}

// MapStatus defines the observed state of Map
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/forward.techsquad.rocks_maps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ spec:
host:
description: Host
type: string
liveness_probe:
description: LivenessProbe
type: boolean
port:
description: Port
type: integer
protocol:
description: TCP/UDP protocol
type: string
required:
- liveness_probe
type: object
status:
description: MapStatus defines the observed state of Map
Expand Down
17 changes: 17 additions & 0 deletions controllers/map_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func newPodForCR(cr *forwardv1beta1.Map) *corev1.Pod {
command = fmt.Sprintf("socat -V")
}

var livenessCommand string
if cr.Spec.LivenessProbe == true {
livenessCommand = fmt.Sprintf("nc -v -n -z %s %s", cr.Spec.Host, strconv.Itoa(cr.Spec.Port))
} else {
livenessCommand = fmt.Sprintf("echo")
}

// Learn more at: https://godoc.org/k8s.io/api/core/v1
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "forward-" + cr.Name + "-pod",
Expand All @@ -75,6 +83,13 @@ func newPodForCR(cr *forwardv1beta1.Map) *corev1.Pod {
Name: "map",
Image: "alpine/socat",
Command: strings.Split(command, " "),
LivenessProbe: &corev1.Probe{
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: strings.Split(livenessCommand, " "),
},
},
},
},
},
RestartPolicy: corev1.RestartPolicyOnFailure,
Expand Down Expand Up @@ -110,6 +125,8 @@ func (r *MapReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
reqLogger.Info("Phase: PENDING")
reqLogger.Info("Waiting to forward", "Host", instance.Spec.Host, "Port", instance.Spec.Port)
instance.Status.Phase = forwardv1beta1.PhaseRunning
// If we ever get here, just requeue the request
return reconcile.Result{}, err
case forwardv1beta1.PhaseRunning:
reqLogger.Info("Phase: RUNNING")
pod := newPodForCR(instance)
Expand Down

0 comments on commit 2a14273

Please sign in to comment.