Skip to content

Commit

Permalink
Use get operation on HelmChart and fix RBAC bug
Browse files Browse the repository at this point in the history
Signed-off-by: chengleqi <leqicheng@stu.xidian.edu.cn>
  • Loading branch information
chengleqi committed Aug 5, 2022
1 parent c4752ad commit 296b3d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
3 changes: 2 additions & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ rules:
- list
- update
- apiGroups:
- source.toolkit.fluxcd.io/v1beta2
- source.toolkit.fluxcd.io
resources:
- helmcharts
verbs:
- create
- get
- list
20 changes: 3 additions & 17 deletions controllers/fluxcd/application-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch
//+kubebuilder:rbac:groups="kustomize.toolkit.fluxcd.io",resources=kustomizations,verbs=get;list;create;update;delete
//+kubebuilder:rbac:groups="helm.toolkit.fluxcd.io",resources=helmreleases,verbs=get;list;create;update;delete
//+kubebuilder:rbac:groups="source.toolkit.fluxcd.io/v1beta2",resources=helmcharts,verbs=get;list
//+kubebuilder:rbac:groups="source.toolkit.fluxcd.io",resources=helmcharts,verbs=get;list;create

// ApplicationReconciler is the reconciler of the FluxCD HelmRelease and FluxCD Kustomization
type ApplicationReconciler struct {
Expand Down Expand Up @@ -95,15 +95,11 @@ func (r *ApplicationReconciler) reconcileHelm(app *v1alpha1.Application) (err er
if helmTemplateName := app.Spec.FluxApp.Spec.Config.HelmRelease.Template; helmTemplateName != "" {
// use template
helmTemplateNS := app.GetNamespace()
helmTemplateList := createBareFluxHelmTemplateObjectList()

if err = r.List(ctx, helmTemplateList, client.InNamespace(helmTemplateNS), client.MatchingLabels{
v1alpha1.HelmTemplateName: helmTemplateName,
}); err != nil {
helmChart = createBareFluxHelmTemplateObject()
if err = r.Get(ctx, types.NamespacedName{Namespace: helmTemplateNS, Name: helmTemplateName}, helmChart); err != nil {
return
}
// there is a helmtemplate that user specified
helmChart = &helmTemplateList.Items[0]
} else {
helmChart = createUnstructuredFluxHelmTemplate(app)
}
Expand Down Expand Up @@ -376,16 +372,6 @@ func createBareFluxHelmTemplateObject() *unstructured.Unstructured {
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
12 changes: 12 additions & 0 deletions controllers/fluxcd/application-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,18 @@ func TestApplicationReconciler_reconcileApp(t *testing.T) {
assert.Equal(t, "./helm-chart/values.yaml", chartValueFiles[0])
},
},
{
name: "create a Multi-Clusters FluxApp(HelmRelease) by using a Template",
fields: fields{
Client: fake.NewFakeClientWithScheme(schema),
},
args: args{
app: helmAppWithTemplate.DeepCopy(),
},
verify: func(t *testing.T, Client client.Client, err error) {
assert.NotNil(t, err)
},
},
{
name: "create a Multi-Clusters FluxApp(Kustomization)",
fields: fields{
Expand Down

0 comments on commit 296b3d1

Please sign in to comment.