Skip to content

Commit

Permalink
Disable subchart decomposition if subchart field nil/empty (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishm authored Jun 16, 2021
1 parent ddfd626 commit 9e6538d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
67 changes: 62 additions & 5 deletions controllers/appgroup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (

"k8s.io/apimachinery/pkg/api/errors"

meta2 "github.com/fluxcd/pkg/apis/meta"

"github.com/Azure/Orkestra/api/v1alpha1"
"github.com/Azure/Orkestra/pkg/meta"
v1alpha13 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
fluxhelmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
meta2 "github.com/fluxcd/pkg/apis/meta"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -32,9 +31,10 @@ var _ = Describe("ApplicationGroup Controller", func() {
)

const (
DefaultNamespace = "orkestra"
DefaultTimeout = time.Minute * 5
TotalHelmReleaseCount = 6
DefaultNamespace = "orkestra"
DefaultTimeout = time.Minute * 5
TotalHelmReleaseCount = 6
OnlyApplicationsHelmReleaseCount = 2
)

BeforeEach(func() {
Expand Down Expand Up @@ -120,6 +120,63 @@ var _ = Describe("ApplicationGroup Controller", func() {

})

It("Should create create only application releases with subchart nil successfully", func() {
applicationGroup := defaultAppGroup(name)
applicationGroup.Name = name
applicationGroup.Namespace = DefaultNamespace
for i := range applicationGroup.Spec.Applications {
applicationGroup.Spec.Applications[i].Spec.Subcharts = nil
}
key := client.ObjectKeyFromObject(applicationGroup)

By("Applying the bookinfo object to the cluster")
err := k8sClient.Create(ctx, applicationGroup)
Expect(err).ToNot(HaveOccurred())

// Defer the cleanup so that we delete the appGroup after creation
defer func() {
By("Deleting the bookinfo object from the cluster")
patch := client.MergeFrom(applicationGroup.DeepCopy())
controllerutil.RemoveFinalizer(applicationGroup, v1alpha1.AppGroupFinalizer)
_ = k8sClient.Patch(ctx, applicationGroup, patch)
_ = k8sClient.Delete(ctx, applicationGroup)
}()

helmReleaseList := &fluxhelmv2beta1.HelmReleaseList{}
err = k8sClient.List(ctx, helmReleaseList, client.InNamespace(name))
Expect(err).ToNot(HaveOccurred())
oldHelmReleaseCount := len(helmReleaseList.Items)

By("Making sure that the workflow goes into a running state")
Eventually(func() bool {
workflow := &v1alpha13.Workflow{}
workflowKey := types.NamespacedName{Name: applicationGroup.Name, Namespace: applicationGroup.Namespace}
_ = k8sClient.Get(ctx, workflowKey, workflow)
return string(workflow.Status.Phase) == string(v1alpha13.NodeRunning)
}, time.Minute, time.Second).Should(BeTrue())

By("Waiting for the bookinfo object to reach a succeeded reason")
Eventually(func() bool {
applicationGroup = &v1alpha1.ApplicationGroup{}
if err := k8sClient.Get(ctx, key, applicationGroup); err != nil {
return false
}
return applicationGroup.GetReadyCondition() == meta.SucceededReason
}, DefaultTimeout, time.Second).Should(BeTrue())

By("checking that the all the HelmReleases have come up and are in a ready state")
err = k8sClient.List(ctx, helmReleaseList, client.InNamespace(name))
Expect(err).ToNot(HaveOccurred())
Expect(len(helmReleaseList.Items)).To(Equal(oldHelmReleaseCount + OnlyApplicationsHelmReleaseCount))
allReady := true
for _, release := range helmReleaseList.Items {
if condition := meta.GetResourceCondition(&release, meta.ReadyCondition); condition.Reason == meta.SucceededReason {
allReady = false
}
}
Expect(allReady).To(BeTrue())
})

It("should fail to create and post a failed error state", func() {
applicationGroup := defaultAppGroup(name)
applicationGroup.Namespace = DefaultNamespace
Expand Down
22 changes: 15 additions & 7 deletions pkg/helpers/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ func (helper *ReconcileHelper) reconcileApplications() error {
return err
}

if appCh.Dependencies() != nil {
var mustStageSubcharts bool

if application.Spec.Subcharts != nil && len(application.Spec.Subcharts) > 0 && appCh.Dependencies() != nil {
mustStageSubcharts = true
}

if mustStageSubcharts {
// take account of all embedded subcharts found in the application chart
embeddedSubcharts := make(map[string]bool)
for _, d := range appCh.Dependencies() {
Expand Down Expand Up @@ -294,12 +300,14 @@ func (helper *ReconcileHelper) reconcileApplications() error {
// provided in the charts directory.
// IMPORTANT: This expects charts to follow best practices to allow enabling and disabling subcharts
// See: https://helm.sh/docs/topics/charts/ #Chart Dependencies
for _, dep := range appCh.Metadata.Dependencies {
// Disable subchart through metadata
dep.Enabled = false
// Precautionary - overwrite values with subcharts disabled
appCh.Values[dep.Name] = map[string]interface{}{
"enabled": false,
if mustStageSubcharts {
for _, dep := range appCh.Metadata.Dependencies {
// Disable subchart through metadata
dep.Enabled = false
// Precautionary - overwrite values with subcharts disabled
appCh.Values[dep.Name] = map[string]interface{}{
"enabled": false,
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/workflow/workflow_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (wc *ForwardWorkflowClient) Submit(ctx context.Context) error {
ns := &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: namespace,
Labels: map[string]string{
"name": namespace,
},
},
}
if err := controllerutil.SetControllerReference(wc.appGroup, ns, wc.Scheme()); err != nil {
Expand Down

0 comments on commit 9e6538d

Please sign in to comment.