Skip to content

Commit 3f5d283

Browse files
committed
feat: add service for managing Kubernetes resources
1 parent a251848 commit 3f5d283

File tree

8 files changed

+685
-9
lines changed

8 files changed

+685
-9
lines changed

app/handlers/kubernetes.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package handlers
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/limanmys/render-engine/app/models"
6+
"github.com/limanmys/render-engine/internal/kubernetes"
7+
"github.com/limanmys/render-engine/pkg/logger"
8+
"github.com/limanmys/render-engine/pkg/validator"
9+
)
10+
11+
var kubernetesService = kubernetes.NewService()
12+
13+
// GetNamespaces lists all namespaces in the Kubernetes cluster
14+
func GetNamespaces(c *fiber.Ctx) error {
15+
var req models.NamespaceRequest
16+
if err := c.BodyParser(&req); err != nil {
17+
return logger.FiberError(fiber.StatusBadRequest, "Invalid request body")
18+
}
19+
20+
if err := validator.ValidateStruct(&req); err != nil {
21+
return logger.FiberError(fiber.StatusBadRequest, err.Error())
22+
}
23+
24+
namespaces, err := kubernetesService.GetNamespaces(c.Context(), req.Kubeconfig)
25+
if err != nil {
26+
return logger.FiberError(fiber.StatusInternalServerError, err.Error())
27+
}
28+
29+
return c.JSON(namespaces)
30+
}
31+
32+
// GetDeployments lists all deployments in a specific namespace
33+
func GetDeployments(c *fiber.Ctx) error {
34+
var req models.DeploymentRequest
35+
if err := c.BodyParser(&req); err != nil {
36+
return logger.FiberError(fiber.StatusBadRequest, "Invalid request body")
37+
}
38+
39+
if err := validator.ValidateStruct(&req); err != nil {
40+
return logger.FiberError(fiber.StatusBadRequest, err.Error())
41+
}
42+
43+
deployments, err := kubernetesService.GetDeployments(c.Context(), req.Kubeconfig, req.Namespace)
44+
if err != nil {
45+
return logger.FiberError(fiber.StatusInternalServerError, err.Error())
46+
}
47+
48+
return c.JSON(deployments)
49+
}
50+
51+
// GetDeploymentDetails gets detailed information about a specific deployment
52+
func GetDeploymentDetails(c *fiber.Ctx) error {
53+
var req models.DeploymentDetailsRequest
54+
if err := c.BodyParser(&req); err != nil {
55+
return logger.FiberError(fiber.StatusBadRequest, "Invalid request body")
56+
}
57+
58+
if err := validator.ValidateStruct(&req); err != nil {
59+
return logger.FiberError(fiber.StatusBadRequest, err.Error())
60+
}
61+
62+
deploymentDetails, err := kubernetesService.GetDeploymentDetails(c.Context(), req.Kubeconfig, req.Namespace, req.Deployment)
63+
if err != nil {
64+
return logger.FiberError(fiber.StatusNotFound, err.Error())
65+
}
66+
67+
return c.JSON(deploymentDetails)
68+
}

app/models/kubernetes.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package models
2+
3+
// Request models for Kubernetes API
4+
type KubeconfigRequest struct {
5+
Kubeconfig string `json:"kubeconfig" validate:"required,base64"`
6+
}
7+
8+
type NamespaceRequest struct {
9+
Kubeconfig string `json:"kubeconfig" validate:"required,base64"`
10+
}
11+
12+
type DeploymentRequest struct {
13+
Kubeconfig string `json:"kubeconfig" validate:"required,base64"`
14+
Namespace string `json:"namespace" validate:"required"`
15+
}
16+
17+
type DeploymentDetailsRequest struct {
18+
Kubeconfig string `json:"kubeconfig" validate:"required,base64"`
19+
Namespace string `json:"namespace" validate:"required"`
20+
Deployment string `json:"deployment" validate:"required"`
21+
}
22+
type NamespaceInfo struct {
23+
Name string `json:"name"`
24+
Status string `json:"status"`
25+
CreationTime interface{} `json:"creationTime"`
26+
Labels map[string]string `json:"labels"`
27+
Annotations map[string]string `json:"annotations"`
28+
}
29+
30+
type DeploymentInfo struct {
31+
Name string `json:"name"`
32+
Namespace string `json:"namespace"`
33+
Replicas int32 `json:"replicas"`
34+
ReadyReplicas int32 `json:"readyReplicas"`
35+
AvailableReplicas int32 `json:"availableReplicas"`
36+
CreationTime interface{} `json:"creationTime"`
37+
Labels map[string]string `json:"labels"`
38+
Annotations map[string]string `json:"annotations"`
39+
}
40+
41+
type PodInfo struct {
42+
Name string `json:"name"`
43+
Status string `json:"status"`
44+
CreationTime interface{} `json:"creationTime"`
45+
Node string `json:"node"`
46+
ContainerStatuses []ContainerStatusInfo `json:"containerStatuses"`
47+
}
48+
49+
type ContainerStatusInfo struct {
50+
Name string `json:"name"`
51+
Ready bool `json:"ready"`
52+
Started *bool `json:"started"`
53+
Image string `json:"image"`
54+
}
55+
56+
type ReplicaSetInfo struct {
57+
Name string `json:"name"`
58+
Replicas int32 `json:"replicas"`
59+
ReadyReplicas int32 `json:"readyReplicas"`
60+
CreationTime interface{} `json:"creationTime"`
61+
OwnerReference interface{} `json:"ownerReference"`
62+
}
63+
64+
type ContainerInfo struct {
65+
Name string `json:"name"`
66+
Image string `json:"image"`
67+
Ports []ContainerPortInfo `json:"ports"`
68+
Env []ContainerEnvInfo `json:"env"`
69+
Command []string `json:"command"`
70+
Args []string `json:"args"`
71+
}
72+
73+
type ContainerPortInfo struct {
74+
ContainerPort int32 `json:"containerPort"`
75+
Protocol string `json:"protocol"`
76+
Name string `json:"name"`
77+
}
78+
79+
type ContainerEnvInfo struct {
80+
Name string `json:"name"`
81+
Value string `json:"value"`
82+
}
83+
84+
type DeploymentDetails struct {
85+
Metadata interface{} `json:"metadata"`
86+
Spec interface{} `json:"spec"`
87+
Status interface{} `json:"status"`
88+
Pods []PodInfo `json:"pods"`
89+
ReplicaSets []ReplicaSetInfo `json:"replicaSets"`
90+
}

app/routes/index.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,10 @@ func Install(app *fiber.App) {
5555
app.Post("/cronjobs", handlers.CreateCronJob)
5656
app.Get("/cronjobs", handlers.IndexCronJobs)
5757
app.Delete("/cronjobs/:id", handlers.DeleteCronJob)
58+
59+
// kubernetes
60+
kubernetesGroup := app.Group("/kubernetes")
61+
kubernetesGroup.Post("/namespaces", handlers.GetNamespaces)
62+
kubernetesGroup.Post("/deployments", handlers.GetDeployments)
63+
kubernetesGroup.Post("/deploymentDetails", handlers.GetDeploymentDetails)
5864
}

go.mod

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module github.com/limanmys/render-engine
22

3-
go 1.23.0
3+
go 1.24.0
44

5-
toolchain go1.24.2
5+
toolchain go1.24.3
66

77
require (
88
al.essio.dev/pkg/shellescape v1.6.0
99
github.com/Nerzal/gocloak/v13 v13.9.0
1010
github.com/go-co-op/gocron v1.37.0
11+
github.com/go-playground/validator/v10 v10.26.0
1112
github.com/go-resty/resty/v2 v2.16.5
1213
github.com/gofiber/fiber/v2 v2.52.6
1314
github.com/gofiber/helmet/v2 v2.2.26
@@ -17,17 +18,31 @@ require (
1718
go.uber.org/zap v1.27.0
1819
gorm.io/driver/postgres v1.5.11
1920
gorm.io/gorm v1.25.12
21+
k8s.io/apimachinery v0.33.1
22+
k8s.io/client-go v0.33.1
2023
)
2124

2225
require (
2326
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
2427
github.com/ChrisTrenkamp/goxpath v0.0.0-20210404020558-97928f7e12b6 // indirect
2528
github.com/bodgit/ntlmssp v0.0.0-20240506230425-31973bb52d9b // indirect
2629
github.com/bodgit/windows v1.0.1 // indirect
30+
github.com/davecgh/go-spew v1.1.1 // indirect
31+
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
32+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
33+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
2734
github.com/geoffgarside/ber v1.1.0 // indirect
2835
github.com/go-logr/logr v1.4.2 // indirect
36+
github.com/go-openapi/jsonpointer v0.21.0 // indirect
37+
github.com/go-openapi/jsonreference v0.20.2 // indirect
38+
github.com/go-openapi/swag v0.23.0 // indirect
39+
github.com/go-playground/locales v0.14.1 // indirect
40+
github.com/go-playground/universal-translator v0.18.1 // indirect
2941
github.com/gofrs/uuid v4.4.0+incompatible // indirect
42+
github.com/gogo/protobuf v1.3.2 // indirect
3043
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
44+
github.com/google/gnostic-models v0.6.9 // indirect
45+
github.com/google/go-cmp v0.7.0 // indirect
3146
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
3247
github.com/hashicorp/go-uuid v1.0.3 // indirect
3348
github.com/jackc/pgx/v5 v5.7.4 // indirect
@@ -38,21 +53,45 @@ require (
3853
github.com/jcmturner/goidentity/v6 v6.0.1 // indirect
3954
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
4055
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
56+
github.com/josharian/intern v1.0.0 // indirect
57+
github.com/json-iterator/go v1.1.12 // indirect
4158
github.com/kr/fs v0.1.0 // indirect
59+
github.com/leodido/go-urn v1.4.0 // indirect
60+
github.com/mailru/easyjson v0.7.7 // indirect
4261
github.com/masterzen/simplexml v0.0.0-20190410153822-31eea3082786 // indirect
4362
github.com/mattn/go-colorable v0.1.14 // indirect
4463
github.com/mattn/go-isatty v0.0.20 // indirect
4564
github.com/mattn/go-runewidth v0.0.16 // indirect
65+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
66+
github.com/modern-go/reflect2 v1.0.2 // indirect
67+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4668
github.com/opentracing/opentracing-go v1.2.0 // indirect
4769
github.com/pkg/errors v0.9.1 // indirect
4870
github.com/rivo/uniseg v0.4.7 // indirect
4971
github.com/robfig/cron/v3 v3.0.1 // indirect
5072
github.com/segmentio/ksuid v1.0.4 // indirect
73+
github.com/spf13/pflag v1.0.5 // indirect
5174
github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde // indirect
75+
github.com/x448/float16 v0.8.4 // indirect
5276
go.uber.org/atomic v1.11.0 // indirect
5377
go.uber.org/multierr v1.11.0 // indirect
5478
golang.org/x/net v0.39.0 // indirect
79+
golang.org/x/oauth2 v0.27.0 // indirect
5580
golang.org/x/sync v0.13.0 // indirect
81+
golang.org/x/term v0.31.0 // indirect
82+
golang.org/x/time v0.9.0 // indirect
83+
google.golang.org/protobuf v1.36.5 // indirect
84+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
85+
gopkg.in/inf.v0 v0.9.1 // indirect
86+
gopkg.in/yaml.v3 v3.0.1 // indirect
87+
k8s.io/api v0.33.1 // indirect
88+
k8s.io/klog/v2 v2.130.1 // indirect
89+
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect
90+
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
91+
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
92+
sigs.k8s.io/randfill v1.0.0 // indirect
93+
sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect
94+
sigs.k8s.io/yaml v1.4.0 // indirect
5695
)
5796

5897
require (

0 commit comments

Comments
 (0)