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

Zos3/web gateway #1370

Merged
merged 21 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
75 changes: 75 additions & 0 deletions cmds/modules/gateway/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package gateway

import (
"context"

"github.com/pkg/errors"
"github.com/threefoldtech/zos/pkg/gateway"
"github.com/threefoldtech/zos/pkg/utils"
"github.com/urfave/cli/v2"

"github.com/rs/zerolog/log"

"github.com/threefoldtech/zbus"
)

const (
module = "gateway"
)

// Module is entry point for module
var Module cli.Command = cli.Command{
Name: "gateway",
Usage: "manage web gateway proxy",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "root",
Usage: "`ROOT` working directory of the module",
Value: "/var/cache/modules/gateway",
},
&cli.StringFlag{
Name: "broker",
Usage: "connection string to the message `BROKER`",
Value: "unix:///var/run/redis.sock",
},
&cli.UintFlag{
Name: "workers",
Usage: "number of workers `N`",
Value: 1,
},
},
Action: action,
}

func action(cli *cli.Context) error {
var (
moduleRoot string = cli.String("root")
msgBrokerCon string = cli.String("broker")
workerNr uint = cli.Uint("workers")
)

server, err := zbus.NewRedisServer(module, msgBrokerCon, workerNr)
if err != nil {
return errors.Wrap(err, "fail to connect to message broker server")
}

mod := gateway.New(moduleRoot)
server.Register(zbus.ObjectID{Name: "manager", Version: "0.0.1"}, mod)

ctx, _ := utils.WithSignal(context.Background())

log.Info().
Str("broker", msgBrokerCon).
Uint("worker nr", workerNr).
Msg("starting gateway module")

utils.OnDone(ctx, func(_ error) {
log.Info().Msg("shutting down")
})

if err := server.Run(ctx); err != nil && err != context.Canceled {
return errors.Wrap(err, "unexpected error")
}

return nil
}
2 changes: 2 additions & 0 deletions cmds/zos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/threefoldtech/zos/cmds/modules/capacityd"
"github.com/threefoldtech/zos/cmds/modules/contd"
"github.com/threefoldtech/zos/cmds/modules/flistd"
"github.com/threefoldtech/zos/cmds/modules/gateway"
"github.com/threefoldtech/zos/cmds/modules/networkd"
"github.com/threefoldtech/zos/cmds/modules/provisiond"
"github.com/threefoldtech/zos/cmds/modules/storaged"
Expand Down Expand Up @@ -43,6 +44,7 @@ func main() {
&networkd.Module,
&provisiond.Module,
&zbusdebug.Module,
&gateway.Module,
},
Action: func(c *cli.Context) error {
if !c.Bool("list") {
Expand Down
3 changes: 3 additions & 0 deletions etc/zinit/gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exec: gateway --broker unix://var/run/redis.sock --root /var/cache/modules/gateway
after:
- boot
10 changes: 10 additions & 0 deletions pkg/gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pkg

//go:generate mkdir -p stubs

//go:generate zbusc -module gateway -version 0.0.1 -name manager -package stubs github.com/threefoldtech/zos/pkg+Gateway stubs/gateway_stub.go

type Gateway interface {
SetNamedProxy(fqdn string, backends []string) error
DeleteNamedProxy(fqdn string) error
}
165 changes: 165 additions & 0 deletions pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package gateway

import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/network/namespace"
"github.com/threefoldtech/zos/pkg/network/types"
"github.com/threefoldtech/zos/pkg/zinit"
"gopkg.in/yaml.v2"
)

const (
traefikService = "traefik"
)

type gatewayModule struct {
proxyConfigPath string
traefikStarted bool
}

type ProxyConfig struct {
Http HTTPConfig
}

type HTTPConfig struct {
Routers map[string]Router
Services map[string]Service
}

type Router struct {
Rule string
Service string
}

type Service struct {
LoadBalancer LoadBalancer
}

type LoadBalancer struct {
Servers []Server
}

type Server struct {
Url string
}

func New(root string) pkg.Gateway {
configPath := filepath.Join(root, "proxy")
// where should service-restart/node-reboot recovery be handled?
if _, err := os.Stat(configPath); os.IsNotExist(err) {
err := os.MkdirAll(configPath, 0644)
OmarElawady marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
// ok to panic?
panic(errors.Wrap(err, "couldn't make gateway config dir"))
OmarElawady marked this conversation as resolved.
Show resolved Hide resolved
}
}

g := &gatewayModule{
proxyConfigPath: configPath,
traefikStarted: false,
}
log.Debug().Bool("exist", namespace.Exists(types.PublicNamespace)).Msg("checking public namespace")
if !isTraefikStarted() && namespace.Exists(types.PublicNamespace) {
err := g.startTraefik()
if err != nil {
log.Error().Err(err).Msg("couldn't start traefik")
}
}
return g
}
func isTraefikStarted() bool {
z, err := zinit.New("")
if err != nil {
panic(errors.Wrap(err, "couldn't get zinit client"))
OmarElawady marked this conversation as resolved.
Show resolved Hide resolved
}
defer z.Close()
started := true
traefikStatus, err := z.Status(traefikService)
if err != nil {
started = false
}
log.Debug().Str("state", traefikStatus.State.String()).Msg("checking traefik state")
started = traefikStatus.State.String() == zinit.ServiceStateRunning
OmarElawady marked this conversation as resolved.
Show resolved Hide resolved
return started
}
func (g *gatewayModule) startTraefik() error {
z, err := zinit.New("")
if err != nil {
return errors.Wrap(err, "couldn't get zinit client")
}
defer z.Close()
cmd := fmt.Sprintf("ip netns exec public traefik --log.level=DEBUG --providers.file.directory=%s --providers.file.watch=true", g.proxyConfigPath)
zinit.AddService(traefikService, zinit.InitService{
Exec: cmd,
})
if err := z.Monitor(traefikService); err != nil {
return errors.Wrap(err, "couldn't monitor traefik service")
}
if err := z.StartWait(time.Second*20, traefikService); err != nil {
return errors.Wrap(err, "waiting for trafik start timed out")
}
g.traefikStarted = true
return nil
}

func (g *gatewayModule) SetNamedProxy(fqdn string, backends []string) error {
if !namespace.Exists(types.PublicNamespace) {
OmarElawady marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("node doesn't support gateway workloads as it doesn't have public config")
}
if !g.traefikStarted {
if err := g.startTraefik(); err != nil {
return errors.Wrap(err, "couldn't start traefik")
}
}

rule := fmt.Sprintf("Host(`%s`) && PathPrefix(`/`)", fqdn)
servers := make([]Server, len(backends))
for idx, backend := range backends {
servers[idx] = Server{
Url: backend,
}
}
config := ProxyConfig{
Http: HTTPConfig{
Routers: map[string]Router{
fqdn: {
Rule: rule,
Service: fqdn,
},
},
Services: map[string]Service{
fqdn: {
LoadBalancer: LoadBalancer{
Servers: servers,
},
},
},
},
}

yamlString, err := yaml.Marshal(&config)
if err != nil {
return errors.Wrap(err, "failed to convert config to yaml")
}
log.Debug().Str("yaml_config", string(yamlString)).Msg("configuration file")
filename := filepath.Join(g.proxyConfigPath, fmt.Sprintf("%s.yaml", fqdn))
if err = os.WriteFile(filename, yamlString, 0644); err != nil {
return errors.Wrap(err, "couldn't open config file for writing")
}

return nil
}
func (g *gatewayModule) DeleteNamedProxy(fqdn string) error {
filename := filepath.Join(g.proxyConfigPath, fmt.Sprintf("%s.yaml", fqdn))
OmarElawady marked this conversation as resolved.
Show resolved Hide resolved
if err := os.Remove(filename); err != nil {
return errors.Wrap(err, "couldn't remove config file")
}
return nil
}
72 changes: 72 additions & 0 deletions pkg/gridtypes/zos/gw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package zos

import (
"fmt"
"io"
"net"
"regexp"

"github.com/pkg/errors"
"github.com/threefoldtech/zos/pkg/gridtypes"
)

var (
gwNameRegex = regexp.MustCompile(`^\w+$`)
)

type Backend string

// check if valid x.x.x.x:port or [::]:port
func (b Backend) Valid() error {
if _, err := net.ResolveTCPAddr("tcp", string(b)); err != nil {
return errors.Wrap(err, "invalid backend address")
}

return nil
}

// GatewayNameProxy definition. this will proxy name.<zos.domain> to backends
type GatewayNameProxy struct {
// Name of the domain prefix. this must be a valid dns name (with no dots)
Name string `json:"name"`

// Backends are list of backend ips
Backends []Backend `json:"backends"`
}

func (g GatewayNameProxy) Valid(getter gridtypes.WorkloadGetter) error {
if !gwNameRegex.MatchString(g.Name) {
return fmt.Errorf("invalid name")
}
if len(g.Backends) == 0 {
return fmt.Errorf("backends list can not be empty")
}

return nil
}

func (g GatewayNameProxy) Challenge(w io.Writer) error {
if _, err := fmt.Fprintf(w, "%s", g.Name); err != nil {
return err
}

for _, backend := range g.Backends {
if _, err := fmt.Fprintf(w, "%s", string(backend)); err != nil {
return err
}
}

return nil
}

func (g GatewayNameProxy) Capacity() (gridtypes.Capacity, error) {
// this has to be calculated per bytes served over the gw. so
// a special handler in reporting that need to calculate and report
// this.
return gridtypes.Capacity{}, nil
}

// GatewayProxyResult results
type GatewayProxyResult struct {
FQDN string `json:"fqdn"`
}
6 changes: 4 additions & 2 deletions pkg/gridtypes/zos/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const (
ZDBType gridtypes.WorkloadType = "zdb"
// ZMachineType type
ZMachineType gridtypes.WorkloadType = "zmachine"

//PublicIPType reservation
//PublicIPType type
PublicIPType gridtypes.WorkloadType = "ipv4"
// GatewayNameProxyType type
GatewayNameProxyType gridtypes.WorkloadType = "gateway-proxy"
)

func init() {
Expand All @@ -26,6 +27,7 @@ func init() {
gridtypes.RegisterType(ZDBType, ZDB{})
gridtypes.RegisterType(ZMachineType, ZMachine{})
gridtypes.RegisterType(PublicIPType, PublicIP{})
gridtypes.RegisterType(GatewayNameProxyType, GatewayNameProxy{})
}

// DeviceType is the actual type of hardware that the storage device runs on,
Expand Down
Loading