Skip to content

Commit

Permalink
fix(expressions): fetch labels from actually deployed manfiest (#4508)
Browse files Browse the repository at this point in the history
* fix(expressions): fetch labels from actually deployed manfiest

* fix(expressions): handle dot in label key

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mattgogerly and mergify[bot] authored Aug 24, 2023
1 parent fea893e commit 857569b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public static String manifestLabelValue(
"A valid Deploy Manifest stage name is required for this function");
}

List<Map> manifests = (List<Map>) stage.get().getContext().get("manifests");
// using outputs.manifests to give access to labels added by Spinnaker itself during the
// deployment
// this is safe as we assert above that we're only using successful deploy stages, so this key
// should always exist
List<Map> manifests = (List<Map>) stage.get().getContext().get("outputs.manifests");

if (manifests == null || manifests.size() == 0) {
throw new SpelHelperFunctionException(
Expand All @@ -104,7 +108,7 @@ public static String manifestLabelValue(
}

Map manifest = manifestOpt.get();
String labelPath = format("$.spec.template.metadata.labels.%s", labelKey);
String labelPath = format("$.spec.template.metadata.labels['%s']", labelKey);
String labelValue;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ class ManifestLabelValueExpressionFunctionProviderSpec extends Specification {
]
]
]
],
"outputs.manifests": [
[
"kind": "ReplicaSet",
"spec": [
"template": [
"metadata": [
"labels": [
"my-label-key": "my-label-value",
"my-other-label-key": "my-other-label-value",
"moniker.spinnaker.io/sequence": "0"
]
]
]
]
]
]
)
status = SUCCEEDED
Expand All @@ -60,9 +76,10 @@ class ManifestLabelValueExpressionFunctionProviderSpec extends Specification {
manifestLabelValue(deployManifestPipeline, "Deploy ReplicaSet", "ReplicaSet", labelKey) == expectedLabelValue

where:
labelKey || expectedLabelValue
"my-label-key" || "my-label-value"
"my-other-label-key" || "my-other-label-value"
labelKey || expectedLabelValue
"my-label-key" || "my-label-value"
"my-other-label-key" || "my-other-label-value"
"moniker.spinnaker.io/sequence" || "0"
}

def "manifestLabelValue should raise exception if stage, manifest, or label not found"() {
Expand Down

0 comments on commit 857569b

Please sign in to comment.