From 450760ef97f27ff5104c71b77a6ca84c4117d42b Mon Sep 17 00:00:00 2001 From: Hongchao Deng Date: Mon, 28 Sep 2020 11:47:03 -0700 Subject: [PATCH] Use AppConfig's generation to get latest dependency only - pass AC's generation into child objects' labels - check generation in getting data input Signed-off-by: Hongchao Deng --- .../v1alpha2/applicationconfiguration/render.go | 17 +++++++++++++++-- pkg/oam/labels.go | 2 ++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/controller/v1alpha2/applicationconfiguration/render.go b/pkg/controller/v1alpha2/applicationconfiguration/render.go index 9de78df9..c76cfa3f 100644 --- a/pkg/controller/v1alpha2/applicationconfiguration/render.go +++ b/pkg/controller/v1alpha2/applicationconfiguration/render.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "reflect" + "strconv" runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1" "github.com/crossplane/crossplane-runtime/pkg/fieldpath" @@ -136,6 +137,7 @@ func (r *components) renderComponent(ctx context.Context, acc v1alpha2.Applicati oam.LabelAppComponent: acc.ComponentName, oam.LabelAppComponentRevision: componentRevisionName, oam.LabelOAMResourceType: oam.ResourceTypeWorkload, + oam.LabelAppGeneration: strconv.FormatInt(ac.Generation, 10), } util.AddLabels(w, compInfoLabels) @@ -494,12 +496,23 @@ func (r *components) getDataInput(ctx context.Context, s *dagSource, ac *unstruc u.SetGroupVersionKind(obj.GroupVersionKind()) err := r.client.Get(ctx, key, u) if err != nil { - reason := fmt.Sprintf("failed to get object (%s)", key.String()) + reason := fmt.Sprintf("failed to get object (%s): %v", key.String(), err) return nil, false, reason, errors.Wrap(resource.IgnoreNotFound(err), reason) } - paved := fieldpath.Pave(u.UnstructuredContent()) + pavedAC := fieldpath.Pave(ac.UnstructuredContent()) + var acGeneration int + if err := pavedAC.GetValueInto("metadata.generation", &acGeneration); err != nil { + return nil, false, err.Error(), err + } + // The source object's app generation should match current AC. Otherwise it is from an old AC and we should wait until + // it is updated then reconcile again. + if g1, g2 := u.GetLabels()[oam.LabelAppGeneration], strconv.Itoa(acGeneration); g1 != g2 { + return nil, false, fmt.Sprintf("generation not match: %s, %s", g1, g2), nil + } + + paved := fieldpath.Pave(u.UnstructuredContent()) rawval, err := paved.GetValue(obj.FieldPath) if err != nil { if fieldpath.IsNotFound(err) { diff --git a/pkg/oam/labels.go b/pkg/oam/labels.go index 97b72447..3377553b 100644 --- a/pkg/oam/labels.go +++ b/pkg/oam/labels.go @@ -25,6 +25,8 @@ const ( LabelAppComponent = "app.oam.dev/component" // LabelAppComponentRevision records the revision name of Component LabelAppComponentRevision = "app.oam.dev/revision" + // LabelAppGeneration records the generation of AppConfig + LabelAppGeneration = "app.oam.dev/generation" // LabelOAMResourceType whether a CR is workload or trait LabelOAMResourceType = "app.oam.dev/resourceType" )