Skip to content

Commit

Permalink
k8s的config配置文件存储数据库和k8s api使用数据库里的kube config
Browse files Browse the repository at this point in the history
  • Loading branch information
qishu321 committed Aug 22, 2023
1 parent a9c25f6 commit 3082c00
Show file tree
Hide file tree
Showing 21 changed files with 1,060 additions and 193 deletions.
49 changes: 49 additions & 0 deletions api/apis_k8s/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package apis_k8s

import (
"cmdb-ops-flow/models/k8s"
"cmdb-ops-flow/service/service_k8s"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"github.com/gin-gonic/gin"
"net/http"
)

func GetallNamespace(c *gin.Context) {
var data struct {
ID int `json:"id"`
}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service_k8s.GetNamespace(data.ID)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}

func AddNamespace(c *gin.Context) {
//var data struct {
// ID int `json:"id"`
//}
//if err := c.ShouldBindJSON(&data); err != nil {
// c.JSON(http.StatusOK, (&result.Result{}).Error(500, err.Error(), msg.GetErrMsg(msg.ERROR)))
// return
//}
var data k8s.NameSpace
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(5001, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service_k8s.AddNamespace(data.ID, data)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}
43 changes: 43 additions & 0 deletions api/apis_k8s/nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package apis_k8s

import (
"cmdb-ops-flow/service/service_k8s"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"github.com/gin-gonic/gin"
"net/http"
)

func GetVersion(c *gin.Context) {
var data struct {
ID int `json:"id"`
}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service_k8s.Version(data.ID)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}

func GetAllNodes(c *gin.Context) {
var data struct {
ID int `json:"id"`
}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service_k8s.Getnodes(data.ID)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}
26 changes: 26 additions & 0 deletions api/apis_k8s/pods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package apis_k8s

import (
"cmdb-ops-flow/service/service_k8s"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"github.com/gin-gonic/gin"
"net/http"
)

func GetAllPods(c *gin.Context) {
var data struct {
ID int `json:"id"`
}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service_k8s.GetPods(data.ID)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}
87 changes: 87 additions & 0 deletions api/kubeconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package api

import (
"cmdb-ops-flow/models"
"cmdb-ops-flow/service"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)

func AddKubeConfig(c *gin.Context) {
file, err := c.FormFile("yamlFile")
if err != nil {
c.JSON(http.StatusOK, gin.H{"error1": err.Error()})
return
}
kubeconfigname := c.PostForm("kubeconfigname")

fileContent, err := file.Open()
if err != nil {
c.JSON(http.StatusOK, gin.H{"error2": err.Error()})
return
}
defer fileContent.Close()
yamlData, err := ioutil.ReadAll(fileContent)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error3": err.Error()})
return
}

data, err := service.AddKubeConfig(yamlData, kubeconfigname)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, (&result.Result{}).Ok(200, data, msg.GetErrMsg(200)))

}
func EditKubeConfig(c *gin.Context) {
var data models.KubeConfig
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service.EditKubeConfig(data)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}
func GetKubeConfig(c *gin.Context) {
//var data models.ScriptManager
var data struct {
ID int `json:"id"`
}

if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service.GetKubeConfigList(data.ID)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))

}

func DelKubeConfig(c *gin.Context) {

var data models.KubeConfig
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := models.DelKubeConfig(data.Kubeconfigid)

c.JSON(http.StatusOK, (&result.Result{}).Ok(code, msg.SUCCSE, msg.GetErrMsg(code)))

}
48 changes: 43 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,58 @@ require (
github.com/go-ini/ini v1.67.0
github.com/gorilla/websocket v1.5.0
github.com/jinzhu/gorm v1.9.16
github.com/prometheus/client_golang v1.11.1
go.etcd.io/etcd/client/v3 v3.5.9
golang.org/x/crypto v0.9.0
golang.org/x/crypto v0.11.0
k8s.io/api v0.28.0
k8s.io/apimachinery v0.28.0
k8s.io/client-go v0.28.0
k8s.io/klog v1.0.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // 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/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
Expand All @@ -43,11 +69,23 @@ require (
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.30.0 // 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/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit 3082c00

Please sign in to comment.