Skip to content

Commit 7f527ab

Browse files
committed
Add optional built-in substitution of last attempted revision
The purpose of this new feature (which is CLI-conditional, and defaults to false in order to preserve compatibility) is to allow to refer to the revision that is currently being applied in the resource manifests, via the standard substitutions mechanism. By enabling the feature, the controller will always add the FLUX_ARTIFACT_REVISION key to the substitutions map, with the value of the artifact revision as provided by the source controller. If, however, the artifact revision matches the following pattern: ([^@]+)@sha1:(.+) then it will also add the following two keys: - FLUX_ARTIFACT_REF: the 1st group in the pattern, representing the branch or ref part for source-control based revisions (e.g. Git) - FLUX_ARTIFACT_SHA: the 2nd group in the pattern, representing the SHA usually in source-control based revisions (e.g. Git). Signed-off-by: arikkfir <[email protected]>
1 parent 968df5e commit 7f527ab

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

internal/controller/kustomization_controller.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"os"
25+
"regexp"
2526
"sort"
2627
"strings"
2728
"time"
@@ -95,6 +96,7 @@ type KustomizationReconciler struct {
9596
DefaultServiceAccount string
9697
KubeConfigOpts runtimeClient.KubeConfigOptions
9798
ConcurrentSSA int
99+
ImplicitSubstitutions bool
98100
}
99101

100102
// KustomizationReconcilerOptions contains options for the KustomizationReconciler.
@@ -104,6 +106,10 @@ type KustomizationReconcilerOptions struct {
104106
RateLimiter ratelimiter.RateLimiter
105107
}
106108

109+
var (
110+
refAndRevisionRE = regexp.MustCompile("([^@]+)@sha1:(.+)")
111+
)
112+
107113
func (r *KustomizationReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opts KustomizationReconcilerOptions) error {
108114
const (
109115
ociRepositoryIndexKey string = ".metadata.ociRepository"
@@ -380,7 +386,7 @@ func (r *KustomizationReconciler) reconcile(
380386
}
381387

382388
// Build the Kustomize overlay and decrypt secrets if needed.
383-
resources, err := r.build(ctx, obj, unstructured.Unstructured{Object: k}, tmpDir, dirPath)
389+
resources, err := r.build(ctx, obj, src.GetArtifact(), unstructured.Unstructured{Object: k}, tmpDir, dirPath)
384390
if err != nil {
385391
conditions.MarkFalse(obj, meta.ReadyCondition, kustomizev1.BuildFailedReason, err.Error())
386392
return err
@@ -573,7 +579,7 @@ func (r *KustomizationReconciler) generate(obj unstructured.Unstructured,
573579
}
574580

575581
func (r *KustomizationReconciler) build(ctx context.Context,
576-
obj *kustomizev1.Kustomization, u unstructured.Unstructured,
582+
obj *kustomizev1.Kustomization, artifact *sourcev1.Artifact, u unstructured.Unstructured,
577583
workDir, dirPath string) ([]byte, error) {
578584
dec, cleanup, err := decryptor.NewTempDecryptor(workDir, r.Client, obj)
579585
if err != nil {
@@ -617,6 +623,22 @@ func (r *KustomizationReconciler) build(ctx context.Context,
617623
}
618624
}
619625

626+
// add built-in substitutions
627+
if r.ImplicitSubstitutions {
628+
if obj.Spec.PostBuild == nil {
629+
obj.Spec.PostBuild = &kustomizev1.PostBuild{}
630+
}
631+
if obj.Spec.PostBuild.Substitute == nil {
632+
obj.Spec.PostBuild.Substitute = make(map[string]string)
633+
}
634+
obj.Spec.PostBuild.Substitute["FLUX_ARTIFACT_REVISION"] = artifact.Revision
635+
matches := refAndRevisionRE.FindStringSubmatch(artifact.Revision)
636+
if len(matches) == 3 {
637+
obj.Spec.PostBuild.Substitute["FLUX_ARTIFACT_REF"] = matches[1]
638+
obj.Spec.PostBuild.Substitute["FLUX_ARTIFACT_SHA"] = matches[2]
639+
}
640+
}
641+
620642
// run variable substitutions
621643
if obj.Spec.PostBuild != nil {
622644
outRes, err := generator.SubstituteVariables(ctx, r.Client, u, res, false)

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func init() {
7575

7676
func main() {
7777
var (
78+
implicitSubstitutions bool
7879
metricsAddr string
7980
eventsAddr string
8081
healthAddr string
@@ -101,6 +102,8 @@ func main() {
101102
flag.IntVar(&concurrent, "concurrent", 4, "The number of concurrent kustomize reconciles.")
102103
flag.IntVar(&concurrentSSA, "concurrent-ssa", 4, "The number of concurrent server-side apply operations.")
103104
flag.DurationVar(&requeueDependency, "requeue-dependency", 30*time.Second, "The interval at which failing dependencies are reevaluated.")
105+
flag.BoolVar(&implicitSubstitutions, "implicit-substitutions", false,
106+
"Perform substitutions of built-in values such as last-attempted-revision; has side effects of ALWAYS performing substitutions!")
104107
flag.BoolVar(&noRemoteBases, "no-remote-bases", false,
105108
"Disallow remote bases usage in Kustomize overlays. When this flag is enabled, all resources must refer to local files included in the source artifact.")
106109
flag.IntVar(&httpRetry, "http-retry", 9, "The maximum number of retries when failing to fetch artifacts over HTTP.")
@@ -223,6 +226,7 @@ func main() {
223226
Metrics: metricsH,
224227
EventRecorder: eventRecorder,
225228
NoCrossNamespaceRefs: aclOptions.NoCrossNamespaceRefs,
229+
ImplicitSubstitutions: implicitSubstitutions,
226230
NoRemoteBases: noRemoteBases,
227231
FailFast: failFast,
228232
ConcurrentSSA: concurrentSSA,

0 commit comments

Comments
 (0)