fix(edge-stack) make protainer compatible with previous agent EE-5614 (#9220)

pull/9221/head
cmeng 1 year ago committed by GitHub
parent 8c533bee67
commit 7acd1080ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -14,6 +14,9 @@ type (
// Name of the stack
Name string
// Content of the stack file (for compatibility to agent version less than 2.19.0)
StackFileContent string
// Content of stack folder
DirEntries []filesystem.DirEntry
// Name of the stack entry file

@ -2,9 +2,12 @@ package filesystem
import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
"strings"
"golang.org/x/mod/semver"
)
type DirEntry struct {
@ -46,6 +49,22 @@ func FilterDirForEntryFile(dirEntries []DirEntry, entryFile string) []DirEntry {
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
// File content is base64-encoded
func LoadDir(dir string) ([]DirEntry, error) {
@ -127,14 +146,22 @@ func PersistDir(dir string, dirEntries []DirEntry) error {
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 {
for index, dirEntry := range dirEntries {
if dirEntry.IsFile && dirEntry.Content != "" {
decodedBytes, err := base64.StdEncoding.DecodeString(dirEntry.Content)
decodedFile, err := DecodeFileContent(dirEntry.Content)
if err != nil {
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)
}
fileContent, err := filesystem.FilterDirForCompatibility(dirEntries, fileName, endpoint.Agent.Version)
if err != nil {
return httperror.InternalServerError("File not found", err)
}
dirEntries = filesystem.FilterDirForEntryFile(dirEntries, fileName)
return response.JSON(w, edge.StackPayload{
DirEntries: dirEntries,
EntryFileName: fileName,
StackFileContent: fileContent,
Name: edgeStack.Name,
Namespace: namespace,
})

Loading…
Cancel
Save