diff --git a/pkg/agent/resource/manager.go b/pkg/agent/resource/manager.go index e9e0aac53..46f975333 100644 --- a/pkg/agent/resource/manager.go +++ b/pkg/agent/resource/manager.go @@ -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 { @@ -118,6 +119,8 @@ func (a *agentResourceManager) FetchAgentResource() error { } else { return err } + } else { + a.checkAgentResource() } a.onResourceChange() @@ -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() } @@ -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 } @@ -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 { @@ -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() diff --git a/pkg/agent/resource/manager_test.go b/pkg/agent/resource/manager_test.go index d9622fcae..0bf70a0c5 100644 --- a/pkg/agent/resource/manager_test.go +++ b/pkg/agent/resource/manager_test.go @@ -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{ @@ -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++ } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 644061dcf..0f7f3a9fd 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -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" @@ -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())