-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from Josh-Everett/488
feat(STONEINTG-488): react to application component deletion
- Loading branch information
Showing
9 changed files
with
716 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
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() | ||
} | ||
|
||
_, err = a.createUpdatedSnapshot(&snapshotComponents) | ||
if err != nil { | ||
a.logger.Error(err, "Failed to create new snapshot after component deletion") | ||
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(snapshotComponents *[]applicationapiv1alpha1.SnapshotComponent) (*applicationapiv1alpha1.Snapshot, error) { | ||
snapshot := gitops.NewSnapshot(a.application, snapshotComponents) | ||
if snapshot.Labels == nil { | ||
snapshot.Labels = map[string]string{} | ||
} | ||
snapshotType := gitops.SnapshotCompositeType | ||
if len(*snapshotComponents) == 1 { | ||
snapshotType = gitops.SnapshotComponentType | ||
} | ||
snapshot.Labels[gitops.SnapshotTypeLabel] = snapshotType | ||
|
||
err := ctrl.SetControllerReference(a.application, snapshot, a.client.Scheme()) | ||
if err != nil { | ||
a.logger.Error(err, "Failed to set controller refrence") | ||
return nil, err | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
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_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) == 1 && err == nil | ||
}, time.Second*20).Should(BeTrue()) | ||
}) | ||
|
||
}) |
Oops, something went wrong.