-
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.
feat(STONEINTG-488): react to application component deletion
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
1 parent
f412ea6
commit de27301
Showing
7 changed files
with
680 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
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" | ||
"fmt" | ||
"reflect" | ||
|
||
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/integration-service/status" | ||
"github.com/redhat-appstudio/operator-toolkit/controller" | ||
"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 | ||
status status.Status | ||
} | ||
|
||
// 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, | ||
status: status.NewAdapter(logger.Logger, client), | ||
} | ||
} | ||
|
||
// 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 { | ||
return controller.RequeueWithError(err) | ||
} | ||
|
||
// TODO broken here | ||
var updatedComponents = []applicationapiv1alpha1.Component{} | ||
|
||
for i, individualComponent := range *applicationComponents { | ||
if reflect.DeepEqual(*a.component, individualComponent) { | ||
updatedComponents = append((*applicationComponents)[:i], (*applicationComponents)[i+1:]...) | ||
} | ||
} | ||
|
||
// create new snapshot with remaining components | ||
_, err = a.RefreshSnapshot(a.client, a.application, &updatedComponents) | ||
if err != nil { | ||
a.logger.Error(err, "Failed to create new snapshot after component deletion") | ||
return controller.RequeueWithError(err) | ||
} | ||
|
||
return controller.ContinueProcessing() | ||
} | ||
|
||
// RefreshSnapshot prepares the Snapshot for a given application and component(s). | ||
// In case the Snapshot can't be created, an error will be returned. | ||
func (a *Adapter) RefreshSnapshot(adapterClient client.Client, application *applicationapiv1alpha1.Application, remainingComponents *[]applicationapiv1alpha1.Component) (*applicationapiv1alpha1.Snapshot, error) { | ||
if len(*remainingComponents) == 0 { | ||
return nil, fmt.Errorf("failed to prepare snapshot due to missing valid digest in containerImage for all components of application") | ||
} | ||
|
||
snapshot, err := gitops.PrepareSnapshot(a.client, a.context, a.application, remainingComponents, nil, "", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
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 { | ||
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,152 @@ | ||
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.ComponentContextKey, | ||
Resource: hasComp, | ||
}, | ||
{ | ||
ContextKey: loader.ComponentContextKey, | ||
Resource: hasComp2, | ||
}, | ||
{ | ||
ContextKey: loader.ApplicationComponentsContextKey, | ||
Resource: []applicationapiv1alpha1.Component{*hasComp, *hasComp2}, | ||
}, | ||
}) | ||
snapshots := &applicationapiv1alpha1.SnapshotList{} | ||
k8sClient.List(ctx, snapshots, &client.ListOptions{Namespace: hasApp.Namespace}) | ||
Expect(len(snapshots.Items) == 0) | ||
Expect(snapshots.Items).To(BeEmpty()) | ||
|
||
now := metav1.NewTime(metav1.Now().Add(time.Second * 1)) | ||
hasComp.SetDeletionTimestamp(&now) | ||
|
||
Eventually(func() bool { | ||
result, err := adapter.EnsureComponentIsCleanedUp() | ||
k8sClient.List(ctx, snapshots, &client.ListOptions{Namespace: hasApp.Namespace}) | ||
return !result.CancelRequest && len(snapshots.Items) > 0 && err == nil | ||
}, time.Second*20).Should(BeTrue()) | ||
}) | ||
|
||
}) |
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,116 @@ | ||
/* | ||
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 andF | ||
limitations under the License. | ||
*/ | ||
|
||
package component | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1" | ||
"github.com/redhat-appstudio/integration-service/helpers" | ||
"github.com/redhat-appstudio/integration-service/loader" | ||
"github.com/redhat-appstudio/operator-toolkit/controller" | ||
tektonv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
) | ||
|
||
// Reconciler reconciles a component object | ||
type Reconciler struct { | ||
client.Client | ||
Log logr.Logger | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
// NewComponentReconciler creates and returns a Reconciler. | ||
func NewComponentReconciler(client client.Client, logger *logr.Logger, scheme *runtime.Scheme) *Reconciler { | ||
return &Reconciler{ | ||
Client: client, | ||
Log: logger.WithName("integration pipeline"), | ||
Scheme: scheme, | ||
} | ||
} | ||
|
||
//+kubebuilder:rbac:groups=appstudio.redhat.com,resources=components,verbs=get;list;watch;create;update;patch;delete | ||
//+kubebuilder:rbac:groups=appstudio.redhat.com,resources=components/status,verbs=get;update;patch | ||
//+kubebuilder:rbac:groups=appstudio.redhat.com,resources=components/finalizers,verbs=update | ||
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch | ||
|
||
// Reconcile is part of the main kubernetes reconciliation loop which aims to | ||
// move the current state of the cluster closer to the desired state. | ||
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
logger := helpers.IntegrationLogger{Logger: r.Log.WithValues("component", req.NamespacedName)} | ||
loader := loader.NewLoader() | ||
|
||
component := &applicationapiv1alpha1.Component{} | ||
err := r.Get(ctx, req.NamespacedName, component) | ||
if err != nil { | ||
logger.Error(err, "Failed to get component for", "req", req.NamespacedName) | ||
if errors.IsNotFound(err) { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
return ctrl.Result{}, err | ||
} | ||
|
||
application := &applicationapiv1alpha1.Application{} | ||
application, err = loader.GetApplicationFromComponent(r.Client, ctx, component) | ||
if err != nil { | ||
logger.Error(err, "Failed to get Application from the component", | ||
"Component.Name", component.Name) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
if application == nil { | ||
err := fmt.Errorf("failed to get Application") | ||
logger.Error(err, "reconcile cannot resolve application") | ||
return ctrl.Result{}, err | ||
} | ||
logger = logger.WithApp(*application) | ||
|
||
adapter := NewAdapter(component, application, logger, loader, r.Client, ctx) | ||
|
||
return controller.ReconcileHandler([]controller.Operation{ | ||
adapter.EnsureComponentIsCleanedUp, | ||
}) | ||
} | ||
|
||
// AdapterInterface is an interface defining all the operations that should be defined in an Integration adapter. | ||
type AdapterInterface interface { | ||
EnsureSnapshotIsFresh() (controller.OperationResult, error) | ||
} | ||
|
||
// SetupController creates a new Component controller and adds it to the Manager. | ||
func SetupController(manager ctrl.Manager, log *logr.Logger) error { | ||
return setupControllerWithManager(manager, NewComponentReconciler(manager.GetClient(), log, manager.GetScheme())) | ||
|
||
} | ||
|
||
// setupControllerWithManager sets up the controller with the Manager which monitors new PipelineRuns and filters | ||
// out status updates. | ||
func setupControllerWithManager(manager ctrl.Manager, controller *Reconciler) error { | ||
return ctrl.NewControllerManagedBy(manager). | ||
For(&tektonv1beta1.PipelineRun{}). | ||
WithEventFilter(predicate.Or( | ||
ComponentDeletedPredicate())). | ||
Complete(controller) | ||
} |
Oops, something went wrong.