Skip to content

Commit 6a3c822

Browse files
authored
Do not recreate pipeline service account on component deletetion (#344)
Do not recreate pipeline service account on component deletetion
1 parent 21aaa32 commit 6a3c822

6 files changed

+86
-54
lines changed

controllers/component_build_controller.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,6 @@ func (r *ComponentBuildReconciler) Reconcile(ctx context.Context, req ctrl.Reque
182182
return ctrl.Result{}, err
183183
}
184184

185-
// Ensure pipeline service account exists
186-
_, err = r.ensurePipelineServiceAccount(ctx, component.Namespace)
187-
if err != nil {
188-
return ctrl.Result{}, err
189-
}
190-
191185
if getContainerImageRepositoryForComponent(&component) == "" {
192186
// Container image must be set. It's not possible to proceed without it.
193187
log.Info("Waiting for ContainerImage to be set")
@@ -244,6 +238,12 @@ func (r *ComponentBuildReconciler) Reconcile(ctx context.Context, req ctrl.Reque
244238
return ctrl.Result{}, nil
245239
}
246240

241+
// Ensure pipeline service account exists
242+
_, err = r.ensurePipelineServiceAccount(ctx, component.Namespace)
243+
if err != nil {
244+
return ctrl.Result{}, err
245+
}
246+
247247
_, err = r.GetBuildPipelineFromComponentAnnotation(ctx, &component)
248248
if err != nil {
249249
buildStatus := readBuildStatus(&component)

controllers/component_build_controller_pac_repository.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"net/url"
2323
"strings"
2424

25-
"github.com/go-logr/logr"
2625
appstudiov1alpha1 "github.com/konflux-ci/application-api/api/v1alpha1"
2726
pacv1alpha1 "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
2827
corev1 "k8s.io/api/core/v1"
@@ -103,7 +102,7 @@ func (r *ComponentBuildReconciler) ensurePaCRepository(ctx context.Context, comp
103102
return err
104103
}
105104
if val, ok := ns.Labels[appstudioWorkspaceNameLabel]; ok {
106-
pacRepoAddParamWorkspaceName(log, repository, val)
105+
pacRepoAddParamWorkspaceName(repository, val)
107106
}
108107

109108
existingRepository := &pacv1alpha1.Repository{}
@@ -153,7 +152,7 @@ func (r *ComponentBuildReconciler) getNamespace(ctx context.Context, name string
153152

154153
// pacRepoAddParamWorkspaceName adds custom parameter workspace name to a PaC repository.
155154
// Existing parameter will be overridden.
156-
func pacRepoAddParamWorkspaceName(log logr.Logger, repository *pacv1alpha1.Repository, workspaceName string) {
155+
func pacRepoAddParamWorkspaceName(repository *pacv1alpha1.Repository, workspaceName string) {
157156
var params []pacv1alpha1.Params
158157
// Before pipelines-as-code gets upgraded for application-service to the
159158
// version supporting custom parameters, this check must be taken.

controllers/component_build_controller_pipeline.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,6 @@ func getContainerImageRepositoryForComponent(component *appstudiov1alpha1.Compon
467467
if component.Spec.ContainerImage != "" {
468468
return getContainerImageRepository(component.Spec.ContainerImage)
469469
}
470-
imageRepo, _, err := getComponentImageRepoAndSecretNameFromImageAnnotation(component)
471-
if err == nil && imageRepo != "" {
472-
return imageRepo
473-
}
474470
return ""
475471
}
476472

@@ -484,25 +480,6 @@ func getContainerImageRepository(image string) string {
484480
return strings.Split(image, ":")[0]
485481
}
486482

487-
// getComponentImageRepoAndSecretNameFromImageAnnotation parses image.redhat.com/image annotation
488-
// for image repository and secret name to access it.
489-
// If image.redhat.com/image is not set, the procedure returns empty values.
490-
func getComponentImageRepoAndSecretNameFromImageAnnotation(component *appstudiov1alpha1.Component) (string, string, error) {
491-
type RepositoryInfo struct {
492-
Image string `json:"image"`
493-
Secret string `json:"secret"`
494-
}
495-
496-
var repoInfo RepositoryInfo
497-
if imageRepoDataJson, exists := component.Annotations[ImageRepoAnnotationName]; exists {
498-
if err := json.Unmarshal([]byte(imageRepoDataJson), &repoInfo); err != nil {
499-
return "", "", boerrors.NewBuildOpError(boerrors.EFailedToParseImageAnnotation, err)
500-
}
501-
return repoInfo.Image, repoInfo.Secret, nil
502-
}
503-
return "", "", nil
504-
}
505-
506483
func getPipelineNameAndBundle(pipelineRef *tektonapi.PipelineRef) (string, string, error) {
507484
if pipelineRef.Resolver != "" && pipelineRef.Resolver != "bundles" {
508485
return "", "", boerrors.NewBuildOpError(

controllers/component_build_controller_test.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -522,18 +522,6 @@ var _ = Describe("Component initial build controller", func() {
522522
expectPacBuildStatus(resourcePacPrepKey, "enabled", 0, "", mergeUrl)
523523
})
524524

525-
It("should not copy PaC secret into local namespace if GitHub application is used", func() {
526-
deleteSecret(namespacePaCSecretKey)
527-
528-
createComponentAndProcessBuildRequest(componentConfig{
529-
componentKey: resourcePacPrepKey,
530-
annotations: defaultPipelineAnnotations,
531-
}, BuildRequestConfigurePaCAnnotationValue)
532-
waitPaCRepositoryCreated(resourcePacPrepKey)
533-
534-
ensureSecretNotCreated(namespacePaCSecretKey)
535-
})
536-
537525
It("should successfully submit PR with PaC definitions using token", func() {
538526
isCreatePaCPullRequestInvoked := false
539527
EnsurePaCMergeRequestFunc = func(repoUrl string, d *gp.MergeRequestData) (string, error) {
@@ -1167,6 +1155,40 @@ var _ = Describe("Component initial build controller", func() {
11671155
expectError := boerrors.NewBuildOpError(boerrors.EUnknownGitProvider, nil)
11681156
expectPacBuildStatus(resourceCleanupKey, "error", expectError.GetErrorId(), expectError.ShortError(), "")
11691157
})
1158+
1159+
It("should not attempt to create service account on component deletion", func() {
1160+
pacSecretData := map[string]string{
1161+
"github-application-id": "12345",
1162+
"github-private-key": githubAppPrivateKey,
1163+
}
1164+
createSecret(pacSecretKey, pacSecretData)
1165+
createComponentAndProcessBuildRequest(componentConfig{
1166+
componentKey: resourceCleanupKey,
1167+
annotations: defaultPipelineAnnotations,
1168+
}, BuildRequestConfigurePaCAnnotationValue)
1169+
waitPaCFinalizerOnComponent(resourceCleanupKey)
1170+
1171+
waitPipelineServiceAccount(resourceCleanupKey.Namespace)
1172+
1173+
// Make sure that proper cleanup was invoked
1174+
isRemovePaCPullRequestInvoked := false
1175+
UndoPaCMergeRequestFunc = func(repoUrl string, d *gp.MergeRequestData) (webUrl string, err error) {
1176+
isRemovePaCPullRequestInvoked = true
1177+
return "merge-url", nil
1178+
}
1179+
1180+
deletePipelineServiceAccount(resourceCleanupKey.Namespace)
1181+
1182+
Expect(isRemovePaCPullRequestInvoked).To(BeFalse())
1183+
// Clean up for the component should not recreate pipeline service account
1184+
deleteComponent(resourceCleanupKey)
1185+
Expect(isRemovePaCPullRequestInvoked).To(BeTrue())
1186+
1187+
pipelineServiceAccountKey := types.NamespacedName{Name: buildPipelineServiceAccountName, Namespace: resourceCleanupKey.Namespace}
1188+
pipelineServiceAccount := &corev1.ServiceAccount{}
1189+
err := k8sClient.Get(ctx, pipelineServiceAccountKey, pipelineServiceAccount)
1190+
Expect(k8sErrors.IsNotFound(err)).To(BeTrue())
1191+
})
11701192
})
11711193

11721194
Context("Test Pipelines as Code multi component git repository", func() {

controllers/component_build_controller_unit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ func TestPaCRepoAddParamWorkspace(t *testing.T) {
11901190

11911191
t.Run("add to Spec.Params", func(t *testing.T) {
11921192
repository, _ := generatePACRepository(*component, secret)
1193-
pacRepoAddParamWorkspaceName(log, repository, workspaceName)
1193+
pacRepoAddParamWorkspaceName(repository, workspaceName)
11941194

11951195
params := convertCustomParamsToMap(repository)
11961196
param, ok := params[pacCustomParamAppstudioWorkspace]
@@ -1210,7 +1210,7 @@ func TestPaCRepoAddParamWorkspace(t *testing.T) {
12101210
}
12111211
repository.Spec.Params = &params
12121212

1213-
pacRepoAddParamWorkspaceName(log, repository, workspaceName)
1213+
pacRepoAddParamWorkspaceName(repository, workspaceName)
12141214

12151215
existingParams := convertCustomParamsToMap(repository)
12161216
param, ok := existingParams[pacCustomParamAppstudioWorkspace]

controllers/suite_util_test.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,6 @@ func waitSecretCreated(resourceKey types.NamespacedName) {
438438
}, timeout, interval).Should(BeTrue())
439439
}
440440

441-
func ensureSecretNotCreated(resourceKey types.NamespacedName) {
442-
secret := &corev1.Secret{}
443-
Consistently(func() bool {
444-
err := k8sClient.Get(ctx, resourceKey, secret)
445-
return k8sErrors.IsNotFound(err)
446-
}, timeout, interval).WithTimeout(ensureTimeout).Should(BeTrue())
447-
}
448-
449441
func waitSecretGone(resourceKey types.NamespacedName) {
450442
secret := &corev1.Secret{}
451443
Eventually(func() bool {
@@ -599,3 +591,45 @@ func deleteBuildPipelineConfigMap(configMapKey types.NamespacedName) {
599591
Fail(err.Error())
600592
}
601593
}
594+
595+
func waitServiceAccount(serviceAccountKey types.NamespacedName) {
596+
serviceAccount := &corev1.ServiceAccount{}
597+
Eventually(func() bool {
598+
if err := k8sClient.Get(ctx, serviceAccountKey, serviceAccount); err != nil {
599+
return false
600+
}
601+
return serviceAccount.ResourceVersion != ""
602+
}, timeout, interval).Should(BeTrue())
603+
}
604+
605+
func deleteServiceAccount(serviceAccountKey types.NamespacedName) {
606+
serviceAccount := &corev1.ServiceAccount{}
607+
608+
// Check if the SA exists
609+
if err := k8sClient.Get(ctx, serviceAccountKey, serviceAccount); k8sErrors.IsNotFound(err) {
610+
return
611+
}
612+
613+
// Delete
614+
Eventually(func() error {
615+
if err := k8sClient.Get(ctx, serviceAccountKey, serviceAccount); err != nil {
616+
return err
617+
}
618+
return k8sClient.Delete(ctx, serviceAccount)
619+
}, timeout, interval).Should(Succeed())
620+
621+
// Wait for the deletion to finish
622+
Eventually(func() bool {
623+
return k8sErrors.IsNotFound(k8sClient.Get(ctx, serviceAccountKey, serviceAccount))
624+
}, timeout, interval).Should(BeTrue())
625+
}
626+
627+
func waitPipelineServiceAccount(namespace string) {
628+
pipelineServiceAccountKey := types.NamespacedName{Name: buildPipelineServiceAccountName, Namespace: namespace}
629+
waitServiceAccount(pipelineServiceAccountKey)
630+
}
631+
632+
func deletePipelineServiceAccount(namespace string) {
633+
pipelineServiceAccountkey := types.NamespacedName{Name: buildPipelineServiceAccountName, Namespace: namespace}
634+
deleteServiceAccount((pipelineServiceAccountkey))
635+
}

0 commit comments

Comments
 (0)