Skip to content
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
39 changes: 22 additions & 17 deletions internal/controller/controllers/agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const (
AgentLabelHasNonrotationalDisk = InventoryLabelPrefix + "storage-hasnonrotationaldisk"
AgentLabelCpuArchitecture = InventoryLabelPrefix + "cpu-architecture"
AgentLabelCpuVirtEnabled = InventoryLabelPrefix + "cpu-virtenabled"
AgentInventoryAnnotation = "agent." + aiv1beta1.Group + "/inventory"
AgentRoleAnnotation = "agent." + aiv1beta1.Group + "/role"
AgentLabelHostManufacturer = InventoryLabelPrefix + "host-manufacturer"
AgentLabelHostProductName = InventoryLabelPrefix + "host-productname"
AgentLabelHostIsVirtual = InventoryLabelPrefix + "host-isvirtual"
Expand Down Expand Up @@ -173,10 +175,10 @@ func (r *AgentReconciler) Reconcile(origCtx context.Context, req ctrl.Request) (
log.WithError(err).Warnf("failed to set infraEnv name label on agent %s/%s", agent.Namespace, agent.Name)
}

// Add/Update 'state' annotation (with host's status)
if setAgentAnnotation(log, agent, AgentStateAnnotation, swag.StringValue(h.Status)) {
// Add/Update Agent annotations
if updateAnnotations(log, agent, &h.Host) {
if err = r.updateAndReplaceAgent(ctx, agent); err != nil {
log.WithError(err).Warnf("failed to set state annotation on agent %s/%s", agent.Namespace, agent.Name)
log.WithError(err).Warnf("failed to set annotations on agent %s/%s", agent.Namespace, agent.Name)
}
}

Expand Down Expand Up @@ -250,6 +252,16 @@ func (r *AgentReconciler) Reconcile(origCtx context.Context, req ctrl.Request) (
return r.updateStatus(ctx, log, agent, origAgent, &h.Host, h.ClusterID, nil, false)
}

func updateAnnotations(log logrus.FieldLogger, agent *v1beta1.Agent, h *models.Host) bool {
updated := false
updated = setAgentAnnotation(log, agent, AgentStateAnnotation, swag.StringValue(h.Status))
updated = updated || setAgentAnnotation(log, agent, AgentRoleAnnotation, string(h.Role))
if h.Inventory != "" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor nit for later, is it crucial to check if inventory is empty?
If not, we can make this func nicer to read I think.
I.e.

updated := false
updated = setAgentAnnotation(log, agent, AgentStateAnnotation, swag.StringValue(h.Status)
updated = setAgentAnnotation(log, agent, AgentRoleAnnotation, string(h.Role)) || updated
updated = setAgentAnnotation(log, agent, AgentInventoryAnnotation, h.Inventory) || updated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! Will work that into a new PR. Thanks Daniel!!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small PR with changes: #6967

updated = updated || setAgentAnnotation(log, agent, AgentInventoryAnnotation, h.Inventory)
}
return updated
}

// shouldCleanUnboundSpokeNode returns true if the agent has deprovision info set and the host is not in the process of unbinding
func shouldCleanUnboundSpokeNode(agent *aiv1beta1.Agent, host *common.Host) bool {
if host.Status == nil {
Expand Down Expand Up @@ -1812,17 +1824,10 @@ func (r *AgentReconciler) restoreHostByAgent(ctx context.Context, agent *aiv1bet
}

func createNewHost(agent *v1beta1.Agent, clusterID *strfmt.UUID, infraEnvID strfmt.UUID) (*models.Host, error) {
// Create Intentory
hostInventory := models.Inventory{}
manufacturer, exist := agent.Labels[AgentLabelHostManufacturer]
if exist {
hostInventory.SystemVendor = &models.SystemVendor{}
hostInventory.SystemVendor.Manufacturer = manufacturer
}
inventory, err := json.Marshal(hostInventory)
if err != nil {
return nil, errors.New("Failed to marshal agent's inventory")
}
// Retrieve host inventory and role
hostrole := agent.Annotations[AgentRoleAnnotation]
role := models.HostRole(hostrole)
inventory := agent.Annotations[AgentInventoryAnnotation]

// Fetch State
var hostStatus string
Expand All @@ -1843,12 +1848,12 @@ func createNewHost(agent *v1beta1.Agent, clusterID *strfmt.UUID, infraEnvID strf
Kind: swag.String(models.HostKindHost),
RegisteredAt: strfmt.DateTime(agent.CreationTimestamp.Time),
CheckedInAt: strfmt.DateTime(agent.CreationTimestamp.Time),
Role: agent.Spec.Role,
SuggestedRole: agent.Spec.Role,
Role: role,
SuggestedRole: role,
ClusterID: clusterID,
InfraEnvID: infraEnvID,
Status: &hostStatus,
Inventory: string(inventory),
Inventory: inventory,
}

return host, nil
Expand Down
13 changes: 6 additions & 7 deletions internal/controller/controllers/agent_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ var _ = Describe("agent reconcile", func() {
func(ctx context.Context, agent *v1beta1.Agent, opts ...client.UpdateOption) error {
return c.Update(ctx, agent)
},
).Times(3)
).Times(5)
mockClient.EXPECT().Status().Return(mockSubResourceWriter).AnyTimes()
mockSubResourceWriter.EXPECT().Update(gomock.Any(), gomock.AssignableToTypeOf(&v1beta1.Agent{})).DoAndReturn(
func(ctx context.Context, agent *v1beta1.Agent, opts ...client.UpdateOption) error {
return c.Status().Update(ctx, agent)
},
).Times(2)
).Times(4)
allowGetInfraEnvInternal(mockInstallerInternal, infraEnvId, "infraEnvName")

// We test 4 times to verify that agent is only updated twice
Expand Down Expand Up @@ -4536,7 +4536,8 @@ var _ = Describe("createNewHost", func() {

It("all properties exist", func() {
agent.ObjectMeta.Annotations = map[string]string{
AgentStateAnnotation: models.HostStatusInstalled,
AgentStateAnnotation: models.HostStatusInstalled,
AgentInventoryAnnotation: "{\"disks\":null,\"gpus\":null,\"interfaces\":null,\"routes\":null,\"system_vendor\":{\"manufacturer\":\"RedHat\"}}",
}
agent.ObjectMeta.Labels = map[string]string{
AgentLabelHostManufacturer: "RedHat",
Expand All @@ -4561,14 +4562,12 @@ var _ = Describe("createNewHost", func() {
Expect(host.Inventory).To(Equal(string(inventory)))
})

It("missing host-manufacturer label", func() {
It("missing inventory annotation", func() {
host, err := createNewHost(agent, &clusterID, infraEnvID)
Expect(err).To(BeNil())

hostInventory := models.Inventory{}
inventory, err := json.Marshal(hostInventory)
Expect(err).To(BeNil())
Expect(host.Inventory).To(Equal(string(inventory)))
Expect(host.Inventory).To(BeEmpty())
})

It("missing state annotation - bound", func() {
Expand Down