Skip to content

Commit

Permalink
fix: remove agent and workflow credentials on delete
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <donnie@acorn.io>
  • Loading branch information
thedadams committed Jan 31, 2025
1 parent 27dcbc7 commit 527ba21
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 24 deletions.
1 change: 1 addition & 0 deletions pkg/api/handlers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (a *AgentHandler) Create(req api.Context) error {
ObjectMeta: metav1.ObjectMeta{
GenerateName: system.AgentPrefix,
Namespace: req.Namespace(),
Finalizers: []string{v1.AgentFinalizer},
},
Spec: v1.AgentSpec{
Manifest: manifest,
Expand Down
1 change: 1 addition & 0 deletions pkg/api/handlers/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func (a *WorkflowHandler) Create(req api.Context) error {
ObjectMeta: metav1.ObjectMeta{
GenerateName: system.WorkflowPrefix,
Namespace: req.Namespace(),
Finalizers: []string{v1.WorkflowFinalizer},
},
Spec: v1.WorkflowSpec{
Manifest: manifest,
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/data/agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ kind: Agent
metadata:
name: a1-obot
namespace: default
finalizers:
- obot.obot.ai/agent
spec:
manifest:
name: Obot
Expand Down
33 changes: 33 additions & 0 deletions pkg/controller/handlers/cleanup/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cleanup

import (
"github.com/gptscript-ai/go-gptscript"
"github.com/obot-platform/nah/pkg/router"
)

type Credentials struct {
gClient *gptscript.GPTScript
}

func NewCredentials(gClient *gptscript.GPTScript) *Credentials {
return &Credentials{
gClient: gClient,
}
}

func (c *Credentials) Remove(req router.Request, _ router.Response) error {
creds, err := c.gClient.ListCredentials(req.Ctx, gptscript.ListCredentialsOptions{
CredentialContexts: []string{req.Object.GetName()},
})
if err != nil {
return err
}

for _, cred := range creds {
if err := c.gClient.DeleteCredential(req.Ctx, req.Object.GetName(), cred.ToolName); err != nil {
return err
}
}

return nil
}
19 changes: 0 additions & 19 deletions pkg/controller/handlers/threads/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,6 @@ func (t *Handler) CreateWorkspaces(req router.Request, _ router.Response) error
return nil
}

func (t *Handler) CleanupThread(req router.Request, _ router.Response) error {
thread := req.Object.(*v1.Thread)

creds, err := t.gptScript.ListCredentials(req.Ctx, gptscript.ListCredentialsOptions{
CredentialContexts: []string{thread.Name},
})
if err != nil {
return err
}

for _, cred := range creds {
if err := t.gptScript.DeleteCredential(req.Ctx, thread.Name, cred.ToolName); err != nil {
return err
}
}

return nil
}

func (t *Handler) CreateKnowledgeSet(req router.Request, _ router.Response) error {
thread := req.Object.(*v1.Thread)
if len(thread.Status.KnowledgeSetNames) > 0 || thread.Spec.AgentName == "" {
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (c *Controller) setupRoutes() error {
knowledgesummary := knowledgesummary.NewHandler(c.services.GPTClient)
toolInfo := toolinfo.New(c.services.GPTClient)
threads := threads.NewHandler(c.services.GPTClient)
credentialCleanup := cleanup.NewCredentials(c.services.GPTClient)

// Runs
root.Type(&v1.Run{}).HandlerFunc(removeOldFinalizers)
Expand All @@ -61,7 +62,7 @@ func (c *Controller) setupRoutes() error {
root.Type(&v1.Thread{}).HandlerFunc(threads.CreateKnowledgeSet)
root.Type(&v1.Thread{}).HandlerFunc(threads.WorkflowState)
root.Type(&v1.Thread{}).HandlerFunc(knowledgesummary.Summarize)
root.Type(&v1.Thread{}).FinalizeFunc(v1.ThreadFinalizer, threads.CleanupThread)
root.Type(&v1.Thread{}).FinalizeFunc(v1.ThreadFinalizer, credentialCleanup.Remove)

// KnowledgeSummary
root.Type(&v1.KnowledgeSummary{}).HandlerFunc(cleanup.Cleanup)
Expand All @@ -74,6 +75,7 @@ func (c *Controller) setupRoutes() error {
root.Type(&v1.Workflow{}).HandlerFunc(alias.AssignAlias)
root.Type(&v1.Workflow{}).HandlerFunc(toolInfo.SetToolInfoStatus)
root.Type(&v1.Workflow{}).HandlerFunc(generationed.UpdateObservedGeneration)
root.Type(&v1.Workflow{}).FinalizeFunc(v1.WorkflowFinalizer, credentialCleanup.Remove)

// WorkflowExecutions
root.Type(&v1.WorkflowExecution{}).HandlerFunc(cleanup.Cleanup)
Expand All @@ -86,6 +88,7 @@ func (c *Controller) setupRoutes() error {
root.Type(&v1.Agent{}).HandlerFunc(alias.AssignAlias)
root.Type(&v1.Agent{}).HandlerFunc(toolInfo.SetToolInfoStatus)
root.Type(&v1.Agent{}).HandlerFunc(generationed.UpdateObservedGeneration)
root.Type(&v1.Agent{}).FinalizeFunc(v1.AgentFinalizer, credentialCleanup.Remove)

// Uploads
root.Type(&v1.KnowledgeSource{}).HandlerFunc(cleanup.Cleanup)
Expand Down
3 changes: 3 additions & 0 deletions pkg/storage/apis/obot.obot.ai/v1/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (

const (
RunFinalizer = "obot.obot.ai/run"
ThreadFinalizer = "obot.obot.ai/thread"
KnowledgeFileFinalizer = "obot.obot.ai/knowledge-file"
WorkspaceFinalizer = "obot.obot.ai/workspace"
KnowledgeSetFinalizer = "obot.obot.ai/knowledge-set"
KnowledgeSourceFinalizer = "obot.obot.ai/knowledge-source"
ToolReferenceFinalizer = "obot.obot.ai/tool-reference"
AgentFinalizer = "obot.obot.ai/agent"
WorkflowFinalizer = "obot.obot.ai/workflow"

ModelProviderSyncAnnotation = "obot.ai/model-provider-sync"
WorkflowSyncAnnotation = "obot.ai/workflow-sync"
Expand Down
4 changes: 0 additions & 4 deletions pkg/storage/apis/obot.obot.ai/v1/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
ThreadFinalizer = "obot.obot.ai/thread"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type Thread struct {
Expand Down

0 comments on commit 527ba21

Please sign in to comment.