Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
iximiuz committed Sep 29, 2023
1 parent 660e5c1 commit 08000fe
Show file tree
Hide file tree
Showing 149 changed files with 17,102 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.so
*.dylib
*.test
*.out
*.swp
*.swo
*.local
.vagrant
node_modules
.DS_Store
dist
dist-ssr
__debug_bin

/bin/*
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# syntax=docker/dockerfile:1

# Build stage
FROM golang:1.20 as build

ARG BUILD_VERSION=v0.0.0
ARG BUILD_COMMIT=HEAD
ARG BUILD_DATE=unknown

WORKDIR /src

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build \
-ldflags "-X main.version=${BUILD_VERSION} -X main.commit=${BUILD_COMMIT} -X main.date=${BUILD_DATE}" \
-o bin/kexp main.go

# App stage
FROM alpine:3.18

COPY --from=build /src/bin/kexp /usr/local/bin/kexp

EXPOSE 5173

CMD ["kexp", "--host", "127.0.0.1", "--port", "5173"]
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
CUR_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

VERSION=v0.0.0
GIT_COMMIT=$(shell git rev-parse --verify HEAD)
UTC_NOW=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")

.PHONY: back-run-dev
back-run-dev:
go run ${CUR_DIR}/main.go --host 0.0.0.0

.PHONY: back-fmt
back-fmt:
cd ${CUR_DIR} && go fmt ./...

.PHONY: back-lint
back-lint: back-fmt
golangci-lint run

.PHONY: front-run-dev
front-run-dev:
cd ${CUR_DIR}/ui && npm run dev

.PHONY: front-lint-fix
front-lint-fix:
cd ${CUR_DIR}/ui && npm run lint-fix

.PHONY: build
build:
cd ${CUR_DIR}/ui && npm run build
go build -o ${CUR_DIR}/bin/kexp ${CUR_DIR}/main.go
CGO_ENABLED=0 go build \
-ldflags "-X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${UTC_NOW}" \
-o ${CUR_DIR}/bin/kexp main.go

.PHONY: docker-build
docker-build:
cd ${CUR_DIR}/ui && npm run build
docker buildx build \
--progress plain \
--build-arg BUILD_VERSION=${VERSION} \
--build-arg BUILD_COMMIT=${GIT_COMMIT} \
--build-arg BUILD_DATE=${UTC_NOW} \
-t ghcr.io/iximiuz/kexp:latest \
-f ${CUR_DIR}/Dockerfile \
${CUR_DIR}

.PHONY: docker-push
docker-push: docker-build
docker push ghcr.io/iximiuz/kexp:latest
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# k'exp - Kubernetes Explorer

## Development

Server:

```sh
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
```

Client:

```sh
cd ui
npm install
npm run dev
```

```sh
make back-run-dev
make front-run-dev

open localhost:5173
```
40 changes: 40 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package api

import (
"context"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/sirupsen/logrus"

"github.com/iximiuz/kexp/logging"
)

const HeaderRequestID = "X-Request-ID"

func MiddlewareRequestID(c *gin.Context) {
rid := c.GetHeader(HeaderRequestID)
if rid == "" {
rid = uuid.New().String()[:8]
}

c.Request = c.Request.WithContext(
context.WithValue(c.Request.Context(), logging.KeyRequestID, rid),
)
c.Header(HeaderRequestID, rid)
c.Next()
}

type Handler struct {
logger *logrus.Entry
}

func NewHandler(name string, logger *logrus.Entry) Handler {
return Handler{
logger: logger.WithField("handler", name),
}
}

func (h *Handler) Logger(c *gin.Context) *logrus.Entry {
return logging.WithRequestID(c.Request.Context(), h.logger)
}
50 changes: 50 additions & 0 deletions api/rest/kube/contexts/contexts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package contexts

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

"github.com/iximiuz/kexp/api"
"github.com/iximiuz/kexp/kubeclient"
)

type Handler struct {
api.Handler

clientPool *kubeclient.ClientPool
}

func NewHandler(clientPool *kubeclient.ClientPool, logger *logrus.Entry) *Handler {
return &Handler{
Handler: api.NewHandler("kube/contexts", logger),
clientPool: clientPool,
}
}

// kube/<ver>/contexts
func (h *Handler) List(c *gin.Context) {
cs := []Context{}
for _, kctx := range h.clientPool.Contexts() {
cs = append(cs, Context{
Name: kctx.Name(),
User: kctx.User(),
Cluster: kctx.Cluster(),
ClusterUID: kctx.ClusterUID(),
Namespace: kctx.Namespace(),
Current: kctx.Name() == h.clientPool.CurrentContext().Name(),
})
}

c.JSON(http.StatusOK, cs)
}

type Context struct {
Name string `json:"name"`
User string `json:"user"`
Cluster string `json:"cluster"`
ClusterUID string `json:"clusterUID"`
Namespace string `json:"namespace"`
Current bool `json:"current"`
}
Loading

0 comments on commit 08000fe

Please sign in to comment.