syntasso / kratix

Kratix is an open-source framework for building platforms
https://kratix.io
Apache License 2.0
482 stars 30 forks source link

bug: status updates in status.yaml aren't applied if message not updated #118

Closed richcooper95 closed 6 months ago

richcooper95 commented 6 months ago

When updating the status in a Pipeline via /kratix/metadata/status.yaml, the result is only updated in k8s if the message has been updated.

Otherwise, the new key-values aren't populated into the resource status.

Investigation

The status-writer looks to be overwriting the status from status.yaml here:

if [ "${IS_LAST_PIPELINE}" = "true" ]; then
  if [ "${current_message}" = "Pending" ]; then
    status="$(yq -n '.message = "Resource requested"')"       # Overwriting `status` here
  fi
fi

See example logs:

+ '[' Pending '=' Pending ]
+ yq -n '.message = "Resource requested"'
+ status='message: Resource requested'       <<<<< This should have included `complianceStatus` (see below)

Examples

Without an update to message

The status-writer picks up the new complianceStatus key-value:

  > k logs configure-pipeline-data-e2315-kxk7t status-writer 
+ status_file=/work-creator-files/metadata/status.yaml
+ date -u '+%Y-%m-%dT%H:%M:%SZ'
+ export 'conditions=message: Pipeline completed
lastTransitionTime: "2024-05-09T13:28:06Z"
status: "True"
type: PipelineCompleted
reason: PipelineExecutedSuccessfully
'
+ kubectl get data.marketplace.kratix.io/do-not-delete --namespace default -oyaml
+ yq '.status // {}' existing-object.yaml
+ echo '{}'
+ incoming_status='{}'
+ '[' -f /work-creator-files/metadata/status.yaml ]
+ cat /work-creator-files/metadata/status.yaml
+ yq '. *= load("incoming-status.yaml")' existing-status.yaml
+ export 'status=conditions:
  - lastTransitionTime: "2024-05-09T13:28:02Z"
    message: Pipeline has not completed
    reason: PipelineNotCompleted
    status: "False"
    type: PipelineCompleted
message: Pending
complianceStatus: "compliance scan result: compliant"'
+ echo 'conditions:
  - lastTransitionTime: "2024-05-09T13:28:02Z"
    message: Pipeline has not completed
    reason: PipelineNotCompleted
    status: "False"
    type: PipelineCompleted
message: Pending
complianceStatus: "compliance scan result: compliant"'
+ yq -r .message
+ current_message=Pending
+ '[' true '=' true ]
+ '[' Pending '=' Pending ]
+ yq -n '.message = "Resource requested"'
+ status='message: Resource requested'                <<<<<<<<<<<<<<< This looks to be the issue (see above)
+ yq -r '.status.conditions // []' existing-object.yaml
+ export 'existing_conditions=- lastTransitionTime: "2024-05-09T13:28:02Z"
  message: Pipeline has not completed
  reason: PipelineNotCompleted
  status: "False"
  type: PipelineCompleted'
+ yq -n '.status = env(status)'
+ yq '.status.conditions = env(existing_conditions)'
+ yq '(.status.conditions[] | select(.type == "PipelineCompleted")) = env(conditions)'
+ kubectl patch data.marketplace.kratix.io/do-not-delete --namespace default --type merge --patch-file status.yaml --subresource status
data.marketplace.kratix.io/do-not-delete patched

But doesn't populate this into the resource status:

status:
  conditions:
  - lastTransitionTime: "2024-05-09T13:28:06Z"
    message: Pipeline completed
    reason: PipelineExecutedSuccessfully
    status: "True"
    type: PipelineCompleted
  message: Resource requested

With an update to message

The resource status picks up the change:

status:
  complianceStatus: 'compliance scan result: compliant'
  conditions:
  - lastTransitionTime: "2024-05-09T13:40:30Z"
    message: Pipeline completed
    reason: PipelineExecutedSuccessfully
    status: "True"
    type: PipelineCompleted
  message: Resource requested
richcooper95 commented 6 months ago

Confirmed that the above is correct - with the following change:

 if [ "${IS_LAST_PIPELINE}" = "true" ]; then
        if [ "${current_message}" = "Pending" ]; then
-               status="$(yq -n '.message = "Resource requested"')"
+               status=$(echo "$status" | yq '.message = "Resource requested"')
        fi
 fi

We see:

+ '[' true '=' true ]
+ '[' Pending '=' Pending ]
+ echo 'conditions:
  - lastTransitionTime: "2024-05-09T16:55:27Z"
    message: Pipeline has not completed
    reason: PipelineNotCompleted
    status: "False"
    type: PipelineCompleted
message: Pending
complianceStatus: "compliance scan result: compliant"'
+ yq '.message = "Resource requested"'
+ status='conditions:                                         <<<<< Not overwritten
  - lastTransitionTime: "2024-05-09T16:55:27Z"
    message: Pipeline has not completed
    reason: PipelineNotCompleted
    status: "False"
    type: PipelineCompleted
message: Resource requested
complianceStatus: "compliance scan result: compliant"'