Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# KOR Backend

Kor Backend is an API wrapper designed to expose Kor.

A tool that identifies unused resources in Kubernetes clusters - over HTTPS.

This backend allows users to access Kor's functionality via RESTful API calls,

making it easier to integrate into web services, dashboards, and automation pipelines.

## Swagger
```swag init```
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the swagger initialized by default in the docker?



## Docker
#### Generate SSL certs
```bash
openssl req -x509 -newkey rsa:4096 -keyout server.key -out server.crt -days 36500 -nodes -subj "/C=US/ST=California/L=San Francisco/O=MyCompany/OU=IT/CN=localhost"
```

```bash
docker buildx build -t kor-backend:latest .
```

## Development
```bash
export NO_AUTH=true
go run .
```
2 changes: 2 additions & 0 deletions backend/kor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server.crt
server.key
33 changes: 33 additions & 0 deletions backend/kor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Use an official Golang image as the build environment
FROM golang:1.22.2-alpine AS builder
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at the kor dockerfile https://github.com/yonahd/kor/blob/main/Dockerfile


# Set the working directory inside the container
WORKDIR /app

# Copy the Go module files to install dependencies
COPY go.mod go.sum ./

# Download the Go dependencies
RUN go mod download

# Copy the rest of the source code
COPY . .

# Build the Go application
RUN go build -o /go-service

# Use a smaller base image for the final container
FROM alpine:latest
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use FROM scratch it will make the image significantly smaller


# Set the working directory for the final container
WORKDIR /root/

# Copy the compiled Go binary from the builder stage
COPY --from=builder /go-service .
COPY server.crt .
COPY server.key .
# Expose the application's port (optional)
EXPOSE 8080

# Command to run the Go service
CMD ["./go-service"]
38 changes: 38 additions & 0 deletions backend/kor/configmaps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"encoding/json"
"fmt"
"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
"github.com/yonahd/kor/pkg/kor"
"k8s.io/client-go/kubernetes"
"net/http"
)

func getUnusedConfigMapWithFilters(w http.ResponseWriter, opts common.Opts, filterOpts *filters.Options, clientset kubernetes.Interface) {
outputFormat := "json"
// Call the function that returns a JSON string
response, err := kor.GetUnusedConfigmaps(filterOpts, clientset, outputFormat, opts)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
errorMsg := fmt.Sprintf("Failed to get configmaps: %v\n", err)
json.NewEncoder(w).Encode(SimpleResponse{Message: errorMsg})
return
}

// Declare a variable to hold the parsed JSON structure
var parsedResponse map[string]interface{}

// Parse the JSON string into a map
if err := json.Unmarshal([]byte(response), &parsedResponse); err != nil {
w.WriteHeader(http.StatusInternalServerError)
errorMsg := fmt.Sprintf("Failed to parse configmaps response: %v\n", err)
json.NewEncoder(w).Encode(SimpleResponse{Message: errorMsg})
return
}

// Send the parsed JSON as the response
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(parsedResponse)
}
92 changes: 92 additions & 0 deletions backend/kor/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/api/v1/configmaps": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Get Unused configmaps from all namespaces",
"parameters": [
{
"type": "string",
"description": "Authorization token",
"name": "Authorization",
"in": "header"
}
],
"responses": {}
}
},
"/api/v1/namespaces/{namespace}/configmaps": {
"get": {
"description": "asd",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Get Unused configmaps from a specific namespace",
"parameters": [
{
"type": "string",
"description": "Authorization token",
"name": "Authorization",
"in": "header"
},
{
"type": "string",
"description": "namespace",
"name": "namespace",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/healthcheck": {
"get": {
"description": "Returns the status of the server",
"summary": "Health Check",
"responses": {}
}
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "",
BasePath: "",
Schemes: []string{},
Title: "KOR API Swagger",
Description: "KOR API Swagger",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
66 changes: 66 additions & 0 deletions backend/kor/docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"swagger": "2.0",
"info": {
"description": "KOR API Swagger",
"title": "KOR API Swagger",
"contact": {},
"version": "1.0"
},
"paths": {
"/api/v1/configmaps": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Get Unused configmaps from all namespaces",
"parameters": [
{
"type": "string",
"description": "Authorization token",
"name": "Authorization",
"in": "header"
}
],
"responses": {}
}
},
"/api/v1/namespaces/{namespace}/configmaps": {
"get": {
"description": "asd",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Get Unused configmaps from a specific namespace",
"parameters": [
{
"type": "string",
"description": "Authorization token",
"name": "Authorization",
"in": "header"
},
{
"type": "string",
"description": "namespace",
"name": "namespace",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/healthcheck": {
"get": {
"description": "Returns the status of the server",
"summary": "Health Check",
"responses": {}
}
}
}
}
44 changes: 44 additions & 0 deletions backend/kor/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
info:
contact: {}
description: KOR API Swagger
title: KOR API Swagger
version: "1.0"
paths:
/api/v1/configmaps:
get:
consumes:
- application/json
parameters:
- description: Authorization token
in: header
name: Authorization
type: string
produces:
- application/json
responses: {}
summary: Get Unused configmaps from all namespaces
/api/v1/namespaces/{namespace}/configmaps:
get:
consumes:
- application/json
description: asd
parameters:
- description: Authorization token
in: header
name: Authorization
type: string
- description: namespace
in: path
name: namespace
required: true
type: string
produces:
- application/json
responses: {}
summary: Get Unused configmaps from a specific namespace
/healthcheck:
get:
description: Returns the status of the server
responses: {}
summary: Health Check
swagger: "2.0"
73 changes: 73 additions & 0 deletions backend/kor/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module main

go 1.22.2

require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/gorilla/mux v1.8.1
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.3
github.com/yonahd/kor v0.5.5
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/prometheus/client_golang v1.20.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.0 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/apimachinery v0.31.0 // indirect
k8s.io/client-go v0.31.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading