Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EE-1120: Feat/registry sync #231

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type (

// EdgeStackConfig represent an Edge stack config
EdgeStackConfig struct {
Name string
FileContent string
Name string
FileContent string
ImageMapping map[string]string // a map of stackfile image to imageCache url(with sha)
}

// HostInfo is the representation of the collection of host information
Expand All @@ -60,7 +61,6 @@ type (
AgentServerPort string
AgentSecurityShutdown time.Duration
ClusterAddress string
HostManagementEnabled bool
SharedSecret string
EdgeMode bool
EdgeKey string
Expand Down
104 changes: 19 additions & 85 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
http2 "github.com/portainer/agent/edge/http"
"log"
gohttp "net/http"
goos "os"
Expand All @@ -13,12 +14,10 @@ import (
"github.com/portainer/agent"
"github.com/portainer/agent/crypto"
"github.com/portainer/agent/docker"
"github.com/portainer/agent/edge"
"github.com/portainer/agent/exec"
"github.com/portainer/agent/filesystem"
"github.com/portainer/agent/ghw"
"github.com/portainer/agent/http"
"github.com/portainer/agent/http/client"
"github.com/portainer/agent/internal/edge"
"github.com/portainer/agent/kubernetes"
"github.com/portainer/agent/logutils"
"github.com/portainer/agent/net"
Expand Down Expand Up @@ -81,7 +80,8 @@ func main() {
log.Printf("[WARN] [main,podman] [message: Unable to retrieve local agent IP address, using '%s' instead] [error: %s]", options.AgentServerAddr, err)
advertiseAddr = options.AgentServerAddr
} else {
log.Fatalf("[ERROR] [main,docker] [message: Unable to retrieve local agent IP address] [error: %s]", err)
log.Printf("[ERROR] [main,docker] [message: Unable to retrieve local agent IP address] [error: %s]", err)
advertiseAddr = options.AgentServerAddr
}
}

Expand Down Expand Up @@ -179,24 +179,26 @@ func main() {
// !Security

// Edge
edgeManagerParameters := &edge.ManagerParameters{
Options: options,
AdvertiseAddr: advertiseAddr,
ClusterService: clusterService,
DockerInfoService: dockerInfoService,
ContainerPlatform: containerPlatform,
}
edgeManager := edge.NewManager(edgeManagerParameters)

var edgeManager *edge.Manager
if options.EdgeMode {
edgeKey, err := retrieveEdgeKey(options.EdgeKey, clusterService)
edgeManagerParameters := &edge.ManagerParameters{
Options: options,
AdvertiseAddr: advertiseAddr,
ClusterService: clusterService,
DockerInfoService: dockerInfoService,
ContainerPlatform: containerPlatform,
}
edgeManager = edge.NewManager(edgeManagerParameters)

edgeKey, err := edge.RetrieveEdgeKey(options.EdgeKey, clusterService)
if err != nil {
log.Printf("[ERROR] [main,edge] [message: Unable to retrieve Edge key] [error: %s]", err)
}

if edgeKey != "" {
log.Println("[DEBUG] [main,edge] [message: Edge key found in environment. Associating Edge key]")

// TODO: this fails when RetreiveEdgeKey came from an existing file, as mode 0444 can't be written to - unless you're root...
err := edgeManager.SetKey(edgeKey)
if err != nil {
log.Fatalf("[ERROR] [main,edge] [message: Unable to associate Edge key] [error: %s]", err)
Expand Down Expand Up @@ -232,7 +234,7 @@ func main() {
ContainerPlatform: containerPlatform,
}

if edgeManager.IsEdgeModeEnabled() {
if options.EdgeMode {
config.Addr = advertiseAddr
}

Expand All @@ -253,7 +255,7 @@ func main() {
func startAPIServer(config *http.APIServerConfig) error {
server := http.NewAPIServer(config)

if config.EdgeManager.IsEdgeModeEnabled() {
if config.EdgeManager != nil {
return server.StartUnsecured()
}

Expand All @@ -266,7 +268,7 @@ func parseOptions() (*agent.Options, error) {
}

func serveEdgeUI(edgeManager *edge.Manager, serverAddr, serverPort string) {
edgeServer := http.NewEdgeServer(edgeManager)
edgeServer := http2.NewEdgeServer(edgeManager)

go func() {
log.Printf("[INFO] [main,edge,http] [server_address: %s] [server_port: %s] [message: Starting Edge server]", serverAddr, serverPort)
Expand All @@ -289,71 +291,3 @@ func serveEdgeUI(edgeManager *edge.Manager, serverAddr, serverPort string) {
}
}()
}

func retrieveEdgeKey(edgeKey string, clusterService agent.ClusterService) (string, error) {

if edgeKey != "" {
log.Println("[INFO] [main,edge] [message: Edge key loaded from options]")
return edgeKey, nil
}

var keyRetrievalError error

edgeKey, keyRetrievalError = retrieveEdgeKeyFromFilesystem()
if keyRetrievalError != nil {
return "", keyRetrievalError
}

if edgeKey == "" && clusterService != nil {
edgeKey, keyRetrievalError = retrieveEdgeKeyFromCluster(clusterService)
if keyRetrievalError != nil {
return "", keyRetrievalError
}
}

return edgeKey, nil
}

func retrieveEdgeKeyFromFilesystem() (string, error) {
var edgeKey string

edgeKeyFilePath := fmt.Sprintf("%s/%s", agent.DataDirectory, agent.EdgeKeyFile)

keyFileExists, err := filesystem.FileExists(edgeKeyFilePath)
if err != nil {
return "", err
}

if keyFileExists {
filesystemKey, err := filesystem.ReadFromFile(edgeKeyFilePath)
if err != nil {
return "", err
}

log.Println("[INFO] [main,edge] [message: Edge key loaded from the filesystem]")
edgeKey = string(filesystemKey)
}

return edgeKey, nil
}

func retrieveEdgeKeyFromCluster(clusterService agent.ClusterService) (string, error) {
var edgeKey string

member := clusterService.GetMemberWithEdgeKeySet()
if member != nil {
httpCli := client.NewAPIClient()

memberAddr := fmt.Sprintf("%s:%s", member.IPAddress, member.Port)
memberKey, err := httpCli.GetEdgeKey(memberAddr)
if err != nil {
log.Printf("[ERROR] [main,edge,http,cluster] [message: Unable to retrieve Edge key from cluster member] [error: %s]", err)
return "", err
}

log.Println("[INFO] [main,edge] [message: Edge key loaded from cluster]")
edgeKey = memberKey
}

return edgeKey, nil
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func NewPortainerClient(serverAddress, endpointID, edgeID string, insecurePoll b
type stackConfigResponse struct {
Name string
StackFileContent string
ImageMapping map[string]string // a map of stackfile image to imageCache url(with sha)
}

// GetEdgeStackConfig retrieves the configuration associated to an Edge stack
Expand Down Expand Up @@ -77,7 +78,7 @@ func (client *PortainerClient) GetEdgeStackConfig(edgeStackID int) (*agent.EdgeS
return nil, err
}

return &agent.EdgeStackConfig{Name: data.Name, FileContent: data.StackFileContent}, nil
return &agent.EdgeStackConfig{Name: data.Name, FileContent: data.StackFileContent, ImageMapping: data.ImageMapping}, nil
}

type setEdgeStackStatusPayload struct {
Expand Down
33 changes: 14 additions & 19 deletions internal/edge/edge.go → edge/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package edge
import (
"errors"
"fmt"
"github.com/portainer/agent/edge/scheduler"
"github.com/portainer/agent/edge/stack"
"log"
"time"

Expand All @@ -16,13 +18,12 @@ type (
advertiseAddr string
agentOptions *agent.Options
clusterService agent.ClusterService
edgeMode bool
dockerInfoService agent.DockerInfoService
key *edgeKey
logsManager *logsManager
logsManager *scheduler.LogsManager
pollService *PollService
pollServiceConfig *pollServiceConfig
stackManager *StackManager
stackManager *stack.StackManager
}

// ManagerParameters represents an object used to create a Manager
Expand All @@ -42,7 +43,6 @@ func NewManager(parameters *ManagerParameters) *Manager {
dockerInfoService: parameters.DockerInfoService,
agentOptions: parameters.Options,
advertiseAddr: parameters.AdvertiseAddr,
edgeMode: parameters.Options.EdgeMode,
containerPlatform: parameters.ContainerPlatform,
}
}
Expand Down Expand Up @@ -70,14 +70,14 @@ func (manager *Manager) Start() error {

log.Printf("[DEBUG] [internal,edge] [api_addr: %s] [edge_id: %s] [poll_frequency: %s] [inactivity_timeout: %s] [insecure_poll: %t]", pollServiceConfig.APIServerAddr, pollServiceConfig.EdgeID, pollServiceConfig.PollFrequency, pollServiceConfig.InactivityTimeout, pollServiceConfig.InsecurePoll)

stackManager, err := newStackManager(manager.key.PortainerInstanceURL, manager.key.EndpointID, manager.agentOptions.EdgeID, pollServiceConfig.InsecurePoll)
stackManager, err := stack.NewStackManager(manager.key.PortainerInstanceURL, manager.key.EndpointID, manager.agentOptions.EdgeID, pollServiceConfig.InsecurePoll)
if err != nil {
return err
}
manager.stackManager = stackManager

manager.logsManager = newLogsManager(manager.key.PortainerInstanceURL, manager.key.EndpointID, manager.agentOptions.EdgeID, pollServiceConfig.InsecurePoll)
manager.logsManager.start()
manager.logsManager = scheduler.NewLogsManager(manager.key.PortainerInstanceURL, manager.key.EndpointID, manager.agentOptions.EdgeID, pollServiceConfig.InsecurePoll)
manager.logsManager.Start()

pollService, err := newPollService(manager.stackManager, manager.logsManager, pollServiceConfig)
if err != nil {
Expand All @@ -93,11 +93,6 @@ func (manager *Manager) Start() error {
return nil
}

// IsEdgeModeEnabled returns true if edge mode is enabled
func (manager *Manager) IsEdgeModeEnabled() bool {
return manager.edgeMode
}

// ResetActivityTimer resets the activity timer
func (manager *Manager) ResetActivityTimer() {
manager.pollService.resetActivityTimer()
Expand Down Expand Up @@ -144,13 +139,13 @@ func (manager *Manager) startEdgeBackgroundProcessOnKubernetes(runtimeCheckFrequ
return
}

err = manager.stackManager.setEngineStatus(engineTypeKubernetes)
err = manager.stackManager.SetEngineStatus(stack.EngineTypeKubernetes)
if err != nil {
log.Printf("[ERROR] [internal,edge,runtime] [message: unable to set engine status] [error: %s]", err)
return
}

err = manager.stackManager.start()
err = manager.stackManager.Start()
if err != nil {
log.Printf("[ERROR] [internal,edge,runtime] [message: unable to start stack manager] [error: %s]", err)
return
Expand Down Expand Up @@ -191,22 +186,22 @@ func (manager *Manager) checkDockerRuntimeConfig() error {
log.Printf("[DEBUG] [internal,edge,runtime,docker] [message: Docker runtime configuration check] [engine_status: %d] [leader_node: %t]", runtimeConfiguration.DockerConfiguration.EngineStatus, agentRunsOnLeaderNode)

if !agentRunsOnSwarm || agentRunsOnLeaderNode {
engineStatus := engineTypeDockerStandalone
engineStatus := stack.EngineTypeDockerStandalone
if agentRunsOnSwarm {
engineStatus = engineTypeDockerSwarm
engineStatus = stack.EngineTypeDockerSwarm
}

err = manager.pollService.start()
if err != nil {
return err
}

err = manager.stackManager.setEngineStatus(engineStatus)
err = manager.stackManager.SetEngineStatus(engineStatus)
if err != nil {
return err
}

err = manager.stackManager.start()
err = manager.stackManager.Start()
if err != nil {
return err
}
Expand All @@ -217,7 +212,7 @@ func (manager *Manager) checkDockerRuntimeConfig() error {
return err
}

err = manager.stackManager.stop()
err = manager.stackManager.Stop()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion http/edge.go → edge/http/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http"
"time"

"github.com/portainer/agent/internal/edge"
"github.com/portainer/agent/edge"

"github.com/gorilla/mux"
)
Expand Down
Loading