Skip to content

Commit

Permalink
Apply pull request suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Oct 15, 2024
1 parent 34024bc commit 9a62805
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import io.dekorate.kubernetes.decorator.Decorator;
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.api.model.ObjectMeta;

public class AddNamespaceDecorator extends Decorator<KubernetesListBuilder> {

private static final Set<String> CLUSTERED_RESOURCES = Set.of("ClusterRoleBinding", "ClusterRole");
private final String namespace;

public AddNamespaceDecorator(String namespace) {
Expand All @@ -24,17 +24,17 @@ public void visit(KubernetesListBuilder list) {
List<HasMetadata> buildItems = list.buildItems()
.stream()
.peek(obj -> {
if (isEligibleForChangingNamespace(obj)) {
obj.setMetadata(obj.getMetadata().edit().withNamespace(namespace).build());
if (obj instanceof Namespaced) {
final ObjectMeta metadata = obj.getMetadata();
if (metadata.getNamespace() == null) {
metadata.setNamespace(namespace);
obj.setMetadata(metadata);
}
}
}).collect(Collectors.toList());
list.withItems(buildItems);
}

private boolean isEligibleForChangingNamespace(HasMetadata obj) {
return obj.getMetadata().getNamespace() == null && !CLUSTERED_RESOURCES.contains(obj.getKind());
}

@Override
public Class<? extends Decorator>[] after() {
return new Class[] { ResourceProvidingDecorator.class, AddSidecarDecorator.class };
Expand Down

0 comments on commit 9a62805

Please sign in to comment.