Skip to content

Commit

Permalink
Merge pull request #2 from zufardhiyaulhaq/feature/advanced-ngrok-1
Browse files Browse the repository at this point in the history
Feature/advanced ngrok & release 0.0.2
  • Loading branch information
zufardhiyaulhaq authored Jul 16, 2020
2 parents 0869295 + f2b87a7 commit e2a084c
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 16 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
Ngrok operator provide developer easy access to private Kubernetes cluster for testing purpose via ngrok. Automate the creation of ngrok tunnel via CRDs!

### Feature
- [x] basic ngrok feature
- [x] support HTTP
- [ ] support TCP
- [ ] support Costum Configuration
- [x] support TCP
- [x] support costum configuration
- [x] custom domain
- [x] custom TCP address
- [x] custom region
- [x] enable/disable inspection
- [x] support HTTP auth
- [ ] service for ngrok object (dashboard related)

### Developing ngrok-operator
This operator build based on [operator-sdk](https://sdk.operatorframework.io/docs/install-operator-sdk/). To build this operator, you need [operator-sdk](https://sdk.operatorframework.io/docs/install-operator-sdk/).
Expand Down Expand Up @@ -38,11 +43,13 @@ kubectl apply -f examples/helloworld/
```
kubectl get ngrok --all-namespaces
NAMESPACE NAME STATUS URL
default nginx-ngrok created https://d5150f7c3588.ngrok.io
helloworld helloworld-ngrok created https://fa03f71fbe18.ngrok.io
default nginx-ngrok created https://9496e56ed0bc.ngrok.io
default nginx-ngrok-full created https://ngrok.zufardhiyaulhaq.com
helloworld helloworld-ngrok created https://d00ba8cb0b95.ngrok.io
```
- access the URL
```
https://d5150f7c3588.ngrok.io
https://ngrok.zufardhiyaulhaq.com
https://fa03f71fbe18.ngrok.io/hello
```
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
4 changes: 2 additions & 2 deletions charts/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
apiVersion: v1
name: ngrok-operator
type: application
version: 0.0.1
appVersion: 0.0.1
version: 0.0.2
appVersion: 0.0.2
description: ngrok-operator for managing ngrok lifecycle
home: https://github.com/zufardhiyaulhaq/ngrok-operator
keywords:
Expand Down
4 changes: 2 additions & 2 deletions charts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Helm chart for ngrok-operators
### Installing the charts
From root directory of ngrok-operator. Please edit the values.yaml inside charts before applying.
```
helm install ./charts --name ngrok-operator
helm install ./charts --name-template ngrok-operator
```

### Configuration

| Parameter | Description | Default |
|-|-| -|
| operator.image | Image for ngrok-operator | zufardhiyaulhaq/ngrok-operator |
| operator.tag | Tag for image ngrok-operator | 0.0.1 |
| operator.tag | Tag for image ngrok-operator | 0.0.2 |
| operator.pullPolicy | pullPolicy | Always |
| operator.replica | number of replica | 1 |

Expand Down
27 changes: 27 additions & 0 deletions charts/crds/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,36 @@ spec:
spec:
description: NgrokSpec defines the desired state of Ngrok
properties:
auth:
type: string
authtoken:
type: string
hostname:
type: string
inspect:
default: false
type: boolean
port:
format: int32
type: integer
protocol:
default: http
enum:
- http
- tcp
type: string
region:
enum:
- us
- eu
- ap
- au
- sa
- jp
- in
type: string
remote_addr:
type: string
service:
type: string
required:
Expand Down
2 changes: 1 addition & 1 deletion charts/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ operator:
# image of ngrok-operator
image: "zufardhiyaulhaq/ngrok-operator"
# tag of ngrok-operator image
tag: "0.0.1"
tag: "0.0.2"
# pullPolicy for operator image
pullPolicy: "Always"
# number of replica for deployment
Expand Down
27 changes: 27 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,36 @@ spec:
spec:
description: NgrokSpec defines the desired state of Ngrok
properties:
auth:
type: string
authtoken:
type: string
hostname:
type: string
inspect:
default: false
type: boolean
port:
format: int32
type: integer
protocol:
default: http
enum:
- http
- tcp
type: string
region:
enum:
- us
- eu
- ap
- au
- sa
- jp
- in
type: string
remote_addr:
type: string
service:
type: string
required:
Expand Down
6 changes: 6 additions & 0 deletions examples/helloworld/ngrok.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ metadata:
name: helloworld-ngrok
namespace: helloworld
spec:
# protocol used, currently support http & tcp
# tcp is less tested, please create issue
# default is http
protocol: http

# service section represent
# the service name in the same namespace
service: helloworld-service

# port section represent
# the service port in the same namespace
port: 5000
20 changes: 20 additions & 0 deletions examples/http-full-configuration/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
spec:
selector:
matchLabels:
run: nginx
replicas: 1
template:
metadata:
labels:
run: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
41 changes: 41 additions & 0 deletions examples/http-full-configuration/ngrok.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: ngrok.com/v1alpha1
kind: Ngrok
metadata:
name: nginx-ngrok-full
namespace: default
spec:
# ngrok authtoken
authtoken: your-auth-token

# protocol used, currently support http & tcp
# tcp is less tested, please create issue
# if there is an issue with tcp
protocol: http

# region where ngrok run
# refer to the docs
# https://ngrok.com/docs
region: ap

# auth protect your http
# with user password combination
# <user>:<password>
auth: user:password

# enable inspection
# only works for http protocol
inspect: true

# this supported starting with basic plan
# using custom hostname require to set authtoken
# the behaviour when set `hostname` with free account
# is unknown!
hostname: ngrok.zufardhiyaulhaq.com

# service section represent
# the service name in the same namespace
service: nginx-service

# port section represent
# the service port in the same namespace
port: 80
11 changes: 11 additions & 0 deletions examples/http-full-configuration/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
spec:
ports:
- port: 80
protocol: TCP
selector:
run: nginx
6 changes: 6 additions & 0 deletions examples/nginx/ngrok.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ metadata:
name: nginx-ngrok
namespace: default
spec:
# protocol used, currently support http & tcp
# tcp is less tested, please create issue
# default is http
protocol: http

# service section represent
# the service name in the same namespace
service: nginx-service

# port section represent
# the service port in the same namespace
port: 80
25 changes: 25 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,31 @@ 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
Auth string `json:"auth"`

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

// +optional
RemoteAddr string `json:"remote_addr"`

// +kubebuilder:validation:Enum=us;eu;ap;au;sa;jp;in
// +optional
Region string `json:"region"`

// +kubebuilder:default:=false
// +optional
Inspect bool `json:"inspect"`
}

// NgrokStatus defines the observed state of Ngrok
Expand Down
28 changes: 22 additions & 6 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 @@ -182,7 +183,7 @@ func (r *ReconcileNgrok) Reconcile(request reconcile.Request) (reconcile.Result,

bodyString := string(body)

matcher, err := regexp.Compile(`https(.*?)io`)
matcher, err := regexp.Compile(`https://.[^"]+`)
if err != nil {
return reconcile.Result{}, err
}
Expand All @@ -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", "start", "--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 := "templates/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()
}
32 changes: 32 additions & 0 deletions templates/configuration.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
web_addr: 0.0.0.0:4040

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

{{if .Spec.Region }}
region: {{ .Spec.Region }}
{{end}}

tunnels:
app:
proto: {{ .Spec.Protocol }}
addr: {{ .Spec.Service }}:{{ .Spec.Port }}

{{if eq .Spec.Protocol "http"}}
inspect: {{ .Spec.Inspect }}

{{if .Spec.Auth }}
auth: {{ .Spec.Auth }}
{{ end }}

{{if .Spec.AuthToken }}{{if .Spec.Hostname }}
hostname: {{ .Spec.Hostname }}
{{end}}{{end}}
{{end}}

{{if eq .Spec.Protocol "tcp"}}
{{if .Spec.AuthToken }}{{if .Spec.Hostname }}
remote_addr: {{ .Spec.Hostname }}
{{end}}{{end}}
{{end}}

0 comments on commit e2a084c

Please sign in to comment.