Skip to content
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

feat: owner reference validity check regarding scope and namespace #5701

Merged
merged 6 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Bugs

#### Improvements
* Fix #5701: Owner reference validity check regarding scope and namespace

#### Dependency Upgrade

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,23 @@ default OwnerReference addOwnerReference(HasMetadata owner) {
+ getKind());
}

if (!(owner instanceof GenericKubernetesResource)
&& !(this instanceof GenericKubernetesResource)
&& owner instanceof Namespaced) {
if (!(this instanceof Namespaced)) {
throw new IllegalArgumentException(
"Cannot add owner reference from a cluster scoped to a namespace scoped resource: "
+ optionalMetadata().map(m -> "'" + m.getName() + "' ").orElse("unnamed ")
+ getKind());
} else if (optionalMetadata().map(m -> !Objects.equals(m.getNamespace(), owner.getMetadata().getNamespace()))
.orElse(false)) {
throw new IllegalArgumentException(
"Cannot add owner reference between two resources in different namespaces:"
+ optionalMetadata().map(m -> "'" + m.getName() + "' ").orElse("unnamed ")
+ getKind());
}
}

final OwnerReference ownerReference = new OwnerReferenceBuilder()
.withUid(metadata.getUid())
.withApiVersion(owner.getApiVersion())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,24 @@ void addingSameOwnerReferenceMultipleTimesShouldAddItOnlyOnce() {
assertEquals(1, hasMetadata.getMetadata().getOwnerReferences().size());
}

@Test
void addingOwnerReferenceToNamespacedResourceFromClusterScopedResourceShouldFail() {
HasMetadata clusterScoped = new Owner();
HasMetadata namespaced = new OwnerNamespaced();

assertThrows(IllegalArgumentException.class, () -> clusterScoped.addOwnerReference(namespaced));
}

@Test
void addingOwnerReferenceToResourceInDifferentNamespaceShouldFail() {
HasMetadata namespaced1 = new OwnerNamespaced();
namespaced1.getMetadata().setNamespace("namespace1");
HasMetadata namespaced2 = new OwnerNamespaced();
namespaced2.getMetadata().setNamespace("namespace2");

assertThrows(IllegalArgumentException.class, () -> namespaced1.addOwnerReference(namespaced2));
}

@Group("fabric8.io")
@Version("v1")
private static class Woman implements HasMetadata {
Expand Down Expand Up @@ -346,4 +364,8 @@ public void setApiVersion(String version) {
throw new RuntimeException("setApiVersion shouldn't be called");
}
}

private static class OwnerNamespaced extends Owner implements Namespaced {
}

}
Loading