-
Notifications
You must be signed in to change notification settings - Fork 145
WIP: kor backend - http server #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hagay3
wants to merge
16
commits into
yonahd:kor-api
Choose a base branch
from
hagay3:feature/hagai/addKorBackend
base: kor-api
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
241d48c
kor server first commit: new go server in net/http, including TLS, To…
hagay3 ef91e73
add configmaps endpoint
hagay3 8e84eef
align healthcheck
hagay3 dbef318
align healthcheck
hagay3 acdb1bb
align healthcheck
hagay3 c98da68
align healthcheck
hagay3 ee9e6e6
align healthcheck
hagay3 852c89c
align swagger docs
hagay3 e3e345a
add go modules to backend/tools
hagay3 ef85571
add Dockerfile, regenerated docs
hagay3 8b2e48d
.gitignore
hagay3 2a85175
backend kor
hagay3 5c23a15
backend
hagay3 82d53b7
configmaps functionallity moved to configmaps.go, list namespaces va…
hagay3 9b320ab
fix readme
hagay3 89d915c
fix readme
hagay3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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``` | ||
|
|
||
|
|
||
| ## 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 . | ||
| ``` | ||
This file contains hidden or 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,2 @@ | ||
| server.crt | ||
| server.key |
This file contains hidden or 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,33 @@ | ||
| # Use an official Golang image as the build environment | ||
| FROM golang:1.22.2-alpine AS builder | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||
This file contains hidden or 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,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) | ||
| } |
This file contains hidden or 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,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) | ||
| } |
This file contains hidden or 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,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": {} | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,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" |
This file contains hidden or 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,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 | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?