mirror of https://github.com/portainer/portainer
fix(edge-stack) make protainer compatible with previous agent EE-5614 (#9220)
parent
8c533bee67
commit
7acd1080ad
|
@ -14,6 +14,9 @@ type (
|
||||||
// Name of the stack
|
// Name of the stack
|
||||||
Name string
|
Name string
|
||||||
|
|
||||||
|
// Content of the stack file (for compatibility to agent version less than 2.19.0)
|
||||||
|
StackFileContent string
|
||||||
|
|
||||||
// Content of stack folder
|
// Content of stack folder
|
||||||
DirEntries []filesystem.DirEntry
|
DirEntries []filesystem.DirEntry
|
||||||
// Name of the stack entry file
|
// Name of the stack entry file
|
||||||
|
|
|
@ -2,9 +2,12 @@ package filesystem
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"golang.org/x/mod/semver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DirEntry struct {
|
type DirEntry struct {
|
||||||
|
@ -46,6 +49,22 @@ func FilterDirForEntryFile(dirEntries []DirEntry, entryFile string) []DirEntry {
|
||||||
return filteredDirEntries
|
return filteredDirEntries
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FilterDirForCompatibility(dirEntries []DirEntry, entryFilePath, agentVersion string) (string, error) {
|
||||||
|
|
||||||
|
if semver.Compare(fmt.Sprintf("v%s", agentVersion), "v2.19.0") == -1 {
|
||||||
|
for _, dirEntry := range dirEntries {
|
||||||
|
if dirEntry.IsFile {
|
||||||
|
if dirEntry.Name == entryFilePath {
|
||||||
|
return DecodeFileContent(dirEntry.Content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("Entry file %s not found in dir entries", entryFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
// LoadDir reads all files and folders recursively from the given directory
|
// LoadDir reads all files and folders recursively from the given directory
|
||||||
// File content is base64-encoded
|
// File content is base64-encoded
|
||||||
func LoadDir(dir string) ([]DirEntry, error) {
|
func LoadDir(dir string) ([]DirEntry, error) {
|
||||||
|
@ -127,14 +146,22 @@ func PersistDir(dir string, dirEntries []DirEntry) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DecodeFileContent(encodedFileContent string) (string, error) {
|
||||||
|
decodedBytes, err := base64.StdEncoding.DecodeString(encodedFileContent)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(decodedBytes), nil
|
||||||
|
}
|
||||||
|
|
||||||
func DecodeDirEntries(dirEntries []DirEntry) error {
|
func DecodeDirEntries(dirEntries []DirEntry) error {
|
||||||
for index, dirEntry := range dirEntries {
|
for index, dirEntry := range dirEntries {
|
||||||
if dirEntry.IsFile && dirEntry.Content != "" {
|
if dirEntry.IsFile && dirEntry.Content != "" {
|
||||||
decodedBytes, err := base64.StdEncoding.DecodeString(dirEntry.Content)
|
decodedFile, err := DecodeFileContent(dirEntry.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dirEntries[index].Content = string(decodedBytes)
|
dirEntries[index].Content = decodedFile
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,11 +75,17 @@ func (handler *Handler) endpointEdgeStackInspect(w http.ResponseWriter, r *http.
|
||||||
return httperror.InternalServerError("Unable to load repository", err)
|
return httperror.InternalServerError("Unable to load repository", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fileContent, err := filesystem.FilterDirForCompatibility(dirEntries, fileName, endpoint.Agent.Version)
|
||||||
|
if err != nil {
|
||||||
|
return httperror.InternalServerError("File not found", err)
|
||||||
|
}
|
||||||
|
|
||||||
dirEntries = filesystem.FilterDirForEntryFile(dirEntries, fileName)
|
dirEntries = filesystem.FilterDirForEntryFile(dirEntries, fileName)
|
||||||
|
|
||||||
return response.JSON(w, edge.StackPayload{
|
return response.JSON(w, edge.StackPayload{
|
||||||
DirEntries: dirEntries,
|
DirEntries: dirEntries,
|
||||||
EntryFileName: fileName,
|
EntryFileName: fileName,
|
||||||
|
StackFileContent: fileContent,
|
||||||
Name: edgeStack.Name,
|
Name: edgeStack.Name,
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue