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

WIP: Add Zero Touch Satellite Registration Process in Ground Control #48

Closed
wants to merge 17 commits into from

Conversation

bupd
Copy link
Collaborator

@bupd bupd commented Sep 13, 2024

Fixes: #41 & #36

Fix: #36

Note: This PR should be merged after merging #40, as it is built on top of the changes introduced in that PR.

Summary by CodeRabbit

  • New Features

    • Introduced new data structures for managing projects and robots, enhancing the app's capabilities.
    • Added a new robots table to store robot information and a robot_projects table to manage relationships between robots and projects.
  • Bug Fixes

    • Improved error handling and logging across various operations.
  • Refactor

    • Streamlined module dependencies for better maintainability and reduced complexity.
  • Documentation

    • Updated comments and versioning for SQL code generation tools.

bupd added 16 commits September 10, 2024 18:44
Signed-off-by: bupd <[email protected]>
Updated group state artifact with nesting
Added flattening of state artifact
top groups contains state of all subgroups

Signed-off-by: bupd <[email protected]>
Copy link

coderabbitai bot commented Sep 13, 2024

Walkthrough

The changes involve significant updates to the ground-control module's dependencies, database models, and SQL schema. Key modifications include the removal of certain dependencies, the introduction of new structs in the database model, and the creation of new SQL tables for managing robots and their associations with projects. These alterations streamline the codebase and enhance its capability to represent hierarchical relationships and new entities.

Changes

Files Change Summary
ground-control/go.mod Removed dependencies on github.com/goharbor/harbor/src and github.com/google/go-containerregistry. Added github.com/opencontainers/image-spec and oras.land/oras-go/v2.
ground-control/internal/database/models.go Modified Group struct to include ParentGroupID. Added new structs: Project, Robot, RobotProject, and SatelliteImage. Updated SQL code generator version from sqlc v1.26.0 to sqlc v1.27.0.
ground-control/sql/schema/*.sql Introduced new SQL tables: robots and robot_projects, establishing relationships for managing robots and their associations with projects.

Assessment against linked issues

Objective Addressed Explanation
Implement Zero Touch Registration (41) The changes do not directly address the implementation of Zero Touch Registration.

🐰 In fields of code where rabbits dwell,
New paths emerge, all working well.
With URLs and groups that intertwine,
Our satellites soar, oh so divine!
In every change, a hop, a leap,
For coding dreams, we joyfully keep! 🐇✨

Tip

OpenAI O1 model for chat
  • We have deployed OpenAI's latest O1 model for chat.
  • OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model.
  • Please share any feedback with us in the discussions post.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range and nitpick comments (14)
ground-control/sql/schema/011_projects.sql (1)

6-6: Consider renaming the projects column to project_name.

To avoid confusion with the table name and to be more specific, consider renaming the projects column to project_name or a similar name that clearly indicates it holds the name of an individual project.

Apply this diff to rename the column:

CREATE TABLE projects (
  id SERIAL PRIMARY KEY,
-  projects VARCHAR(255) NOT NULL,
+  project_name VARCHAR(255) NOT NULL,
  created_at TIMESTAMP DEFAULT NOW() NOT NULL,
  updated_at TIMESTAMP DEFAULT NOW() NOT NULL
);
ground-control/sql/queries/satellite_groups.sql (2)

6-8: The query is correctly implemented, but consider adding a check for the existence of the satellite and group.

The RemoveSatelliteFromGroup query is correctly implemented. The WHERE clause ensures that only the specified satellite is removed from the specified group.

However, it might be a good practice to add a check to ensure that the satellite and group exist before attempting to remove the association. This can be done by using a SELECT statement to check for the existence of the satellite_id and group_id in their respective tables before executing the DELETE statement.


10-13: The query is correctly implemented, but consider adding a check for the existence of the satellite.

The GetGroupsBySatelliteID query is correctly implemented. The JOIN operation ensures that only the groups associated with the specified satellite are returned.

However, it might be a good practice to add a check to ensure that the satellite exists before attempting to retrieve the associated groups. This can be done by using a SELECT statement to check for the existence of the satellite_id in the satellites table before executing the JOIN operation.

ground-control/sql/queries/groups.sql (1)

10-13: Consider selecting only the required columns.

The GetSubgroups query looks good. It correctly retrieves all subgroups of a given parent group by its ID using the WHERE clause, which is a good practice. The query returns all columns from the groups table for the subgroups, which may not be necessary in all cases.

Consider selecting only the required columns in the query to improve performance and reduce the amount of data transferred. For example:

SELECT id, group_name, parent_group_id, created_at, updated_at
FROM groups
WHERE parent_group_id = $1;
internal/utils/auth.go (1)

10-24: LGTM!

The Auth function is well-implemented and follows good practices:

  • It handles the case when the required environment variables are not set by returning an appropriate error.
  • It uses os.Getenv to retrieve the credentials from the environment variables.
  • It constructs an authn.Authenticator using the authn.FromConfig method from the go-containerregistry package with the provided credentials.
  • It returns the authenticator and a nil error on success, or nil and the corresponding error on failure.

To improve the code's readability and maintainability, consider adding a brief comment explaining the function's purpose and the expected environment variables. Here's an example:

+// Auth creates an authentication mechanism using credentials sourced from environment variables.
+// It expects the HARBOR_USERNAME and HARBOR_PASSWORD environment variables to be set.
func Auth() (authn.Authenticator, error) {
  // ...
}
ground-control/sql/queries/images.sql (1)

5-6: LGTM! Consider pagination for scalability.

The ListImages query is implemented correctly and retrieves all records from the images table.

However, as the table grows, retrieving all records without any filtering or pagination might impact performance. Consider adding pagination support in the future if needed for better scalability.

ground-control/internal/server/server.go (1)

38-38: LGTM! The comment is a good placeholder for future enhancements.

The comment indicates a plan to create a dynamic connection string based on the environment (production or development). This aligns with the PR objective of enhancing the functionality related to satellite registration within the Ground Control system, as it suggests an intention to improve the configurability of the server in future iterations.

When implementing this enhancement in the future, consider the following suggestions:

  • Use environment variables to determine the current environment (e.g., ENV=production or ENV=development).
  • Create separate connection string templates for each environment, with placeholders for the dynamic values (e.g., connStrProd and connStrDev).
  • Use a switch statement or if-else block to select the appropriate connection string template based on the environment.
  • Replace the placeholders in the selected connection string template with the actual values from the environment variables.

This approach will allow for a clean separation of connection settings based on the environment and make it easier to manage and configure the server in different deployment contexts.

ground-control/internal/database/label_images.sql.go (1)

80-94: LGTM!

The new SQL command RemoveImageFromLabel and its corresponding Go code are correctly implemented and follow the existing coding style and conventions. The addition of this functionality enhances the database interaction capabilities by enabling the removal of specific image-label associations, which aligns with the PR objectives of enhancing the functionality related to satellite registration within the Ground Control system.

Please consider updating the documentation to reflect the new RemoveImageFromLabel functionality, including its purpose, usage, and any relevant examples or considerations.

ground-control/reg/satelliteState.go (1)

84-199: LGTM with a few suggestions!

The PushStateArtifact function is well-structured and follows a logical flow for creating and pushing state artifacts. The use of a temporary file, handling of both group states and satellite states, and sourcing credentials from environment variables are all good practices.

Here are a few suggestions for improvement:

  1. Consider adding more detailed error messages or logging statements to help with debugging in case of failures.
  2. The function currently returns an error if the manifest already exists in the repository. Consider providing an option to overwrite the existing artifact if desired.
  3. The function could be split into smaller, more focused functions to improve readability and maintainability. For example, the code for establishing the remote repository connection could be extracted into a separate function.

Overall, the function is well-implemented and provides a solid foundation for pushing state artifacts to a registry.

ground-control/internal/server/handlers.go (5)

129-154: Consider adding logging statements.

The function looks good overall. However, consider adding logging statements to track the deletion of groups for better visibility and debugging purposes.


281-306: Clarify the difference between the ZTR process and the regular satellite registration process.

The ztrHandler function is very similar to the registerSatelliteHandler function. Please clarify how the Zero Touch Registration (ZTR) process is different from the regular satellite registration process. Consider adding comments to explain the difference or consolidating the two functions if they serve the same purpose.


387-418: Consider adding logging statements.

The function looks good overall. However, consider adding logging statements to track the assignment of images to satellites for better visibility and debugging purposes.


420-456: Consider adding logging statements.

The function looks good overall. However, consider adding logging statements to track the removal of images from satellites for better visibility and debugging purposes.


579-615: Consider adding logging statements.

The function looks good overall. However, consider adding logging statements to track the deletion of images from groups for better visibility and debugging purposes.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 02ba374 and f19ccad.

Files ignored due to path filters (1)
  • ground-control/go.sum is excluded by !**/*.sum
Files selected for processing (40)
  • ground-control/.env (1 hunks)
  • ground-control/go.mod (1 hunks)
  • ground-control/internal/database/db.go (1 hunks)
  • ground-control/internal/database/group_images.sql.go (3 hunks)
  • ground-control/internal/database/groups.sql.go (4 hunks)
  • ground-control/internal/database/images.sql.go (3 hunks)
  • ground-control/internal/database/label_images.sql.go (3 hunks)
  • ground-control/internal/database/labels.sql.go (1 hunks)
  • ground-control/internal/database/models.go (2 hunks)
  • ground-control/internal/database/satellite_groups.sql.go (3 hunks)
  • ground-control/internal/database/satellite_images.sql.go (1 hunks)
  • ground-control/internal/database/satellite_labels.sql.go (1 hunks)
  • ground-control/internal/database/satellites.sql.go (2 hunks)
  • ground-control/internal/server/handlers.go (9 hunks)
  • ground-control/internal/server/routes.go (1 hunks)
  • ground-control/internal/server/server.go (1 hunks)
  • ground-control/internal/utils/helper.go (1 hunks)
  • ground-control/reg/adapter.go (1 hunks)
  • ground-control/reg/satelliteState.go (1 hunks)
  • ground-control/sql/queries/group_images.sql (1 hunks)
  • ground-control/sql/queries/groups.sql (1 hunks)
  • ground-control/sql/queries/images.sql (1 hunks)
  • ground-control/sql/queries/label_images.sql (1 hunks)
  • ground-control/sql/queries/satellite_groups.sql (1 hunks)
  • ground-control/sql/queries/satellite_images.sql (1 hunks)
  • ground-control/sql/queries/satellites.sql (1 hunks)
  • ground-control/sql/schema/001_satellites.sql (1 hunks)
  • ground-control/sql/schema/002_groups.sql (1 hunks)
  • ground-control/sql/schema/006_images.sql (1 hunks)
  • ground-control/sql/schema/007_group_images.sql (1 hunks)
  • ground-control/sql/schema/008_label_images.sql (1 hunks)
  • ground-control/sql/schema/009_satellite_images.sql (1 hunks)
  • ground-control/sql/schema/010_robots.sql (1 hunks)
  • ground-control/sql/schema/011_projects.sql (1 hunks)
  • ground-control/sql/schema/012_robot_projects.sql (1 hunks)
  • internal/replicate/replicate.go (2 hunks)
  • internal/satellite/satellite.go (2 hunks)
  • internal/state/stateArtifact.go (1 hunks)
  • internal/store/http-fetch.go (2 hunks)
  • internal/utils/auth.go (1 hunks)
Files skipped from review due to trivial changes (8)
  • ground-control/internal/database/db.go
  • ground-control/internal/database/labels.sql.go
  • ground-control/internal/database/satellite_labels.sql.go
  • ground-control/reg/adapter.go
  • ground-control/sql/schema/001_satellites.sql
  • ground-control/sql/schema/006_images.sql
  • ground-control/sql/schema/007_group_images.sql
  • ground-control/sql/schema/008_label_images.sql
Additional comments not posted (90)
ground-control/sql/schema/012_robot_projects.sql (1)

1-10: LGTM! The schema changes are well-designed and follow best practices.

The group_images table effectively establishes a many-to-many relationship between robots and projects. The use of foreign keys with ON DELETE CASCADE ensures data integrity by automatically deleting associated records when a robot or project is deleted. The composite primary key enforces uniqueness for each combination of robot and project.

The inclusion of a rollback command is a good practice, as it allows for easy reversal of the schema changes if needed.

Overall, the schema design is clean, efficient, and maintainable.

ground-control/sql/schema/011_projects.sql (2)

1-13: The schema design follows best practices.

The projects table schema includes:

  • A primary key for unique record identification
  • Appropriate data types and constraints for each column
  • Timestamps for record keeping
  • A clear table name and column names
  • A down migration for easy rollback

Great job on the overall schema design!


6-6: Verify if the projects column length is appropriate.

The projects column currently allows string values up to 255 characters in length. Depending on the expected length of project names, this may be excessive and could potentially waste storage space.

Please confirm if project names are expected to be up to 255 characters long. If not, consider reducing the length to a more appropriate value based on the expected maximum length of project names.

ground-control/sql/schema/009_satellite_images.sql (1)

1-9: LGTM!

The SQL schema changes for creating and dropping the satellite_images table are well-structured and maintain data integrity. The table establishes a proper many-to-many relationship between satellites and images, with appropriate foreign key constraints and cascading deletes. The composite primary key ensures uniqueness for each satellite-image combination. The naming conventions are consistent with the existing schema, and the migrations allow for easy creation and deletion of the table.

ground-control/.env (1)

3-3: Verify the HARBOR_URL value for the production environment.

The HARBOR_URL is set to demo.goharbor.io, which might be a placeholder or a demo instance. Please ensure that this URL points to the correct Harbor instance for the production environment.

To verify, run the following script:

ground-control/sql/schema/002_groups.sql (1)

6-6: LGTM!

The addition of the parent_group_id column with the foreign key constraint and ON DELETE SET NULL behavior is a good approach to introduce a hierarchical structure within the groups. This change aligns with the PR objectives and the AI-generated summary.

The SQL syntax and the foreign key constraint are correctly implemented.

ground-control/sql/queries/label_images.sql (2)

4-4: LGTM!

The addition of the ON CONFLICT DO NOTHING clause is a good practice to handle potential duplicates gracefully. It ensures that the INSERT operation is skipped if an entry with the same label_id and image_id already exists, preventing errors and maintaining data integrity.


6-8: LGTM!

The new RemoveImageFromLabel command is a valuable addition that complements the existing AssignImageToLabel command. It provides the necessary functionality to remove specific label-image associations using a DELETE statement with a WHERE clause matching both label_id and image_id.

The command name and parameter placeholders follow the established conventions, making it consistent with the rest of the file. This addition enhances the overall functionality of the label_images table by enabling both assignment and removal operations.

ground-control/go.mod (3)

9-9: LGTM!

The addition of the github.com/opencontainers/image-spec dependency without the indirect comment aligns with the project's requirements and suggests a direct usage in the codebase.


10-10: LGTM!

The addition of the gopkg.in/yaml.v2 dependency introduces YAML processing capabilities to the project. The chosen version v2.4.0 is a stable release of the library.


11-11: Verify the usage of oras.land/oras-go/v2 in the codebase.

The addition of the oras.land/oras-go/v2 dependency introduces Open Registry API interaction capabilities to the project. The chosen version v2.5.0 is a recent release of the library.

Run the following script to verify the usage of this dependency:

Verification successful

Dependency oras.land/oras-go/v2 is correctly implemented and utilized.

The oras.land/oras-go/v2 package is extensively used in the PushStateArtifact function within satelliteState.go. This function leverages various ORAS features to create and push state artifacts to a remote registry, including:

  • Packaging manifests with oras.PackManifest
  • Copying artifacts with oras.Copy
  • Handling remote repositories with remote.NewRepository
  • Implementing authentication using auth.Client and auth.StaticCredential
  • Utilizing retry mechanisms with retry.DefaultClient

The implementation demonstrates proper usage of the ORAS library for interacting with OCI registries, aligning with the intended purpose of the dependency.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `oras.land/oras-go/v2` in the codebase.

# Test: Search for the package import and usage. Expect: At least one occurrence of the package import and usage.
rg --type go -A 5 $'oras.land/oras-go/v2'

Length of output: 753


Script:

#!/bin/bash
# Description: Examine the content of satelliteState.go to verify the usage of oras.land/oras-go/v2

# Display the content of the file
cat ground-control/reg/satelliteState.go

# Search for function calls or variable usage related to the oras package
rg --type go -n 'oras\.' ground-control/reg/satelliteState.go

Length of output: 5853

ground-control/sql/schema/010_robots.sql (3)

7-8: LGTM!

The foreign key constraint on the satellite_id column ensures data integrity by enforcing a valid relationship between the robots and satellites tables. The cascading delete behavior prevents orphaned records in the robots table when a satellite is deleted.


9-10: LGTM!

Including created_at and updated_at timestamp columns is a good practice for tracking record creation and modification times. Setting default values to the current time ensures that the timestamps are automatically populated when a new record is inserted, and marking the columns as non-nullable ensures that the timestamp values are always present.


13-14: LGTM!

Including a rollback command is a good practice for reversing schema changes if necessary. The command correctly drops the robots table, which is the only table created in this schema file.

ground-control/sql/queries/satellite_groups.sql (1)

1-4: LGTM!

The AddSatelliteToGroup query is correctly implemented. The use of ON CONFLICT DO NOTHING is a good practice to handle potential duplicates and prevent errors.

ground-control/sql/queries/satellite_images.sql (3)

1-4: LGTM!

The AssignImageToSatellite query correctly implements the logic to assign an image to a satellite. The use of ON CONFLICT DO NOTHING is a good practice to handle duplicates gracefully. This query aligns with the PR objective of implementing the Zero Touch Registration (ZTR) process for satellites by associating images with satellites.


6-8: LGTM!

The RemoveImageFromSatellite query correctly implements the logic to remove an image from a satellite. It uses the WHERE clause to match both the satellite_id and image_id to ensure the correct record is deleted. This query aligns with the PR objective of implementing the Zero Touch Registration (ZTR) process for satellites by allowing the removal of image associations from satellites.


10-14: LGTM!

The GetImagesForSatellite query correctly implements the logic to retrieve all images associated with a specific satellite. It uses the JOIN clause to combine the images and satellite_images tables based on the image_id foreign key, and the WHERE clause filters the results based on the provided satellite_id to ensure only the relevant images are returned. This query aligns with the PR objective of implementing the Zero Touch Registration (ZTR) process for satellites by allowing the retrieval of associated images for a satellite.

ground-control/sql/queries/groups.sql (2)

2-3: LGTM!

The changes to the CreateGroup query look good. The addition of the parent_group_id parameter allows for the creation of hierarchical group structures, which is a valuable enhancement. The query correctly inserts a new group with the provided parameters and returns the inserted group using the RETURNING clause, which is a good practice.


6-8: Verify the delete operation.

The DeleteGroup query looks good. It correctly deletes a group by its ID using the WHERE clause, which is a good practice. The query does not return any data, which is expected for a delete operation.

To ensure that the query deletes the group and its associated data correctly, please run the following verification script:

ground-control/sql/queries/satellites.sql (1)

21-23: LGTM!

The new GetSatelliteByID query is a valuable addition that aligns with the PR objectives of enhancing the satellite registration process. The query is correctly implemented and complements the existing functionality without altering the existing logic. The LIMIT 1 clause ensures that the response is concise and focused.

This query can be useful in scenarios where a satellite needs to be retrieved by its unique identifier, such as during the registration process or for displaying satellite details.

ground-control/sql/queries/group_images.sql (3)

4-4: LGTM!

Using ON CONFLICT DO NOTHING is a good practice to handle duplicate entries gracefully. It ensures idempotency of the INSERT operation.


6-8: LGTM!

The RemoveImageFromGroup command is correctly implemented and aligns with the objective of removing an image from a group.


16-31: LGTM!

The GetImagesForGroupAndSubgroups command is correctly implemented using a recursive CTE to build a hierarchy of groups and retrieve images for a group and its subgroups. This is a good approach to achieve the desired functionality.

ground-control/internal/database/models.go (2)

13-17: LGTM!

The addition of the ParentGroupID field of type sql.NullInt32 is a good way to represent hierarchical relationships between groups. The field name clearly conveys its purpose and using sql.NullInt32 type correctly handles cases where a group may not have a parent group.


60-63: LGTM!

The new SatelliteImage struct with SatelliteID and ImageID fields is a good way to associate satellite entities with their corresponding images. The field names clearly convey the purpose of the struct and using int32 type for the fields is appropriate.

ground-control/sql/queries/images.sql (1)

13-15: LGTM! The renaming improves clarity.

The DeleteImage query is implemented correctly and deletes a single record from the images table based on the provided id.

The renaming from DeleteImageList to DeleteImage improves clarity, as the query deletes a single record, not a list of records.

internal/state/stateArtifact.go (1)

14-48: The implementation of PullStateArtifact looks good overall.

The function follows a clear structure, handles errors appropriately, and uses structured logging. It effectively pulls the specified state artifact from the given registry.

ground-control/internal/database/satellite_groups.sql.go (5)

3-3: LGTM!

The version update for the sqlc tool is a good practice to leverage the latest improvements and bug fixes in the code generation process.


15-15: LGTM!

The addition of the ON CONFLICT DO NOTHING clause is a good practice to handle potential conflicts and maintain data integrity when inserting satellite-group associations.


28-55: LGTM!

The implementation of the GetGroupsBySatelliteID function is well-structured and correctly retrieves the group names associated with a specific satellite ID. The SQL query, error handling, and rows processing logic are all implemented correctly.


57-60: LGTM!

The RemoveSatelliteFromGroup SQL query is correctly implemented to delete a satellite from a specified group based on the provided satellite and group IDs. It effectively facilitates the management of satellite-group relationships.


62-70: LGTM!

The implementation of the RemoveSatelliteFromGroupParams struct and the RemoveSatelliteFromGroup function is correct. The struct defines the required parameters, and the function correctly executes the SQL query using those parameters. The error handling is also implemented correctly.

ground-control/internal/utils/helper.go (2)

13-51: LGTM!

The ParseArtifactURL function is well-implemented with the following highlights:

  1. Handles URLs without a scheme by adding "https://" as a default.
  2. Uses url.Parse to parse the URL and handles errors appropriately.
  3. Extracts the necessary components (registry, repository, tag, digest) from the parsed URL.
  4. Performs validations to ensure the presence of required components and logs appropriate error messages.
  5. Returns a populated reg.Images struct if all validations pass.

The function follows good practices and provides a robust way to parse and validate artifact URLs.


54-74: LGTM!

The splitRepoTagDigest helper function is well-implemented with the following highlights:

  1. Splits the path to extract the digest based on the presence of "@" symbol.
  2. If digest is not present, splits the path to extract the tag based on the presence of ":" symbol.
  3. Returns the split repository, tag, and digest components.

The function provides a clear and concise way to split the path into its constituent parts.

ground-control/internal/database/label_images.sql.go (1)

3-3: LGTM!

The sqlc version update is a good practice to stay up-to-date with the latest enhancements and bug fixes in the code generation tool.

ground-control/internal/database/images.sql.go (2)

50-50: LGTM!

The renaming of the function and the corresponding SQL constant simplifies the naming convention and better reflects the function's purpose. The changes are consistent and the function logic is correct.

Also applies to: 55-57


80-81: LGTM!

The renaming of the function and the corresponding SQL constant simplifies the naming convention and indicates a shift from a satellite-specific query to a more general image listing. The SQL query simplification enhances the functionality by allowing broader access to image data. The changes are consistent and the function logic is correct.

Also applies to: 84-113

ground-control/internal/server/routes.go (16)

15-15: LGTM!

The endpoint renaming from /registry/list to /repos/list aligns with the PR objective of enhancing functionality related to satellite registration and reflects a shift in focus from registry management to repository management.


17-17: LGTM!

The addition of the /images endpoint for adding images aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing images.


18-18: LGTM!

The addition of the /images/list endpoint for listing images aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing images.


19-19: LGTM!

The addition of the /images/{imageID} endpoint for removing images by ID aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing images.


22-22: LGTM!

The addition of the /groups endpoint for creating groups aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing groups.


23-23: LGTM!

The addition of the /groups/{groupID} endpoint for deleting groups by ID aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing groups.


24-24: LGTM!

The endpoint renaming from /group/list to /groups/list aligns with the PR objective of enhancing functionality related to satellite registration and reflects a more organized and comprehensive approach to managing groups.


25-25: LGTM!

The endpoint renaming from /group/{group} to /groups/{groupID} aligns with the PR objective of enhancing functionality related to satellite registration and reflects a more organized and comprehensive approach to managing groups.


26-26: LGTM!

The addition of the /groups/images endpoint for assigning images to groups aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing images associated with groups.


27-27: LGTM!

The addition of the /groups/images endpoint for deleting images from groups aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing images associated with groups.


28-28: LGTM!

The addition of the /groups/{groupID}/images endpoint for listing images for a specific group aligns with the PR objective of enhancing functionality related to satellite registration and introduces a new functionality for managing images associated with groups.


29-30: LGTM!

The addition of the /groups/satellite endpoints for adding and removing satellites from groups aligns with the PR objective of enhancing functionality related to satellite registration and introduces new functionalities for managing satellites associated with groups.


33-34: LGTM!

The addition of the /label/image endpoints for assigning and deleting images from labels aligns with the PR objective of enhancing functionality related to satellite registration and introduces new functionalities for managing images associated with labels.


37-38: LGTM!

The addition of the /satellites/register and /satellites/ztr endpoints for registering satellites and handling Zero Touch Registration (ZTR) aligns with the PR objective of implementing the Zero Touch Registration (ZTR) process for satellites and introduces new functionalities for satellite registration.


40-42: LGTM!

The addition of the /satellites/list, /satellites/{satellite} (GET), and /satellites/{satellite} (DELETE) endpoints for listing satellites, retrieving satellites by ID, and deleting satellites by ID aligns with the PR objective of enhancing functionality related to satellite registration and introduces new functionalities for managing satellites.


44-45: LGTM!

The addition of the /satellites/image endpoints for assigning and removing images from satellites aligns with the PR objective of enhancing functionality related to satellite registration and introduces new functionalities for managing images associated with satellites.

ground-control/internal/database/satellite_images.sql.go (3)

24-27: LGTM!

The AssignImageToSatellite function correctly implements the logic to assign an image to a satellite by inserting a record into the satellite_images table. The use of the ON CONFLICT DO NOTHING clause is a good choice to handle potential duplicates gracefully.

This function aligns with the PR objective of implementing the Zero Touch Registration (ZTR) process for satellites.


48-79: LGTM!

The GetImagesForSatellite function correctly implements the logic to retrieve all images associated with a given satellite ID. The join between the images and satellite_images tables ensures that only the relevant images are fetched.

The error handling is appropriate, covering potential errors during query execution, row scanning, and closing the rows.

This function aligns with the PR objective of enhancing the functionality related to satellite registration within the Ground Control system.


91-94: LGTM!

The RemoveImageFromSatellite function correctly implements the logic to remove the association between a satellite and an image based on their respective IDs. The delete statement uses the appropriate condition to identify the record to be removed.

This function aligns with the PR objective of enhancing the functionality related to satellite registration within the Ground Control system.

internal/store/http-fetch.go (2)

14-14: LGTM!

The import statement for the utils package is correctly formatted.


106-115: Excellent refactoring!

The changes in this code segment significantly improve the authentication mechanism:

  1. The authentication logic is encapsulated in the utils.Auth() function, making the code cleaner and more maintainable.
  2. Error handling is enhanced by checking the error returned by utils.Auth() and logging an error message if authentication fails.
  3. The auth object is directly used in the call to crane.Digest, streamlining the authentication process.

These modifications enhance the robustness, clarity, and security of the code by centralizing credential management.

ground-control/internal/database/satellites.sql.go (2)

54-57: LGTM!

The SQL query is correctly implemented to retrieve a satellite record by its ID. It selects the required columns, uses a parameter placeholder for the ID value, and limits the result to 1 row.


59-70: LGTM!

The GetSatelliteByID function is correctly implemented to execute the getSatelliteByID SQL query and return the result as a Satellite struct. It takes the necessary parameters, executes the query using QueryRowContext, and scans the result into the struct fields. The function returns the Satellite struct and any error encountered during the execution.

ground-control/internal/database/group_images.sql.go (4)

3-3: LGTM!

Updating the SQL code generator to v1.27.0 is a good practice to ensure the latest features, bug fixes, and security patches are available. The minor version update is not expected to introduce any breaking changes or compatibility issues.


16-16: LGTM!

Adding the ON CONFLICT DO NOTHING clause to the assignImageToGroup SQL query improves the robustness of the query by gracefully handling potential conflicts (e.g., duplicate key) during the insertion. This change ensures that the operation is skipped without raising an error in case of a conflict.


81-128: LGTM!

The addition of the getImagesForGroupAndSubgroups SQL query and the corresponding GetImagesForGroupAndSubgroups Go function enhances the functionality by providing a more comprehensive retrieval of images based on the group hierarchy. The recursive Common Table Expression (CTE) allows for traversing the group hierarchy and retrieving images from the specified group and its child groups.

The implementation of the Go function follows the standard pattern of executing the query, scanning the results, and returning the list of images. The error handling and resource management (e.g., closing the rows) are properly implemented.


130-143: LGTM!

The addition of the removeImageFromGroup SQL query and the corresponding RemoveImageFromGroup Go function enhances the functionality by allowing users to remove an image from a specific group. This change provides the capability to manage image associations more effectively.

The implementation of the Go function follows the standard pattern of executing the query and returning any error that occurred. The RemoveImageFromGroupParams struct provides a clear and structured way to pass the required parameters to the function.

ground-control/internal/database/groups.sql.go (8)

3-3: LGTM!

The sqlc version update is a minor change and does not affect the functionality.


15-17: LGTM!

The createGroup SQL query is correctly updated to include the parent_group_id field, which is necessary to support the hierarchical group structure.


21-24: LGTM!

The CreateGroupParams struct is correctly updated to include the ParentGroupID field of type sql.NullInt32, which is necessary to support the hierarchical group structure and handle nullable integer values in the database.


28-33: LGTM!

The CreateGroup function is correctly updated to include the ParentGroupID field in the SQL query parameters and the Group struct, which is necessary to support the hierarchical group structure.

Also applies to: 38-38


45-53: LGTM!

The new DeleteGroup function is correctly implemented to delete a group by its ID using the deleteGroup SQL query. The function takes the group ID as a parameter, executes the SQL query using ExecContext, and performs error handling by returning the error from the SQL execution.


56-56: LGTM!

The GetGroupByID, GetGroupByName, and their corresponding SQL queries are correctly updated to include the parent_group_id field in the SELECT statements and the Group struct, which is necessary to support the hierarchical group structure.

Also applies to: 66-66, 74-74, 84-84


91-124: LGTM!

The new GetSubgroups function is correctly implemented to retrieve all subgroups of a given parent group ID using the getSubgroups SQL query. The function takes the parent group ID as a parameter, executes the SQL query using QueryContext, correctly handles the database rows, scans the results into Group structs, and performs error handling for database query execution, row scanning, and closing the rows.


127-127: LGTM!

The listGroups SQL query and the ListGroups function are correctly updated to include the parent_group_id field in the SELECT statement and the Group struct, which is necessary to support the hierarchical group structure.

Also applies to: 142-142

internal/replicate/replicate.go (1)

167-170: LGTM!

The changes to the authentication process in the CopyImage function look good. Replacing the previous logic with a call to the new utils.Auth() function simplifies the code and centralizes the authentication logic in the utils package. This improves maintainability and reduces the risk of errors related to environment variable management.

The error handling for authentication is also streamlined by directly logging any errors returned by utils.Auth(), which enhances the clarity and robustness of the code.

Overall, the changes align with the provided list of alterations and maintain the core functionality of the image replication process.

ground-control/reg/satelliteState.go (4)

17-21: LGTM!

The GroupState struct is well-defined, with clear field names and appropriate serialization tags. The use of the omitempty tag for the Images field is a good practice to avoid serializing empty slices.


23-28: LGTM!

The SatelliteState struct is well-defined, with clear field names and appropriate serialization tags. The use of the omitempty tag for the Images field is a good practice to avoid serializing empty slices.


45-50: LGTM!

The State interface is well-defined, with clear method signatures for marshaling, retrieving state information, and determining the state type. The interface provides a flexible and extensible way to handle different types of states.


52-82: LGTM!

The implementation of the State interface methods for GroupState and SatelliteState is straightforward and adheres to the interface contract. The use of json.MarshalIndent provides nicely formatted JSON output, and the GetName, GetRegistry, and isGroupState methods provide convenient access to the struct fields and state type.

ground-control/internal/server/handlers.go (12)

220-230: LGTM!

The function is implemented correctly and handles errors appropriately.


232-252: LGTM!

The function is implemented correctly and handles errors appropriately.


254-279: LGTM!

The function is implemented correctly and handles errors appropriately.


335-344: LGTM!

The function is implemented correctly and handles errors appropriately.


346-364: LGTM!

The function is implemented correctly and handles errors appropriately.


366-385: LGTM!

The function is implemented correctly and handles errors appropriately.


480-500: LGTM!

The function is implemented correctly and handles errors appropriately.


560-563: LGTM!

The changes to the function are implemented correctly and handle errors appropriately.


617-640: LGTM!

The function is implemented correctly and handles errors appropriately.


642-662: LGTM!

The function is implemented correctly and handles errors appropriately.


675-688: LGTM!

The changes to the function are implemented correctly and handle errors appropriately.


699-747: Consider adding logging statements.

The function looks

ground-control/sql/schema/010_robots.sql Outdated Show resolved Hide resolved
ground-control/sql/queries/images.sql Show resolved Hide resolved
internal/state/stateArtifact.go Show resolved Hide resolved
internal/state/stateArtifact.go Show resolved Hide resolved
internal/satellite/satellite.go Show resolved Hide resolved
Signed-off-by: bupd <[email protected]>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (3)
ground-control/internal/database/models.go (3)

47-52: Consider renaming the Projects field for clarity.

The addition of the Project struct is a good way to introduce a new entity in the data model to represent projects. The field names are clear and follow Go naming conventions.

However, the Projects field name is plural, which might be confusing. Consider renaming it to Name or ProjectName for clarity.

 type Project struct {
 	ID        int32
-	Projects  string
+	Name      string
 	CreatedAt time.Time
 	UpdatedAt time.Time
 }

63-66: Consider adding timestamp fields for consistency and tracking.

The addition of the RobotProject struct is a good way to establish a relationship between robots and projects. The field names are clear and follow Go naming conventions.

However, the struct does not have any timestamp fields like CreatedAt and UpdatedAt. Consider adding them for consistency with other structs and to track the creation and modification times of the relationship.

 type RobotProject struct {
 	RobotID   int32
 	ProjectID int32
+	CreatedAt time.Time
+	UpdatedAt time.Time
 }

81-84: Consider adding timestamp fields for consistency and tracking.

The addition of the SatelliteImage struct is a good way to establish a relationship between satellites and images. The field names are clear and follow Go naming conventions.

However, similar to the RobotProject struct, consider adding CreatedAt and UpdatedAt fields for consistency with other structs and to track the creation and modification times of the relationship.

 type SatelliteImage struct {
 	SatelliteID int32
 	ImageID     int32
+	CreatedAt   time.Time
+	UpdatedAt   time.Time
 }
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f19ccad and 1963c57.

Files ignored due to path filters (1)
  • ground-control/go.sum is excluded by !**/*.sum
Files selected for processing (4)
  • ground-control/go.mod (1 hunks)
  • ground-control/internal/database/models.go (3 hunks)
  • ground-control/sql/schema/010_robots.sql (1 hunks)
  • ground-control/sql/schema/012_robot_projects.sql (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • ground-control/sql/schema/010_robots.sql
  • ground-control/sql/schema/012_robot_projects.sql
Additional comments not posted (4)
ground-control/go.mod (2)

9-9: LGTM!

The addition of the direct dependency on github.com/opencontainers/image-spec with a specific version constraint is a good practice. It ensures consistent builds and aligns with the removal of the go-containerregistry dependency, suggesting a strategic shift in the module's image-related functionality.


10-10: Verify the usage of the new oras-go dependency in the codebase.

The addition of the direct dependency on oras.land/oras-go/v2 with a specific version constraint is a good practice. It suggests that the module now interacts with container registries using the ORAS protocol, which aligns with the PR objectives related to implementing the Zero Touch Registration (ZTR) process for satellites.

To ensure that the new dependency is being used effectively, please run the following script to search for references to the oras-go package in the codebase:

Verification successful

To gain a more comprehensive understanding of how the oras-go package is being used, let's examine the actual usage of the imported packages within the satelliteState.go file.


It seems our previous search was too narrow and didn't capture the usage of the oras-go package. Let's broaden our search to find any references to oras or its subpackages within the satelliteState.go file.


The oras-go dependency is being used effectively and extensively in the codebase.

The verification process has confirmed that the oras-go package and its subpackages are being utilized properly in the ground-control/reg/satelliteState.go file. The implementation demonstrates appropriate usage of ORAS features, including:

  • File store creation and management
  • Manifest packing and handling
  • Remote repository interactions
  • Authentication and credential management
  • Artifact copying (pushing) to the registry

This usage aligns well with the PR objectives related to implementing the Zero Touch Registration (ZTR) process for satellites. The code shows a thoughtful implementation that supports both group and individual satellite states, indicating a flexible and well-designed solution.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the `oras-go` package in the codebase.

# Test: Search for import statements and references to the `oras-go` package.
# Expect: At least one import statement and multiple references to the package.
rg --type go -A 5 $'oras-go'

Length of output: 740


Script:

#!/bin/bash
# Description: Retrieve the content of the satelliteState.go file, focusing on the usage of oras-go package.

# Test: Search for functions or methods that use the oras-go package.
# Expect: To find implementations using the imported oras-go packages.
rg --type go -A 20 'func.*oras' ground-control/reg/satelliteState.go

Length of output: 70


Script:

#!/bin/bash
# Description: Search for any usage of oras-go package in satelliteState.go

# Test: Look for any references to oras or its subpackages
# Expect: To find code snippets using the oras-go package
rg --type go -A 10 -e 'oras\.' -e 'file\.' -e 'remote\.' -e 'auth\.' -e 'retry\.' ground-control/reg/satelliteState.go

Length of output: 2790

ground-control/internal/database/models.go (2)

13-17: LGTM!

The addition of the ParentGroupID field using sql.NullInt32 is a suitable way to represent optional hierarchical relationships between groups. This change enhances the data model's capability to represent nested group structures, which aligns with the PR objectives.


54-61: Securely handle the Secret field.

The addition of the Robot struct is a good way to introduce a new entity in the data model to represent robots. The field names are clear, follow Go naming conventions, and the use of sql.NullInt32 for the SatelliteID field suggests an optional association between robots and satellites.

However, the Secret field might contain sensitive information. Please ensure that it is properly encrypted and handled securely to prevent any potential leaks or unauthorized access.

@bupd
Copy link
Collaborator Author

bupd commented Sep 22, 2024

closing this PR in favor of #54

@bupd bupd closed this Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement ZTR for satellites zero touch device registration
1 participant