Skip to content

Commit

Permalink
introduce dependencies for helm chart addons
Browse files Browse the repository at this point in the history
  • Loading branch information
byDimasik committed Sep 24, 2024
1 parent cb4b2a9 commit e441dc2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
5 changes: 3 additions & 2 deletions api/v1alpha1/addon_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ type ChartInfo struct {
// +kubebuilder:validation:Required
Version string `json:"version"`

Set map[string]intstr.IntOrString `json:"set,omitempty"`
Values string `json:"values,omitempty"`
DependsOn []string `json:"dependsOn,omitempty"`
Set map[string]intstr.IntOrString `json:"set,omitempty"`
Values string `json:"values,omitempty"`
}

type ManifestInfo struct {
Expand Down
54 changes: 34 additions & 20 deletions api/v1alpha1/blueprint_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1alpha1

import (
"fmt"
"slices"
"strings"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -59,30 +60,43 @@ func (r *Blueprint) ValidateDelete() (admission.Warnings, error) {
}

func validate(spec BlueprintSpec) (admission.Warnings, error) {
if len(spec.Components.Addons) > 0 {
for _, val := range spec.Components.Addons {
if strings.EqualFold(kindChart, val.Kind) {
if val.Manifest != nil {
blueprintlog.Info("received manifest object.", "Kind", kindChart)
return nil, fmt.Errorf("manifest object is not allowed for addon kind %s", kindChart)
}
if val.Chart == nil {
blueprintlog.Info("received empty chart object.", "Kind", kindChart)
return nil, fmt.Errorf("chart object can't be empty for addon kind %s", kindChart)
}
}
if len(spec.Components.Addons) == 0 {
return nil, nil
}

if strings.EqualFold(kindManifest, val.Kind) {
if val.Chart != nil {
blueprintlog.Info("received chart object.", "Kind", kindManifest)
return nil, fmt.Errorf("chart object is not allowed for addon kind %s", kindManifest)
}
if val.Manifest == nil {
blueprintlog.Info("received empty manifest object.", "Kind", kindManifest)
return nil, fmt.Errorf("manifest object can't be empty for addon kind %s", kindManifest)
var addonNames []string
for _, a := range spec.Components.Addons {
addonNames = append(addonNames, a.Name)
}

for _, val := range spec.Components.Addons {
if strings.EqualFold(kindChart, val.Kind) {
if val.Manifest != nil {
blueprintlog.Info("received manifest object.", "Kind", kindChart)
return nil, fmt.Errorf("manifest object is not allowed for addon kind %s", kindChart)
}
if val.Chart == nil {
blueprintlog.Info("received empty chart object.", "Kind", kindChart)
return nil, fmt.Errorf("chart object can't be empty for addon kind %s", kindChart)
}
if len(val.Chart.DependsOn) > 0 {
for _, dep := range val.Chart.DependsOn {
if !slices.Contains(addonNames, dep) {
return nil, fmt.Errorf("addon %s depends on %s which is not present in the list of addons", val.Name, dep)
}
}
}
}

if strings.EqualFold(kindManifest, val.Kind) {
if val.Chart != nil {
blueprintlog.Info("received chart object.", "Kind", kindManifest)
return nil, fmt.Errorf("chart object is not allowed for addon kind %s", kindManifest)
}
if val.Manifest == nil {
blueprintlog.Info("received empty manifest object.", "Kind", kindManifest)
return nil, fmt.Errorf("manifest object can't be empty for addon kind %s", kindManifest)
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/controllers/helm/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

helmv2 "github.com/fluxcd/helm-controller/api/v2"
"github.com/fluxcd/pkg/apis/meta"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
"github.com/go-logr/logr"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -88,6 +89,14 @@ func (hc *Controller) CreateHelmRelease(ctx context.Context, addon *v1alpha1.Add
values = &apiextensionsv1.JSON{Raw: v}
}

var dependsOn []meta.NamespacedObjectReference
for _, addonName := range chartSpec.DependsOn {
dependsOn = append(dependsOn, meta.NamespacedObjectReference{
Name: addonName,
Namespace: consts.NamespaceBoundlessSystem,
})
}

release := &helmv2.HelmRelease{
TypeMeta: helmReleaseTypeMeta,
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -130,6 +139,7 @@ func (hc *Controller) CreateHelmRelease(ctx context.Context, addon *v1alpha1.Add
Interval: metav1.Duration{
Duration: driftDetectionInterval,
},
DependsOn: dependsOn,
},
}

Expand Down

0 comments on commit e441dc2

Please sign in to comment.