Skip to content
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

Add rust implementation of container Python script #38

Open
wants to merge 30 commits into
base: master
Choose a base branch
from

Conversation

saiarcot895
Copy link

Add a rust implementation of the container script in the SONiC image. This script is primarily responsible for starting, waiting on, and stopping containers.

In the case of local containers, this script is basically a wrapper around docker start, docker wait, and docker stop. In the case of Kubernetes containers, there's some additional logic/checks on whether the container should be started or not; this is not fully implemented.

Basic testing has been done with local containers (the most common use case).

This handles most use cases for local containers. For Kubernetes
containers, there's very limited handling in the start function, and
pretty much nothing else.

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@r12f
Copy link
Collaborator

r12f commented Jan 20, 2025

Hi Sai, looks like it is hitting CI errors. Do you mind to help take a look?

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

[dependencies]
bollard = { version = "0.17.1", features = ["chrono"] }
chrono = "0.4.38"
enumset = "1.1.5"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be better to move these dependencies to workspace root.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

bollard = { version = "0.17.1", features = ["chrono"] }
chrono = "0.4.38"
enumset = "1.1.5"
futures-util = "0.3.30"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.3 will be good enough. The last minor version can be ignored, since we have the lock file and will help with upgrade.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// DB field names
//const FEATURE_TABLE: &str = "FEATURE";
const SET_OWNER: &str = "set_owner";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better to add a prefix to specify these are db fields.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added DB_ or DB_FIELD_ prefix.


fn set_label(db_connections: &DbConnections, _feature: &str, _create: bool) {
if db_connections.remote_ctr_enabled {
todo!();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this part missing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was. Current todo! sections have been implemented, but since they're Kubernetes-specific, I can't test them.

}

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can use async_main to simplify the code. Is this delayed block_on intentional?

Copy link
Author

@saiarcot895 saiarcot895 Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked the code to use async main with tokio. Not sure what you mean by if the delayed block_on is intentional, but docker.wait_container is an async method, and I needed to block until that was complete.

Edit: Collecting the results specifically (the try_collect method) is async, and I needed to wait on that.

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

let cli = Cli::parse();

match cli.action {
Action::Start => container_start(&cli.name).await,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Sai, I wonder if we can create a container class and make it more OOP.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the container code will be better to move into container.rs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I will note that memory usage has increased slightly with this change, by almost 2MB. I'm guessing it's because Rust now needs to actually create and store an object.

admin@vlab-01:~$ ps -eF | grep "wait pmon"
admin      91569   91562  0 17556 47720   0 Mar07 ?        00:00:00 python3 /usr/local/bin/container wait pmon
admin     459955    5819  0  5770 10996   0 20:45 pts/0    00:00:00 ./container-before-updates wait pmon
admin     459956    5819  0 73382 12628   0 20:45 pts/0    00:00:00 ./container-after-oop wait pmon

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@@ -0,0 +1,399 @@
use bollard::container::*;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, specifically https://github.com/sonic-net/sonic-buildimage/blob/master/src/sonic-ctrmgrd/ctrmgr/container.

Local containers has been tested, Kubernetes has not been tested.

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
@mssonicbld
Copy link

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants