Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't reconcile resource requests while a Promise pipeline is in-flight #298

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions controllers/promise_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ func (r *PromiseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

if ctrlResult != nil {
logger.Info("stopping reconciliation while reconciling dependencies")
if r.dynamicControllerHasAlreadyStarted(promise) {
logger.Info("dynamic controller already started, marking it not to create resources")
dynamicController := r.StartedDynamicControllers[string(promise.GetUID())]
canCreateResources := false
dynamicController.CanCreateResources = &canCreateResources
}
return *ctrlResult, nil
}

Expand Down
162 changes: 106 additions & 56 deletions controllers/promise_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,83 +740,133 @@ var _ = Describe("PromiseController", func() {
})

When("the promise is being updated", func() {
BeforeEach(func() {
promise = promiseFromFile(promiseWithOnlyDepsPath)
promiseName = types.NamespacedName{
Name: promise.GetName(),
Namespace: promise.GetNamespace(),
}
promiseCommonLabels = map[string]string{
"kratix.io/promise-name": promise.GetName(),
}
promiseResourcesName = types.NamespacedName{
Name: promise.GetName() + "-promise-pipeline",
Namespace: "kratix-platform-system",
}

Expect(fakeK8sClient.Create(ctx, promise)).To(Succeed())
Expect(fakeK8sClient.Get(ctx, promiseName, promise)).To(Succeed())
promise.UID = "1234abcd"
Expect(fakeK8sClient.Update(ctx, promise)).To(Succeed())

setReconcileConfigureWorkflowToReturnFinished()
result, err := t.reconcileUntilCompletion(reconciler, promise, &opts{
funcs: []func(client.Object) error{autoMarkCRDAsEstablished},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{RequeueAfter: controllers.DefaultReconciliationInterval}))
})
When("the promise has only deps", func() {
BeforeEach(func() {
promise = promiseFromFile(promiseWithOnlyDepsPath)
promiseName = types.NamespacedName{
Name: promise.GetName(),
Namespace: promise.GetNamespace(),
}
promiseCommonLabels = map[string]string{
"kratix.io/promise-name": promise.GetName(),
}
promiseResourcesName = types.NamespacedName{
Name: promise.GetName() + "-promise-pipeline",
Namespace: "kratix-platform-system",
}

When("it contains static dependencies", func() {
It("re-reconciles until completion", func() {
Expect(fakeK8sClient.Create(ctx, promise)).To(Succeed())
Expect(fakeK8sClient.Get(ctx, promiseName, promise)).To(Succeed())
updatedPromise := promiseFromFile(promiseWithOnlyDepsUpdatedPath)
promise.Spec = updatedPromise.Spec
promise.UID = "1234abcd"
Expect(fakeK8sClient.Update(ctx, promise)).To(Succeed())

setReconcileConfigureWorkflowToReturnFinished()
result, err := t.reconcileUntilCompletion(reconciler, promise, &opts{
funcs: []func(client.Object) error{autoMarkCRDAsEstablished},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
Expect(result).To(Equal(ctrl.Result{RequeueAfter: controllers.DefaultReconciliationInterval}))
})

By("updating the work", func() {
works := &v1alpha1.WorkList{}
Expect(fakeK8sClient.List(ctx, works)).To(Succeed())
Expect(works.Items).To(HaveLen(1))
Expect(works.Items[0].Spec.WorkloadGroups[0].Workloads).To(HaveLen(1))
Expect(inCompressedContents(works.Items[0].Spec.WorkloadGroups[0].Workloads[0].Content, []byte("postgresoperator"))).To(BeTrue())
Expect(works.Items[0].Spec.WorkloadGroups[0].DestinationSelectors).To(HaveLen(1))
Expect(works.Items[0].Spec.WorkloadGroups[0].DestinationSelectors[0]).To(Equal(
v1alpha1.WorkloadGroupScheduling{
MatchLabels: map[string]string{
"environment": "prod",
When("it contains static dependencies", func() {
It("re-reconciles until completion", func() {
Expect(fakeK8sClient.Get(ctx, promiseName, promise)).To(Succeed())
updatedPromise := promiseFromFile(promiseWithOnlyDepsUpdatedPath)
promise.Spec = updatedPromise.Spec
Expect(fakeK8sClient.Update(ctx, promise)).To(Succeed())

setReconcileConfigureWorkflowToReturnFinished()
result, err := t.reconcileUntilCompletion(reconciler, promise, &opts{
funcs: []func(client.Object) error{autoMarkCRDAsEstablished},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))

By("updating the work", func() {
works := &v1alpha1.WorkList{}
Expect(fakeK8sClient.List(ctx, works)).To(Succeed())
Expect(works.Items).To(HaveLen(1))
Expect(works.Items[0].Spec.WorkloadGroups[0].Workloads).To(HaveLen(1))
Expect(inCompressedContents(works.Items[0].Spec.WorkloadGroups[0].Workloads[0].Content, []byte("postgresoperator"))).To(BeTrue())
Expect(works.Items[0].Spec.WorkloadGroups[0].DestinationSelectors).To(HaveLen(1))
Expect(works.Items[0].Spec.WorkloadGroups[0].DestinationSelectors[0]).To(Equal(
v1alpha1.WorkloadGroupScheduling{
MatchLabels: map[string]string{
"environment": "prod",
},
Source: "promise",
},
Source: "promise",
},
))
))
})
})
})

When("the static dependencies are removed", func() {
It("re-reconciles until completion", func() {
Expect(fakeK8sClient.Get(ctx, promiseName, promise)).To(Succeed())
promise.Spec.Dependencies = nil
Expect(fakeK8sClient.Update(ctx, promise)).To(Succeed())

setReconcileConfigureWorkflowToReturnFinished()
result, err := t.reconcileUntilCompletion(reconciler, promise, &opts{
funcs: []func(client.Object) error{autoMarkCRDAsEstablished},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))

By("deleting the work", func() {
works := &v1alpha1.WorkList{}
Expect(fakeK8sClient.List(ctx, works)).To(Succeed())
Expect(works.Items).To(HaveLen(0))
})
})
})
})
When("the promise has workflows", func() {
var resReq *unstructured.Unstructured
BeforeEach(func() {
promise = createPromise(promiseWithWorkflowPath)
setReconcileConfigureWorkflowToReturnFinished()
_, err := t.reconcileUntilCompletion(reconciler, promise, &opts{
funcs: []func(client.Object) error{autoMarkCRDAsEstablished},
})
Expect(err).NotTo(HaveOccurred())

When("the static dependencies are removed", func() {
It("re-reconciles until completion", func() {
resReqBytes, err := os.ReadFile(resourceRequestPath)
Expect(err).ToNot(HaveOccurred())

resReq = &unstructured.Unstructured{}
Expect(yaml.Unmarshal(resReqBytes, resReq)).To(Succeed())
Expect(fakeK8sClient.Create(ctx, resReq)).To(Succeed())

Expect(fakeK8sClient.Get(ctx, types.NamespacedName{
Name: resReq.GetName(),
Namespace: resReq.GetNamespace(),
}, resReq)).To(Succeed())
Expect(fakeK8sClient.Get(ctx, promiseName, promise)).To(Succeed())
promise.Spec.Dependencies = nil
promise.UID = "1234abcd"
Expect(fakeK8sClient.Update(ctx, promise)).To(Succeed())

setReconcileConfigureWorkflowToReturnFinished()
setReconcileConfigureWorkflowToReturnRunning()
})
It("re-reconciles until completion", func() {
result, err := t.reconcileUntilCompletion(reconciler, promise, &opts{
funcs: []func(client.Object) error{autoMarkCRDAsEstablished},
})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))

By("deleting the work", func() {
works := &v1alpha1.WorkList{}
Expect(fakeK8sClient.List(ctx, works)).To(Succeed())
Expect(works.Items).To(HaveLen(0))
By("not requeueing while the job is in flight", func() {
By("not allowing to create resources", func() {
Expect(*reconciler.StartedDynamicControllers["1234abcd"].CanCreateResources).To(BeFalse())
})
Expect(result).To(Equal(ctrl.Result{}))
Expect(err).To(BeNil())
})

By("finishing the update once the job is finished", func() {
setReconcileConfigureWorkflowToReturnFinished()
result, err := t.reconcileUntilCompletion(reconciler, promise)

Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
})
})
})
Expand Down
7 changes: 7 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func setReconcileConfigureWorkflowToReturnFinished() {
})
}

func setReconcileConfigureWorkflowToReturnRunning() {
controllers.SetReconcileConfigureWorkflow(func(w workflow.Opts) (bool, error) {
reconcileConfigureOptsArg = w
return true, nil
})
}

func setReconcileDeleteWorkflowToReturnFinished(obj client.Object) {
controllers.SetReconcileDeleteWorkflow(func(w workflow.Opts) (bool, error) {
us := &unstructured.Unstructured{}
Expand Down