-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from kubeshop/kylehodgetts/feature/stream-logs…
…-websocket Websocket logs endpoint for polling envoy fleet container logs
- Loading branch information
Showing
16 changed files
with
512 additions
and
20 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
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,22 @@ | ||
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 ../server/go.mod ../server/go.sum ./ | ||
# Download dependencies | ||
RUN go mod download | ||
|
||
ARG TELEMETRY_TOKEN | ||
ARG VERSION | ||
ARG TARGETARCH | ||
ARG TARGETOS | ||
|
||
COPY ../server ./ | ||
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-websocket cmd/websocket/*.go | ||
|
||
FROM --platform=$BUILDPLATFORM gcr.io/distroless/static:nonroot | ||
COPY --from=builder --chown=65532:65532 /go/src/kusk-gateway-api-websocket ./ | ||
|
||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["./kusk-gateway-api-websocket"] |
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 |
---|---|---|
@@ -1,11 +1,23 @@ | ||
version: "3" | ||
services: | ||
kgwapi: | ||
build: ./server | ||
build: | ||
context: . | ||
dockerfile: ./build/api-server/Dockerfile | ||
environment: | ||
- KUBECONFIG=/kube/config | ||
- ANALYTICS_ENABLED=false | ||
volumes: | ||
- $HOME/.kube/config:/kube/config:ro | ||
ports: | ||
- 8080:8080 | ||
- "8080:8080" | ||
websocket: | ||
build: | ||
context: . | ||
dockerfile: ./build/websocket/Dockerfile | ||
environment: | ||
- KUBECONFIG=/kube/config | ||
volumes: | ||
- $HOME/.kube/config:/kube/config:ro | ||
ports: | ||
- "8081:8080" |
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,99 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"io" | ||
"log" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
type client struct { | ||
conn *websocket.Conn | ||
logStream io.ReadCloser | ||
|
||
writeWait time.Duration | ||
pongWait time.Duration | ||
pingPeriod time.Duration | ||
maxMessageSize int64 | ||
} | ||
|
||
func (c *client) readPump(ctx context.Context, stopCh chan struct{}) { | ||
defer func() { | ||
c.conn.Close() | ||
stopCh <- struct{}{} | ||
}() | ||
c.conn.SetReadLimit(c.maxMessageSize) | ||
c.conn.SetReadDeadline(time.Now().Add(c.pongWait)) | ||
c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(c.pongWait)); return nil }) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
_, _, err := c.conn.ReadMessage() | ||
if err != nil { | ||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure, websocket.CloseNormalClosure) { | ||
log.Printf("error: %v", err) | ||
} | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *client) writePump(ctx context.Context, stopCh chan struct{}) { | ||
ticker := time.NewTicker(c.pingPeriod) | ||
defer func() { | ||
ticker.Stop() | ||
c.conn.Close() | ||
stopCh <- struct{}{} | ||
}() | ||
|
||
reader := bufio.NewReader(c.logStream) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
case <-stopCh: | ||
return | ||
case <-ticker.C: | ||
c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)) | ||
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { | ||
return | ||
} | ||
default: | ||
c.conn.SetWriteDeadline(time.Now().Add(c.writeWait)) | ||
line, err := readLongLine(reader) | ||
if err != nil { | ||
log.Println("writePump: cannot read line", err) | ||
continue | ||
} | ||
|
||
if err := c.conn.WriteMessage(websocket.TextMessage, line); err != nil { | ||
log.Println("writePump: cannot write message", err) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
|
||
func readLongLine(r *bufio.Reader) (line []byte, err error) { | ||
var buffer []byte | ||
var isPrefix bool | ||
|
||
for { | ||
buffer, isPrefix, err = r.ReadLine() | ||
line = append(line, buffer...) | ||
if err != nil { | ||
break | ||
} | ||
|
||
if !isPrefix { | ||
break | ||
} | ||
} | ||
|
||
return line, err | ||
} |
Oops, something went wrong.