Skip to content

Commit

Permalink
APIGOV-28713 - migrate dataplane types on agents (#825)
Browse files Browse the repository at this point in the history
* migrate dataplane types on agents

* updates on dataplane type

* update test
  • Loading branch information
jcollins-axway authored Sep 5, 2024
1 parent e790ab5 commit c1fb4c8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
47 changes: 43 additions & 4 deletions pkg/agent/resource/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type executeAPIClient interface {
CreateSubResource(rm v1.ResourceMeta, subs map[string]interface{}) error
GetResource(url string) (*v1.ResourceInstance, error)
CreateResourceInstance(ri apiv1.Interface) (*apiv1.ResourceInstance, error)
UpdateResourceInstance(ri apiv1.Interface) (*apiv1.ResourceInstance, error)
}

type agentResourceManager struct {
Expand Down Expand Up @@ -118,6 +119,8 @@ func (a *agentResourceManager) FetchAgentResource() error {
} else {
return err
}
} else {
a.checkAgentResource()
}

a.onResourceChange()
Expand Down Expand Up @@ -149,7 +152,7 @@ func (a *agentResourceManager) UpdateAgentStatus(status, prevStatus, message str
}

// See if we need to rebuildCache
timeToRebuild, err := a.shouldRebuildCache()
timeToRebuild, _ := a.shouldRebuildCache()
if timeToRebuild && a.rebuildCache != nil {
a.rebuildCache.RebuildCache()
}
Expand All @@ -162,7 +165,7 @@ func (a *agentResourceManager) UpdateAgentStatus(status, prevStatus, message str
subResources[definitions.XAgentDetails] = agentInstance.SubResources[definitions.XAgentDetails]
}

err = a.apicClient.CreateSubResource(agentInstance.ResourceMeta, subResources)
err := a.apicClient.CreateSubResource(agentInstance.ResourceMeta, subResources)
return err
}

Expand Down Expand Up @@ -275,9 +278,13 @@ func (a *agentResourceManager) getAgentResourceType() *v1.ResourceInstance {
var agentRes v1.Interface
switch a.cfg.GetAgentType() {
case config.DiscoveryAgent:
agentRes = management.NewDiscoveryAgent(a.cfg.GetAgentName(), a.cfg.GetEnvironmentName())
res := management.NewDiscoveryAgent(a.cfg.GetAgentName(), a.cfg.GetEnvironmentName())
res.Spec.DataplaneType = config.AgentDataPlaneType
agentRes = res
case config.TraceabilityAgent:
agentRes = management.NewTraceabilityAgent(a.cfg.GetAgentName(), a.cfg.GetEnvironmentName())
res := management.NewTraceabilityAgent(a.cfg.GetAgentName(), a.cfg.GetEnvironmentName())
res.Spec.DataplaneType = config.AgentDataPlaneType
agentRes = res
}
var agentInstance *v1.ResourceInstance
if agentRes != nil {
Expand All @@ -300,6 +307,38 @@ func (a *agentResourceManager) createAgentResource() (*v1.ResourceInstance, erro
return a.apicClient.CreateResourceInstance(agentRes)
}

// GetAgentResource - returns the agent resource
func (a *agentResourceManager) checkAgentResource() (*v1.ResourceInstance, error) {
var agentRes v1.Interface
logger := a.logger.WithField("scope", a.agentResource.Metadata.Scope).WithField("kind", a.agentResource.Kind).WithField("name", a.agentResource.Name)

update := false
if a.agentResource.Kind == management.DiscoveryAgentGVK().Kind {
da := management.NewDiscoveryAgent("", "")
da.FromInstance(a.agentResource)
if da.Spec.DataplaneType == "" {
da.Spec.DataplaneType = config.AgentDataPlaneType
agentRes = da
update = true
}
} else if a.agentResource.Kind == management.TraceabilityAgentGVK().Kind {
ta := management.NewTraceabilityAgent("", "")
ta.FromInstance(a.agentResource)
if ta.Spec.DataplaneType == "" {
ta.Spec.DataplaneType = config.AgentDataPlaneType
agentRes = ta
update = true
}
}

if !update {
return a.agentResource, nil
}

logger.Info("updating agent resource")
return a.apicClient.UpdateResourceInstance(agentRes)
}

// GetAgentResource - returns the agent resource
func (a *agentResourceManager) getAgentResource() (*v1.ResourceInstance, error) {
agentRes := a.getAgentResourceType()
Expand Down
8 changes: 8 additions & 0 deletions pkg/agent/resource/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func createDiscoveryAgentRes(id, name, dataplane, teamID string) *v1.ResourceIns
Name: name,
Metadata: v1.Metadata{
ID: id,
Scope: v1.MetadataScope{
Kind: management.EnvironmentGVK().Kind,
Name: "env",
},
},
},
Spec: management.DiscoveryAgentSpec{
Expand Down Expand Up @@ -77,6 +81,10 @@ func TestNewManager(t *testing.T) {
}
return json.Marshal(resource)
},
GetResourceMock: func(url string) (*v1.ResourceInstance, error) {
ri, _ := resource.AsInstance()
return ri, nil
},
}
agentResChangeHandlerCall := 0
f := func() { agentResChangeHandlerCall++ }
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/Axway/agent-sdk/pkg/agent"
"github.com/Axway/agent-sdk/pkg/apic"
"github.com/Axway/agent-sdk/pkg/cmd/agentsync"
"github.com/Axway/agent-sdk/pkg/cmd/properties"
"github.com/Axway/agent-sdk/pkg/cmd/properties/resolver"
Expand Down Expand Up @@ -76,7 +77,11 @@ type agentRootCommand struct {
func init() {
config.AgentTypeName = BuildAgentName
config.AgentVersion = BuildVersion + "-" + BuildCommitSha
config.AgentDataPlaneType = BuildDataPlaneType
config.AgentDataPlaneType = apic.Unidentified.String()
if BuildDataPlaneType != "" {
config.AgentDataPlaneType = BuildDataPlaneType
}

config.SDKVersion = SDKBuildVersion
// initalize the global Source used by rand.Intn() and other functions of the rand package using rand.Seed().
rand.Seed(time.Now().UnixNano())
Expand Down

0 comments on commit c1fb4c8

Please sign in to comment.