This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |