Skip to content

Commit

Permalink
Cleanup unused ServiceType
Browse files Browse the repository at this point in the history
We only support Kubernetes as ServiceType
  • Loading branch information
johnksv committed Aug 26, 2024
1 parent 5e48651 commit 2e34d71
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import fr.insee.onyxia.api.configuration.kubernetes.KubernetesClientProvider;
import fr.insee.onyxia.api.configuration.properties.RegionsConfiguration;
import fr.insee.onyxia.model.service.Service;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.github.inseefrlab.helmwrapper.service.HelmVersionService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -63,24 +62,20 @@ public void checkKubernetesVersion() {
.getResolvedRegions()
.forEach(
region -> {
if (region.getServices()
.getType()
.equals(Service.ServiceType.KUBERNETES)) {
KubernetesClient client =
kubernetesClientProvider.getRootClient(region);
try {
LOGGER.info(
"Region {} kubernetes v{}.{}",
region.getName(),
client.getKubernetesVersion().getMajor(),
client.getKubernetesVersion().getMinor());
} catch (Exception e) {
LOGGER.error(
"Could not contact Kubernetes APIServer for region {} at {}",
region.getName(),
client.getMasterUrl(),
e);
}
KubernetesClient client =
kubernetesClientProvider.getRootClient(region);
try {
LOGGER.info(
"Region {} kubernetes v{}.{}",
region.getName(),
client.getKubernetesVersion().getMajor(),
client.getKubernetesVersion().getMinor());
} catch (Exception e) {
LOGGER.error(
"Could not contact Kubernetes APIServer for region {} at {}",
region.getName(),
client.getMasterUrl(),
e);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.insee.onyxia.model.region.Region;
import fr.insee.onyxia.model.service.Service;
import jakarta.annotation.PostConstruct;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -44,30 +43,28 @@ public void load() throws Exception {
resolvedRegions = Arrays.asList(mapper.readValue(regions, Region[].class));
resolvedRegions.forEach(
region -> {
if (region.getServices().getType().equals(Service.ServiceType.KUBERNETES)) {
if (region.getServices()
.getAuthenticationMode()
.equals(Region.Services.AuthenticationMode.SERVICEACCOUNT)) {
LOGGER.warn(
"Using serviceAccount authentication for region {}. Onyxia will deploy services using it's own global permissions, this may be a security issue.",
region.getId());
}
if (region.getServices()
.getAuthenticationMode()
.equals(Region.Services.AuthenticationMode.SERVICEACCOUNT)) {
LOGGER.warn(
"Using serviceAccount authentication for region {}. Onyxia will deploy services using it's own global permissions, this may be a security issue.",
region.getId());
}

if (region.getServices()
.getAuthenticationMode()
.equals(Region.Services.AuthenticationMode.IMPERSONATE)) {
LOGGER.info(
"Using impersonation authentication for region {}.",
region.getId());
}
if (region.getServices()
.getAuthenticationMode()
.equals(Region.Services.AuthenticationMode.IMPERSONATE)) {
LOGGER.info(
"Using impersonation authentication for region {}.",
region.getId());
}

if (region.getServices()
.getAuthenticationMode()
.equals(Region.Services.AuthenticationMode.TOKEN_PASSTHROUGH)) {
LOGGER.info(
"Using token passthrough authentication for region {}. User token will be used by Onyxia to interact with the API Server.",
region.getId());
}
if (region.getServices()
.getAuthenticationMode()
.equals(Region.Services.AuthenticationMode.TOKEN_PASSTHROUGH)) {
LOGGER.info(
"Using token passthrough authentication for region {}. User token will be used by Onyxia to interact with the API Server.",
region.getId());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ public ServicesListing getMyServices(
User user = userProvider.getUser(region);
ServicesListing dto = new ServicesListing();
List<CompletableFuture<ServicesListing>> futures = new ArrayList<>();
if (region.getServices().getType().equals(Service.ServiceType.KUBERNETES)) {
futures.add(helmAppsService.getUserServices(region, project, user, groupId));
}
futures.add(helmAppsService.getUserServices(region, project, user, groupId));
for (var future : futures) {
ServicesListing listing = future.get();
dto.getApps().addAll(listing.getApps());
Expand Down Expand Up @@ -138,11 +136,8 @@ public Service getApp(
@Parameter(hidden = true) Project project,
@RequestParam("serviceId") String serviceId)
throws Exception {
if (Service.ServiceType.KUBERNETES.equals(region.getServices().getType())) {
return helmAppsService.getUserService(
region, project, userProvider.getUser(region), serviceId);
}
return null;
return helmAppsService.getUserService(
region, project, userProvider.getUser(region), serviceId);
}

@PostMapping("/app/rename")
Expand All @@ -151,15 +146,12 @@ public void renameApp(
@Parameter(hidden = true) Project project,
@RequestBody RenameRequestDTO request)
throws Exception {
if (Service.ServiceType.KUBERNETES.equals(region.getServices().getType())) {
User user = userProvider.getUser(region);
helmAppsService.rename(
region,
project,
userProvider.getUser(region),
request.getServiceID(),
request.getFriendlyName());
}
helmAppsService.rename(
region,
project,
userProvider.getUser(region),
request.getServiceID(),
request.getFriendlyName());
}

@PostMapping("/app/share")
Expand All @@ -168,15 +160,12 @@ public void shareApp(
@Parameter(hidden = true) Project project,
@RequestBody ShareRequestDTO request)
throws Exception {
if (Service.ServiceType.KUBERNETES.equals(region.getServices().getType())) {
User user = userProvider.getUser(region);
helmAppsService.share(
region,
project,
userProvider.getUser(region),
request.getServiceID(),
request.isShare());
}
helmAppsService.share(
region,
project,
userProvider.getUser(region),
request.getServiceID(),
request.isShare());
}

@PostMapping("/app/suspend")
Expand All @@ -199,70 +188,67 @@ public void resumeApp(

private void suspendOrResume(Region region, Project project, String serviceId, boolean suspend)
throws Exception {
if (Service.ServiceType.KUBERNETES.equals(region.getServices().getType())) {
User user = userProvider.getUser(region);
Service userService =
helmAppsService.getUserService(
region, project, userProvider.getUser(region), serviceId);
if (!userService.isSuspendable()) {
throw new ServiceNotSuspendableException();
}
String chart = userService.getChart();
int split = chart.lastIndexOf('-');
String chartName = chart.substring(0, split);
String version = chart.substring(split + 1);
String catalogId = userService.getCatalogId();
// This code is for legacy compat for services that were created with Onyxia < v2.7.0
// before introduction of Onyxia's secret
if (catalogId == null) {
List<CatalogWrapper> elligibleCatalogs =
catalogService.getCatalogs(region, user).getCatalogs().stream()
.filter(
catalog ->
catalog.getCatalog()
.getPackageByName(chartName)
.isPresent())
.toList();
if (elligibleCatalogs.isEmpty()) {
throw new NotFoundException();
}
if (elligibleCatalogs.size() > 1) {
throw new IllegalStateException("Chart is present in multiple catalogs, abort");
}
CatalogWrapper catalog = elligibleCatalogs.getFirst();
catalogId = catalog.getId();
User user = userProvider.getUser(region);
Service userService =
helmAppsService.getUserService(
region, project, userProvider.getUser(region), serviceId);
if (!userService.isSuspendable()) {
throw new ServiceNotSuspendableException();
}
String chart = userService.getChart();
int split = chart.lastIndexOf('-');
String chartName = chart.substring(0, split);
String version = chart.substring(split + 1);
String catalogId = userService.getCatalogId();
// This code is for legacy compat for services that were created with Onyxia < v2.7.0
// before introduction of Onyxia's secret
if (catalogId == null) {
List<CatalogWrapper> elligibleCatalogs =
catalogService.getCatalogs(region, user).getCatalogs().stream()
.filter(
catalog ->
catalog.getCatalog()
.getPackageByName(chartName)
.isPresent())
.toList();
if (elligibleCatalogs.isEmpty()) {
throw new NotFoundException();
}
Optional<CatalogWrapper> catalog = catalogService.getCatalogById(catalogId, user);
if (catalog.isEmpty()) {
throw new IllegalStateException(
"Catalog " + catalogId + " is not available anymore");
if (elligibleCatalogs.size() > 1) {
throw new IllegalStateException("Chart is present in multiple catalogs, abort");
}
CatalogWrapper catalog = elligibleCatalogs.getFirst();
catalogId = catalog.getId();
}
Optional<CatalogWrapper> catalog = catalogService.getCatalogById(catalogId, user);
if (catalog.isEmpty()) {
throw new IllegalStateException("Catalog " + catalogId + " is not available anymore");
}

if (suspend) {
helmAppsService.suspend(
region,
project,
catalog.get().getId(),
chartName,
version,
user,
serviceId,
catalog.get().getSkipTlsVerify(),
catalog.get().getCaFile(),
false);
} else {
helmAppsService.resume(
region,
project,
catalog.get().getId(),
chartName,
version,
user,
serviceId,
catalog.get().getSkipTlsVerify(),
catalog.get().getCaFile(),
false);
}
if (suspend) {
helmAppsService.suspend(
region,
project,
catalog.get().getId(),
chartName,
version,
user,
serviceId,
catalog.get().getSkipTlsVerify(),
catalog.get().getCaFile(),
false);
} else {
helmAppsService.resume(
region,
project,
catalog.get().getId(),
chartName,
version,
user,
serviceId,
catalog.get().getSkipTlsVerify(),
catalog.get().getCaFile(),
false);
}
}

Expand Down Expand Up @@ -299,11 +285,8 @@ public String getLogs(
@Parameter(hidden = true) Project project,
@RequestParam("serviceId") String serviceId,
@RequestParam("taskId") String taskId) {
if (Service.ServiceType.KUBERNETES.equals(region.getServices().getType())) {
return helmAppsService.getLogs(
region, project, userProvider.getUser(region), serviceId, taskId);
}
return null;
return helmAppsService.getLogs(
region, project, userProvider.getUser(region), serviceId, taskId);
}

@Operation(
Expand All @@ -327,36 +310,32 @@ public SseEmitter getEvents(
@Parameter(hidden = true) Region region, @Parameter(hidden = true) Project project)
throws Exception {

if (Service.ServiceType.KUBERNETES.equals(region.getServices().getType())) {
final SseEmitter emitter = new SseEmitter();
final CustomWatcher watcher = new CustomWatcher(emitter, objectMapper);
final Watch watch =
helmAppsService.getEvents(
region, project, userProvider.getUser(region), watcher);
emitter.onCompletion(
new Runnable() {
@Override
public void run() {
watch.close();
}
});
emitter.onError(
new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
watch.close();
}
});
emitter.onTimeout(
new Runnable() {
@Override
public void run() {
watch.close();
}
});
return emitter;
}
return null;
final SseEmitter emitter = new SseEmitter();
final CustomWatcher watcher = new CustomWatcher(emitter, objectMapper);
final Watch watch =
helmAppsService.getEvents(region, project, userProvider.getUser(region), watcher);
emitter.onCompletion(
new Runnable() {
@Override
public void run() {
watch.close();
}
});
emitter.onError(
new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
watch.close();
}
});
emitter.onTimeout(
new Runnable() {
@Override
public void run() {
watch.close();
}
});
return emitter;
}

public static class CustomWatcher implements Watcher<Event> {
Expand Down Expand Up @@ -423,11 +402,8 @@ public UninstallService destroyApp(
@RequestParam(value = "path", required = false) String path,
@RequestParam(value = "bulk", required = false) Optional<Boolean> bulk)
throws Exception {
if (Service.ServiceType.KUBERNETES.equals(region.getServices().getType())) {
return helmAppsService.destroyService(
region, project, userProvider.getUser(region), path, bulk.orElse(false));
}
return null;
return helmAppsService.destroyService(
region, project, userProvider.getUser(region), path, bulk.orElse(false));
}

@Operation(
Expand Down
Loading

0 comments on commit 2e34d71

Please sign in to comment.