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
1 change: 1 addition & 0 deletions .github/workflows/beeai-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
push:
branches:
- main
- tekton-pod-cleanup
paths:
- 'beeai/**'
pull_request:
Expand Down
9 changes: 5 additions & 4 deletions beeai/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ spec:

# auto deploy the agent when pipeline succeeds
deployAfterBuild: true

# auto cleanup Tekton pods when pipeline succeeds
cleanupAfterBuild: true

# define agent configuration parameters
agent:
name: "research-agent"
Expand Down Expand Up @@ -133,10 +137,7 @@ When a Tekton pipeline succeeds you should see the following pods:
```
NAME READY STATUS RESTARTS AGE
kagenti-operator-controller-manager-585c587f7f-nxctl 1/1 Running 0 85s
research-agent-build-183740ed-build-and-push-pod 0/1 Completed 0 47s
research-agent-build-183740ed-check-subfolder-pod 0/1 Completed 0 56s
research-agent-build-183740ed-git-clone-pod 0/1 Completed 0 66s
research-agent-c7cc5f568-82chd 1/1 Running 0 9s
research-agent-c7cc5f568-82chd 1/1 Running 0 9s
```

`Important:` Take note of the agent pod name research-agent-c7cc5f568-82chd.
Expand Down
4 changes: 4 additions & 0 deletions beeai/api/v1/agentbuild_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ type AgentBuildSpec struct {
// +optional
DeployAfterBuild bool `json:"deployAfterBuild,omitempty"`

// DeployAfterBuild indicates whether to automatically deploy the agent after build
// +optional
CleanupAfterBuild bool `json:"cleanupAfterBuild,omitempty"`

// Agent defines the configuration for the Agent CR that will be created
// +optional
Agent AgentTemplate `json:"agent,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion beeai/bin/controller-gen
2 changes: 1 addition & 1 deletion beeai/bin/kustomize
4 changes: 4 additions & 0 deletions beeai/config/crd/bases/beeai.beeai.dev_agentbuilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ spec:
type: object
type: object
type: object
cleanupAfterBuild:
description: DeployAfterBuild indicates whether to automatically deploy
the agent after build
type: boolean
deployAfterBuild:
description: DeployAfterBuild indicates whether to automatically deploy
the agent after build
Expand Down
9 changes: 9 additions & 0 deletions beeai/config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- delete
- apiGroups:
- ""
resources:
Expand Down
3 changes: 2 additions & 1 deletion beeai/config/samples/beeai-agentbuild-local-registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ spec:

# auto deploy the agent when pipeline succeeds
deployAfterBuild: true

cleanupAfterBuild: true

# define the agent configuration to deploy
agent:
name: "research-agent"
Expand Down
46 changes: 42 additions & 4 deletions beeai/internal/controller/agentbuild_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const (
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.0/pkg/reconcile
func (r *AgentBuildReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.Info("Reconciling AgentBuild", "namespacedName", req.NamespacedName)
logger.Info("- Reconciling AgentBuild - ", "namespacedName", req.NamespacedName)

agentBuild := &beeaiv1.AgentBuild{}
err := r.Get(ctx, req.NamespacedName, agentBuild)
Expand Down Expand Up @@ -167,14 +167,19 @@ func (r *AgentBuildReconciler) Reconcile(ctx context.Context, req ctrl.Request)
//
pullRepoSecret, err := r.createSecret(ctx, agentBuild, ".dockerconfigjson", token, corev1.SecretTypeDockerConfigJson, "docker")
if err != nil {
logger.Error(err, "Failed to create secret for the image repository",
"pipelineRunName", agentBuild.Status.PipelineRunName)
logger.Error(err, "Failed to create secret for the image repository", "pipelineRunName", agentBuild.Status.PipelineRunName)
return ctrl.Result{}, err
}
return r.createAgentCR(ctx, agentBuild, pullRepoSecret, logger)
}
} else if agentBuild.Status.BuildStatus == "Completed" && agentBuild.Spec.CleanupAfterBuild {
logger.Info("Reconciling AgentBuild", "Cleaning Up Tekton Pods", req.NamespacedName)
if err := r.cleanupTektonCompletedPods(ctx, agentBuild, logger); err != nil {
logger.Error(err, "Failed to cleanup tekton pods",
"pipelineRunName", agentBuild.Status.PipelineRunName)
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
} else {
if agentBuild.Status.BuildStatus != "Failed" {
Expand All @@ -200,6 +205,39 @@ func (r *AgentBuildReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
}

func (r *AgentBuildReconciler) cleanupTektonCompletedPods(ctx context.Context, agentBuild *beeaiv1.AgentBuild, logger logr.Logger) error {
// Get the pods associated with the PipelineRun
pipelineRunName := agentBuild.Status.PipelineRunName

// List the pods with the PipelineRun label
podList := &corev1.PodList{}
err := r.List(ctx, podList, client.MatchingLabels{"tekton.dev/pipelineRun": pipelineRunName})
if err != nil {
logger.Error(err, "Failed to list pods for PipelineRun", "pipelineRunName", pipelineRunName)
return err
}

// Filter for completed pods (Succeeded)
var completedPods []corev1.Pod
for _, pod := range podList.Items {
if pod.Status.Phase == corev1.PodSucceeded { //|| pod.Status.Phase == corev1.PodFailed {
completedPods = append(completedPods, pod)
}
}

// Delete completed pods
for _, pod := range completedPods {
logger.Info("Deleting completed pod", "podName", pod.Name)
err := r.Delete(ctx, &pod)
if err != nil {
logger.Error(err, "Failed to delete completed pod", "podName", pod.Name)
// Continue with other pods rather than failing the cleanup
}
}

return nil
}

func (r *AgentBuildReconciler) handleDeletion(ctx context.Context, agentBuild *beeaiv1.AgentBuild, logger logr.Logger) (ctrl.Result, error) {
if controllerutil.ContainsFinalizer(agentBuild, agentBuildFinalizer) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ spec:
type: object
type: object
type: object
cleanupAfterBuild:
description: DeployAfterBuild indicates whether to automatically deploy
the agent after build
type: boolean
deployAfterBuild:
description: DeployAfterBuild indicates whether to automatically deploy
the agent after build
Expand Down
9 changes: 9 additions & 0 deletions charts/beeai-operator/templates/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ rules:
- get
- list
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- delete
- apiGroups:
- ""
resources: ["persistentvolumes", "persistentvolumeclaims"]
Expand Down