Skip to content

Commit

Permalink
Only dispatch initnamespace event if the namespace is actually new
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt committed Jun 19, 2024
1 parent 8088ff6 commit 019ba34
Showing 1 changed file with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.fabric8.kubernetes.api.model.rbac.SubjectBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.Resource;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -92,19 +93,22 @@ private String createNamespace(Region region, String namespaceId, Owner owner, U
}
}

kubClient
.namespaces()
.resource(
new NamespaceBuilder()
.withNewMetadata()
.withName(namespaceId)
.withLabels(region.getServices().getNamespaceLabels())
.addToLabels("onyxia_owner", owner.getId())
.withAnnotations(region.getServices().getNamespaceAnnotations())
.addToAnnotations(userMetadata)
.endMetadata()
.build())
.serverSideApply();
Resource<Namespace> namespace =
kubClient
.namespaces()
.resource(
new NamespaceBuilder()
.withNewMetadata()
.withName(namespaceId)
.withLabels(region.getServices().getNamespaceLabels())
.addToLabels("onyxia_owner", owner.getId())
.withAnnotations(
region.getServices().getNamespaceAnnotations())
.addToAnnotations(userMetadata)
.endMetadata()
.build());
boolean newNamespace = namespace.get() == null;
namespace.serverSideApply();

final RoleBinding bindingToCreate =
kubClient
Expand Down Expand Up @@ -168,9 +172,11 @@ private String createNamespace(Region region, String namespaceId, Owner owner, U
quota,
!region.getServices().getQuotas().isAllowUserModification());
}
InitNamespaceEvent initNamespaceEvent =
new InitNamespaceEvent(region.getName(), namespaceId, owner.getId());
onyxiaEventPublisher.publishEvent(initNamespaceEvent);
if (newNamespace) {
InitNamespaceEvent initNamespaceEvent =
new InitNamespaceEvent(region.getName(), namespaceId, owner.getId());
onyxiaEventPublisher.publishEvent(initNamespaceEvent);
}

return namespaceId;
}
Expand Down

0 comments on commit 019ba34

Please sign in to comment.