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

[BOP-951] Introduce dependencies for helm chart addons #92

Merged
merged 3 commits into from
Sep 24, 2024
Merged
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
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"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be of type NamespacedObjectReference so we can depend on a addon from another namespace?

Copy link
Contributor Author

@byDimasik byDimasik Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ranyodh

Nope, FluxCD needs the HelmRelease name and namespace. BOP generates all helm releases with the following values:

&helmv2.HelmRelease{
    ObjectMeta: metav1.ObjectMeta{
        Name:      addon.Spec.Name,
        Namespace: consts.NamespaceBoundlessSystem,
    },
},

here the namespace is always blueprint-system, and the release name is the addon name, so, all we need to allow dependencies between addons is the addon name.

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)
byDimasik marked this conversation as resolved.
Show resolved Hide resolved
}

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
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions config/crd/bases/blueprint.mirantis.com_addons.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ spec:
properties:
chart:
properties:
dependsOn:
items:
type: string
type: array
name:
type: string
repo:
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/blueprint.mirantis.com_blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ spec:
properties:
chart:
properties:
dependsOn:
items:
type: string
type: array
name:
type: string
repo:
Expand Down
11 changes: 6 additions & 5 deletions controllers/blueprint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,12 @@ func addonResource(spec *boundlessv1alpha1.AddonSpec) *boundlessv1alpha1.Addon {

if spec.Chart != nil {
addon.Spec.Chart = &boundlessv1alpha1.ChartInfo{
Name: spec.Chart.Name,
Repo: spec.Chart.Repo,
Version: spec.Chart.Version,
Set: spec.Chart.Set,
Values: spec.Chart.Values,
Name: spec.Chart.Name,
Repo: spec.Chart.Repo,
Version: spec.Chart.Version,
Set: spec.Chart.Set,
Values: spec.Chart.Values,
DependsOn: spec.Chart.DependsOn,
}
}

Expand Down
8 changes: 8 additions & 0 deletions deploy/static/blueprint-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ spec:
properties:
chart:
properties:
dependsOn:
items:
type: string
type: array
name:
type: string
repo:
Expand Down Expand Up @@ -313,6 +317,10 @@ spec:
properties:
chart:
properties:
dependsOn:
items:
type: string
type: array
name:
type: string
repo:
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
Loading