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

Commit

Permalink
feat: implement real time update server on top of web sockets
Browse files Browse the repository at this point in the history
Closes: #2

Add backend server that is able to connect to Kubernetes `controller-runtime`
and fetch data from it and send updates to the JS frontend through a web socket.

As we have pretty similar implementation of `controller-runtime` in Talos all
communication with K8s runtime is done through an abstraction layer.

Potentially, we should get exactly the same interface for Talos resource
watch.

WebSocket protocol is currently used for all client-server
communication, but later on we may decide to add REST RPC calls as well.
Mainly for Talos API calls.

UI is still more of a draft, but it is possible to get updated list of
pods, nodes, namespaces, deployments, daemonsets.

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
  • Loading branch information
Unix4ever authored and talos-bot committed Apr 9, 2021
1 parent 76b39ae commit 16475f5
Show file tree
Hide file tree
Showing 33 changed files with 3,622 additions and 319 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
//
// Generated on 2021-03-30T15:37:32Z by kres def665a-dirty.
// Generated on 2021-04-02T18:16:15Z by kres 957c8d9-dirty.

module.exports = {
presets: [
Expand Down
2 changes: 1 addition & 1 deletion .jestrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
//
// Generated on 2021-03-30T15:37:32Z by kres def665a-dirty.
// Generated on 2021-04-02T18:16:15Z by kres 957c8d9-dirty.

module.exports = {
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel/',
Expand Down
2 changes: 1 addition & 1 deletion .tsconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"baseUrl": "./",
"baseUrl": "./",
"experimentalDecorators": true,
"strict": true,
"moduleResolution": "node",
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2021-03-30T15:28:27Z by kres def665a-dirty.
# Generated on 2021-04-02T18:16:15Z by kres 957c8d9-dirty.

ARG JS_TOOLCHAIN
ARG TOOLCHAIN
Expand Down Expand Up @@ -45,6 +45,8 @@ COPY .tsconfig ./tsconfig.json
COPY ./frontend/src ./src
COPY ./frontend/tests ./tests
COPY ./frontend/public ./public
COPY ./frontend/postcss.config.js ./postcss.config.js
COPY ./frontend/tailwind.config.js ./tailwind.config.js

# build tools
FROM toolchain AS tools
Expand Down
70 changes: 34 additions & 36 deletions cmd/theila/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,55 @@
package main

import (
"fmt"
"io/fs"
"context"
"errors"
"log"
"net/http"
"os/signal"
"syscall"

"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/talos-systems/theila/internal/frontend"
"github.com/talos-systems/theila/internal/backend"
"github.com/talos-systems/theila/internal/backend/logging"
)

func main() {
log.Println("Starting app")

router := registerRoutes()

port := 8080

log.Printf("Serving on port %d", port)

if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), router); err != nil {
log.Fatal(err)
}
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "theila",
Short: "Talos and Sidero frontend",
Long: ``,
}

func registerRoutes() http.Handler {
log.Println("Registering routes")

r := chi.NewMux()
var rootCmdArgs struct {
address string
port int
}

r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
func main() {
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder

sub, err := fs.Sub(frontend.Dist, "dist")
zapLogger, err := config.Build()
if err != nil {
log.Fatalf("failed to get dist/frontend directory")
log.Fatalf("failed to set up logging %s", err)
}

r.Handle("/*", http.FileServer(http.FS(sub)))
logger := zapLogger.Sugar()
logging.Logger = logger

logRoutes(r)
server := backend.NewServer(rootCmdArgs.address, rootCmdArgs.port)

return r
}
ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

func logRoutes(r chi.Routes) {
log.Println("Serving with routes:")
//nolint:errcheck
chi.Walk(r, func(method, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
log.Printf("\t%s %s", method, route)
if err := server.Run(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalf("failed to run server %s", err)
}
}

return nil
})
func init() {
rootCmd.Flags().IntVarP(&rootCmdArgs.port, "port", "p", 8080, "Start HTTP server on the defined port.")
rootCmd.Flags().StringVar(&rootCmdArgs.address, "address", "", "Start HTTP server on the defined address.")
}
Loading

0 comments on commit 16475f5

Please sign in to comment.