Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Split sample-app and -watch
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Aug 21, 2020
1 parent 4b3f584 commit feb5819
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 5 additions & 56 deletions cmd/sample-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -36,39 +32,21 @@ 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
}

// Set the log level
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 {
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions cmd/sample-gitops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down
89 changes: 89 additions & 0 deletions cmd/sample-watch/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit feb5819

Please sign in to comment.