Skip to content

Commit

Permalink
feat: initial implementation of git.FilesystemStore
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Jul 24, 2023
1 parent 3b65d6b commit 984c94c
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 26 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/golang-lru/v2 v2.0.4 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand All @@ -30,7 +32,8 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.9.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ github.com/go-git/go-billy/v5 v5.4.1/go.mod h1:vjbugF6Fz7JIflbVpl1hJsGjSHNltrSw4
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20230305113008-0c11038e723f h1:Pz0DHeFij3XFhoBRGUDPzSJ+w2UcK5/0JvF8DRI58r8=
github.com/go-git/go-git/v5 v5.7.0 h1:t9AudWVLmqzlo+4bqdf7GY+46SUuRsx59SboFxkq2aE=
github.com/go-git/go-git/v5 v5.7.0/go.mod h1:coJHKEOk5kUClpsNlXrUvPrDxY3w3gjHvhcZd8Fodw8=
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/hashicorp/golang-lru/v2 v2.0.4 h1:7GHuZcgid37q8o5i3QI9KMT4nCWQQ3Kx3Ov6bb9MfK0=
github.com/hashicorp/golang-lru/v2 v2.0.4/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
Expand Down Expand Up @@ -79,10 +83,13 @@ golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down
39 changes: 24 additions & 15 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"path"
"sort"
Expand All @@ -14,9 +15,13 @@ import (
"go.flipt.io/cup/pkg/controller"
)

// FSFunc is a function passed to a FilesystemStore implementation to be invoked
// over a provided FSConfig in either a View or Update transaction context.
type FSFunc func(controller.FSConfig) error
// ViewFunc is a function provided to FilesystemStore.View.
// It is provided with a read-only view of the target filesystem.
type ViewFunc func(fs.FS) error

// UpdateFunc is a function passed to a FilesystemStore implementation to be invoked
// over a provided FSConfig in a call to Update.
type UpdateFunc func(controller.FSConfig) error

// Result is the result of performing an update on a target FilesystemStore.
type Result struct{}
Expand All @@ -27,11 +32,11 @@ type Result struct{}
type FilesystemStore interface {
// View invokes the provided function with an FSConfig which should enforce
// a read-only view for the requested source and revision
View(_ context.Context, source, revision string, fn FSFunc) error
View(_ context.Context, source, revision string, fn ViewFunc) error
// Update invokes the provided function with an FSConfig which can be written to
// Any writes performed to the target during the execution of fn will be added,
// comitted, pushed and proposed for review on a target SCM
Update(_ context.Context, source, revision string, fn FSFunc) (*Result, error)
Update(_ context.Context, source, revision, message string, fn UpdateFunc) (*Result, error)
}

// Controller is the core controller interface for handling interactions with a
Expand Down Expand Up @@ -90,7 +95,7 @@ func (s *Server) addDefinition(source string, gvk string, def *core.ResourceDefi
}

// RegisterController adds a new controller and definition for a particular source to the server.
// This potentially will happen dynamically in the future, so it is guarded with a write lock.
// This may happen dynamically in the future, so it is guarded with a write lock.
func (s *Server) RegisterController(source string, cntl Controller) {
s.mu.Lock()
defer s.mu.Unlock()
Expand All @@ -108,15 +113,15 @@ func (s *Server) RegisterController(source string, cntl Controller) {

// list kind
s.mux.Get(prefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := s.fs.View(r.Context(), source, s.rev, func(f controller.FSConfig) error {
if err := s.fs.View(r.Context(), source, s.rev, func(f fs.FS) error {
resources, err := cntl.List(r.Context(), &controller.ListRequest{
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
FS: f,
})
if err != nil {
return err
Expand All @@ -138,15 +143,15 @@ func (s *Server) RegisterController(source string, cntl Controller) {

// get kind
s.mux.Get(named, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := s.fs.View(r.Context(), source, s.rev, func(f controller.FSConfig) error {
if err := s.fs.View(r.Context(), source, s.rev, func(f fs.FS) error {
resource, err := cntl.Get(r.Context(), &controller.GetRequest{
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
FS: f,
Name: chi.URLParamFromCtx(r.Context(), "name"),
})
if err != nil {
Expand All @@ -162,20 +167,22 @@ func (s *Server) RegisterController(source string, cntl Controller) {

// put kind
s.mux.Put(named, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result, err := s.fs.Update(r.Context(), source, s.rev, func(f controller.FSConfig) error {
// TODO(georgemac): derive a suitable message
var message string
result, err := s.fs.Update(r.Context(), source, s.rev, message, func(f controller.FSConfig) error {
var resource core.Resource
if err := json.NewDecoder(r.Body).Decode(&resource); err != nil {
return err
}

return cntl.Put(r.Context(), &controller.PutRequest{
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
FSConfig: f,
Name: chi.URLParamFromCtx(r.Context(), "name"),
Resource: &resource,
})
Expand All @@ -193,16 +200,18 @@ func (s *Server) RegisterController(source string, cntl Controller) {

// delete kind
s.mux.Delete(named, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result, err := s.fs.Update(r.Context(), source, s.rev, func(f controller.FSConfig) error {
// TODO(georgemac): derive a suitable message
var message string
result, err := s.fs.Update(r.Context(), source, s.rev, message, func(f controller.FSConfig) error {
return cntl.Delete(r.Context(), &controller.DeleteRequest{
Request: controller.Request{
FSConfig: f,
Group: def.Spec.Group,
Version: version,
Kind: def.Names.Kind,
Namespace: chi.URLParamFromCtx(r.Context(), "ns"),
},
Name: chi.URLParamFromCtx(r.Context(), "name"),
FSConfig: f,
Name: chi.URLParamFromCtx(r.Context(), "name"),
})
})
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package controller

import "go.flipt.io/cup/pkg/api/core"
import (
"io/fs"

"go.flipt.io/cup/pkg/api/core"
)

type Controller struct{}

type Request struct {
FSConfig
Group string
Version string
Kind string
Expand All @@ -14,21 +17,25 @@ type Request struct {

type GetRequest struct {
Request
FS fs.FS
Name string
}

type ListRequest struct {
Request
FS fs.FS
Labels [][2]string
}

type PutRequest struct {
Request
FSConfig
Name string
Resource *core.Resource
}

type DeleteRequest struct {
Request
FSConfig
Name string
}
9 changes: 4 additions & 5 deletions pkg/controllers/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"context"
"fmt"
"io"
"io/fs"
"os"
"strings"
"text/template"

"github.com/go-git/go-billy/v5/util"
"go.flipt.io/cup/pkg/api/core"
"go.flipt.io/cup/pkg/containers"
"go.flipt.io/cup/pkg/controller"
Expand Down Expand Up @@ -87,7 +87,7 @@ func (c *Controller) Get(_ context.Context, req *controller.GetRequest) (_ *core
return nil, err
}

fi, err := req.FSConfig.ToFS().Open(buf.String())
fi, err := req.FS.Open(buf.String())
if err != nil {
return nil, err
}
Expand All @@ -110,14 +110,13 @@ func (c *Controller) List(_ context.Context, req *controller.ListRequest) (resou
return nil, err
}

ffs := req.FSConfig.ToFS()
matches, err := util.Glob(ffs, buf.String())
matches, err := fs.Glob(req.FS, buf.String())
if err != nil {
return nil, err
}

for _, match := range matches {
fi, err := ffs.Open(match)
fi, err := req.FS.Open(match)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 984c94c

Please sign in to comment.