-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow disabling var substitution for certain resources
Signed-off-by: Stefan Prodan <[email protected]>
- Loading branch information
1 parent
0ac1f9e
commit 1f8cb01
Showing
6 changed files
with
131 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/drone/envsubst" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/kustomize/api/resource" | ||
"sigs.k8s.io/yaml" | ||
|
||
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" | ||
) | ||
|
||
// substituteVariables replaces the vars with their values in the specified resource. | ||
// If a resource is labeled or annotated with | ||
// 'kustomize.toolkit.fluxcd.io/substitute: disabled' the substitution is skipped. | ||
func substituteVariables(ctx context.Context, kubeClient client.Client, kustomization kustomizev1.Kustomization, res *resource.Resource) (*resource.Resource, error) { | ||
resData, err := res.AsYAML() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key := fmt.Sprintf("%s/substitute", kustomizev1.GroupVersion.Group) | ||
|
||
if res.GetLabels()[key] == kustomizev1.DisabledValue || res.GetAnnotations()[key] == kustomizev1.DisabledValue { | ||
return nil, nil | ||
} | ||
|
||
vars := make(map[string]string) | ||
|
||
// load vars from ConfigMaps and Secrets data keys | ||
for _, reference := range kustomization.Spec.PostBuild.SubstituteFrom { | ||
namespacedName := types.NamespacedName{Namespace: kustomization.Namespace, Name: reference.Name} | ||
switch reference.Kind { | ||
case "ConfigMap": | ||
resource := &corev1.ConfigMap{} | ||
if err := kubeClient.Get(ctx, namespacedName, resource); err != nil { | ||
return nil, fmt.Errorf("substitute from 'ConfigMap/%s' error: %w", reference.Name, err) | ||
} | ||
for k, v := range resource.Data { | ||
vars[k] = strings.Replace(v, "\n", "", -1) | ||
} | ||
case "Secret": | ||
resource := &corev1.Secret{} | ||
if err := kubeClient.Get(ctx, namespacedName, resource); err != nil { | ||
return nil, fmt.Errorf("substitute from 'Secret/%s' error: %w", reference.Name, err) | ||
} | ||
for k, v := range resource.Data { | ||
vars[k] = strings.Replace(string(v), "\n", "", -1) | ||
} | ||
} | ||
} | ||
|
||
// load in-line vars (overrides the ones from resources) | ||
if kustomization.Spec.PostBuild.Substitute != nil { | ||
for k, v := range kustomization.Spec.PostBuild.Substitute { | ||
vars[k] = strings.Replace(v, "\n", "", -1) | ||
} | ||
} | ||
|
||
// run bash variable substitutions | ||
if len(vars) > 0 { | ||
output, err := envsubst.Eval(string(resData), func(s string) string { | ||
return vars[s] | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("variable substitution failed: %w", err) | ||
} | ||
|
||
jsonData, err := yaml.YAMLToJSON([]byte(output)) | ||
if err != nil { | ||
return nil, fmt.Errorf("YAMLToJSON: %w", err) | ||
} | ||
|
||
err = res.UnmarshalJSON(jsonData) | ||
if err != nil { | ||
return nil, fmt.Errorf("UnmarshalJSON: %w", err) | ||
} | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters