Skip to content

Commit

Permalink
Merge pull request #52 from amazeeio/http-handler
Browse files Browse the repository at this point in the history
feat: add http handler
  • Loading branch information
shreddedbacon authored Jan 23, 2022
2 parents 6d13b8f + 6ce90c1 commit 19e04e7
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 11 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ RUN go mod download
COPY main.go main.go
COPY apis/ apis/
COPY controllers/ controllers/
COPY handlers/ handlers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
Expand Down
20 changes: 18 additions & 2 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ metadata:
control-plane: controller-manager
name: system
---
apiVersion: v1
kind: Service
metadata:
namespace: controller-backend
name: controller-backend
spec:
selector:
control-plane: controller-manager
ports:
- name: backend
port: 5000
targetPort: backend
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -29,11 +42,14 @@ spec:
- --enable-leader-election
image: controller:latest
name: manager
ports:
- containerPort: 5000
name: backend
resources:
limits:
cpu: 100m
memory: 50Mi
memory: 300Mi
requests:
cpu: 100m
memory: 20Mi
memory: 200Mi
terminationGracePeriodSeconds: 10
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-logr/logr v0.1.0
github.com/go-sql-driver/mysql v1.5.0
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.8.0
github.com/lib/pq v1.3.0
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
Expand Down
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -158,6 +157,8 @@ github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsC
github.com/googleapis/gnostic v0.3.1 h1:WeAefnSUHlBb0iJKwxFDZdbfGwkd7xRNuV+IpXMJhYk=
github.com/googleapis/gnostic v0.3.1/go.mod h1:on+2t9HRStVgn95RSsFWFz+6Q0Snyqv1awfrALZdbtU=
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/grpc-ecosystem/go-grpc-middleware v0.0.0-20190222133341-cfaf5686ec79/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
Expand Down Expand Up @@ -235,7 +236,6 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -275,7 +275,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -332,7 +331,6 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc h1:gkKoSkUmnU6bpS/VhkuO27bzQeSA51uaEfbOW5dNb68=
golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down Expand Up @@ -369,7 +367,6 @@ golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fq
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20171227012246-e19ae1496984/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand All @@ -391,7 +388,6 @@ golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBn
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -427,7 +423,6 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.0.0/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
168 changes: 168 additions & 0 deletions handlers/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package handlers

import (
"context"
"fmt"
"net/http"
"os"

"github.com/go-logr/logr"
"github.com/gorilla/mux"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"

mariadbv1 "github.com/amazeeio/dbaas-operator/apis/mariadb/v1"
mongodbv1 "github.com/amazeeio/dbaas-operator/apis/mongodb/v1"
postgresv1 "github.com/amazeeio/dbaas-operator/apis/postgres/v1"
)

const (
// ContentType name of the header that defines the format of the reply
ContentType = "Content-Type"
// DBaaSEnvironmentType name of the header that contains if this has been served by dbaas
DBaaSEnvironmentType = "X-DBaaS-Environment-Type"
// CacheControl name of the header that defines the cache control config
CacheControl = "Cache-Control"
)

var (
favicon = "data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
)

// Client is the client structure for http handlers.
type Client struct {
Client ctrlClient.Client
Log logr.Logger
Debug bool
// RequestCount *prometheus.CounterVec
// RequestDuration *prometheus.HistogramVec
}

func faviconHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/x-icon")
w.Header().Set("Cache-Control", "public, max-age=7776000")
fmt.Fprintln(w, fmt.Sprintf("%s\n", favicon))
}

// Run runs the http server.
func Run(h *Client, setupLog logr.Logger) {
r := mux.NewRouter()
r.HandleFunc("/favicon.ico", faviconHandler)
r.HandleFunc("/mariadb/{environment}", h.mariaDBHandler)
r.HandleFunc("/mongodb/{environment}", h.mongoDBHandler)
r.HandleFunc("/postgres/{environment}", h.postgresHandler)

r.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

err := http.ListenAndServe(fmt.Sprintf(":5000"), r)
if err != nil {
setupLog.Error(err, "unable to start http server")
os.Exit(1)
}
}

func (h *Client) mariaDBHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
environmentType := vars["environment"]

ctx := context.Background()
opLog := h.Log.WithValues("dbaas-operator", "mariadb")
providers := &mariadbv1.MariaDBProviderList{}

w.Header().Set(ContentType, "application/json")
w.Header().Set(DBaaSEnvironmentType, environmentType)
w.Header().Set(CacheControl, "private,no-store")

if err := h.Client.List(ctx, providers); err != nil {
opLog.Info(fmt.Sprintf("Unable to get any providers, error was: %s", err.Error()))
w.WriteHeader(200)
fmt.Fprintln(w, fmt.Sprintf(`{"result":{"found":false},"error":"%s"}`, err.Error()))
return
}
hasProvider := false
for _, provider := range providers.Items {
if provider.Spec.Environment == environmentType {
hasProvider = true
}
}
if hasProvider {
opLog.Info(fmt.Sprintf("Providers for %s exist", environmentType))
w.WriteHeader(200)
fmt.Fprintln(w, `{"result":{"found":true}}`)
} else {
opLog.Info(fmt.Sprintf("No providers for %s exist", environmentType))
w.WriteHeader(200)
fmt.Fprintln(w, fmt.Sprintf(`{"result":{"found":false},"error":"no providers for dbaas environment %s"}`, environmentType))
}
}

func (h *Client) mongoDBHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
environmentType := vars["environment"]

ctx := context.Background()
opLog := h.Log.WithValues("dbaas-operator", "mongodb")
providers := &mongodbv1.MongoDBProviderList{}

w.Header().Set(ContentType, "application/json")
w.Header().Set(DBaaSEnvironmentType, environmentType)
w.Header().Set(CacheControl, "private,no-store")

if err := h.Client.List(ctx, providers); err != nil {
opLog.Info(fmt.Sprintf("Unable to get any providers, error was: %s", err.Error()))
w.WriteHeader(200)
fmt.Fprintln(w, fmt.Sprintf(`{"result":{"found":false},"error":"%s"}`, err.Error()))
return
}
hasProvider := false
for _, provider := range providers.Items {
if provider.Spec.Environment == environmentType {
hasProvider = true
}
}
if hasProvider {
opLog.Info(fmt.Sprintf("Providers for %s exist", environmentType))
w.WriteHeader(200)
fmt.Fprintln(w, `{"result":{"found":true}}`)
} else {
opLog.Info(fmt.Sprintf("No providers for %s exist", environmentType))
w.WriteHeader(200)
fmt.Fprintln(w, fmt.Sprintf(`{"result":{"found":false},"error":"no providers for dbaas environment %s"}`, environmentType))
}
}

func (h *Client) postgresHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
environmentType := vars["environment"]

ctx := context.Background()
opLog := h.Log.WithValues("dbaas-operator", "postgres")
providers := &postgresv1.PostgreSQLProviderList{}

w.Header().Set(ContentType, "application/json")
w.Header().Set(DBaaSEnvironmentType, environmentType)
w.Header().Set(CacheControl, "private,no-store")

if err := h.Client.List(ctx, providers); err != nil {
opLog.Info(fmt.Sprintf("Unable to get any providers, error was: %s", err.Error()))
w.WriteHeader(200)
fmt.Fprintln(w, fmt.Sprintf(`{"result":{"found":false},"error":"%s"}`, err.Error()))
return
}
hasProvider := false
for _, provider := range providers.Items {
if provider.Spec.Environment == environmentType {
hasProvider = true
}
}
if hasProvider {
opLog.Info(fmt.Sprintf("Providers for %s exist", environmentType))
w.WriteHeader(200)
fmt.Fprintln(w, `{"result":{"found":true}}`)
} else {
opLog.Info(fmt.Sprintf("No providers for %s exist", environmentType))
w.WriteHeader(200)
fmt.Fprintln(w, fmt.Sprintf(`{"result":{"found":false},"error":"no providers for dbaas environment %s"}`, environmentType))
}
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
mariadbcontroller "github.com/amazeeio/dbaas-operator/controllers/mariadb"
mongodbcontroller "github.com/amazeeio/dbaas-operator/controllers/mongodb"
postgrescontroller "github.com/amazeeio/dbaas-operator/controllers/postgres"

"github.com/amazeeio/dbaas-operator/handlers"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
Expand Down Expand Up @@ -141,6 +143,14 @@ func main() {
}
// +kubebuilder:scaffold:builder

h := &handlers.Client{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers-http").WithName("DBaaS"),
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting http server")
go handlers.Run(h, setupLog)

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
Loading

0 comments on commit 19e04e7

Please sign in to comment.