-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #905 from SteveHNH/envoy_to_caddy
Switch out the envoy reverse proxy for caddy
- Loading branch information
Showing
8 changed files
with
159 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
controllers/cloud.redhat.com/providers/web/caddy_reverse_proxy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package web | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1" | ||
|
||
caddy "github.com/caddyserver/caddy/v2" | ||
caddyconfig "github.com/caddyserver/caddy/v2/caddyconfig" | ||
caddyhttp "github.com/caddyserver/caddy/v2/modules/caddyhttp" | ||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy" | ||
caddytls "github.com/caddyserver/caddy/v2/modules/caddytls" | ||
) | ||
|
||
func generateServers(pub bool, priv bool, pubPort uint32, privPort uint32, appPubPort int32, appPrivPort int32) (map[string]*caddyhttp.Server, error) { | ||
servers := make(map[string]*caddyhttp.Server) | ||
|
||
tlsConnPolicy := []*caddytls.ConnectionPolicy{{ | ||
CertSelection: &caddytls.CustomCertSelectionPolicy{ | ||
AnyTag: []string{"cert0"}, | ||
}, | ||
}} | ||
|
||
if pub { | ||
pubServer := generateServer(pubPort, appPubPort, tlsConnPolicy) | ||
servers["pubServer"] = pubServer | ||
} | ||
|
||
if priv { | ||
privServer := generateServer(privPort, appPrivPort, tlsConnPolicy) | ||
servers["privServer"] = privServer | ||
} | ||
|
||
return servers, nil | ||
} | ||
|
||
func generateServer(port uint32, appPort int32, tlsConnPolicy []*caddytls.ConnectionPolicy) *caddyhttp.Server { | ||
|
||
var warnings []caddyconfig.Warning | ||
|
||
reverseProxy := reverseproxy.Handler{ | ||
Upstreams: []*reverseproxy.Upstream{{ | ||
Dial: fmt.Sprintf("localhost:%d", appPort), | ||
}}, | ||
} | ||
|
||
server := &caddyhttp.Server{ | ||
Listen: []string{fmt.Sprintf(":%d", port)}, | ||
Routes: caddyhttp.RouteList{{ | ||
HandlersRaw: []json.RawMessage{ | ||
caddyconfig.JSONModuleObject(reverseProxy, "handler", "reverse_proxy", &warnings), | ||
}, | ||
}}, | ||
TLSConnPolicies: tlsConnPolicy, | ||
} | ||
|
||
return server | ||
} | ||
|
||
func generateCaddyConfig(pub bool, priv bool, pubPort uint32, privPort uint32, env *crd.ClowdEnvironment) (string, error) { | ||
var warnings []caddyconfig.Warning | ||
|
||
var servers map[string]*caddyhttp.Server | ||
var err error | ||
|
||
appPubPort := env.Spec.Providers.Web.Port | ||
appPrivPort := env.Spec.Providers.Web.PrivatePort | ||
|
||
servers, err = generateServers(pub, priv, pubPort, privPort, appPubPort, appPrivPort) | ||
if err != nil { | ||
fmt.Print("error generating caddy server config. Server generation failed") | ||
} | ||
|
||
appConfig := caddyhttp.App{ | ||
Servers: servers, | ||
} | ||
|
||
fl := caddytls.FileLoader{{ | ||
Certificate: "/certs/tls.crt", | ||
Key: "/certs/tls.key", | ||
Tags: []string{"cert0"}, | ||
}} | ||
|
||
tlsConfig := caddytls.TLS{ | ||
CertificatesRaw: caddy.ModuleMap{"load_files": caddyconfig.JSON(fl, &warnings)}, | ||
} | ||
|
||
v := caddy.Config{ | ||
StorageRaw: []byte{}, | ||
AppsRaw: map[string]json.RawMessage{ | ||
"http": caddyconfig.JSON(appConfig, &warnings), | ||
"tls": caddyconfig.JSON(tlsConfig, &warnings), | ||
}, | ||
} | ||
|
||
pretty, _ := json.MarshalIndent(v, "", " ") | ||
return string(pretty), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.