mirror of
https://github.com/portainer/portainer.git
synced 2025-07-25 08:19:40 +02:00
chore(code): clean up the code EE-5719 (#9183)
This commit is contained in:
parent
979af5301e
commit
64b227b2e1
24 changed files with 93 additions and 87 deletions
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/portainer/libhttp/response"
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/stacks/stackutils"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
|
@ -68,10 +69,12 @@ func (handler *Handler) customTemplateGitFetch(w http.ResponseWriter, r *http.Re
|
|||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("failed to download git repository")
|
||||
rbErr := rollbackCustomTemplate(backupPath, customTemplate.ProjectPath)
|
||||
|
||||
if err != nil {
|
||||
rbErr := rollbackCustomTemplate(backupPath, customTemplate.ProjectPath)
|
||||
return httperror.InternalServerError("Failed to rollback the custom template folder", rbErr)
|
||||
}
|
||||
|
||||
return httperror.InternalServerError("Failed to download git repository", err)
|
||||
}
|
||||
|
||||
|
@ -104,11 +107,7 @@ func backupCustomTemplate(projectPath string) (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
err = os.Mkdir(projectPath, stat.Mode())
|
||||
if err != nil {
|
||||
return backupPath, err
|
||||
}
|
||||
return backupPath, nil
|
||||
return backupPath, os.Mkdir(projectPath, stat.Mode())
|
||||
}
|
||||
|
||||
func rollbackCustomTemplate(backupPath, projectPath string) error {
|
||||
|
@ -116,6 +115,7 @@ func rollbackCustomTemplate(backupPath, projectPath string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Rename(backupPath, projectPath)
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var testFileContent string = "abcdefg"
|
||||
var testFileContent = "abcdefg"
|
||||
|
||||
type TestGitService struct {
|
||||
portainer.GitService
|
||||
|
@ -32,6 +32,7 @@ type TestGitService struct {
|
|||
|
||||
func (g *TestGitService) CloneRepository(destination string, repositoryURL, referenceName string, username, password string, tlsSkipVerify bool) error {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
return createTestFile(g.targetFilePath)
|
||||
}
|
||||
|
||||
|
@ -53,7 +54,9 @@ func createTestFile(targetPath string) error {
|
|||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(testFileContent)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -152,7 +155,7 @@ func Test_customTemplateGitFetch(t *testing.T) {
|
|||
wg.Add(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func(j int) {
|
||||
if j%1 == 0 {
|
||||
if j%2 == 0 {
|
||||
singleAPIRequest(h, jwt1, is, "abcdefg")
|
||||
} else {
|
||||
singleAPIRequest(h, jwt2, is, "abcdefg")
|
||||
|
|
|
@ -38,6 +38,7 @@ func (handler *Handler) getKubernetesConfig(w http.ResponseWriter, r *http.Reque
|
|||
if err != nil {
|
||||
return httperror.Forbidden("Permission denied to access environment", err)
|
||||
}
|
||||
|
||||
bearerToken, err := handler.JwtService.GenerateTokenForKubeconfig(tokenData)
|
||||
if err != nil {
|
||||
return httperror.InternalServerError("Unable to generate JWT token", err)
|
||||
|
@ -47,14 +48,12 @@ func (handler *Handler) getKubernetesConfig(w http.ResponseWriter, r *http.Reque
|
|||
if handlerErr != nil {
|
||||
return handlerErr
|
||||
}
|
||||
|
||||
if len(endpoints) == 0 {
|
||||
return httperror.BadRequest("empty endpoints list", errors.New("empty endpoints list"))
|
||||
}
|
||||
|
||||
config, handlerErr := handler.buildConfig(r, tokenData, bearerToken, endpoints, false)
|
||||
if handlerErr != nil {
|
||||
return handlerErr
|
||||
}
|
||||
config := handler.buildConfig(r, tokenData, bearerToken, endpoints, false)
|
||||
|
||||
return writeFileContent(w, r, endpoints, tokenData, config)
|
||||
}
|
||||
|
@ -92,7 +91,9 @@ func (handler *Handler) filterUserKubeEndpoints(r *http.Request) ([]portainer.En
|
|||
}
|
||||
endpoints = append(endpoints, *endpoint)
|
||||
}
|
||||
|
||||
filteredEndpoints := security.FilterEndpoints(endpoints, endpointGroups, securityContext)
|
||||
|
||||
return filteredEndpoints, nil
|
||||
}
|
||||
|
||||
|
@ -101,10 +102,12 @@ func (handler *Handler) filterUserKubeEndpoints(r *http.Request) ([]portainer.En
|
|||
if err != nil {
|
||||
return nil, httperror.InternalServerError("Unable to retrieve environments from the database", err)
|
||||
}
|
||||
|
||||
for _, endpoint := range endpoints {
|
||||
if !endpointutils.IsKubernetesEndpoint(&endpoint) {
|
||||
continue
|
||||
}
|
||||
|
||||
kubeEndpoints = append(kubeEndpoints, endpoint)
|
||||
}
|
||||
|
||||
|
@ -112,13 +115,15 @@ func (handler *Handler) filterUserKubeEndpoints(r *http.Request) ([]portainer.En
|
|||
if len(excludeEndpointIDs) > 0 {
|
||||
filteredEndpoints = endpointutils.FilterByExcludeIDs(filteredEndpoints, excludeEndpointIDs)
|
||||
}
|
||||
|
||||
return filteredEndpoints, nil
|
||||
}
|
||||
|
||||
func (handler *Handler) buildConfig(r *http.Request, tokenData *portainer.TokenData, bearerToken string, endpoints []portainer.Endpoint, isInternal bool) (*clientV1.Config, *httperror.HandlerError) {
|
||||
func (handler *Handler) buildConfig(r *http.Request, tokenData *portainer.TokenData, bearerToken string, endpoints []portainer.Endpoint, isInternal bool) *clientV1.Config {
|
||||
var configAuthInfos []clientV1.NamedAuthInfo
|
||||
|
||||
configClusters := make([]clientV1.NamedCluster, len(endpoints))
|
||||
configContexts := make([]clientV1.NamedContext, len(endpoints))
|
||||
var configAuthInfos []clientV1.NamedAuthInfo
|
||||
authInfosSet := make(map[string]bool)
|
||||
|
||||
for idx, endpoint := range endpoints {
|
||||
|
@ -127,6 +132,7 @@ func (handler *Handler) buildConfig(r *http.Request, tokenData *portainer.TokenD
|
|||
|
||||
configClusters[idx] = handler.buildCluster(r, endpoint, isInternal)
|
||||
configContexts[idx] = buildContext(serviceAccountName, endpoint)
|
||||
|
||||
if !authInfosSet[serviceAccountName] {
|
||||
configAuthInfos = append(configAuthInfos, buildAuthInfo(serviceAccountName, bearerToken))
|
||||
authInfosSet[serviceAccountName] = true
|
||||
|
@ -140,7 +146,7 @@ func (handler *Handler) buildConfig(r *http.Request, tokenData *portainer.TokenD
|
|||
Contexts: configContexts,
|
||||
CurrentContext: configContexts[0].Name,
|
||||
AuthInfos: configAuthInfos,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// buildCluster builds a Kubernetes cluster configuration based on the endpoint and if it's used internally or externally.
|
||||
|
|
|
@ -166,22 +166,13 @@ func (handler *Handler) kubeClient(next http.Handler) http.Handler {
|
|||
singleEndpointList := []portainer.Endpoint{
|
||||
*endpoint,
|
||||
}
|
||||
config, handlerErr := handler.buildConfig(
|
||||
config := handler.buildConfig(
|
||||
r,
|
||||
tokenData,
|
||||
bearerToken,
|
||||
singleEndpointList,
|
||||
true,
|
||||
)
|
||||
if err != nil {
|
||||
httperror.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
"Unable to build endpoint kubeconfig",
|
||||
handlerErr.Err,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if len(config.Clusters) == 0 {
|
||||
httperror.WriteError(
|
||||
|
|
|
@ -59,9 +59,11 @@ func (r K8sIngressInfo) Validate(request *http.Request) error {
|
|||
if r.Name == "" {
|
||||
return errors.New("missing ingress name from the request payload")
|
||||
}
|
||||
|
||||
if r.Namespace == "" {
|
||||
return errors.New("missing ingress Namespace from the request payload")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -69,10 +71,12 @@ func (r K8sIngressDeleteRequests) Validate(request *http.Request) error {
|
|||
if len(r) == 0 {
|
||||
return errors.New("missing deletion request list in payload")
|
||||
}
|
||||
|
||||
for ns := range r {
|
||||
if len(ns) == 0 {
|
||||
return errors.New("deletion given with empty namespace")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -48,12 +48,15 @@ func (s *K8sServiceInfo) Validate(request *http.Request) error {
|
|||
if s.Name == "" {
|
||||
return errors.New("missing service name from the request payload")
|
||||
}
|
||||
|
||||
if s.Namespace == "" {
|
||||
return errors.New("missing service namespace from the request payload")
|
||||
}
|
||||
|
||||
if s.Ports == nil {
|
||||
return errors.New("missing service ports from the request payload")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -61,10 +64,12 @@ func (r K8sServiceDeleteRequests) Validate(request *http.Request) error {
|
|||
if len(r) == 0 {
|
||||
return errors.New("missing deletion request list in payload")
|
||||
}
|
||||
|
||||
for ns := range r {
|
||||
if len(ns) == 0 {
|
||||
return errors.New("deletion given with empty namespace")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -8,8 +8,7 @@ func (transport *baseTransport) proxyDeploymentsRequest(request *http.Request, n
|
|||
switch request.Method {
|
||||
case http.MethodPost, http.MethodPatch:
|
||||
transport.refreshRegistry(request, namespace)
|
||||
return transport.executeKubernetesRequest(request)
|
||||
default:
|
||||
return transport.executeKubernetesRequest(request)
|
||||
}
|
||||
|
||||
return transport.executeKubernetesRequest(request)
|
||||
}
|
||||
|
|
|
@ -5,11 +5,9 @@ import (
|
|||
)
|
||||
|
||||
func (transport *baseTransport) proxyPodsRequest(request *http.Request, namespace, requestPath string) (*http.Response, error) {
|
||||
switch request.Method {
|
||||
case "DELETE":
|
||||
if request.Method == http.MethodDelete {
|
||||
transport.refreshRegistry(request, namespace)
|
||||
return transport.executeKubernetesRequest(request)
|
||||
default:
|
||||
return transport.executeKubernetesRequest(request)
|
||||
}
|
||||
|
||||
return transport.executeKubernetesRequest(request)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue