Skip to content

Conversation

dhaiducek
Copy link
Member

When the .ObjectName template variable was used outside .metadata.name, the controller complained that the name didn't match.

This also removes an errant E2E test that was testing that a template that resolved to an empty string when the objectSelector was in play was NonCompliant, but really an empty string would be expected there regardless so that the controller could fill in the value.

// If the name doesn't match the original, return an error
if needsPerNameTemplating && desiredObj.GetName() != name {
// Error if the name doesn't match the parsed name from the objectSelector
if objectSelector != nil && desiredObj.GetName() != name {
Copy link
Member Author

Choose a reason for hiding this comment

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

I reconsidered this block a lot and even considered removing it, not wanting to unnecessarily throttle the capabilities of policy users. I think the logic here makes sense now. My initial concern was the ability to set the name or namespace to something other than what was determined by the namespaceSelector or objectSelector, which seemed like somewhat of an antipattern. But I could be swayed if the logic seems off or there are cases I'm not considering where setting the name or namespace to something else could be beneficial somehow.

Copy link
Member

Choose a reason for hiding this comment

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

I'm concerned that if I create a policy that uses object selector, and at first it only selects one object, I can use that name in the policy. But if later another object is created that matches the selector, suddenly by policy will have this error? Is that correct?

If that's the current behavior (I could've misunderstood), I think it might be better if we always report an error if the policy defines both the name and the object selector. Does that make sense?

I was also worried a little bit about the error message, since it doesn't give the details of what the "selected name" is - if the error was just something like name may not be set when object selector is used, it might be more clear to the user how to fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@JustinKuli GOOD Point!!

Copy link
Member Author

Choose a reason for hiding this comment

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

@JustinKuli Hmmm. The differing error behavior is a good point. We could forbid users from using templated strings for the name or namespace if the objectSelector or namespaceSelector are in use, since that's really the complexity here. (Providing a non-templated string overrides the selector as it does with the namespaceSelector.) Does that align with your point? It'd seem to align with your point about the error message also.

Copy link
Member

Choose a reason for hiding this comment

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

Providing a non-templated string overrides the selector as it does with the namespaceSelector.

I hadn't remembered that detail. It makes sense for namespace, since I think I've been guilty of just copying a default namespaceSelector into new policies, and still setting the namespace in a template to whatever I want. And then for consistency, it makes sense to do the same thing for object selector and names... I have a feeling if we were building this from scratch I might try to make slightly different decisions, but it's too late now.

I don't think I'd want us to forbid templated strings entirely here, since name: {{.ObjectName}} "feels right". But name: {{.ObjectName}}-new definitely feels wrong. So the conditional statement that is currently there seems right. And now, after talking about it for a while, I finally understand what the error message is trying to say. But I don't have a great way to reword it to be more clear immediately.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've updated the message and also added an additional conditional in the namespace selector block since I feel like there are so many edge cases that have come up during development I wanted to try to make the conditional clearer/more explicit just in case.

Copy link
Member

Choose a reason for hiding this comment

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

I was kind of referring to the fact that setting the namespace can override the namespace selector. Something like

namespaceSelector:
  exclude: "prod-*"
...
    objectDefinition:
      metadata:
        namespace: prod-stuff

could/should be a user-error that gets reported. But it would be a breaking change to do that at this point.

Copy link
Contributor

Choose a reason for hiding this comment

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

good point!! @JustinKuli finding edge cases!!!

Copy link
Member Author

Choose a reason for hiding this comment

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

@JustinKuli The objectSelector is new, so we could update its behavior to disallow it specifying both... 😅

Copy link
Member

Choose a reason for hiding this comment

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

We could, but I'd rather not, personally.

@dhaiducek
Copy link
Member Author

/cc @yiraeChristineKim @JustinKuli

@yiraeChristineKim
Copy link
Contributor

LGTM but hold for Justin
/hold

JustinKuli
JustinKuli previously approved these changes Dec 9, 2024
Copy link
Member

@JustinKuli JustinKuli left a comment

Choose a reason for hiding this comment

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

The new message looks better, thank you!

// If the name doesn't match the original, return an error
if needsPerNameTemplating && desiredObj.GetName() != name {
// Error if the name doesn't match the parsed name from the objectSelector
if objectSelector != nil && desiredObj.GetName() != name {
Copy link
Member

Choose a reason for hiding this comment

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

I was kind of referring to the fact that setting the namespace can override the namespace selector. Something like

namespaceSelector:
  exclude: "prod-*"
...
    objectDefinition:
      metadata:
        namespace: prod-stuff

could/should be a user-error that gets reported. But it would be a breaking change to do that at this point.

When the `.ObjectName` template variable was used
outside `.metadata.name`, the controller
complained that the name didn't match.

This also removes an errant E2E test.

Signed-off-by: Dale Haiducek <[email protected]>
@dhaiducek
Copy link
Member Author

Unrelated E2E failure:

• [FAILED] [65.762 seconds]
Testing OperatorPolicy Test reporting of unapproved version after installation [It] Should report a violation after the versions list is patched to exclude the current version [supports-hosted]
/home/runner/work/config-policy-controller/config-policy-controller/test/e2e/case38_install_operator_test.go:3697

  Timeline >>
  STEP: Creating the parent object @ 12/09/24 20:49:32.834
  STEP: Creating the child object with the owner reference @ 12/09/24 20:49:32.96
  STEP: Verifying the child object exists @ 12/09/24 20:49:32.965
  STEP: Patching the versions field to exclude the installed version @ 12/09/24 20:49:32.967
  [FAILED] in [It] - /home/runner/work/config-policy-controller/config-policy-controller/test/e2e/case38_install_operator_test.go:3702 @ 12/09/24 20:50:33.028

  [FAILED] Timed out after 60.000s.
  The function passed to Eventually failed at /home/runner/work/config-policy-controller/config-policy-controller/test/e2e/case38_install_operator_test.go:153 with:
  Should have related object ClusterServiceVersion with name 'quay-operator.v3.10.6'
  Expected
      <bool>: false
  to be true
  In [It] at: /home/runner/work/config-policy-controller/config-policy-controller/test/e2e/case38_install_operator_test.go:3702 @ 12/09/24 20:50:33.028

ref: https://github.com/open-cluster-management-io/config-policy-controller/actions/runs/12243701334/job/34153830369?pr=321#step:5:28208

@dhaiducek
Copy link
Member Author

/unhold

CI is passing--ready for another review!

@openshift-ci openshift-ci bot added the lgtm label Dec 10, 2024
Copy link

openshift-ci bot commented Dec 10, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dhaiducek, JustinKuli

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [JustinKuli,dhaiducek]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants