Skip to content

Commit

Permalink
Improvement : renaming
Browse files Browse the repository at this point in the history
* more changes

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

* clearer docs

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

* abstract

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

* naming

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

* revert accidental change

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

* ugh

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

* fix accidental renames

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

---------

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
  • Loading branch information
crenshaw-dev authored May 24, 2023
1 parent 33c8517 commit 9813471
Show file tree
Hide file tree
Showing 18 changed files with 1,179 additions and 879 deletions.
42 changes: 2 additions & 40 deletions applicationset/generators/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (g *ClusterGenerator) GenerateParams(
params["nameNormalized"] = cluster.Name
params["server"] = cluster.Server

err = appendTemplatedValues(appSetGenerator.Clusters.Values, params, appSet)
err = utils.AppendTemplatedValues(render, appSetGenerator.Clusters.Values, params, appSet)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (g *ClusterGenerator) GenerateParams(
}
}

err = appendTemplatedValues(appSetGenerator.Clusters.Values, params, appSet)
err = utils.AppendTemplatedValues(render, appSetGenerator.Clusters.Values, params, appSet)
if err != nil {
return nil, err
}
Expand All @@ -162,44 +162,6 @@ func (g *ClusterGenerator) GenerateParams(
return res, nil
}

func appendTemplatedValues(clusterValues map[string]string, params map[string]interface{}, appSet *argoappsetv1alpha1.ApplicationSet) error {
// We create a local map to ensure that we do not fall victim to a billion-laughs attack. We iterate through the
// cluster values map and only replace values in said map if it has already been whitelisted in the params map.
// Once we iterate through all the cluster values we can then safely merge the `tmp` map into the main params map.
tmp := map[string]interface{}{}

for key, value := range clusterValues {
result, err := replaceTemplatedString(value, params, appSet)

if err != nil {
return fmt.Errorf("error replacing templated String: %w", err)
}

if appSet.Spec.GoTemplate {
if tmp["values"] == nil {
tmp["values"] = map[string]string{}
}
tmp["values"].(map[string]string)[key] = result
} else {
tmp[fmt.Sprintf("values.%s", key)] = result
}
}

for key, value := range tmp {
params[key] = value
}

return nil
}

func replaceTemplatedString(value string, params map[string]interface{}, appSet *argoappsetv1alpha1.ApplicationSet) (string, error) {
replacedTmplStr, err := render.Replace(value, params, appSet.Spec.GoTemplate)
if err != nil {
return "", err
}
return replacedTmplStr, nil
}

func (g *ClusterGenerator) getSecretsByClusterName(appSetGenerator *argoappsetv1alpha1.ApplicationSetGenerator) (map[string]corev1.Secret, error) {
// List all Clusters:
clusterSecretList := &corev1.SecretList{}
Expand Down
4 changes: 2 additions & 2 deletions applicationset/generators/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"fmt"
"testing"

"github.com/argoproj/argo-cd/v2/applicationset/services/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/argoproj/argo-cd/v2/applicationset/services/mocks"

argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
)


func Test_generateParamsFromGitFile(t *testing.T) {
params, err := (*GitGenerator)(nil).generateParamsFromGitFile("path/dir/file_name.yaml", []byte(`
foo:
Expand Down
51 changes: 25 additions & 26 deletions applicationset/generators/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/argoproj/argo-cd/v2/applicationset/services/mocks"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -14,6 +13,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/argoproj/argo-cd/v2/applicationset/services/mocks"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -848,7 +849,7 @@ func TestMatrixGenerateListElementsYaml(t *testing.T) {
}

listGenerator := &argoprojiov1alpha1.ListGenerator{
Elements: []apiextensionsv1.JSON{},
Elements: []apiextensionsv1.JSON{},
ElementsYaml: "{{ .foo.bar | toJson }}",
}

Expand All @@ -870,60 +871,59 @@ func TestMatrixGenerateListElementsYaml(t *testing.T) {
},
expected: []map[string]interface{}{
{
"chart": "a",
"version": "1",
"chart": "a",
"version": "1",
"foo": map[string]interface{}{
"bar": []interface{}{
map[string]interface{}{
"chart": "a",
"chart": "a",
"version": "1",
},
map[string]interface{}{
"chart": "b",
"chart": "b",
"version": "2",
},
},
},
"path": map[string]interface{}{
"basename": "dir",
"basename": "dir",
"basenameNormalized": "dir",
"filename": "file_name.yaml",
"filename": "file_name.yaml",
"filenameNormalized": "file-name.yaml",
"path": "path/dir",
"segments": []string {
"path": "path/dir",
"segments": []string{
"path",
"dir",
},
},
},
{
"chart": "b",
"version": "2",
"chart": "b",
"version": "2",
"foo": map[string]interface{}{
"bar": []interface{}{
map[string]interface{}{
"chart": "a",
"chart": "a",
"version": "1",
},
map[string]interface{}{
"chart": "b",
"chart": "b",
"version": "2",
},
},
},
"path": map[string]interface{}{
"basename": "dir",
"basename": "dir",
"basenameNormalized": "dir",
"filename": "file_name.yaml",
"filename": "file_name.yaml",
"filenameNormalized": "file-name.yaml",
"path": "path/dir",
"segments": []string {
"path": "path/dir",
"segments": []string{
"path",
"dir",
},
},
},

},
},
}
Expand Down Expand Up @@ -952,27 +952,26 @@ func TestMatrixGenerateListElementsYaml(t *testing.T) {
"foo": map[string]interface{}{
"bar": []interface{}{
map[string]interface{}{
"chart": "a",
"chart": "a",
"version": "1",
},
map[string]interface{}{
"chart": "b",
"chart": "b",
"version": "2",
},
},
},
"path": map[string]interface{}{
"basename": "dir",
"basename": "dir",
"basenameNormalized": "dir",
"filename": "file_name.yaml",
"filename": "file_name.yaml",
"filenameNormalized": "file-name.yaml",
"path": "path/dir",
"segments": []string {
"path": "path/dir",
"segments": []string{
"path",
"dir",
},
},

}}, nil)
genMock.On("GetTemplate", &gitGeneratorSpec).
Return(&argoprojiov1alpha1.ApplicationSetTemplate{})
Expand Down
19 changes: 9 additions & 10 deletions applicationset/generators/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/argoproj/argo-cd/v2/applicationset/utils"
argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/settings"

Expand Down Expand Up @@ -75,12 +76,12 @@ func (g *PluginGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.App
return nil, err
}

list, err := pluginClient.List(ctx, providerConfig.Parameters)
list, err := pluginClient.List(ctx, providerConfig.InputParameters)
if err != nil {
return nil, fmt.Errorf("error listing params: %w", err)
}

res, err := g.generateParams(list.Parameters, appSetGenerator.Plugin.Parameters, appSetGenerator.Plugin.AppendParamsToValues, applicationSetInfo.Spec.GoTemplate)
res, err := g.generateParams(appSetGenerator, applicationSetInfo, list.OutputParameters, appSetGenerator.Plugin.InputParameters, applicationSetInfo.Spec.GoTemplate)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -114,7 +115,7 @@ func (g *PluginGenerator) getPluginFromGenerator(ctx context.Context, appSetName
return pluginClient, nil
}

func (g *PluginGenerator) generateParams(objectsFound []map[string]interface{}, pluginParams map[string]apiextensionsv1.JSON, appendParamsToValues bool, useGoTemplate bool) ([]map[string]interface{}, error) {
func (g *PluginGenerator) generateParams(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator, appSet *argoprojiov1alpha1.ApplicationSet, objectsFound []map[string]interface{}, pluginArgs map[string]apiextensionsv1.JSON, useGoTemplate bool) ([]map[string]interface{}, error) {
res := []map[string]interface{}{}

for _, objectFound := range objectsFound {
Expand All @@ -135,13 +136,11 @@ func (g *PluginGenerator) generateParams(objectsFound []map[string]interface{},
}
}

if appendParamsToValues {
for k, v := range pluginParams {
// value returned by the plugin has precedence
if _, present := params[k]; !present {
params[k] = v
}
}
params["inputParameters"] = pluginArgs

err := utils.AppendTemplatedValues(render, appSetGenerator.Plugin.Values, params, appSet)
if err != nil {
return nil, err
}

res = append(res, params)
Expand Down
Loading

0 comments on commit 9813471

Please sign in to comment.