Skip to content

Commit

Permalink
fix: agent type name update not evicting old agent type from registry (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspin authored Jan 23, 2024
1 parent 8c89d71 commit f018a49
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/agenttypes/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,30 @@ func (r *Registry) OnUpdate(oldObj, newObj interface{}) {
return
}

agentType, err := parseAgentType(newSecret)
newAgentType, err := parseAgentType(newSecret)
if err != nil {
klog.Errorf("Error when parsing agent type: %v", err)
return
}

klog.Infof("Agent type updated: %s", agentType.AgentTypeName)
r.agentTypes[agentType.AgentTypeName] = agentType
oldAgentType, err := parseAgentType(oldSecret)
if err != nil {
klog.Errorf("Error when parsing agent type: %v", err)
return
}

// If the agent type name remained the same, we only need to update it.
if oldAgentType.AgentTypeName == newAgentType.AgentTypeName {
klog.Infof("Agent type updated: %s", newAgentType.AgentTypeName)
r.agentTypes[newAgentType.AgentTypeName] = newAgentType
return
}

// Otherwise, we need to delete the old one before adding the new one
klog.Infof("Agent type deleted: %s", oldAgentType.AgentTypeName)
delete(r.agentTypes, oldAgentType.AgentTypeName)
klog.Infof("Agent type added: %s", newAgentType.AgentTypeName)
r.agentTypes[newAgentType.AgentTypeName] = newAgentType
}

func (r *Registry) OnDelete(obj interface{}) {
Expand Down
30 changes: 30 additions & 0 deletions pkg/agenttypes/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,36 @@ func Test__Registry(t *testing.T) {
require.Equal(t, newToken, agentType.RegistrationToken)
})

t.Run("on update -> old is deleted, new is added", func(t *testing.T) {
oldAgentTypeName := randAgentTypeName()
secretName := randSecretName()
oldToken := randomString(t)
require.Nil(t, r.Get(oldAgentTypeName))

old := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{Name: secretName, ResourceVersion: "1"},
Data: map[string][]byte{
"agentTypeName": []byte(oldAgentTypeName),
"registrationToken": []byte(oldToken),
},
}

// agent type is added
r.OnAdd(old, false)
require.NotNil(t, r.Get(oldAgentTypeName))

// agent type is updated with different name
new := old.DeepCopy()
newAgentTypeName := randAgentTypeName()
new.Data["agentTypeName"] = []byte(newAgentTypeName)
new.ObjectMeta.ResourceVersion = "2"
r.OnUpdate(old, new)

// old is deleted, new is added
require.Nil(t, r.Get(oldAgentTypeName))
require.NotNil(t, r.Get(newAgentTypeName))
})

t.Run("on delete -> agent type is deleted", func(t *testing.T) {
agentTypeName := randAgentTypeName()
secretName := randSecretName()
Expand Down

0 comments on commit f018a49

Please sign in to comment.