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(cluster): initial commit for scale-out cluster #2041

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.28.6
github.com/aws/aws-secretsmanager-caching-go v1.1.3
github.com/containers/image/v5 v5.30.0
github.com/dchest/siphash v1.2.3
github.com/google/go-github/v52 v52.0.0
github.com/gorilla/securecookie v1.1.2
github.com/gorilla/sessions v1.2.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/siphash v1.2.3 h1:QXwFc8cFOR2dSa/gE6o/HokBMWtLUaNDVd+22aKHeEA=
github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIXMPAkHc=
github.com/depcheck-test/depcheck-test v0.0.0-20220607135614-199033aaa936 h1:foGzavPWwtoyBvjWyKJYDYsyzy+23iBV7NKTwdk+LRY=
github.com/depcheck-test/depcheck-test v0.0.0-20220607135614-199033aaa936/go.mod h1:ttKPnOepYt4LLzD+loXQ1rT6EmpyIYHro7TAJuIIlHo=
github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg=
Expand Down
8 changes: 8 additions & 0 deletions pkg/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ type SchedulerConfig struct {
NumWorkers int
}

// ClusterConfig is the scale-out configuration which is identical for all
// replicas
type ClusterConfig struct {
Members []string
HashKey string
}

type LDAPCredentials struct {
BindDN string
BindPassword string
Expand Down Expand Up @@ -230,6 +237,7 @@ type Config struct {
Log *LogConfig
Extensions *extconf.ExtensionConfig
Scheduler *SchedulerConfig `json:"scheduler" mapstructure:",omitempty"`
Cluster *ClusterConfig `json:"cluster" mapstructure:",omitempty"`
}

func New() *Config {
Expand Down
7 changes: 7 additions & 0 deletions pkg/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (c *Controller) Run() error {
engine.Use(SessionAuditLogger(c.Audit))
}

/*
if c.Cluster != nil {
engine.Use(ProxyCluster)
}

*/

c.Router = engine
c.Router.UseEncodedPath()

Expand Down
88 changes: 88 additions & 0 deletions pkg/cluster/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cluster

Check failure on line 1 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

: # zotregistry.dev/zot/pkg/cluster

import (
"fmt"
"net/http"

"github.com/dchest/siphash"

"zotregistry.dev/zot/pkg/api"
"zotregistry.dev/zot/pkg/api/constants"
zreg "zotregistry.dev/zot/pkg/regexp"
)

type ProxyRouteHandler struct {
c *api.Controller
}

func NewRouteHandler(c *api.Controller) *RouteHandler {
rh := &ProxyRouteHandler{c: c}
rh.SetupRoutes()

// FIXME: this is a scale-out load balancer cluster so doesn't do replicas

return rh
}

func (rh *ProxyRouteHandler) SetupRoutes() {
prefixedRouter := rh.c.Router.PathPrefix(constants.RoutePrefix).Subrouter()
prefixedDistSpecRouter := prefixedRouter.NewRoute().Subrouter()

prefixedDistSpecRouter.HandleFunc(fmt.Sprintf("/{name:%s}/", zreg.NameRegexp.String()), proxyRequestResponse(rh.c.Config)())
}

func proxyRequestResponse(config rh.c.Config) func(http.HandlerFunc) http.HandlerFunc {

Check failure on line 34 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected . in parameter list; possibly missing comma or )

Check failure on line 34 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in parameter list (typecheck)
return func(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
// if no cluster or single-node cluster, handle locally
if config.Cluster == nil || len(config.Cluster.Members) {
next.ServeHTTP(response, request)
}

vars := mux.Vars(request)

name, ok := vars["name"]

if !ok || name == "" {
response.WriteHeader(http.StatusNotFound)

return
}

h := siphash.New(key)
h.Write([]byte(name)

Check failure on line 53 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected newline in argument list; possibly missing comma or ) (typecheck)

Check failure on line 53 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' before newline in argument list (typecheck)
sum64 := h.Sum64(nil)

Check failure on line 54 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in argument list (typecheck)

member := config.Cluster.Members[sum64%len(config.Cluster.Members)]

Check failure on line 56 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in argument list (typecheck)
/*

// from the member list and our DNS/IP address, figure out if this request should be handled locally
if member == localMember {
next.ServeHTTP(response, request)
}

*/
handleHTTP(response, request)

Check failure on line 65 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' before newline in argument list (typecheck)
})

Check failure on line 66 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found '}' (typecheck)
}
}

func handleHTTP(w http.ResponseWriter, req *http.Request) {
resp, err := http.DefaultTransport.RoundTrip(req)
if err != nil {

Check failure on line 72 in pkg/cluster/proxy.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in argument list (typecheck)
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
defer resp.Body.Close()
copyHeader(w.Header(), resp.Header)
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}

func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
Loading