-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate database svc and minio svc (#4447)
* separate database svc and minio svc * delete OBJECT_STORAGE_INSTANCE env * fix deploy.yaml
- Loading branch information
Showing
14 changed files
with
461 additions
and
224 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,202 +1,38 @@ | ||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"text/template" | ||
|
||
"github.com/labring/sealos/service/pkg/auth" | ||
|
||
"github.com/labring/sealos/service/database/api" | ||
"github.com/labring/sealos/service/database/request" | ||
"github.com/labring/sealos/service/pkg/server" | ||
) | ||
|
||
type PromServer struct { | ||
Config *Config | ||
} | ||
|
||
func NewPromServer(c *Config) (*PromServer, error) { | ||
ps := &PromServer{ | ||
Config: c, | ||
} | ||
return ps, nil | ||
} | ||
|
||
func (ps *PromServer) Authenticate(pr *api.PromRequest) error { | ||
return auth.Authenticate(pr.NS, pr.Pwd) | ||
} | ||
|
||
func (ps *PromServer) Request(pr *api.PromRequest) (*api.QueryResult, error) { | ||
body, err := request.PrometheusPre(pr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var result *api.QueryResult | ||
if err := json.Unmarshal(body, &result); err != nil { | ||
return nil, err | ||
} | ||
return result, err | ||
} | ||
|
||
func (ps *PromServer) DBReq(pr *api.PromRequest) (*api.QueryResult, error) { | ||
body, err := request.PrometheusNew(pr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var result *api.QueryResult | ||
if err := json.Unmarshal(body, &result); err != nil { | ||
return nil, err | ||
} | ||
return result, nil | ||
} | ||
|
||
func (ps *PromServer) ParseRequest(req *http.Request) (*api.PromRequest, error) { | ||
pr := &api.PromRequest{} | ||
|
||
auth := req.Header.Get("Authorization") | ||
|
||
if pwd, err := url.PathUnescape(auth); err == nil { | ||
pr.Pwd = pwd | ||
} else { | ||
return nil, err | ||
} | ||
if pr.Pwd == "" { | ||
return nil, api.ErrEmptyKubeconfig | ||
} | ||
|
||
if err := req.ParseForm(); err != nil { | ||
return nil, err | ||
} | ||
|
||
for key, val := range req.Form { | ||
switch key { | ||
case "query": | ||
pr.Query = val[0] | ||
case "step": | ||
pr.Range.Step = val[0] | ||
case "start": | ||
pr.Range.Start = val[0] | ||
case "end": | ||
pr.Range.End = val[0] | ||
case "time": | ||
pr.Range.Time = val[0] | ||
case "namespace": | ||
pr.NS = val[0] | ||
case "type": | ||
pr.Type = val[0] | ||
case "app": | ||
pr.Cluster = val[0] | ||
} | ||
} | ||
|
||
if pr.NS == "" || pr.Query == "" { | ||
return nil, api.ErrUncompleteParam | ||
} | ||
|
||
return pr, nil | ||
} | ||
|
||
// 获取客户端请求的信息 | ||
func (ps *PromServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
pathPrefix := "" | ||
switch { | ||
case req.URL.Path == pathPrefix+"/query": // 将废弃 | ||
ps.doReqPre(rw, req) | ||
case req.URL.Path == pathPrefix+"/q": | ||
ps.doReqNew(rw, req) | ||
default: | ||
http.Error(rw, "Not found", http.StatusNotFound) | ||
return | ||
} | ||
} | ||
|
||
func (ps *PromServer) doReqNew(rw http.ResponseWriter, req *http.Request) { | ||
pr, err := ps.ParseRequest(req) | ||
if err != nil { | ||
http.Error(rw, fmt.Sprintf("Bad request (%s)", err), http.StatusBadRequest) | ||
log.Printf("Bad request (%s)\n", err) | ||
return | ||
} | ||
|
||
if err := ps.Authenticate(pr); err != nil { | ||
http.Error(rw, fmt.Sprintf("Authentication failed (%s)", err), http.StatusInternalServerError) | ||
log.Printf("Authentication failed (%s)\n", err) | ||
return | ||
} | ||
|
||
res, err := ps.DBReq(pr) | ||
if err != nil { | ||
http.Error(rw, fmt.Sprintf("Query failed (%s)", err), http.StatusInternalServerError) | ||
log.Printf("Query failed (%s)\n", err) | ||
return | ||
} | ||
|
||
result, err := json.Marshal(res) | ||
if err != nil { | ||
http.Error(rw, "Result failed (invalid query expression)", http.StatusInternalServerError) | ||
log.Printf("Reulst failed (%s)\n", err) | ||
return | ||
} | ||
|
||
rw.Header().Set("Content-Type", "application/json") | ||
|
||
tmpl := template.New("responseTemplate").Delims("{{", "}}") | ||
tmpl, err = tmpl.Parse(`{{.}}`) | ||
if err != nil { | ||
log.Printf("template failed: %s\n", err) | ||
http.Error(rw, "Internal Server Error", http.StatusInternalServerError) | ||
return | ||
} | ||
if err = tmpl.Execute(rw, string(result)); err != nil { | ||
log.Printf("Reulst failed: %s\n", err) | ||
http.Error(rw, "Internal Server Error", http.StatusInternalServerError) | ||
return | ||
} | ||
type DatabaseServer struct { | ||
ConfigFile string | ||
} | ||
|
||
func (ps *PromServer) doReqPre(rw http.ResponseWriter, req *http.Request) { | ||
pr, err := ps.ParseRequest(req) | ||
func (rs *DatabaseServer) Serve(c *server.Config) { | ||
ps, err := server.NewPromServer(c) | ||
if err != nil { | ||
http.Error(rw, fmt.Sprintf("Bad request (%s)", err), http.StatusBadRequest) | ||
log.Printf("Bad request (%s)\n", err) | ||
fmt.Printf("Failed to create auth server: %s\n", err) | ||
return | ||
} | ||
|
||
if err := ps.Authenticate(pr); err != nil { | ||
http.Error(rw, fmt.Sprintf("Authentication failed (%s)", err), http.StatusInternalServerError) | ||
log.Printf("Authentication failed (%s)\n", err) | ||
log.Printf("Kubeconfig (%s)\n", pr.Pwd) | ||
hs := &http.Server{ | ||
Addr: c.Server.ListenAddress, | ||
Handler: ps, | ||
} | ||
|
||
res, err := ps.Request(pr) | ||
listener, err := net.Listen("tcp", c.Server.ListenAddress) | ||
if err != nil { | ||
http.Error(rw, fmt.Sprintf("Query failed (%s)", err), http.StatusInternalServerError) | ||
log.Printf("Query failed (%s)\n", err) | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Printf("Serve on %s\n", c.Server.ListenAddress) | ||
|
||
result, err := json.Marshal(res) | ||
if err != nil { | ||
http.Error(rw, "Result failed (invalid query expression)", http.StatusInternalServerError) | ||
log.Printf("Reulst failed (%s)\n", err) | ||
return | ||
} | ||
|
||
rw.Header().Set("Content-Type", "application/json") | ||
|
||
tmpl := template.New("responseTemplate").Delims("{{", "}}") | ||
tmpl, err = tmpl.Parse(`{{.}}`) | ||
if err != nil { | ||
log.Printf("template failed: %s\n", err) | ||
http.Error(rw, "Internal Server Error", http.StatusInternalServerError) | ||
return | ||
} | ||
if err = tmpl.Execute(rw, string(result)); err != nil { | ||
log.Printf("Reulst failed: %s\n", err) | ||
http.Error(rw, "Internal Server Error", http.StatusInternalServerError) | ||
if err := hs.Serve(listener); err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
} |
This file contains 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,9 @@ | ||
# FROM scratch | ||
FROM gcr.io/distroless/static:nonroot | ||
# FROM gengweifeng/gcr-io-distroless-static-nonroot | ||
ARG TARGETARCH | ||
COPY bin/service-minio-$TARGETARCH /manager | ||
EXPOSE 9090 | ||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["/manager"] |
This file contains 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,55 @@ | ||
IMG ?= ghcr.io/labring/sealos-minio-service:latest | ||
|
||
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) | ||
ifeq (,$(shell go env GOBIN)) | ||
GOBIN=$(shell go env GOPATH)/bin | ||
else | ||
GOBIN=$(shell go env GOBIN) | ||
endif | ||
|
||
# only support linux, non cgo | ||
PLATFORMS ?= linux_arm64 linux_amd64 | ||
GOOS=linux | ||
CGO_ENABLED=0 | ||
GOARCH=$(shell go env GOARCH) | ||
|
||
GO_BUILD_FLAGS=-trimpath -ldflags "-s -w" | ||
|
||
.PHONY: all | ||
all: build | ||
|
||
##@ General | ||
|
||
# The help target prints out all targets with their descriptions organized | ||
# beneath their categories. The categories are represented by '##@' and the | ||
# target descriptions by '##'. The awk commands is responsible for reading the | ||
# entire set of makefiles included in this invocation, looking for lines of the | ||
# file as xyz: ## something, and then pretty-format the target and help. Then, | ||
# if there's a line with ##@ something, that gets pretty-printed as a category. | ||
# More info on the usage of ANSI control characters for terminal formatting: | ||
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters | ||
# More info on the awk command: | ||
# http://linuxcommand.org/lc3_adv_awk.php | ||
|
||
.PHONY: help | ||
help: ## Display this help. | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
##@ Build | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f $(SERVICE_NAME) | ||
|
||
.PHONY: build | ||
build: clean ## Build service-hub binary. | ||
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) go build $(GO_BUILD_FLAGS) -o bin/manager main.go | ||
|
||
.PHONY: docker-build | ||
docker-build: build | ||
mv bin/manager bin/service-minio-${TARGETARCH} | ||
docker build -t $(IMG) . | ||
|
||
.PHONY: docker-push | ||
docker-push: | ||
docker push $(IMG) |
This file contains 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,5 @@ | ||
FROM scratch | ||
COPY registry registry | ||
COPY manifests manifests | ||
|
||
CMD ["kubectl apply -f manifests/deploy.yaml"] |
Oops, something went wrong.