Skip to content

CMP-2859: Resolve failing Image-stream-sets-schedule #12895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

KaushikOP
Copy link
Contributor

@KaushikOP KaushikOP commented Jan 24, 2025

Description:

  • Resolve failing Image-stream-sets-schedule. Changes to samples operator which were not accounted for, and ClusterVersion managed imagestreams should be excluded.

Rationale:

  • fixes failing rule

  • Fixes #CMP-2859

Review Hints:

  • Part of DISA-STIG profile for OCP

@openshift-ci openshift-ci bot added the needs-ok-to-test Used by openshift-ci bot. label Jan 24, 2025
Copy link

openshift-ci bot commented Jan 24, 2025

Hi @KaushikOP. Thanks for your PR.

I'm waiting for a ComplianceAsCode member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@KaushikOP KaushikOP changed the title CMP2859: Resolve failing Image-stream-sets-schedule CMP-2859: Resolve failing Image-stream-sets-schedule Jan 24, 2025
@prb112
Copy link
Contributor

prb112 commented Jan 24, 2025

/jira-refresh

@@ -32,17 +32,17 @@ references:
srg: SRG-APP-000456-CTR-001125

{{% set api_path = '/apis/image.openshift.io/v1/imagestreams' %}}
{{% set jqfilter = '[.items[] | .spec.tags[]? | select(.from.kind != "ImageStreamTag") | (.importPolicy.scheduled != null and .importPolicy.scheduled != false)] | all' %}}
{{% set jqfilter = '[.items[] | select( .metadata.ownerReferences? // [] | all(.kind != "ClusterVersion")) | select(.metadata.labels[]? | select("samples.operator.openshift.io/managed: true") | not) | select(.spec.tags[]?.from.kind != "ImageStreamTag" and (.importPolicy.scheduled != null or .importPolicy.scheduled != false))] | any' %}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Gather all ImageStreams - .items[]
  2. Select only those not managed by ClusterVersion - select( .metadata.ownerReferences? // [] | all(.kind != "ClusterVersion"))
  • ClusterVersion implies on upgrade that these ImageStream entries are upgraded
  1. From those that remain, check if it's managed by the Samples Operator, if so ignore those - select(.metadata.labels[]? | select("samples.operator.openshift.io/managed: true") | not)
  2. Check the Remaining ones are not tags - select(.spec.tags[]?.from.kind != "ImageStreamTag" and (partial query here)
  • e.g. ImageStreamTag latest points to v1.2.3
  1. The ones that remain from that check to see if scheduled is false or not set - (.importPolicy.scheduled != null or .importPolicy.scheduled != false))] | any' %}}

If any fail the last condition it returns false for the query.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a bunch for the breakdown and explanation of the jq filter.

@jan-cerny jan-cerny added the OpenShift OpenShift product related. label Jan 27, 2025
@prb112
Copy link
Contributor

prb112 commented Jan 27, 2025

/retest-required

Copy link

openshift-ci bot commented Jan 27, 2025

@prb112: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest-required

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@prb112
Copy link
Contributor

prb112 commented Feb 13, 2025

/jira-refresh

@yuumasato yuumasato self-assigned this Feb 28, 2025
@yuumasato
Copy link
Member

/ok-to-test

@openshift-ci openshift-ci bot added ok-to-test Used by openshift-ci bot. and removed needs-ok-to-test Used by openshift-ci bot. labels Feb 28, 2025
Copy link
Member

@yuumasato yuumasato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/LGTM

But could you also update the expected results so that our CI doesn't flag this change?

Change the following lines from FAIL to PASS.

$ grep -rA2 "imagestream-sets-schedule" tests/assertions/ocp4/
tests/assertions/ocp4/ocp4-stig-4.12.yml:  e2e-stig-imagestream-sets-schedule:
tests/assertions/ocp4/ocp4-stig-4.12.yml-    default_result: FAIL
tests/assertions/ocp4/ocp4-stig-4.12.yml-    result_after_remediation: FAIL
--
tests/assertions/ocp4/ocp4-stig-4.13.yml:  e2e-stig-imagestream-sets-schedule:
tests/assertions/ocp4/ocp4-stig-4.13.yml-    default_result: FAIL
tests/assertions/ocp4/ocp4-stig-4.13.yml-    result_after_remediation: FAIL
--
tests/assertions/ocp4/ocp4-stig-4.14.yml:  e2e-stig-imagestream-sets-schedule:
tests/assertions/ocp4/ocp4-stig-4.14.yml-    default_result: FAIL
tests/assertions/ocp4/ocp4-stig-4.14.yml-    result_after_remediation: FAIL
--
tests/assertions/ocp4/ocp4-stig-4.15.yml:  e2e-stig-imagestream-sets-schedule:
tests/assertions/ocp4/ocp4-stig-4.15.yml-    default_result: FAIL
tests/assertions/ocp4/ocp4-stig-4.15.yml-    result_after_remediation: FAIL
--
tests/assertions/ocp4/ocp4-stig-4.16.yml:  e2e-stig-imagestream-sets-schedule:
tests/assertions/ocp4/ocp4-stig-4.16.yml-    default_result: FAIL
tests/assertions/ocp4/ocp4-stig-4.16.yml-    result_after_remediation: FAIL
--
tests/assertions/ocp4/ocp4-stig-4.17.yml:  e2e-stig-imagestream-sets-schedule:
tests/assertions/ocp4/ocp4-stig-4.17.yml-    default_result: FAIL
tests/assertions/ocp4/ocp4-stig-4.17.yml-    result_after_remediation: FAIL
--
tests/assertions/ocp4/ocp4-stig-4.18.yml:  e2e-stig-imagestream-sets-schedule:
tests/assertions/ocp4/ocp4-stig-4.18.yml-    default_result: FAIL
tests/assertions/ocp4/ocp4-stig-4.18.yml-    result_after_remediation: FAIL
$ cat ./applications/openshift/registry/imagestream_sets_schedule/tests/ocp4/e2e.yml 
---
default_result: FAIL

@prb112
Copy link
Contributor

prb112 commented Feb 28, 2025

Thanks @yuumasato I'll leave a message with Kaushik letting him know

Copy link
Member

@yuumasato yuumasato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KaushikOP tests/assertions/ocp4/ocp4-stig-4.18.yml was left out of the assertion update.

Please update it as well.

@yuumasato
Copy link
Member

/test 4.17-e2e-aws-ocp4-stig
/test 4.18-e2e-aws-ocp4-stig

@prb112
Copy link
Contributor

prb112 commented Mar 4, 2025

/retest-required

@KaushikOP KaushikOP requested a review from yuumasato March 5, 2025 07:08
@KaushikOP
Copy link
Contributor Author

Hi @yuumasato

I have made the changes. Please re-review.

@yuumasato yuumasato added this to the 0.1.77 milestone Mar 5, 2025
@rhmdnd
Copy link
Collaborator

rhmdnd commented Mar 5, 2025

/retest-required

@yuumasato
Copy link
Member

/test 4.12-e2e-aws-ocp4-stig
/test 4.16-e2e-aws-ocp4-stig

@prb112
Copy link
Contributor

prb112 commented Mar 5, 2025

Grabbed the imagestreams from the must-gather. https://gist.github.com/prb112/06768521d08ecf0eba49476252580e8e

Will ask @KaushikOP to test the jq locally to see why it failed.

@prb112
Copy link
Contributor

prb112 commented Mar 6, 2025

Analyzed the gist / imagestreams - it should have passed. Perhaps a retest?

@prb112
Copy link
Contributor

prb112 commented Mar 6, 2025

/retest-required

4 similar comments
@prb112
Copy link
Contributor

prb112 commented Mar 12, 2025

/retest-required

@prb112
Copy link
Contributor

prb112 commented Mar 14, 2025

/retest-required

@KaushikOP
Copy link
Contributor Author

/retest-required

@KaushikOP
Copy link
Contributor Author

/retest-required

@xiaojiey
Copy link
Collaborator

xiaojiey commented Apr 7, 2025

@prb112 @KaushikOP I am not sure below imagestream is a good example or not. I created a imagestream without ownerReferences set, and set the importPolicy.scheduled to null. Under this scenario, the rule should FAIL, right?
However, I can see the rule show as PASS. Could you please help to check? Thanks.
I also tried to create a similar imagestream with importPolicy.scheduled set to true. Both filters in the instructions will still return empty.

% oc get imagestream nginx -o yaml -n openshift-compliance
apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"image.openshift.io/v1","kind":"ImageStream","metadata":{"annotations":{},"name":"nginx","namespace":"openshift-compliance"},"spec":{"tags":[{"from":{"kind":"DockerImage","name":"quay.io/security-profiles-operator/test-nginx-unprivileged:1.21"},"importPolicy":{"scheduled":false},"name":"latest"}]}}
    openshift.io/image.dockerRepositoryCheck: "2025-04-07T07:16:44Z"
  creationTimestamp: "2025-04-07T07:16:41Z"
  generation: 2
  name: nginx
  namespace: openshift-compliance
  resourceVersion: "108869"
  uid: 8364f1d4-475d-40a4-9f16-60dd13978282
spec:
  lookupPolicy:
    local: false
  tags:
  - annotations: null
    from:
      kind: DockerImage
      name: quay.io/security-profiles-operator/test-nginx-unprivileged:1.21
    generation: 2
    importPolicy:
      importMode: Legacy
    name: latest
    referencePolicy:
      type: Source
status:
  dockerImageRepository: image-registry.openshift-image-registry.svc:5000/openshift-compliance/nginx
  tags:
  - items:
    - created: "2025-04-07T07:16:44Z"
      dockerImageReference: quay.io/security-profiles-operator/test-nginx-unprivileged@sha256:362f8cfc2fed11a2037f29bfcf91c9d9ab5e3b8b46aabb846d8f1f7d81a939d2
      generation: 2
      image: sha256:362f8cfc2fed11a2037f29bfcf91c9d9ab5e3b8b46aabb846d8f1f7d81a939d2
    tag: latest

% oc get rule upstream-ocp4-imagestream-sets-schedule -o=jsonpath={.instructions}
To list all the imagestreams and identify which imagestream tags are
configured to periodically check for updates (imagePolicy = { scheduled: true }), run the following command:
oc get imagestream -A -ojson | jq -r '.items[] | select( .metadata.ownerReferences? // [] | all(.kind == "ClusterVersion")) | select(.metadata.labels[]? | select( "samples.operator.openshift.io/managed: true")) | select( .spec.tags[]?.from.kind != "ImageStreamTag" and .importPolicy.scheduled == true).metadata.name' | sort | uniq
Alternatively, to view a list of ImageStreams that do not schedule updates,
run:
oc get imagestream -A -ojson | jq -r '.items[] | select( .metadata.ownerReferences? // [] | all(.kind != "ClusterVersion")) | select(.metadata.labels[]? | select( "samples.operator.openshift.io/managed: true") | not) | select( .spec.tags[]?.from.kind != "ImageStreamTag" and (.importPolicy.scheduled == null or .importPolicy.scheduled == false)) | "\(.metadata.namespace),\(.metadata.name)"' | sort | uniq
Is it the case that imagestream is not configured to perform periodical updates?%

% oc get imagestream -A -ojson | jq -r '.items[] | select( .metadata.ownerReferences? // [] | all(.kind == "ClusterVersion")) | select(.metadata.labels[]? | select( "samples.operator.openshift.io/managed: true")) | select( .spec.tags[]?.from.kind != "ImageStreamTag" and .importPolicy.scheduled == true).metadata.name' | sort | uniq

% oc get imagestream -A -ojson | jq -r '.items[] | select( .metadata.ownerReferences? // [] | all(.kind != "ClusterVersion")) | select(.metadata.labels[]? | select( "samples.operator.openshift.io/managed: true") | not) | select( .spec.tags[]?.from.kind != "ImageStreamTag" and (.importPolicy.scheduled == null or .importPolicy.scheduled == false)) | "\(.metadata.namespace),\(.metadata.name)"' | sort | uniq
% oc get ccr | grep -i imagestream-sets-schedule
ocp4-stig-imagestream-sets-schedule                             FAIL     medium
upstream-ocp4-stig-imagestream-sets-schedule                    PASS     medium

@xiaojiey
Copy link
Collaborator

xiaojiey commented Apr 7, 2025

/hold for test

@openshift-ci openshift-ci bot added the do-not-merge/hold Used by openshift-ci-robot bot. label Apr 7, 2025
@prb112
Copy link
Contributor

prb112 commented Apr 7, 2025

Discussed xiaojiey's comments
refactoring dropped this edge case, we're fixing it.
Thanks :)

@KaushikOP
Copy link
Contributor Author

Hi @xiaojiey,

We have refactored the rule to include cases that mentioned your review.

CC: @prb112

@@ -32,17 +32,17 @@ references:
srg: SRG-APP-000456-CTR-001125

{{% set api_path = '/apis/image.openshift.io/v1/imagestreams' %}}
{{% set jqfilter = '[[.items[] | select( .metadata.ownerReferences? // [] | all(.kind != "ClusterVersion")) | select(.metadata.labels[]? | select("samples.operator.openshift.io/managed: true") | not) | select(.spec.tags[]?.from.kind != "ImageStreamTag" and (.importPolicy.scheduled != null or .importPolicy.scheduled != false))] | any]' %}}
{{% set jqfilter = '[[.items[] | ( .spec.tags[]?.from.kind != "ImageStreamTag" and ([.spec.tags[]? | (.importPolicy.scheduled == null or .importPolicy.scheduled == false)] | any) ) and ((.metadata.labels == null) or (.metadata.labels."samples.operator.openshift.io/managed" == null) or (.metadata.labels."samples.operator.openshift.io/managed" != "true")) and ((.metadata.ownerReferences == null) or (.metadata.ownerReferences | length == 0) or (.metadata.ownerReferences?[].kind == null) or (isempty(.metadata.ownerReferences?[] | select(.kind == "ClusterVersion"))))] | any]' %}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation:

  1. Get all ImageStreams: .items[]
  2. Target user-defined streams only: .spec.tags[]?.from.kind != "ImageStreamTag"
  3. Check for non-scheduled tags: (.importPolicy.scheduled == null or .importPolicy.scheduled == false)] | any).
  4. Ignore Sample Operator-managed streams: ((.metadata.labels == null) or (.metadata.labels."samples.operator.openshift.io/managed" == null) or (.metadata.labels."samples.operator.openshift.io/managed" != "true")).
  5. Exclude system-managed ImageStreams: ((.metadata.ownerReferences == null) or (.metadata.ownerReferences | length == 0) or (.metadata.ownerReferences?[].kind == null) or (isempty(.metadata.ownerReferences?[] | select(.kind == "ClusterVersion")).
  6. Summary: This detects user-managed, unmanaged, external ImageStreams that are not auto-updating.
  7. Final condition: | any returns true if any such non-compliant ImageStream exists.

@xiaojiey
Copy link
Collaborator

Verification pass.
##scenario 1: there is a imagestream with schedule unset, the rule returns FAIL:
% oc get imagestream -A -ojson | jq -r '.items[] | select(( (.spec.tags[]?.from.kind != "ImageStreamTag") and ([.spec.tags[]? | (.importPolicy.scheduled == null or .importPolicy.scheduled == false)] | any) and ((.metadata.labels == null) or (.metadata.labels["samples.operator.openshift.io/managed"] == null) or (.metadata.labels["samples.operator.openshift.io/managed"] != "true")) and ((.metadata.ownerReferences == null) or (.metadata.ownerReferences | length == 0) or (.metadata.ownerReferences[]?.kind == null) or (isempty(.metadata.ownerReferences[]? | select(.kind == "ClusterVersion")))) )) | "(.metadata.namespace),(.metadata.name)"' | sort | uniq
openshift-compliance,nginx
% oc get ccr | grep imagestream-sets-schedule
upstream-ocp4-stig-imagestream-sets-schedule FAIL medium
##Scenario 2: there is no imagestream with schedule unset, the rule returns PASS:
% oc get imagestream -A -ojson | jq -r '.items[] | select(( (.spec.tags[]?.from.kind != "ImageStreamTag") and ([.spec.tags[]? | (.importPolicy.scheduled == null or .importPolicy.scheduled == false)] | any) and ((.metadata.labels == null) or (.metadata.labels["samples.operator.openshift.io/managed"] == null) or (.metadata.labels["samples.operator.openshift.io/managed"] != "true")) and ((.metadata.ownerReferences == null) or (.metadata.ownerReferences | length == 0) or (.metadata.ownerReferences[]?.kind == null) or (isempty(.metadata.ownerReferences[]? | select(.kind == "ClusterVersion")))) )) | "(.metadata.namespace),(.metadata.name)"' | sort | uniq
% oc get ccr | grep imagestream-sets-schedule
upstream-ocp4-stig-imagestream-sets-schedule PASS medium

@xiaojiey
Copy link
Collaborator

/unhold

@openshift-ci openshift-ci bot removed the do-not-merge/hold Used by openshift-ci-robot bot. label May 17, 2025
@xiaojiey
Copy link
Collaborator

/lgtm

@vojtapolasek vojtapolasek modified the milestones: 0.1.77, 0.1.78 May 21, 2025
@yuumasato
Copy link
Member

@KaushikOP Hi, looks good to me.

Sorry for delay in picking this up.
Since this was pushed there a new CI checks that need to pass.
Could you rebase this?
Thanks

KaushikOP and others added 5 commits May 26, 2025 20:46
…ator and ClusterVersion

Signed-off-by: Kaushik Talathi <kaushik.talathi1@ibm.com>
Signed-off-by: Kaushik Talathi <kaushik.talathi1@ibm.com>
Signed-off-by: Kaushik Talathi <kaushik.talathi1@ibm.com>
Enclosing the jqfilter in array and updating result as false.

Signed-off-by: Kaushik Talathi <kaushik.talathi1@ibm.com>
Signed-off-by: Kaushik Talathi <kaushik.talathi1@ibm.com>
@KaushikOP
Copy link
Contributor Author

Hi @yuumasato,

I have rebased this with upstream/master.

@yuumasato
Copy link
Member

@KaushikOP Sorry for delay, I got sick.

There should be no merge commits in the PR:
https://github.com/ComplianceAsCode/content/actions/runs/15256465663/job/43296097548?pr=12895

@KaushikOP KaushikOP force-pushed the CMP2859-imagestream-rule-fail branch from 172e7ae to 7b81ca2 Compare June 4, 2025 07:07
@KaushikOP
Copy link
Contributor Author

/test all

Copy link

codeclimate bot commented Jun 4, 2025

Code Climate has analyzed commit 7b81ca2 and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 61.9% (0.0% change).

View more on Code Climate.

@yuumasato yuumasato merged commit 3d4eee4 into ComplianceAsCode:master Jun 4, 2025
123 of 131 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Used by openshift-ci bot. OpenShift OpenShift product related.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants