-
Notifications
You must be signed in to change notification settings - Fork 72
feat(cf-api): Add Event Ledger publication for NVCF function deployment status transitions. #1911
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
Open
dmikhaylovnv
wants to merge
5
commits into
main
Choose a base branch
from
feat/1290-cloud-functions-event-ledger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1013393
feat(cf-api): Add Event Ledger publication for NVCF function deployme…
dmikhaylovnv 558e136
rebuild event
dmikhaylovnv 4222d03
updated NOTICE file
dmikhaylovnv 883a46e
fixed source field
dmikhaylovnv 2fcc476
make Event Ledger call synchronous
dmikhaylovnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
...ctions/nvcf-core/src/main/java/com/nvidia/nvcf/service/eventledger/EventLedgerClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.nvidia.nvcf.service.eventledger; | ||
|
|
||
| import static io.cloudevents.jackson.JsonFormat.CONTENT_TYPE; | ||
|
|
||
| import com.nvidia.nvcf.persistence.function.entity.FunctionStatus; | ||
| import com.nvidia.nvcf.util.NvcfOAuth2ClientUtils; | ||
| import io.cloudevents.CloudEvent; | ||
| import io.cloudevents.core.builder.CloudEventBuilder; | ||
| import io.cloudevents.core.format.EventFormat; | ||
| import io.cloudevents.core.provider.EventFormatProvider; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
| import tools.jackson.databind.json.JsonMapper; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RefreshScope | ||
| public class EventLedgerClient { | ||
|
|
||
| static final String CLIENT_REGISTRATION_ID = "event-ledger"; | ||
| static final String CLOUD_EVENTS_PATH = "/v3/ledger/cloudevents"; | ||
| static final String CLOUD_EVENTS_CONTENT_TYPE = CONTENT_TYPE; | ||
| static final String CLOUD_EVENT_SOURCE = "nvidia-cloud-functions"; | ||
|
|
||
| private static final String MESG_UNKNOWN_FUNCTION_STATUS = | ||
| "Event Ledger unknown function status: {}, skip publishing."; | ||
| private static final String MESG_FAILED_TO_PUBLISH_FUNCTION_STATUS = | ||
| "Failed to publish function status to Event Ledger: accountId={}, functionId={}, " | ||
| + "functionVersionId={}, deploymentId={}, status={}"; | ||
| private static final Map<FunctionStatus, String> EVENT_NAMES = Map.of( | ||
| FunctionStatus.DEPLOYING, "Function.Deploying", | ||
| FunctionStatus.ACTIVE, "Function.Ready", | ||
| FunctionStatus.DEGRADING, "Function.Degrading", | ||
| FunctionStatus.DEGRADED, "Function.Degraded", | ||
| FunctionStatus.ERROR, "Function.Error", | ||
| FunctionStatus.INACTIVE, "Function.Inactive"); | ||
| private final Duration timeout; | ||
| private final boolean enabled; | ||
| private final WebClient webClient; | ||
| private final JsonMapper jsonMapper; | ||
| private final EventFormat eventFormat; | ||
|
|
||
| private record FunctionStatusTransition( | ||
| UUID functionId, | ||
| UUID functionVersionId, | ||
| UUID deploymentId, | ||
| FunctionStatus previousStatus, | ||
| FunctionStatus currentStatus, | ||
| Instant persistedAt) { | ||
| } | ||
|
|
||
| @Autowired | ||
| public EventLedgerClient( | ||
| @Value("${nvcf.event-ledger.enabled:false}") boolean enabled, | ||
| @Value("${nvcf.event-ledger.base-url:http://event-ledger.nvcf.svc.cluster.local:8080}") | ||
| String baseUrl, | ||
|
dmikhaylovnv marked this conversation as resolved.
|
||
| @Value("${nvcf.event-ledger.timeout:2s}") Duration timeout, | ||
| @Value("${spring.security.oauth2.client.registration.event-ledger.client-id:}") | ||
| String clientId, | ||
| @Value("${spring.security.oauth2.client.registration.event-ledger.client-secret:}") | ||
| String clientSecret, | ||
| @Value("${spring.security.oauth2.client.registration.event-ledger.scope:}") String scope, | ||
| @Value("${spring.security.oauth2.client.provider.event-ledger.token-uri:}") String tokenUri, | ||
| WebClient.Builder webClientBuilder, | ||
| JsonMapper jsonMapper) { | ||
| this(enabled, timeout, enabled | ||
| ? authenticatedWebClient( | ||
| baseUrl, clientId, clientSecret, scope, tokenUri, webClientBuilder) | ||
| : webClientBuilder.baseUrl(baseUrl).build(), jsonMapper); | ||
| } | ||
|
|
||
| EventLedgerClient( | ||
| boolean enabled, | ||
| Duration timeout, | ||
| WebClient webClient, | ||
| JsonMapper jsonMapper) { | ||
| this.enabled = enabled; | ||
| this.timeout = timeout; | ||
| this.webClient = webClient; | ||
| this.jsonMapper = jsonMapper; | ||
| this.eventFormat = EventFormatProvider.getInstance().resolveFormat(CONTENT_TYPE); | ||
| } | ||
|
|
||
| public void publish( | ||
| String ncaId, | ||
| UUID functionId, | ||
| UUID functionVersionId, | ||
| UUID deploymentId, | ||
| FunctionStatus previousStatus, | ||
| FunctionStatus currentStatus, | ||
| Instant persistedAt) { | ||
| if (!enabled) { | ||
| return; | ||
| } | ||
|
|
||
| var transition = new FunctionStatusTransition( | ||
| functionId, | ||
| functionVersionId, | ||
| deploymentId, | ||
| previousStatus, | ||
| currentStatus, | ||
| persistedAt); | ||
| var eventName = EVENT_NAMES.get(transition.currentStatus()); | ||
| if (eventName == null) { | ||
| log.warn(MESG_UNKNOWN_FUNCTION_STATUS, transition.currentStatus()); | ||
| return; | ||
| } | ||
|
|
||
| send(ncaId, transition, eventName); | ||
| } | ||
|
|
||
| private void send( | ||
| String ncaId, FunctionStatusTransition transition, String eventName) { | ||
| try { | ||
| webClient.post() | ||
| .uri(CLOUD_EVENTS_PATH) | ||
| .contentType(MediaType.parseMediaType(CLOUD_EVENTS_CONTENT_TYPE)) | ||
| .bodyValue(eventFormat.serialize(buildCloudEvent(ncaId, transition, eventName))) | ||
| .retrieve() | ||
| .toBodilessEntity() | ||
| .block(timeout); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } catch (Exception ex) { | ||
| log.warn(MESG_FAILED_TO_PUBLISH_FUNCTION_STATUS, | ||
| ncaId, transition.functionId(), transition.functionVersionId(), | ||
| transition.deploymentId(), transition.currentStatus(), ex); | ||
| } | ||
| } | ||
|
|
||
| private CloudEvent buildCloudEvent( | ||
| String ncaId, FunctionStatusTransition transition, String eventName) throws Exception { | ||
| var details = Map.of( | ||
| "previousStatus", transition.previousStatus().toString(), | ||
| "currentStatus", transition.currentStatus().toString()); | ||
| return CloudEventBuilder.v1() | ||
| .withId(UUID.randomUUID().toString()) | ||
| .withSource(URI.create(CLOUD_EVENT_SOURCE)) | ||
| .withTime(transition.persistedAt().atOffset(ZoneOffset.UTC)) | ||
| .withType(eventName) | ||
| .withExtension("namespace", transition.functionVersionId().toString()) | ||
|
shelleyshen-0 marked this conversation as resolved.
|
||
| .withExtension("functionid", transition.functionId().toString()) | ||
| .withExtension("functionversionid", transition.functionVersionId().toString()) | ||
| .withExtension("deploymentid", transition.deploymentId().toString()) | ||
| .withExtension("ncaid", ncaId) | ||
| .withData(jsonMapper.writeValueAsString(details).getBytes(StandardCharsets.UTF_8)) | ||
| .build(); | ||
| } | ||
|
|
||
| private static WebClient authenticatedWebClient( | ||
| String baseUrl, | ||
| String clientId, | ||
| String clientSecret, | ||
| String scope, | ||
| String tokenUri, | ||
| WebClient.Builder builder) { | ||
| return builder.baseUrl(baseUrl) | ||
| .filter(NvcfOAuth2ClientUtils.getOAuth2ExchangeFilter( | ||
| builder, CLIENT_REGISTRATION_ID, tokenUri, clientId, clientSecret, scope)) | ||
| .build(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...-core/src/main/java/com/nvidia/nvcf/service/function/FunctionStatusTransitionService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.nvidia.nvcf.service.function; | ||
|
|
||
| import com.nvidia.nvcf.persistence.function.FunctionsRepository; | ||
| import com.nvidia.nvcf.persistence.function.entity.FunctionEntity; | ||
| import com.nvidia.nvcf.persistence.function.entity.FunctionStatus; | ||
| import com.nvidia.nvcf.service.eventledger.EventLedgerClient; | ||
| import java.time.Clock; | ||
| import java.time.Instant; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class FunctionStatusTransitionService { | ||
|
|
||
| private final FunctionsRepository functionsRepository; | ||
| private final EventLedgerClient eventLedgerClient; | ||
| // Inject the time source so transition timestamps can be controlled in tests. | ||
| private final Clock clock; | ||
|
|
||
| public void persist( | ||
| FunctionEntity function, UUID deploymentId, FunctionStatus newStatus) { | ||
| var previousStatus = function.getFunctionStatus(); | ||
| if (previousStatus == newStatus) { | ||
| return; | ||
| } | ||
| function.setFunctionStatus(newStatus); | ||
| functionsRepository.insert(function); | ||
| eventLedgerClient.publish( | ||
| function.getNcaId(), | ||
| function.getFunctionId(), | ||
| function.getFunctionVersionId(), | ||
| deploymentId, | ||
| previousStatus, | ||
| newStatus, | ||
| Instant.now(clock)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.