Skip to content
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
46 changes: 44 additions & 2 deletions src/main/java/com/kruize/optimizer/service/ProfileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.kruize.optimizer.client.KruizeClient;
import com.kruize.optimizer.exception.KruizeServiceException;
import com.kruize.optimizer.model.kruize.BulkConfig;
import com.kruize.optimizer.model.kruize.Datasource;
import com.kruize.optimizer.model.kruize.KruizeProfile;
import com.kruize.optimizer.utils.OptimizerConstants.MessageConstants;
import com.kruize.optimizer.utils.OptimizerConstants.ProfileType;
Expand Down Expand Up @@ -48,6 +49,9 @@ public class ProfileService {
@RestClient
KruizeClient kruizeClient;

@Inject
DatasourceService datasourceService;

private final ObjectMapper objectMapper = new ObjectMapper();

/**
Expand Down Expand Up @@ -297,8 +301,9 @@ private void installProfile(String profileType, String profileName, String profi
kruizeClient.createLayer(profileDefinition);
break;
case ProfileType.BULK:
kruizeClient.createBulkConfig(
objectMapper.convertValue(profileDefinition, BulkConfig.class));
BulkConfig bulkConfig = objectMapper.convertValue(profileDefinition, BulkConfig.class);
resolveBulkConfigDatasource(bulkConfig);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
kruizeClient.createBulkConfig(bulkConfig);
break;
default:
throw new IllegalArgumentException("Unknown profile type: " + profileType);
Expand All @@ -308,6 +313,42 @@ private void installProfile(String profileType, String profileName, String profi
}
}

/**
* The bulk config's datasources list is an ordered preference list (e.g. ["thanos-1",
* "prometheus-1"]). Pick the first one registered in Kruize and replace the list with that
* single entry, since a bulk job supports only one datasource. This lets the bundled default
* config work on any platform. If none of the listed datasources is registered, the first
* datasource that is registered in Kruize is used and an error is logged asking to update the
* config with the right datasource.
*/
private void resolveBulkConfigDatasource(BulkConfig bulkConfig) {
List<String> preferred = bulkConfig.getDatasources();
if (preferred == null || preferred.isEmpty()) {
return;
}

List<String> available = datasourceService.getDatasources().stream()
.map(Datasource::getName)
.collect(Collectors.toList());

String resolved = preferred.stream()
.filter(available::contains)
.findFirst()
.orElse(null);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved

if (resolved == null) {
resolved = available.get(0);
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
LOG.errorf("None of the datasources %s of bulk config '%s' are registered in Kruize (registered: %s). "
+ "Continuing the install with the registered datasource '%s'. Update the bulk config "
+ "with the right datasource, e.g. PUT /bulkConfigs?config_name=%s.",
preferred, bulkConfig.getConfigName(), available, resolved, bulkConfig.getConfigName());
} else {
LOG.infof("Using datasource '%s' for bulk config '%s' (preferred: %s, available: %s)",
resolved, bulkConfig.getConfigName(), preferred, available);
}
bulkConfig.setDatasources(List.of(resolved));
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved

/**
* Load profile definition from local resources
*
Expand Down Expand Up @@ -456,3 +497,4 @@ private List<KruizeProfile> getInstalledProfiles(String profileType) {
}
}


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"config_name": "default-bulk-config",
"cluster_name": "default",
"datasources": ["thanos-1"],
"datasources": ["thanos-1", "prometheus-1"],
"namespaces": [],
"labels": {
"ibm.runtimes.intelligence.monitoring": "true"
Expand All @@ -19,4 +19,4 @@
},
"webhook_url": "http://kruize-optimizer:8080/webhook/config/bulk",
"enabled": true
}
}
Loading