Skip to content

Commit

Permalink
Remove helm-template-controller && Use label to manage Template
Browse files Browse the repository at this point in the history
  • Loading branch information
chengleqi committed Jul 22, 2022
1 parent b4ffa19 commit f963c56
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 231 deletions.
7 changes: 0 additions & 7 deletions cmd/controller/app/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ func getAllControllers(mgr manager.Manager, client k8s.Client, informerFactory i
Client: mgr.GetClient(),
}

fluxcdHelmtemplateReconciler := &fluxcd.HelmTemplateReconciler{
Client: mgr.GetClient(),
}

fluxcdApplicationReconciler := &fluxcd.ApplicationReconciler{
Client: mgr.GetClient(),
}
Expand Down Expand Up @@ -210,9 +206,6 @@ func getAllControllers(mgr manager.Manager, client k8s.Client, informerFactory i
if err = fluxcdGitRepoReconciler.SetupWithManager(mgr); err != nil {
return
}
if err = fluxcdHelmtemplateReconciler.SetupWithManager(mgr); err != nil {
return
}
return fluxcdApplicationReconciler.SetupWithManager(mgr)
},
}
Expand Down
3 changes: 0 additions & 3 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,5 @@ rules:
resources:
- helmcharts
verbs:
- create
- delete
- get
- list
- update
177 changes: 118 additions & 59 deletions controllers/fluxcd/application-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (r *ApplicationReconciler) Reconcile(req ctrl.Request) (result ctrl.Result,

if app.Spec.FluxApp != nil {
err = r.reconcileApp(app)
if app.Spec.FluxApp.Spec.Config.HelmRelease.Chart != nil {
err = r.saveTemplate(app)
}
}
return
}
Expand All @@ -79,27 +82,31 @@ func (r *ApplicationReconciler) reconcileApp(app *v1alpha1.Application) (err err
func (r *ApplicationReconciler) reconcileHelm(app *v1alpha1.Application) (err error) {
ctx := context.Background()

var helmTemplate *unstructured.Unstructured
var helmChart *unstructured.Unstructured
if helmTemplateName := app.Spec.FluxApp.Spec.Config.HelmRelease.Template; helmTemplateName != "" {
// use template
helmTemplateNS := app.GetNamespace()
helmTemplate = createBareFluxHelmTemplateObject()
// this HelmChart is generated by helmtemplate-controller
if err = r.Get(ctx, types.NamespacedName{Namespace: helmTemplateNS, Name: helmTemplateName}, helmTemplate); err != nil {
helmTemplateList := createBareFluxHelmTemplateObjectList()

if err = r.List(ctx, helmTemplateList, client.InNamespace(helmTemplateNS), client.MatchingLabels{
v1alpha1.HelmTemplateName: helmTemplateName,
}); err != nil {
return
}
// there is a helmtemplate that user specified
helmChart = &helmTemplateList.Items[0]
} else {
helmTemplate = createUnstructuredFluxHelmTemplate(app)
helmChart = createUnstructuredFluxHelmTemplate(app)
}

if err = r.reconcileHelmReleaseList(app, helmTemplate); err != nil {
if err = r.reconcileHelmReleaseList(app, helmChart); err != nil {
return
}

return
}

func (r *ApplicationReconciler) reconcileHelmReleaseList(app *v1alpha1.Application, helmTemplate *unstructured.Unstructured) (err error) {
func (r *ApplicationReconciler) reconcileHelmReleaseList(app *v1alpha1.Application, helmChart *unstructured.Unstructured) (err error) {
ctx := context.Background()

fluxApp := app.Spec.FluxApp.DeepCopy()
Expand All @@ -124,27 +131,27 @@ func (r *ApplicationReconciler) reconcileHelmReleaseList(app *v1alpha1.Applicati
if _, ok := hrMap[helmReleaseName]; !ok {
// there is no matching helmRelease
// create
if err = r.createHelmRelease(app, helmTemplate, deploy); err != nil {
if err = r.createHelmRelease(app, helmChart, deploy); err != nil {
return
}
} else {
// there is a matching helmRelease
// update the helmRelease
if err = r.updateHelmRelease(hrMap[helmReleaseName], helmTemplate, deploy); err != nil {
if err = r.updateHelmRelease(hrMap[helmReleaseName], helmChart, deploy); err != nil {
return
}
}
}
return
}

func (r *ApplicationReconciler) createHelmRelease(app *v1alpha1.Application, helmTemplate *unstructured.Unstructured, deploy *v1alpha1.Deploy) (err error) {
func (r *ApplicationReconciler) createHelmRelease(app *v1alpha1.Application, helmChart *unstructured.Unstructured, deploy *v1alpha1.Deploy) (err error) {
ctx := context.Background()
appNS, appName := app.GetNamespace(), app.GetName()
hrNS, hrName := appNS, getHelmReleaseName(deploy.Destination.TargetNamespace)

newFluxHelmRelease := createBareFluxHelmReleaseObject()
setFluxHelmReleaseFields(newFluxHelmRelease, helmTemplate, deploy)
setFluxHelmReleaseFields(newFluxHelmRelease, helmChart, deploy)
newFluxHelmRelease.SetNamespace(hrNS)
newFluxHelmRelease.SetName(hrName)
newFluxHelmRelease.SetLabels(map[string]string{
Expand All @@ -166,23 +173,29 @@ func (r *ApplicationReconciler) createHelmRelease(app *v1alpha1.Application, hel
return
}

func getHelmReleaseName(targetNamespace string) string {
return targetNamespace
func (r *ApplicationReconciler) updateHelmRelease(hr, helmChart *unstructured.Unstructured, deploy *v1alpha1.Deploy) (err error) {
ctx := context.Background()

setFluxHelmReleaseFields(hr, helmChart, deploy)
if err = r.Update(ctx, hr); err != nil {
return
}
return
}

func setFluxHelmReleaseFields(newFluxHelmRelease *unstructured.Unstructured, helmTemplate *unstructured.Unstructured, deploy *v1alpha1.Deploy) {
func setFluxHelmReleaseFields(hr, helmTemplate *unstructured.Unstructured, deploy *v1alpha1.Deploy) {
helmTemplateSpec, _, _ := unstructured.NestedFieldNoCopy(helmTemplate.Object, "spec")
_ = argocd.SetNestedField(newFluxHelmRelease.Object, helmTemplateSpec, "spec", "chart", "spec")
_ = argocd.SetNestedField(hr.Object, helmTemplateSpec, "spec", "chart", "spec")

configs, _ := argocd.InterfaceToMap(*deploy)
for k, config := range configs {
if k != "destination" {
_ = unstructured.SetNestedField(newFluxHelmRelease.Object, config, "spec", k)
_ = unstructured.SetNestedField(hr.Object, config, "spec", k)
}
}

_ = argocd.SetNestedField(newFluxHelmRelease.Object, deploy.Destination.KubeConfig, "spec", "kubeConfig")
_ = unstructured.SetNestedField(newFluxHelmRelease.Object, deploy.Destination.TargetNamespace, "spec", "targetNamespace")
_ = argocd.SetNestedField(hr.Object, deploy.Destination.KubeConfig, "spec", "kubeConfig")
_ = unstructured.SetNestedField(hr.Object, deploy.Destination.TargetNamespace, "spec", "targetNamespace")
}

func (r *ApplicationReconciler) reconcileKustomization(app *v1alpha1.Application) (err error) {
Expand Down Expand Up @@ -211,6 +224,93 @@ func isHelmOrKustomize(app *v1alpha1.Application) AppType {
return Kustomization
}

// GetGroupName returns the group name
func (r *ApplicationReconciler) GetGroupName() string {
return controllerGroupName
}

// GetName returns the name of this reconciler
func (r *ApplicationReconciler) GetName() string {
return "FluxApplicationReconciler"
}

func getHelmReleaseName(targetNamespace string) string {
return targetNamespace
}

func getHelmTemplateName(ns, name string) string {
return ns + "-" + name
}

func (r *ApplicationReconciler) saveTemplate(app *v1alpha1.Application) (err error) {
ctx := context.Background()
helmTemplate := createBareFluxHelmTemplateObject()

appNS, appName := app.GetNamespace(), app.GetName()
helmTemplateNS, helmTemplateName := appNS, getHelmTemplateName(appNS, appName)

if err = r.Get(ctx, types.NamespacedName{Namespace: helmTemplateNS, Name: helmTemplateName}, helmTemplate); err != nil {
if !apierrors.IsNotFound(err) {
return
}
// not found
// check whether save HelmTemplate
if wantSaveHelmTemplate(app) {
// CREATE Operation
newFluxHelmTemplate := createUnstructuredFluxHelmTemplate(app)
newFluxHelmTemplate.SetName(helmTemplateName)
newFluxHelmTemplate.SetNamespace(helmTemplateNS)
newFluxHelmTemplate.SetLabels(map[string]string{
v1alpha1.HelmTemplateName: helmTemplateName,
})
if err = r.Create(ctx, newFluxHelmTemplate); err != nil {
return
}
r.log.Info("Create HelmTemplate", "name", newFluxHelmTemplate.GetName())
r.recorder.Eventf(app, corev1.EventTypeNormal, "Created", "Created HelmTemplate %s", newFluxHelmTemplate.GetName())
}
}
return
}

func wantSaveHelmTemplate(app *v1alpha1.Application) bool {
if v, ok := app.GetLabels()[v1alpha1.SaveTemplateLabelKey]; ok {
return v == "true"
}
return false
}

func createUnstructuredFluxHelmTemplate(app *v1alpha1.Application) (newFluxHelmTemplate *unstructured.Unstructured) {
newFluxHelmTemplate = createBareFluxHelmTemplateObject()
newFluxHelmTemplate.SetLabels(map[string]string{
"app.kubernetes.io/managed-by": v1alpha1.GroupName,
})

_ = argocd.SetNestedField(newFluxHelmTemplate.Object, app.Spec.FluxApp.Spec.Config.HelmRelease.Chart, "spec")
_ = argocd.SetNestedField(newFluxHelmTemplate.Object, app.Spec.FluxApp.Spec.Source.SourceRef, "spec", "sourceRef")
return
}

func createBareFluxHelmTemplateObject() *unstructured.Unstructured {
fluxHelmChart := &unstructured.Unstructured{}
fluxHelmChart.SetGroupVersionKind(schema.GroupVersionKind{
Group: "source.toolkit.fluxcd.io",
Version: "v1beta2",
Kind: "HelmChart",
})
return fluxHelmChart
}

func createBareFluxHelmTemplateObjectList() *unstructured.UnstructuredList {
fluxHelmTemplateList := &unstructured.UnstructuredList{}
fluxHelmTemplateList.SetGroupVersionKind(schema.GroupVersionKind{
Group: "source.toolkit.fluxcd.io",
Version: "v1beta2",
Kind: "HelmChartList",
})
return fluxHelmTemplateList
}

func createBareFluxHelmReleaseObject() *unstructured.Unstructured {
fluxHelmRelease := &unstructured.Unstructured{}
fluxHelmRelease.SetGroupVersionKind(schema.GroupVersionKind{
Expand Down Expand Up @@ -241,16 +341,6 @@ func createBareFluxKustomizationObject() *unstructured.Unstructured {
return fluxKustomization
}

// GetGroupName returns the group name
func (r *ApplicationReconciler) GetGroupName() string {
return controllerGroupName
}

// GetName returns the name of this reconciler
func (r *ApplicationReconciler) GetName() string {
return "FluxApplicationReconciler"
}

// SetupWithManager setups the reconciler with a manager
// setup the logger, recorder
func (r *ApplicationReconciler) SetupWithManager(mgr manager.Manager) error {
Expand All @@ -261,34 +351,3 @@ func (r *ApplicationReconciler) SetupWithManager(mgr manager.Manager) error {
For(&v1alpha1.Application{}).
Complete(r)
}

func (r *ApplicationReconciler) updateHelmRelease(hr *unstructured.Unstructured, helmTemplate *unstructured.Unstructured, deploy *v1alpha1.Deploy) (err error) {
ctx := context.Background()

setFluxHelmReleaseFields(hr, helmTemplate, deploy)
if err = r.Update(ctx, hr); err != nil {
return
}
return
}

//func (r *ApplicationReconciler) reconcileFluxHelmReleasesDelete(ctx context.Context, app *v1alpha1.Application) (err error) {
// appNS, appName := app.GetNamespace(), app.GetName()
//
// fluxHelmReleaseList := createBareFluxHelmReleaseListObject()
// if err = r.List(ctx, fluxHelmReleaseList, client.InNamespace(appNS), client.MatchingLabels{
// "app.kubernetes.io/managed-by": getHelmTemplateName(appNS, appName),
// }); err != nil {
// return
// }
//
// for _, fluxHelmRelease := range fluxHelmReleaseList.Items {
// if err = r.Delete(ctx, &fluxHelmRelease); err != nil {
// return
// }
// r.log.Info("Delete FluxCD HelmRelease", "name", fluxHelmRelease.GetName())
// r.recorder.Eventf(app, corev1.EventTypeNormal, "Deleted", "Deleted HelmRelease %s", fluxHelmRelease.GetName())
// }
//
// return
//}
Loading

0 comments on commit f963c56

Please sign in to comment.