-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 3602 pod network bandwidth usage action (#3621)
(cherry picked from commit 9d77b53)
- Loading branch information
1 parent
34333b4
commit d3b9e6f
Showing
16 changed files
with
442 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
api/src/main/java/com/epam/pipeline/manager/docker/LimitBandwidthCommand.java
This file contains 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 @@ | ||
/* | ||
* Copyright 2024 EPAM Systems, Inc. (https://www.epam.com/) | ||
* | ||
* 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.epam.pipeline.manager.docker; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Builder | ||
public class LimitBandwidthCommand extends AbstractDockerCommand { | ||
private static final String COMMAND_TEMPLATE = "curl -k -s \"%s\" | sudo -E /bin/bash --login /dev/stdin %s"; | ||
|
||
private final String runId; | ||
private final String api; | ||
private final String apiToken; | ||
private final String containerId; | ||
private final String enable; | ||
private final String uploadRate; | ||
private final String downloadRate; | ||
|
||
private final String runScriptUrl; | ||
|
||
@Override | ||
public String getCommand() { | ||
return getDockerCommand(COMMAND_TEMPLATE, runScriptUrl); | ||
} | ||
|
||
@Override | ||
protected List<String> buildCommandArguments() { | ||
final List<String> command = new ArrayList<>(); | ||
command.add(runId); | ||
command.add(api); | ||
command.add(apiToken); | ||
command.add(containerId); | ||
command.add(enable); | ||
command.add(uploadRate); | ||
command.add(downloadRate); | ||
return command; | ||
} | ||
} |
This file contains 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
36 changes: 36 additions & 0 deletions
36
api/src/main/java/com/epam/pipeline/manager/pipeline/BandwidthMonitoringService.java
This file contains 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,36 @@ | ||
/* | ||
* Copyright 2024 EPAM Systems, Inc. (https://www.epam.com/) | ||
* | ||
* 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.epam.pipeline.manager.pipeline; | ||
|
||
import com.epam.pipeline.manager.preference.SystemPreferences; | ||
import com.epam.pipeline.manager.scheduling.AbstractSchedulingManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class BandwidthMonitoringService extends AbstractSchedulingManager { | ||
private final BandwidthMonitoringServiceCore core; | ||
|
||
@PostConstruct | ||
public void init() { | ||
scheduleFixedDelaySecured(core::monitor, SystemPreferences.SYSTEM_POD_BANDWIDTH_MONITOR_DELAY, | ||
"BandwidthMonitor"); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
api/src/main/java/com/epam/pipeline/manager/pipeline/BandwidthMonitoringServiceCore.java
This file contains 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,73 @@ | ||
/* | ||
* Copyright 2024 EPAM Systems, Inc. (https://www.epam.com/) | ||
* | ||
* 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.epam.pipeline.manager.pipeline; | ||
|
||
import com.epam.pipeline.controller.vo.TagsVO; | ||
import com.epam.pipeline.entity.pipeline.PipelineRun; | ||
import com.epam.pipeline.entity.utils.DateUtils; | ||
import com.epam.pipeline.manager.docker.DockerContainerOperationManager; | ||
import com.epam.pipeline.manager.preference.PreferenceManager; | ||
import com.epam.pipeline.manager.preference.SystemPreferences; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.javacrumbs.shedlock.core.SchedulerLock; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.epam.pipeline.manager.pipeline.PipelineRunManager.NETWORK_LIMIT; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class BandwidthMonitoringServiceCore { | ||
private final PipelineRunManager pipelineRunManager; | ||
private final DockerContainerOperationManager dockerContainerOperationManager; | ||
private final PreferenceManager preferenceManager; | ||
|
||
@SchedulerLock(name = "BandwidthMonitoringService_monitor", lockAtMostForString = "PT10M") | ||
public void monitor() { | ||
final List<PipelineRun> runs = pipelineRunManager.loadRunningPipelineRuns(); | ||
final String networkLimitDateTag = getNetworkLimitDateTag(); | ||
for (PipelineRun run: runs) { | ||
final Map<String, String> tags = run.getTags(); | ||
if (shouldSetLimit(tags)) { | ||
final int boundary = Integer.parseInt(tags.get(NETWORK_LIMIT)); | ||
dockerContainerOperationManager.limitNetworkBandwidth(run, boundary, true); | ||
run.addTag(networkLimitDateTag, DateUtils.nowUTCStr()); | ||
} else if (shouldCleanLimit(tags)) { | ||
dockerContainerOperationManager.limitNetworkBandwidth(run, 0, false); | ||
run.removeTag(networkLimitDateTag); | ||
} | ||
pipelineRunManager.updateTags(run.getId(), new TagsVO(run.getTags()), true); | ||
} | ||
} | ||
|
||
private boolean shouldSetLimit(final Map<String, String> tags) { | ||
return tags.containsKey(NETWORK_LIMIT) && !tags.containsKey(getNetworkLimitDateTag()); | ||
} | ||
|
||
private boolean shouldCleanLimit(final Map<String, String> tags) { | ||
return !tags.containsKey(NETWORK_LIMIT) && tags.containsKey(getNetworkLimitDateTag()); | ||
} | ||
|
||
private String getNetworkLimitDateTag() { | ||
final String suffix = preferenceManager.getPreference(SystemPreferences.SYSTEM_RUN_TAG_DATE_SUFFIX); | ||
return String.format("%s_%s", NETWORK_LIMIT, suffix); | ||
} | ||
} |
This file contains 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 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 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 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 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
Oops, something went wrong.