Skip to content

Commit

Permalink
feat(STONEINTG-488): react to application component deletion
Browse files Browse the repository at this point in the history
Creation of component controller to watch for component deletion events.
When a component is deleted, IS will create a new snapshot to reflect
those changes.

Signed-off-by: Josh Everett <[email protected]>
  • Loading branch information
Josh-Everett committed Sep 21, 2023
1 parent c94d063 commit 1950be1
Show file tree
Hide file tree
Showing 9 changed files with 734 additions and 1 deletion.
24 changes: 24 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ rules:
resources:
- components
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- appstudio.redhat.com
resources:
- components/finalizers
verbs:
- update
- apiGroups:
- appstudio.redhat.com
resources:
Expand Down Expand Up @@ -213,6 +221,22 @@ rules:
- get
- patch
- update
- apiGroups:
- ""
resources:
- configmaps
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- pipelinesascode.tekton.dev
resources:
Expand Down
128 changes: 128 additions & 0 deletions controllers/component/component_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package component

import (
"context"

applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"github.com/redhat-appstudio/integration-service/gitops"
h "github.com/redhat-appstudio/integration-service/helpers"
"github.com/redhat-appstudio/integration-service/loader"
"github.com/redhat-appstudio/integration-service/metrics"
"github.com/redhat-appstudio/operator-toolkit/controller"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Adapter holds the objects needed to reconcile a integration PipelineRun.
type Adapter struct {
component *applicationapiv1alpha1.Component
application *applicationapiv1alpha1.Application
loader loader.ObjectLoader
logger h.IntegrationLogger
client client.Client
context context.Context
}

// NewAdapter creates and returns an Adapter instance.
func NewAdapter(component *applicationapiv1alpha1.Component, application *applicationapiv1alpha1.Application, logger h.IntegrationLogger, loader loader.ObjectLoader, client client.Client,
context context.Context) *Adapter {
return &Adapter{
component: component,
application: application,
logger: logger,
loader: loader,
client: client,
context: context,
}
}

// EnsureComponentIsCleanedUp is an operation that will ensure components
// marked for deletion have a snapshot created without said component
func (a *Adapter) EnsureComponentIsCleanedUp() (controller.OperationResult, error) {
if !hasComponentBeenDeleted(a.component) {
return controller.ContinueProcessing()
}

applicationComponents, err := a.loader.GetAllApplicationComponents(a.client, a.context, a.application)
if err != nil {
a.logger.Error(err, "Failed to load application components")
return controller.RequeueWithError(err)
}

var snapshotComponents []applicationapiv1alpha1.SnapshotComponent

for _, individualComponent := range *applicationComponents {
component := individualComponent
if a.component.Name != component.Name {
containerImage := component.Spec.ContainerImage
componentSource := gitops.GetComponentSourceFromComponent(&component)
snapshotComponents = append(snapshotComponents, applicationapiv1alpha1.SnapshotComponent{
Name: component.Name,
ContainerImage: containerImage,
Source: *componentSource,
})
}
}

if len(snapshotComponents) == 0 {
a.logger.Info("Application has no available snapshot components for snapshot creation")
return controller.StopProcessing()
}

snapshot, err := a.createUpdatedSnapshot(a.client, a.application, &snapshotComponents)
if err != nil {
a.logger.Error(err, "Failed to create new snapshot after component deletion")
return controller.RequeueWithError(err)
}

err = ctrl.SetControllerReference(a.application, snapshot, a.client.Scheme())
if err != nil {
a.logger.Error(err, "Failed to set controller refrence")
return controller.RequeueWithError(err)
}

return controller.ContinueProcessing()
}

// createUpdatedSnapshot prepares a Snapshot for a given application and component(s).
// In case the Snapshot can't be created, an error will be returned.
func (a *Adapter) createUpdatedSnapshot(adapterClient client.Client, application *applicationapiv1alpha1.Application, snapshotComponents *[]applicationapiv1alpha1.SnapshotComponent) (*applicationapiv1alpha1.Snapshot, error) {
snapshot := gitops.NewSnapshot(a.application, snapshotComponents)
if snapshot.Labels == nil {
snapshot.Labels = map[string]string{}
}
snapshot.Labels[gitops.SnapshotTypeLabel] = gitops.SnapshotCompositeType

err := a.client.Create(a.context, snapshot)
if err != nil {
a.logger.Error(err, "Failed to create new snapshot on client")
return nil, err
}

go metrics.RegisterNewSnapshot()
return snapshot, nil
}

func hasComponentBeenDeleted(object client.Object) bool {

if comp, ok := object.(*applicationapiv1alpha1.Component); ok {
return !comp.ObjectMeta.DeletionTimestamp.IsZero()
}
return false
}
146 changes: 146 additions & 0 deletions controllers/component/component_adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package component

import (
"bytes"
"reflect"
"time"

"github.com/tonglil/buflogr"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"github.com/redhat-appstudio/integration-service/loader"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/redhat-appstudio/integration-service/helpers"
"k8s.io/apimachinery/pkg/api/errors"

"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Component Adapter", Ordered, func() {
var (
adapter *Adapter
logger helpers.IntegrationLogger

hasApp *applicationapiv1alpha1.Application
hasComp *applicationapiv1alpha1.Component
hasComp2 *applicationapiv1alpha1.Component
)
const (
SampleCommit = "a2ba645d50e471d5f084b"
SampleRepoLink = "https://github.com/devfile-samples/devfile-sample-java-springboot-basic"
sample_image = "quay.io/redhat-appstudio/sample-image"
sample_revision = "random-value"
SampleDigest = "sha256:841328df1b9f8c4087adbdcfec6cc99ac8308805dea83f6d415d6fb8d40227c1"
SampleImageWithoutDigest = "quay.io/redhat-appstudio/sample-image"
SampleImage = SampleImageWithoutDigest + "@" + SampleDigest
)

BeforeAll(func() {

hasApp = &applicationapiv1alpha1.Application{
ObjectMeta: metav1.ObjectMeta{
Name: "application-sample",
Namespace: "default",
},
Spec: applicationapiv1alpha1.ApplicationSpec{
DisplayName: "application-sample",
Description: "This is an example application",
},
}
Expect(k8sClient.Create(ctx, hasApp)).Should(Succeed())

hasComp = &applicationapiv1alpha1.Component{
ObjectMeta: metav1.ObjectMeta{
Name: "component-sample",
Namespace: "default",
},
Spec: applicationapiv1alpha1.ComponentSpec{
ComponentName: "component-sample-2",
Application: hasApp.Name,
ContainerImage: SampleImage,
Source: applicationapiv1alpha1.ComponentSource{
ComponentSourceUnion: applicationapiv1alpha1.ComponentSourceUnion{
GitSource: &applicationapiv1alpha1.GitSource{
URL: SampleRepoLink,
Revision: SampleCommit,
},
},
},
},
Status: applicationapiv1alpha1.ComponentStatus{
LastBuiltCommit: "",
},
}
Expect(k8sClient.Create(ctx, hasComp)).Should(Succeed())

hasComp2 = &applicationapiv1alpha1.Component{
ObjectMeta: metav1.ObjectMeta{
Name: "component-second-sample",
Namespace: "default",
},
Spec: applicationapiv1alpha1.ComponentSpec{
ComponentName: "component-second-sample",
Application: "application-sample",
ContainerImage: SampleImage,
Source: applicationapiv1alpha1.ComponentSource{
ComponentSourceUnion: applicationapiv1alpha1.ComponentSourceUnion{
GitSource: &applicationapiv1alpha1.GitSource{
URL: SampleRepoLink,
},
},
},
},
}
Expect(k8sClient.Create(ctx, hasComp2)).Should(Succeed())
})

AfterAll(func() {
err := k8sClient.Delete(ctx, hasApp)
Expect(err == nil || errors.IsNotFound(err)).To(BeTrue())
err = k8sClient.Delete(ctx, hasComp)
Expect(err == nil || errors.IsNotFound(err)).To(BeTrue())
err = k8sClient.Delete(ctx, hasComp2)
Expect(err == nil || errors.IsNotFound(err)).To(BeTrue())
})

It("can create a new Adapter instance", func() {
Expect(reflect.TypeOf(NewAdapter(hasComp, hasApp, logger, loader.NewMockLoader(), k8sClient, ctx))).To(Equal(reflect.TypeOf(&Adapter{})))
})
It("ensures removing a component will result in a new snapshot being created", func() {
buf := bytes.Buffer{}

log := helpers.IntegrationLogger{Logger: buflogr.NewWithBuffer(&buf)}
adapter = NewAdapter(hasComp, hasApp, log, loader.NewMockLoader(), k8sClient, ctx)
adapter.context = loader.GetMockedContext(ctx, []loader.MockData{
{
ContextKey: loader.ApplicationContextKey,
Resource: hasApp,
},
{
ContextKey: loader.ApplicationComponentsContextKey,
Resource: []applicationapiv1alpha1.Component{*hasComp, *hasComp2},
},
})
snapshots := &applicationapiv1alpha1.SnapshotList{}
Eventually(func() bool {
k8sClient.List(ctx, snapshots, &client.ListOptions{Namespace: hasApp.Namespace})
return len(snapshots.Items) == 0
}, time.Second*20).Should(BeTrue())

now := metav1.NewTime(metav1.Now().Add(time.Second * 1))
hasComp.SetDeletionTimestamp(&now)

result, err := adapter.EnsureComponentIsCleanedUp()

Eventually(func() bool {
k8sClient.List(ctx, snapshots, &client.ListOptions{Namespace: hasApp.Namespace})
return !result.CancelRequest && len(snapshots.Items) > 0 && err == nil
}, time.Second*20).Should(BeTrue())
})

})
Loading

0 comments on commit 1950be1

Please sign in to comment.