Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add host override for any number of hosts #1014

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions golang/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ down:

# target `upd` is for development purposes it defaults to the latest images
# and expects crux and crux-ui to run locally
# for host name overrides you can define: --host localhost,yourdomain.com
.PHONY: upd
upd:
cd cmd/dyo && \
go run . --disable-crux --disable-crux-ui --image-tag latest --prefix dyo-latest up
go run . --disable-crux --disable-crux-ui --image-tag latest --prefix dyo-latest --host localhost up

.PHONY: downd
downd:
Expand All @@ -65,7 +66,7 @@ downd:

.PHONY: go-crane
go-crane:
air --build.cmd "" --build.bin "cd cmd/crane && go run ."
air --build.cmd "" --build.bin "cd cmd/crane && ${GRPC_DEBUG_FLAGS} go run ."

.PHONY: go-crane-init
go-crane-init:
Expand All @@ -74,7 +75,7 @@ go-crane-init:

.PHONY: go-dagent
go-dagent:
air --build.cmd "" --build.bin "cd cmd/dagent && go run ."
air --build.cmd "" --build.bin "cd cmd/dagent && ${GRPC_DEBUG_FLAGS} go run ."

.PHONY: cli
cli:
Expand Down
10 changes: 10 additions & 0 deletions golang/pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
FlagDebug = "debug"
FlagPreferLocalImages = "prefer-local-images"
FlagConfigPath = "config"
FlagHosts = "host"
FlagPrefix = "prefix"
FlagImageTag = "image-tag"
FlagSilent = "silent"
Expand Down Expand Up @@ -112,6 +113,14 @@ func InitCLI() *ucli.App {
Required: false,
EnvVars: []string{"DYO_CONFIG"},
},
&ucli.StringSliceFlag{
Name: FlagHosts,
Aliases: []string{},
Value: ucli.NewStringSlice("localhost"),
DefaultText: "localhost",
Usage: "Use this hosts instead of default one. Eg. 'localhost,extradomain1.example.com,extradomain2.example.com'",
Required: false,
},
&ucli.StringFlag{
Name: FlagImageTag,
Value: "",
Expand Down Expand Up @@ -179,6 +188,7 @@ func run(cCtx *ucli.Context) error {
LocalAgent: cCtx.Bool(FlagLocalAgent),
Command: cCtx.Command.Name,
EnvFile: cCtx.String(FlagEnvFile),
Hosts: cCtx.StringSlice(FlagHosts),
}

initialState := State{
Expand Down
1 change: 1 addition & 0 deletions golang/pkg/cli/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ArgsFlags struct {
Command string
ImageTag string
Prefix string
Hosts []string
CruxDisabled bool
CruxUIDisabled bool
LocalAgent bool
Expand Down
29 changes: 21 additions & 8 deletions golang/pkg/cli/container_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/dyrector-io/dyrectorio/golang/internal/helper/image"
"github.com/dyrector-io/dyrectorio/golang/internal/label"
"github.com/dyrector-io/dyrectorio/golang/internal/logdefer"
"github.com/dyrector-io/dyrectorio/golang/internal/util"
containerbuilder "github.com/dyrector-io/dyrectorio/golang/pkg/builder/container"
dagentutils "github.com/dyrector-io/dyrectorio/golang/pkg/dagent/utils"
)
Expand Down Expand Up @@ -70,9 +71,9 @@ func GetCrux(state *State, args *ArgsFlags) containerbuilder.Builder {
WithCmd([]string{"serve"}).
WithLabels(map[string]string{
"traefik.enable": "true",
"traefik.http.routers.crux.rule": fmt.Sprintf("(Host(`localhost`) || Host(`%s`) || Host(`%s`)) && "+
"traefik.http.routers.crux.rule": fmt.Sprintf("(%s) && "+
"PathPrefix(`/api`) && !PathPrefix(`/api/auth`) && !PathPrefix(`/api/status`) ",
state.Containers.Traefik.Name, state.InternalHostDomain),
RenderTraefikHostRules(append(args.Hosts, state.Containers.Traefik.Name, state.InternalHostDomain)...)),
"traefik.http.routers.crux.entrypoints": "web",
"traefik.http.services.crux.loadbalancer.server.port": fmt.Sprintf("%d", defaultCruxHTTPPort),
"com.docker.compose.project": args.Prefix,
Expand Down Expand Up @@ -220,8 +221,8 @@ func GetCruxUI(state *State, args *ArgsFlags) containerbuilder.Builder {
WithNetworkAliases(state.Containers.CruxUI.Name).
WithLabels(map[string]string{
"traefik.enable": "true",
"traefik.http.routers.crux-ui.rule": fmt.Sprintf("Host(`%s`) || Host(`%s`) || Host(`%s`)", traefikHost, state.InternalHostDomain,
state.Containers.Traefik.Name),
"traefik.http.routers.crux-ui.rule": RenderTraefikHostRules(
append(args.Hosts, traefikHost, state.InternalHostDomain, state.Containers.Traefik.Name)...),
"traefik.http.routers.crux-ui.entrypoints": "web",
"traefik.http.services.crux-ui.loadbalancer.server.port": fmt.Sprintf("%d", defaultCruxUIPort),
"com.docker.compose.project": args.Prefix,
Expand Down Expand Up @@ -304,6 +305,7 @@ func GetTraefik(state *State, args *ArgsFlags) containerbuilder.Builder {
ctx,
cont.Name,
state.InternalHostDomain,
args.Hosts,
state.SettingsFile.CruxHTTPPort,
state.SettingsFile.CruxUIPort,
)
Expand Down Expand Up @@ -345,8 +347,9 @@ func GetKratos(state *State, args *ArgsFlags) containerbuilder.Builder {
WithNetworkAliases(state.Containers.Kratos.Name).
WithLabels(map[string]string{
"traefik.enable": "true",
"traefik.http.routers.kratos.rule": fmt.Sprintf("(Host(`localhost`) || Host(`%s`) || Host(`%s`)) && "+
"PathPrefix(`/kratos`)", state.Containers.Traefik.Name, state.InternalHostDomain),
"traefik.http.routers.kratos.rule": fmt.Sprintf("(%s) && "+
"PathPrefix(`/kratos`)",
RenderTraefikHostRules(append(args.Hosts, state.Containers.Traefik.Name, state.InternalHostDomain)...)),
"traefik.http.routers.kratos.entrypoints": "web",
"traefik.http.services.kratos.loadbalancer.server.port": fmt.Sprintf("%d", defaultKratosPublicPort),
"traefik.http.middlewares.kratos-strip.stripprefix.prefixes": "/kratos",
Expand Down Expand Up @@ -569,7 +572,7 @@ func getBasePostgres(state *State, args *ArgsFlags) containerbuilder.Builder {
}

// CopyTraefikConfiguration copies a config file to Traefik Container
func CopyTraefikConfiguration(ctx context.Context, name, internalHostDomain string, cruxPort, cruxUIPort uint) error {
func CopyTraefikConfiguration(ctx context.Context, name, internalHost string, hosts []string, cruxPort, cruxUIPort uint) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
Expand All @@ -587,7 +590,8 @@ func CopyTraefikConfiguration(ctx context.Context, name, internalHostDomain stri
var result bytes.Buffer

traefikData := traefikFileProviderData{
InternalHost: internalHostDomain,
HostRules: RenderTraefikHostRules(append(hosts, internalHost)...),
InternalHost: internalHost,
CruxUIPort: cruxUIPort,
CruxPort: cruxPort,
}
Expand Down Expand Up @@ -656,3 +660,12 @@ func healthProbe(ctx context.Context, address string) error {
}
}
}

func RenderTraefikHostRules(hosts ...string) string {
hostRules := []string{}
for _, v := range hosts {
hostRules = append(hostRules, fmt.Sprintf("Host(`%s`)", v))
}

return util.JoinV(" || ", hostRules...)
}
51 changes: 51 additions & 0 deletions golang/pkg/cli/container_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//go:build unit
// +build unit

package cli

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRenderTraefikHostRules(t *testing.T) {
type args struct {
hosts []string
}
tests := []struct {
name string
want string
desc string
args args
}{
{
name: "one-host",
args: args{
hosts: []string{"localhost:8000"},
},
want: "Host(`localhost:8000`)",
},
{
name: "two-hosts",
args: args{
hosts: []string{"localhost:8000", "test.example.com"},
},
want: "Host(`localhost:8000`) || Host(`test.example.com`)",
},
{
name: "no-host",
args: args{
hosts: []string{},
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, RenderTraefikHostRules(tt.args.hosts...), tt.desc)
})
}

assert.True(t, true, true)
}
1 change: 1 addition & 0 deletions golang/pkg/cli/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (

type traefikFileProviderData struct {
InternalHost string
HostRules string
CruxUIPort uint
CruxPort uint
}
Expand Down
22 changes: 17 additions & 5 deletions golang/pkg/cli/traefik.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
http:
routers:
crux-ui:
rule: Host(`localhost`) || Host(`{{.InternalHost}}`)
rule: {{.HostRules}}
service: crux-ui
entryPoints:
- web

crux:
rule: (Host(`localhost`) || Host(`{{.InternalHost}}`)) && (PathPrefix(`/api`) && !PathPrefix(`/api/auth`) && !PathPrefix(`/api/status`))
service: crux
crux-http:
rule: ({{.HostRules}}) && (PathPrefix(`/api`) && !PathPrefix(`/api/auth`) && !PathPrefix(`/api/status`))
service: crux-http
entryPoints:
- web

crux-grpc:
rule: ({{.HostRules}}) && Headers(`content-type`, `application/grpc`)
service: crux-grpc
entryPoints:
- web

Expand All @@ -18,7 +24,13 @@ http:
servers:
- url: http://{{.InternalHost}}:{{.CruxUIPort}}

crux:
crux-http:
loadBalancer:
servers:
- url: http://{{.InternalHost}}:{{.CruxPort}}

crux-grpc:
loadBalancer:
servers:
- url: h2c://{{.InternalHost}}:5000

Loading