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

Rework VirtualMachine, introduce cloudinit and sshKeys configuration, fix externalPorts #303

Merged
merged 3 commits into from
Aug 30, 2024

Conversation

artemrootman
Copy link
Contributor

@artemrootman artemrootman commented Aug 21, 2024

Previously, an error occurred when trying to access .Values.service.ports in the template, leading to a nil pointer dereference. This commit corrects the issue by ensuring that service.ports is checked for existence before attempting to access it, preventing the error from occurring.

Changes:

  • Updated service.yaml to use {{- with .Values.service }}...{{- end }} for conditional rendering of ports.

Summary by CodeRabbit

  • New Features

    • Introduced new parameters for enhanced virtual machine configuration: externalPorts for external access, and cloudInit for user data configuration.
    • Added a new section in the documentation for accessing virtual machines using virtctl.
    • Enhanced YAML templates to dynamically create Kubernetes Secrets for SSH keys and cloud-init data.
  • Bug Fixes

    • Improved default storage allocation for virtual machines, increasing disk size from 5Gi to 10Gi.
  • Documentation

    • Updated README to reflect new features and configuration options, improving user guidance.
  • Chores

    • Version updated from 0.3.0 to 0.4.0, indicating a new release with significant changes.

Previously, an error occurred when trying to access .Values.service.ports in the template, leading to a nil pointer dereference. This commit corrects the issue by ensuring that service.ports is checked for existence before attempting to access it, preventing the error from occurring.

Changes:
- Updated service.yaml to use `{{- with .Values.service }}...{{- end }}` for conditional rendering of ports.
@artemrootman artemrootman requested a review from kvaps as a code owner August 21, 2024 16:38
@ginn13
Copy link

ginn13 commented Aug 21, 2024

{{- if .Values.service.ports }} already tests for existence of .service.ports
The only thing is you can test for existence of .Values.service via
{{- if (.Values.service).ports }}

@artemrootman
Copy link
Contributor Author

@ginn13 is that what you mean?

ports:
    - name: ssh
      port: 22
      targetPort: 22
    {{- if (.Values.service).ports }}
    {{- range .Values.service.ports }}
    - name: {{ .name }}
      port: {{ .port }}
      targetPort: {{ .targetPort }}
    {{- end }}
    {{- end }}

- Added `user` parameter to the Helm chart with a default value of `username`.
- Updated `values.yaml` to include documentation for the new `user` parameter.
- Updated `values.schema.json` to validate the `user` parameter as a string.
- Modified `vm.yaml` template to include `user` in the cloud-init configuration.
- Fixed `service.yaml` template to correctly handle the `ports` field in `service`.
- Improved documentation in `README.md` to reflect the addition of the `user` parameter.
@@ -63,6 +63,7 @@ spec:
#cloud-config
ssh_pwauth: {{ if .Values.sshPwauth | default false }}True{{ else }}False{{ end }}
disable_root: {{ if .Values.disableRoot | default false }}True{{ else }}False{{ end }}
user: {{ .Values.user }}
Copy link
Member

Choose a reason for hiding this comment

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

Let's make these parameterns optional:

Suggested change
user: {{ .Values.user }}
{{- with .Values.user }}
user: {{ . }}
{{- end }}

Or we can simple map cloud-config as parameter in values.yaml as is

@@ -18,7 +18,7 @@ spec:
- name: ssh
port: 22
targetPort: 22
{{- if .Values.service.ports }}
{{- if (.Values.service).ports }}
Copy link
Member

Choose a reason for hiding this comment

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

Does it really makes sense?

Comment on lines 25 to 29
sshPwauth: true
disableRoot: true
user: username
password: hackme
chpasswdExpire: false
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about using common cloudInit config parameter for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that what you mean?

cloudInit:
  ssh_pwauth: true
  disable_root: true
  user: username
...

Copy link
Member

@kvaps kvaps Aug 22, 2024

Choose a reason for hiding this comment

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

It might be better to pass all cloud-init configuration as a string:

cloudInitUserConfig: |
  #cloud-config
  ssh_pwauth: true
  disable_root: true
  user: username

due to fact user configuration have no formalized spec, it can include multiple header, scripts and even jinja2 templates
https://cloudinit.readthedocs.io/en/latest/explanation/instancedata.html#example-cloud-config-with-instance-data

variuos vendors can define their own format for this, eg. Talos Linux allows to write machine-config in user-data

This config should be saved into external secret and included with secretRef into KubeVirt VM:

    - name: cloudinitdisk
      cloudInitNoCloud:
        secretRef:
          name: my-vmi-secret

As about sshKeys, cloud-init defines formalized spec for this in meta-data:
https://cloudinit.readthedocs.io/en/latest/explanation/instancedata.html#v1-public-ssh-keys

KubeVirt allows to specify them as

      accessCredentials:
      - sshPublicKey:
          source:
            secret:
              secretName: my-pub-key
          propagationMethod:
            noCloud: {}

details:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What difference would it make? We'd have to create a secret anyway.

Signed-off-by: Andrei Kvapil <[email protected]>
Copy link
Contributor

coderabbitai bot commented Aug 30, 2024

Walkthrough

The updates to the virtual machine application include a version increment from 0.3.0 to 0.4.0, enhancements to the README documentation, and the introduction of new configuration parameters such as externalPorts and cloudInit. Additionally, new YAML templates for Kubernetes Secrets and modifications to the service and VM templates were made to improve flexibility and security in managing virtual machines. The JSON schema and values files were also updated to reflect these changes.

Changes

Files Change Summary
packages/apps/virtual-machine/Chart.yaml Version updated from 0.3.0 to 0.4.0.
packages/apps/virtual-machine/README.md Added "Accessing virtual machine" section; introduced externalPorts, updated sshKeys, increased disk size, added cloudInit, and modified examples.
packages/apps/virtual-machine/templates/secret.yaml New template for creating Kubernetes Secrets for SSH keys and cloud-init data.
packages/apps/virtual-machine/templates/service.yaml Changed service port configuration to use externalPorts for flexibility.
packages/apps/virtual-machine/templates/vm.yaml Added conditional logic for SSH keys and cloud-init, introduced accessCredentials, and updated volumes and networks sections.
packages/apps/virtual-machine/values.schema.json Added externalPorts and cloudInit; removed several SSH-related properties; updated sshKeys default.
packages/apps/virtual-machine/values.yaml Introduced externalPorts and cloudInit; modified sshKeys to be an empty array; removed legacy SSH parameters.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant VM_Config
    participant Kubernetes

    User->>VM_Config: Specify externalPorts and cloudInit
    VM_Config->>Kubernetes: Generate VM with new configurations
    Kubernetes->>Kubernetes: Create Secrets for SSH keys and cloud-init
    Kubernetes->>User: Provision virtual machine with access methods
Loading

🐰 "In fields of code, I hop and play,
New ports and clouds brighten my day!
With secrets safe and access clear,
My virtual friends are finally here!
Let's leap through changes, one and all,
In this techy world, we stand tall!" 🐇✨


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.

@kvaps kvaps changed the title Fix service.yaml template error: handle nil values for service.ports Rework VirtualMachine, introduce cloudinit and sshKeys configuration, fix externalPorts Aug 30, 2024
@kvaps kvaps merged commit 4282843 into aenix-io:main Aug 30, 2024
Copy link
Contributor

@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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6c5cf5b and c47a6a2.

Files selected for processing (7)
  • packages/apps/virtual-machine/Chart.yaml (1 hunks)
  • packages/apps/virtual-machine/README.md (1 hunks)
  • packages/apps/virtual-machine/templates/secret.yaml (1 hunks)
  • packages/apps/virtual-machine/templates/service.yaml (1 hunks)
  • packages/apps/virtual-machine/templates/vm.yaml (1 hunks)
  • packages/apps/virtual-machine/values.schema.json (2 hunks)
  • packages/apps/virtual-machine/values.yaml (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/apps/virtual-machine/Chart.yaml
Additional context used
yamllint
packages/apps/virtual-machine/templates/secret.yaml

[error] 1-1: syntax error: expected the node content, but found '-'

(syntax)


[error] 9-9: trailing spaces

(trailing-spaces)

LanguageTool
packages/apps/virtual-machine/README.md

[style] ~49-~49: To form a complete sentence, be sure to include a subject or ‘there’.
Context: ... of SSH public keys for authentication. Can be a single key or a list of keys. ...

(MISSING_IT_THERE)

Markdownlint
packages/apps/virtual-machine/README.md

55-55: Punctuation: ':'
Trailing punctuation in heading

(MD026, no-trailing-punctuation)


19-19: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


25-25: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


31-31: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


50-50: Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe
Table pipe style

(MD055, table-pipe-style)


51-51: Expected: leading_and_trailing; Actual: trailing_only; Missing leading pipe
Table pipe style

(MD055, table-pipe-style)


51-51: Expected: 3; Actual: 1; Too few cells, row will be missing data
Table column count

(MD056, table-column-count)

Additional comments not posted (13)
packages/apps/virtual-machine/templates/secret.yaml (1)

12-21: Review and approve the cloud-init secret configuration.

The template correctly creates a Kubernetes secret for cloud-init configurations if .Values.cloudInit is provided. The use of nindent to properly indent the cloud-init data is a good practice, ensuring that the YAML structure within the secret is correctly formatted.

The cloud-init secret configuration is correctly implemented and follows best practices for Helm charts.

packages/apps/virtual-machine/templates/service.yaml (1)

16-19: Approve the dynamic port configuration in the service template.

The new implementation using .Values.externalPorts for dynamic port configuration enhances flexibility and reduces the risk of errors by not relying on a potentially undefined .Values.service.ports. This approach allows for specifying any number of external ports directly, which is a significant improvement over the previous static method.

The changes to the service port configuration are correctly implemented and improve the template's flexibility and robustness.

packages/apps/virtual-machine/values.yaml (2)

4-15: Approve the addition of externalPorts and its documentation.

The addition of externalPorts as a new configuration parameter allows users to specify which ports should be forwarded from outside the cluster. This is a significant enhancement for external access configurations, providing flexibility in network setups. The documentation is clear and provides sufficient detail about its usage.

The implementation and documentation of externalPorts are correctly handled and meet the objectives of enhancing VM configuration flexibility.


23-43: Review and approve the changes to sshKeys and the addition of cloudInit.

The modification of sshKeys to be an empty array by default is a prudent security measure, encouraging users to explicitly define their SSH keys rather than relying on potentially insecure defaults. The addition of cloudInit with detailed documentation and examples provides users with the ability to customize VM instances extensively. This is aligned with best practices for cloud-native applications.

The changes to sshKeys and the addition of cloudInit are well-implemented, enhancing security and customization capabilities for VM deployments.

packages/apps/virtual-machine/values.schema.json (2)

62-63: Update to sshKeys default value

The default value for sshKeys has been updated to an empty array, which is a sensible default for cases where no SSH keys are provided. This change is correctly implemented.

The code changes are approved.


68-71: Addition of cloudInit property

The cloudInit property has been added to allow users to specify cloud-init user data. The property is correctly defined as a string with a sensible default value. This addition enhances the flexibility of VM initialization.

The code changes are approved.

packages/apps/virtual-machine/templates/vm.yaml (4)

48-52: Conditional inclusion of cloudinitdisk

The conditional logic to include cloudinitdisk based on the presence of .Values.sshKeys or .Values.cloudInit is correctly implemented. This change ensures that resources are allocated dynamically based on configuration.

The code changes are approved.


61-69: New accessCredentials structure

The new structure for accessCredentials to source SSH public keys from a Kubernetes secret is a significant improvement in terms of security and manageability. The implementation is correct and aligns with best practices for handling credentials in Kubernetes.

The code changes are approved.


72-80: Modified volumes section for cloudinitdisk

The modification to nest the cloudInitNoCloud configuration under cloudinitdisk is a good practice, ensuring that cloud-init settings are applied only when necessary. The implementation is correct and enhances the template's flexibility.

The code changes are approved.


81-83: Addition of networks section

The addition of a networks section with a default network configuration is a valuable enhancement, improving the networking capabilities of the virtual machine. The implementation is straightforward and correctly done.

The code changes are approved.

packages/apps/virtual-machine/README.md (3)

12-33: New section on accessing the virtual machine

The new section provides detailed instructions on accessing the virtual machine using the virtctl tool. This addition significantly enhances user guidance and is well-documented.

The code changes are approved.

Tools
Markdownlint

19-19: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


25-25: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


31-31: null
Fenced code blocks should have a language specified

(MD040, fenced-code-language)


39-51: Updated Parameters section

The Parameters section has been updated with new and modified parameters, including externalPorts and cloudInit. These changes are well-documented and provide clear guidance on the new configuration options.

The code changes are approved.

Tools
LanguageTool

[style] ~49-~49: To form a complete sentence, be sure to include a subject or ‘there’.
Context: ... of SSH public keys for authentication. Can be a single key or a list of keys. ...

(MISSING_IT_THERE)

Markdownlint

50-50: Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe
Table pipe style

(MD055, table-pipe-style)


51-51: Expected: leading_and_trailing; Actual: trailing_only; Missing leading pipe
Table pipe style

(MD055, table-pipe-style)


51-51: Expected: 3; Actual: 1; Too few cells, row will be missing data
Table column count

(MD056, table-column-count)


55-79: Example virtual machine configuration

The example configuration has been updated to reflect the new parameters and changes in default values. This update is crucial for users to understand how to apply the new settings effectively.

The code changes are approved.

Tools
Markdownlint

55-55: Punctuation: ':'
Trailing punctuation in heading

(MD026, no-trailing-punctuation)

Comment on lines +1 to +11
{{- if .Values.sshKeys }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ include "virtual-machine.fullname" $ }}-ssh-keys
stringData:
{{- range $k, $v := .Values.sshKeys }}
key{{ $k }}: {{ quote $v }}
{{- end }}
{{- end }}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper Helm syntax and address yamllint errors.

The Helm syntax error flagged by yamllint on line 1 might be a false positive, as the Helm templating syntax often conflicts with standard YAML linters. However, the trailing spaces on line 9 should be removed to adhere to best practices.

To address the yamllint errors, consider the following changes:

-  key{{ $k }}: {{ quote $v }} 
+  key{{ $k }}: {{ quote $v }}

Additionally, ensure that the Helm syntax is correctly interpreted in your CI/CD pipeline by using a Helm-specific linter or testing the template deployment.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{{- if .Values.sshKeys }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ include "virtual-machine.fullname" $ }}-ssh-keys
stringData:
{{- range $k, $v := .Values.sshKeys }}
key{{ $k }}: {{ quote $v }}
{{- end }}
{{- end }}
{{- if .Values.sshKeys }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ include "virtual-machine.fullname" $ }}-ssh-keys
stringData:
{{- range $k, $v := .Values.sshKeys }}
key{{ $k }}: {{ quote $v }}
{{- end }}
{{- end }}
Tools
yamllint

[error] 1-1: syntax error: expected the node content, but found '-'

(syntax)


[error] 9-9: trailing spaces

(trailing-spaces)

Comment on lines +10 to +17
"externalPorts": {
"type": "array",
"description": "Specify ports to forward from outside the cluster",
"default": "[]",
"items": {
"type": "string"
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Addition of externalPorts property

The new externalPorts property allows users to specify ports for external access. The schema is correctly defined as an array of strings. However, the default value should be an array, not a string representation of an array.

Change the default value from "[]" to [] to correctly represent an empty array in JSON:

- "default": "[]",
+ "default": [],
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"externalPorts": {
"type": "array",
"description": "Specify ports to forward from outside the cluster",
"default": "[]",
"items": {
"type": "string"
}
},
"externalPorts": {
"type": "array",
"description": "Specify ports to forward from outside the cluster",
"default": [],
"items": {
"type": "string"
}
},

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.

3 participants