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

fix: agent type name update not evicting old agent type from registry #9

Merged
merged 1 commit into from
Jan 23, 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
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