Skip to content

Commit

Permalink
add advanced ngrok feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zufardhiyaulhaq committed Jul 16, 2020
1 parent 0869295 commit 032eaff
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ ENV OPERATOR=/usr/local/bin/ngrok-operator \
COPY build/_output/bin/ngrok-operator ${OPERATOR}

COPY build/bin /usr/local/bin
COPY templates /templates

RUN /usr/local/bin/user_setup

ENTRYPOINT ["/usr/local/bin/entrypoint"]
Expand Down
10 changes: 10 additions & 0 deletions deploy/crds/ngrok.com_ngroks_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ spec:
spec:
description: NgrokSpec defines the desired state of Ngrok
properties:
authtoken:
type: string
hostname:
type: string
port:
format: int32
type: integer
protocol:
default: http
enum:
- http
- tcp
type: string
service:
type: string
required:
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/ngrok/v1alpha1/ngrok_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
type NgrokSpec struct {
Service string `json:"service"`
Port int32 `json:"port"`

// +kubebuilder:validation:Enum=http;tcp
// +kubebuilder:default:=http
// +optional
Protocol string `json:"protocol"`

// +optional
AuthToken string `json:"authtoken"`

// +optional
Hostname string `json:"hostname"`
}

// NgrokStatus defines the observed state of Ngrok
Expand Down
26 changes: 21 additions & 5 deletions pkg/controller/ngrok/ngrok_controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ngrok

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"text/template"
"time"

ngrokv1alpha1 "github.com/zufardhiyaulhaq/ngrok-operator/pkg/apis/ngrok/v1alpha1"
Expand Down Expand Up @@ -202,9 +203,7 @@ func (r *ReconcileNgrok) Reconcile(request reconcile.Request) (reconcile.Result,

func newNgrokConfigMap(cr *ngrokv1alpha1.Ngrok) *corev1.ConfigMap {
configMapData := make(map[string]string, 0)
ngrokProperties := `
web_addr: 0.0.0.0:4040`
configMapData["ngrok.conf"] = ngrokProperties
configMapData["ngrok.conf"] = generateConfiguration(cr)

labels := map[string]string{
"app": cr.Name,
Expand Down Expand Up @@ -236,7 +235,7 @@ func newNgrokPod(cr *ngrokv1alpha1.Ngrok) *corev1.Pod {
{
Name: "ngrok",
Image: "wernight/ngrok",
Command: []string{"ngrok", "http", "--config", "/ngrok/ngrok.conf", cr.Spec.Service + ":" + strconv.FormatInt(int64(cr.Spec.Port), 10)},
Command: []string{"ngrok", "--config", "/ngrok/ngrok.conf", "--all"},
Ports: []corev1.ContainerPort{
{ContainerPort: ngrokPort},
},
Expand All @@ -263,3 +262,20 @@ func newNgrokPod(cr *ngrokv1alpha1.Ngrok) *corev1.Pod {
},
}
}

func generateConfiguration(cr *ngrokv1alpha1.Ngrok) string {
var output bytes.Buffer
tmpl := "template/configuration.tmpl"

tpl, err := template.ParseFiles(tmpl)
if err != nil {
panic(err)
}

err = tpl.Execute(&output, cr)
if err != nil {
panic(err)
}

return output.String()
}
13 changes: 13 additions & 0 deletions templates/configuration.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
web_addr: 0.0.0.0:4040

{{if .Spec.AuthToken }}
authtoken: {{ .Spec.AuthToken }}
{{end}}

tunnels:
app:
proto: {{ .Spec.Protocol }}
addr: {{ .Spec.Service }}:{{ .Spec.Port }}
{{if .Spec.AuthToken }}{{if .Spec.Hostname }}
hostname: {{ .Spec.Hostname }}
{{end}}{{end}}

0 comments on commit 032eaff

Please sign in to comment.