Skip to content

Commit

Permalink
fix: Use finalizers in build PLR to avoid getting pruned (#1019)
Browse files Browse the repository at this point in the history
Add finalizer to build plr to avoid getting pruned
  • Loading branch information
tisutisu authored Jan 29, 2024
1 parent efa6639 commit a38adda
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
28 changes: 28 additions & 0 deletions pkg/clients/tekton/pipelineruns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"sigs.k8s.io/controller-runtime/pkg/client"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/yaml"

g "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -260,3 +262,29 @@ func (t *TektonController) StoreAllPipelineRuns(namespace string) error {

return nil
}

func (t *TektonController) AddFinalizerToPipelineRun(pipelineRun *v1beta1.PipelineRun, finalizerName string) error {
ctx := context.Background()
kubeClient := t.KubeRest()
patch := crclient.MergeFrom(pipelineRun.DeepCopy())
if ok := controllerutil.AddFinalizer(pipelineRun, finalizerName); ok {
err := kubeClient.Patch(ctx, pipelineRun, patch)
if err != nil {
return fmt.Errorf("error occurred while patching the updated PipelineRun after finalizer addition: %v", err)
}
}
return nil
}

func (t *TektonController) RemoveFinalizerFromPipelineRun(pipelineRun *v1beta1.PipelineRun, finalizerName string) error {
ctx := context.Background()
kubeClient := t.KubeRest()
patch := client.MergeFrom(pipelineRun.DeepCopy())
if ok := controllerutil.RemoveFinalizer(pipelineRun, finalizerName); ok {
err := kubeClient.Patch(ctx, pipelineRun, patch)
if err != nil {
return fmt.Errorf("error occurred while patching the updated PipelineRun after finalizer removal: %v", err)
}
}
return nil
}
62 changes: 45 additions & 17 deletions tests/build/build_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",

var applicationName, componentName, symlinkComponentName, testNamespace string
var kubeadminClient *framework.ControllerHub
var finalizerName string = "e2e-test"

BeforeAll(func() {
if os.Getenv("APP_SUFFIX") != "" {
Expand Down Expand Up @@ -112,6 +113,20 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
})

AfterAll(func() {
//Remove finalizer from symlink check pipelinerun
pr, err := kubeadminClient.HasController.GetComponentPipelineRun(symlinkComponentName, applicationName, testNamespace, "")
Expect(err).ShouldNot(HaveOccurred())
err = kubeadminClient.TektonController.RemoveFinalizerFromPipelineRun(pr, finalizerName)
Expect(err).ShouldNot(HaveOccurred())

//Remove finalizer from other pipelineruns
for i := range componentUrls {
pr, err := kubeadminClient.HasController.GetComponentPipelineRun(componentNames[i], applicationName, testNamespace, "")
Expect(err).ShouldNot(HaveOccurred())
err = kubeadminClient.TektonController.RemoveFinalizerFromPipelineRun(pr, finalizerName)
Expect(err).ShouldNot(HaveOccurred())
}

// Do cleanup only in case the test succeeded
if !CurrentSpecReport().Failed() {
// Clean up only Application CR (Component and Pipelines are included) in case we are targeting specific namespace
Expand All @@ -125,10 +140,28 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
}
})

It(fmt.Sprintf("triggers PipelineRun for symlink component with source URL %s", pythonComponentGitSourceURL), Label(buildTemplatesTestLabel), func() {
timeout := time.Minute * 5
Eventually(func() error {
pipelineRun, err := kubeadminClient.HasController.GetComponentPipelineRun(symlinkComponentName, applicationName, testNamespace, "")
if err != nil {
GinkgoWriter.Printf("PipelineRun has not been created yet for symlink Component %s/%s\n", testNamespace, symlinkComponentName)
return err
}
if !pipelineRun.HasStarted() {
return fmt.Errorf("pipelinerun %s/%s has not started yet", pipelineRun.GetNamespace(), pipelineRun.GetName())
}
err = kubeadminClient.TektonController.AddFinalizerToPipelineRun(pipelineRun, finalizerName)
if err != nil {
return fmt.Errorf("error while adding finalizer %q to the pipelineRun %q: %v", finalizerName, pipelineRun.GetName(), err)
}
return nil
}, timeout, constants.PipelineRunPollingInterval).Should(Succeed(), fmt.Sprintf("timed out when waiting for the PipelineRun to start for the Component %s/%s", testNamespace, symlinkComponentName))
})

for i, gitUrl := range componentUrls {
i := i
gitUrl := gitUrl
var pr *v1beta1.PipelineRun
It(fmt.Sprintf("triggers PipelineRun for component with source URL %s", gitUrl), Label(buildTemplatesTestLabel), func() {
timeout := time.Minute * 5

Expand All @@ -141,10 +174,21 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
if !pipelineRun.HasStarted() {
return fmt.Errorf("pipelinerun %s/%s has not started yet", pipelineRun.GetNamespace(), pipelineRun.GetName())
}
err = kubeadminClient.TektonController.AddFinalizerToPipelineRun(pipelineRun, finalizerName)
if err != nil {
return fmt.Errorf("error while adding finalizer %q to the pipelineRun %q: %v", finalizerName, pipelineRun.GetName(), err)
}
return nil
}, timeout, constants.PipelineRunPollingInterval).Should(Succeed(), fmt.Sprintf("timed out when waiting for the PipelineRun to start for the Component %s/%s", testNamespace, componentNames[i]))
})

}

for i, gitUrl := range componentUrls {
i := i
gitUrl := gitUrl
var pr *v1beta1.PipelineRun

It(fmt.Sprintf("should eventually finish successfully for component with Git source URL %s", gitUrl), Label(buildTemplatesTestLabel), func() {
component, err := kubeadminClient.HasController.GetComponent(componentNames[i], testNamespace)
Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -506,22 +550,6 @@ var _ = framework.BuildSuiteDescribe("Build templates E2E test", Label("build",
})
}

It(fmt.Sprintf("triggers PipelineRun for symlink component with source URL %s", pythonComponentGitSourceURL), Label(buildTemplatesTestLabel), func() {
timeout := time.Minute * 5

Eventually(func() error {
pipelineRun, err := kubeadminClient.HasController.GetComponentPipelineRun(symlinkComponentName, applicationName, testNamespace, "")
if err != nil {
GinkgoWriter.Printf("PipelineRun has not been created yet for symlink Component %s/%s\n", testNamespace, symlinkComponentName)
return err
}
if !pipelineRun.HasStarted() {
return fmt.Errorf("pipelinerun %s/%s has not started yet", pipelineRun.GetNamespace(), pipelineRun.GetName())
}
return nil
}, timeout, constants.PipelineRunPollingInterval).Should(Succeed(), fmt.Sprintf("timed out when waiting for the PipelineRun to start for the Component %s/%s", testNamespace, symlinkComponentName))
})

It(fmt.Sprintf("pipelineRun should fail for symlink component with Git source URL %s", pythonComponentGitSourceURL), Label(buildTemplatesTestLabel), func() {
component, err := kubeadminClient.HasController.GetComponent(symlinkComponentName, testNamespace)
Expect(err).ShouldNot(HaveOccurred())
Expand Down

0 comments on commit a38adda

Please sign in to comment.