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

APIGOV-29342 - agent startup improvements #865

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/Axway/agent-sdk/pkg/cache"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/Axway/agent-sdk/pkg/util/errors"
hc "github.com/Axway/agent-sdk/pkg/util/healthcheck"
"github.com/Axway/agent-sdk/pkg/util/log"
)
Expand Down Expand Up @@ -233,7 +234,19 @@ func InitializeProfiling(cpuProfile, memProfile string) {
}
}

func FinalizeInitialization() error {
err := registerExternalIDPs()
if err != nil {
logger.WithError(err).Error("failed to register CRDs for external IdP config")
}
return nil
}

func registerExternalIDPs() error {
if !util.IsNotTest() || !agent.agentFeaturesCfg.ConnectionToCentralEnabled() || agent.cfg.GetUsageReportingConfig().IsOfflineMode() {
return nil
}

if agent.cfg.GetAgentType() != config.TraceabilityAgent {
idPCfg := agent.agentFeaturesCfg.GetExternalIDPConfig()
if idPCfg == nil {
Expand All @@ -256,6 +269,24 @@ func registerExternalIDPs() error {
return nil
}

func CacheInitSync() error {
if !util.IsNotTest() || !agent.agentFeaturesCfg.ConnectionToCentralEnabled() || agent.cfg.GetUsageReportingConfig().IsOfflineMode() {
return nil
}

eventSync, err := newEventSync()
if err != nil {
return errors.Wrap(errors.ErrInitServicesNotReady, err.Error())
}

if err := eventSync.SyncCache(); err != nil {
return errors.Wrap(errors.ErrInitServicesNotReady, err.Error())
}
// set the rebuild function in the agent resource manager
agent.agentResourceManager.SetRebuildCacheFunc(eventSync)
return nil
}

func registerCredentialProvider(idp config.IDPConfig, tlsCfg config.TLSConfig, proxyURL string, clientTimeout time.Duration) error {
err := GetAuthProviderRegistry().RegisterProvider(idp, tlsCfg, proxyURL, clientTimeout)
if err != nil {
Expand Down
11 changes: 3 additions & 8 deletions pkg/agent/eventsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type EventSync struct {
discoveryCache *discoveryCache
}

// NewEventSync creates an EventSync
func NewEventSync() (*EventSync, error) {
// newEventSync creates an EventSync
func newEventSync() (*EventSync, error) {
migrations := []migrate.Migrator{}

// Make sure only DA and Governance agents run migration processes
Expand Down Expand Up @@ -84,12 +84,7 @@ func (es *EventSync) SyncCache() error {
}
}

err := registerExternalIDPs()
if err != nil {
logger.WithError(err).Warn("failed to register CRDs for external IdP config")
}

err = es.startCentralEventProcessor()
err := es.startCentralEventProcessor()
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/eventsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestEventSync_pollMode(t *testing.T) {
InitializeForTest(mc)
assert.Nil(t, err)

es, err := NewEventSync()
es, err := newEventSync()
assert.Nil(t, err)
assert.NotNil(t, es.watchTopic)
assert.NotNil(t, es.discoveryCache)
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestEventSync_streamMode(t *testing.T) {
InitializeForTest(mc)
assert.Nil(t, err)

es, err := NewEventSync()
es, err := newEventSync()
assert.Nil(t, err)
assert.NotNil(t, es.watchTopic)
assert.NotNil(t, es.discoveryCache)
Expand Down
43 changes: 32 additions & 11 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ const (
httpprofile = "httpprofile"
)

type NewCommandOption func(*agentRootCommand)

// CommandHandler - Root command execution handler
type CommandHandler func() error

// InitConfigHandler - Handler to be invoked on config initialization
type InitConfigHandler func(centralConfig config.CentralConfig) (interface{}, error)
type FinalizeAgentInitHandler func() error

// AgentRootCmd - Root Command for the Agents
type AgentRootCmd interface {
Expand All @@ -61,6 +64,7 @@ type agentRootCommand struct {
rootCmd *cobra.Command
commandHandler CommandHandler
initConfigHandler InitConfigHandler
finalizeAgentInit FinalizeAgentInitHandler
agentType config.AgentType
props properties.Properties
statusCfg config.StatusConfig
Expand Down Expand Up @@ -95,8 +99,14 @@ func buildAgentInfo(desc string) string {
return fmt.Sprintf("%s version %s-%s, Amplify Agents SDK version %s", desc, BuildVersion, BuildCommitSha, SDKBuildVersion)
}

func WithFinalizeAgentInitFunc(f FinalizeAgentInitHandler) NewCommandOption {
return func(c *agentRootCommand) {
c.finalizeAgentInit = f
}
}

// NewRootCmd - Creates a new Agent Root Command
func NewRootCmd(exeName, desc string, initConfigHandler InitConfigHandler, commandHandler CommandHandler, agentType config.AgentType) AgentRootCmd {
func NewRootCmd(exeName, desc string, initConfigHandler InitConfigHandler, commandHandler CommandHandler, agentType config.AgentType, opts ...NewCommandOption) AgentRootCmd {
c := &agentRootCommand{
agentName: exeName,
commandHandler: commandHandler,
Expand All @@ -106,6 +116,10 @@ func NewRootCmd(exeName, desc string, initConfigHandler InitConfigHandler, comma
initialized: false,
}

for _, o := range opts {
o(c)
}

// use the description from the build if available
if BuildAgentDescription != "" {
desc = BuildAgentDescription
Expand Down Expand Up @@ -136,7 +150,7 @@ func NewRootCmd(exeName, desc string, initConfigHandler InitConfigHandler, comma
}

// NewCmd - Creates a new Agent Root Command using existing cmd
func NewCmd(rootCmd *cobra.Command, exeName, desc string, initConfigHandler InitConfigHandler, commandHandler CommandHandler, agentType config.AgentType) AgentRootCmd {
func NewCmd(rootCmd *cobra.Command, exeName, desc string, initConfigHandler InitConfigHandler, commandHandler CommandHandler, agentType config.AgentType, opts ...NewCommandOption) AgentRootCmd {
c := &agentRootCommand{
agentName: exeName,
commandHandler: commandHandler,
Expand All @@ -146,6 +160,10 @@ func NewCmd(rootCmd *cobra.Command, exeName, desc string, initConfigHandler Init
initialized: false,
}

for _, o := range opts {
o(c)
}

// use the description from the build if available
if BuildAgentDescription != "" {
desc = BuildAgentDescription
Expand Down Expand Up @@ -354,18 +372,21 @@ func (c *agentRootCommand) initConfig() error {
}

func (c *agentRootCommand) finishInit() error {
if util.IsNotTest() && c.agentFeaturesCfg.ConnectionToCentralEnabled() && !c.centralCfg.GetUsageReportingConfig().IsOfflineMode() {
eventSync, err := agent.NewEventSync()
if err != nil {
return errors.Wrap(errors.ErrInitServicesNotReady, err.Error())
}
err := agent.FinalizeInitialization()
if err != nil {
return err
}

if err := eventSync.SyncCache(); err != nil {
return errors.Wrap(errors.ErrInitServicesNotReady, err.Error())
if c.finalizeAgentInit != nil {
err := c.finalizeAgentInit()
if err != nil {
return err
}
// set the rebuild function in the agent resource manager
agent.GetAgentResourceManager().SetRebuildCacheFunc(eventSync)
}

err = agent.CacheInitSync()
if err != nil {
return err
}

// Start the initial and recurring version check jobs
Expand Down
Loading