diff --git a/Makefile b/Makefile index 3ce7ba50..4f3230b9 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ SRC_PKGS := cmd pkg DOCKER_ARGS := --rm CACHE_DIR := $(shell pwd)/bin/cache API_DOCS := api/sample-app.md api/runtime.md -BINARIES := bin/sample-app bin/sample-gitops +BINARIES := bin/sample-app bin/sample-gitops bin/sample-watch # If we're not running in CI, run Docker interactively ifndef CI diff --git a/cmd/sample-app/main.go b/cmd/sample-app/main.go index 3103324c..729f19c6 100644 --- a/cmd/sample-app/main.go +++ b/cmd/sample-app/main.go @@ -3,13 +3,12 @@ package main import ( "bytes" "fmt" - "github.com/weaveworks/libgitops/pkg/storage/watch" - "github.com/weaveworks/libgitops/pkg/storage/watch/update" "net/http" "os" "github.com/labstack/echo" "github.com/sirupsen/logrus" + "github.com/spf13/pflag" "github.com/weaveworks/libgitops/cmd/common" "github.com/weaveworks/libgitops/cmd/sample-app/apis/sample/scheme" "github.com/weaveworks/libgitops/cmd/sample-app/apis/sample/v1alpha1" @@ -19,10 +18,7 @@ import ( "github.com/weaveworks/libgitops/pkg/storage" ) -const ( - ManifestDir = "/tmp/libgitops/manifest" - WatchDir = "/tmp/libgitops/watch" -) +var manifestDirFlag = pflag.String("data-dir", "/tmp/libgitops/manifest", "Where to store the JSON files") func main() { // Parse the version flag @@ -36,11 +32,8 @@ func main() { } func run() error { - // Create the manifest and watch directories - if err := os.MkdirAll(ManifestDir, 0755); err != nil { - return err - } - if err := os.MkdirAll(WatchDir, 0755); err != nil { + // Create the manifest directory + if err := os.MkdirAll(*manifestDirFlag, 0755); err != nil { return err } @@ -48,27 +41,12 @@ func run() error { logs.Logger.SetLevel(logrus.TraceLevel) plainStorage := storage.NewGenericStorage( - storage.NewGenericRawStorage(ManifestDir, v1alpha1.SchemeGroupVersion, serializer.ContentTypeYAML), + storage.NewGenericRawStorage(*manifestDirFlag, v1alpha1.SchemeGroupVersion, serializer.ContentTypeYAML), scheme.Serializer, []runtime.IdentifierFactory{runtime.Metav1NameIdentifier}, ) defer func() { _ = plainStorage.Close() }() - watchStorage, err := watch.NewManifestStorage(WatchDir, scheme.Serializer) - if err != nil { - return err - } - defer func() { _ = watchStorage.Close() }() - - updates := make(chan update.Update, 4096) - watchStorage.SetUpdateStream(updates) - - go func() { - for upd := range updates { - logrus.Infof("Got %s update for: %v %v", upd.Event, upd.PartialObject.GetObjectKind().GroupVersionKind(), upd.PartialObject.GetObjectMeta()) - } - }() - e := common.NewEcho() e.GET("/plain/:name", func(c echo.Context) error { @@ -112,34 +90,5 @@ func run() error { return c.String(200, "OK!") }) - e.GET("/watch/:name", func(c echo.Context) error { - name := c.Param("name") - if len(name) == 0 { - return echo.NewHTTPError(http.StatusBadRequest, "Please set name") - } - - obj, err := watchStorage.Get(common.CarKeyForName(name)) - if err != nil { - return err - } - var content bytes.Buffer - if err := scheme.Serializer.Encoder().Encode(serializer.NewJSONFrameWriter(&content), obj); err != nil { - return err - } - return c.JSONBlob(http.StatusOK, content.Bytes()) - }) - - e.PUT("/watch/:name", func(c echo.Context) error { - name := c.Param("name") - if len(name) == 0 { - return echo.NewHTTPError(http.StatusBadRequest, "Please set name") - } - - if err := common.SetNewCarStatus(plainStorage, common.CarKeyForName(name)); err != nil { - return err - } - return c.String(200, "OK!") - }) - return common.StartEcho(e) } diff --git a/cmd/sample-gitops/main.go b/cmd/sample-gitops/main.go index 8a53a37c..e8c21805 100644 --- a/cmd/sample-gitops/main.go +++ b/cmd/sample-gitops/main.go @@ -3,8 +3,6 @@ package main import ( "context" "fmt" - "github.com/weaveworks/libgitops/pkg/storage/watch" - "github.com/weaveworks/libgitops/pkg/storage/watch/update" "io/ioutil" "net/http" "os" @@ -23,6 +21,8 @@ import ( "github.com/weaveworks/libgitops/pkg/storage" "github.com/weaveworks/libgitops/pkg/storage/transaction" githubpr "github.com/weaveworks/libgitops/pkg/storage/transaction/pullrequest/github" + "github.com/weaveworks/libgitops/pkg/storage/watch" + "github.com/weaveworks/libgitops/pkg/storage/watch/update" ) var ( diff --git a/cmd/sample-watch/main.go b/cmd/sample-watch/main.go new file mode 100644 index 00000000..745862da --- /dev/null +++ b/cmd/sample-watch/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "bytes" + "fmt" + "net/http" + "os" + + "github.com/labstack/echo" + "github.com/sirupsen/logrus" + "github.com/spf13/pflag" + "github.com/weaveworks/libgitops/cmd/common" + "github.com/weaveworks/libgitops/cmd/sample-app/apis/sample/scheme" + "github.com/weaveworks/libgitops/pkg/logs" + "github.com/weaveworks/libgitops/pkg/serializer" + "github.com/weaveworks/libgitops/pkg/storage/watch" + "github.com/weaveworks/libgitops/pkg/storage/watch/update" +) + +var watchDirFlag = pflag.String("watch-dir", "/tmp/libgitops/watch", "Where to watch for YAML/JSON manifests") + +func main() { + // Parse the version flag + common.ParseVersionFlag() + + // Run the application + if err := run(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func run() error { + // Create the watch directory + if err := os.MkdirAll(*watchDirFlag, 0755); err != nil { + return err + } + + // Set the log level + logs.Logger.SetLevel(logrus.TraceLevel) + + watchStorage, err := watch.NewManifestStorage(*watchDirFlag, scheme.Serializer) + if err != nil { + return err + } + defer func() { _ = watchStorage.Close() }() + + updates := make(chan update.Update, 4096) + watchStorage.SetUpdateStream(updates) + + go func() { + for upd := range updates { + logrus.Infof("Got %s update for: %v %v", upd.Event, upd.PartialObject.GetObjectKind().GroupVersionKind(), upd.PartialObject.GetObjectMeta()) + } + }() + + e := common.NewEcho() + + e.GET("/watch/:name", func(c echo.Context) error { + name := c.Param("name") + if len(name) == 0 { + return echo.NewHTTPError(http.StatusBadRequest, "Please set name") + } + + obj, err := watchStorage.Get(common.CarKeyForName(name)) + if err != nil { + return err + } + var content bytes.Buffer + if err := scheme.Serializer.Encoder().Encode(serializer.NewJSONFrameWriter(&content), obj); err != nil { + return err + } + return c.JSONBlob(http.StatusOK, content.Bytes()) + }) + + e.PUT("/watch/:name", func(c echo.Context) error { + name := c.Param("name") + if len(name) == 0 { + return echo.NewHTTPError(http.StatusBadRequest, "Please set name") + } + + if err := common.SetNewCarStatus(watchStorage, common.CarKeyForName(name)); err != nil { + return err + } + return c.String(200, "OK!") + }) + + return common.StartEcho(e) +}