Skip to content

Commit

Permalink
initial websocket commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Hodgetts committed Nov 1, 2022
1 parent a680639 commit 8bef725
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 2 deletions.
30 changes: 30 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,36 @@ paths:
type: object
404:
description: "Envoy fleet CRD not found"
/fleets/{namespace}/{name}/logs:
x-kusk:
websocket: true
get:
tags:
- "fleets"
summary: "Get envoy fleet logs"
operationId: getEnvoyFleetLogs
parameters:
- name: namespace
in: path
required: true
schema:
type: string
default: "default"
- name: name
in: path
required: true
schema:
type: string
responses:
200:
description: "Envoy fleet logs"
content:
application/json:
schema:
type: object
404:
description: "Envoy fleet logs not found"

/staticroutes:
get:
tags:
Expand Down
4 changes: 2 additions & 2 deletions server/Dockerfile → server/build/api-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM --platform=$BUILDPLATFORM docker.io/golang:1.18 as builder
WORKDIR /go/src
# Copy `go.mod` for definitions and `go.sum` to invalidate the next layer
# in case of a change in the dependencies
COPY go.mod go.sum ./
COPY ../../go.mod go.sum ./
# Download dependencies
RUN go mod download

Expand All @@ -11,7 +11,7 @@ ARG VERSION
ARG TARGETARCH
ARG TARGETOS

COPY . .
COPY ../.. .
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH CGO_ENABLED=0 go build -v -ldflags "-X github.com/kubeshop/kusk-gateway/pkg/analytics.TelemetryToken=$TELEMETRY_TOKEN -X github.com/kubeshop/kusk-gateway/pkg/build.Version=$VERSION" -o kusk-gateway-api

FROM --platform=$BUILDPLATFORM gcr.io/distroless/static:nonroot
Expand Down
Empty file.
File renamed without changes.
81 changes: 81 additions & 0 deletions server/cmd/websocket/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"log"
"net/http"
"os"
"path"
"time"

"github.com/gorilla/websocket"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

func wsEndpoint(w http.ResponseWriter, r *http.Request) {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
}

k8sConfig, err := getKubeConfig()
if err != nil {
log.Fatal(err)
}

clientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
log.Fatal(err)
}

log.Println("Client Connected")
for {
err = ws.WriteMessage(websocket.TextMessage, []byte("Hi Client!"))
if err != nil {
log.Println(err)
}

time.Sleep(time.Second * 1)
}
}

func main() {
http.HandleFunc("/logs", wsEndpoint)
log.Fatal(http.ListenAndServe(":8080", nil))
}

func getKubeConfig() (*rest.Config, error) {
var err error
var config *rest.Config
k8sConfigExists := false
homeDir, _ := os.UserHomeDir()
cubeConfigPath := path.Join(homeDir, ".kube/config")

if _, err := os.Stat(cubeConfigPath); err == nil {
k8sConfigExists = true
}

if cfg, exists := os.LookupEnv("KUBECONFIG"); exists {
config, err = clientcmd.BuildConfigFromFlags("", cfg)
} else if k8sConfigExists {
config, err = clientcmd.BuildConfigFromFlags("", cubeConfigPath)
} else {
config, err = rest.InClusterConfig()
}
if err != nil {
return nil, err
}
// default query per second is set to 5
config.QPS = 40.0
// default burst is set to 10
config.Burst = 400.0

return config, err
}
1 change: 1 addition & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down

0 comments on commit 8bef725

Please sign in to comment.