Skip to content

Commit

Permalink
Watch HelmRelease
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Nov 8, 2024
1 parent 4fddb26 commit d568a97
Show file tree
Hide file tree
Showing 25 changed files with 3,972 additions and 7 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.1
toolchain go1.23.2

require (
github.com/fluxcd/helm-controller/api v1.0.1
github.com/onsi/ginkgo/v2 v2.17.2
github.com/onsi/gomega v1.33.1
github.com/spf13/cobra v1.8.0
Expand Down Expand Up @@ -44,6 +45,8 @@ require (
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fluxcd/pkg/apis/kustomize v1.5.0 // indirect
github.com/fluxcd/pkg/apis/meta v1.5.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fluxcd/helm-controller/api v1.0.1 h1:Gn9qEVuif6D5+gHmVwTEZkR4+nmLOcOhKx4Sw2gL2EA=
github.com/fluxcd/helm-controller/api v1.0.1/go.mod h1:/6AD5a2qjo/ttxVM8GR33syLZwqigta60DCLdy8GrME=
github.com/fluxcd/pkg/apis/kustomize v1.5.0 h1:ah4sfqccnio+/5Edz/tVz6LetFhiBoDzXAElj6fFCzU=
github.com/fluxcd/pkg/apis/kustomize v1.5.0/go.mod h1:nEzhnhHafhWOUUV8VMFLojUOH+HHDEsL75y54mt/c30=
github.com/fluxcd/pkg/apis/meta v1.5.0 h1:/G82d2Az5D9op3F+wJUpD8jw/eTV0suM6P7+cSURoUM=
github.com/fluxcd/pkg/apis/meta v1.5.0/go.mod h1:Y3u7JomuuKtr5fvP1Iji2/50FdRe5GcBug2jawNVkdM=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmds/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.bytebuilders.dev/aceshifter/pkg/controller"

helmapi "github.com/fluxcd/helm-controller/api/v2"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -45,6 +46,7 @@ var (

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(helmapi.AddToScheme(scheme))
utilruntime.Must(uiapi.AddToScheme(scheme))
}

Expand Down
38 changes: 31 additions & 7 deletions pkg/controller/feature_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"go.bytebuilders.dev/aceshifter/pkg/featuresets"
"go.bytebuilders.dev/aceshifter/pkg/tracker"

helmapi "github.com/fluxcd/helm-controller/api/v2"
core "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -61,7 +63,18 @@ func (r *FeatureReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, client.IgnoreNotFound(err)
}

uidStart, _, err := tracker.GetUid(r.Client, feature.Spec.Chart.Namespace)
ns := core.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: feature.Spec.Chart.Namespace,
},
}
if err := r.Get(ctx, client.ObjectKeyFromObject(&ns), &ns); apierrors.IsNotFound(err) {
if err := r.Create(ctx, &ns); err != nil {
return ctrl.Result{}, client.IgnoreAlreadyExists(err)
}
}

uidStart, _, err := tracker.GetUid(r.Client, ns.Name)
if err != nil || uidStart == tracker.UidNone {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -96,7 +109,7 @@ func (r *FeatureReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

// SetupWithManager sets up the controller with the Manager.
func (r *FeatureReconciler) SetupWithManager(mgr ctrl.Manager) error {
fn := handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
mapNamespaceToFeature := func(ctx context.Context, obj client.Object) []reconcile.Request {
log := log.FromContext(ctx)

var list uiapi.FeatureList
Expand All @@ -116,12 +129,23 @@ func (r *FeatureReconciler) SetupWithManager(mgr ctrl.Manager) error {
})
}
return reqs
})
}

mapHelmReleaseToFeature := func(ctx context.Context, obj client.Object) []reconcile.Request {
return []reconcile.Request{{
NamespacedName: types.NamespacedName{Name: obj.GetName()},
}}
}

return ctrl.NewControllerManagedBy(mgr).
For(&uiapi.Feature{}).
Watches(&core.Namespace{}, fn, builder.WithPredicates(predicate.NewPredicateFuncs(func(obj client.Object) bool {
_, ok := obj.GetAnnotations()[tracker.KeyUid]
return ok
}))).
Watches(
&core.Namespace{},
handler.EnqueueRequestsFromMapFunc(mapNamespaceToFeature),
builder.WithPredicates(predicate.NewPredicateFuncs(func(obj client.Object) bool {
_, ok := obj.GetAnnotations()[tracker.KeyUid]
return ok
}))).
Watches(&helmapi.HelmRelease{}, handler.EnqueueRequestsFromMapFunc(mapHelmReleaseToFeature)).
Complete(r)
}
201 changes: 201 additions & 0 deletions vendor/github.com/fluxcd/helm-controller/api/LICENSE

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

Loading

0 comments on commit d568a97

Please sign in to comment.