Skip to content

Conversation

mondragonfx
Copy link
Collaborator

@mondragonfx mondragonfx commented Jun 26, 2025

User description

Description

Add support for cloud-init based userdata from a file, needed for bootstrapping RKE2 clusters via rancher while using the vultr-docker-machine-driver

Related Issues

Checklist:

  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you linted your code locally prior to submission?
  • Have you successfully ran tests with your changes locally?

PR Type

Enhancement


Description

  • Add cloud-init userdata from file support

  • Introduce CloudConfig struct for YAML parsing

  • Add UFW disable flag and conditional logic

  • Add go-yaml dependency for cloud-init handling


Diagram Walkthrough

flowchart LR
  CLI["CLI Flags"] --> FileFlag{"--vultr-cloud-init-from-file"}
  FileFlag -->|true| ReadFile["Read file"]
  FileFlag -->|false| Base64["Use base64 string"]
  ReadFile --> ParseYAML["Parse YAML to CloudConfig"]
  ParseYAML --> DisableUFW{"--vultr-disable-ufw"}
  DisableUFW -->|true| AppendCmd["Append UFW disable"]
  DisableUFW -->|false| SkipCmd["Skip UFW disable"]
  AppendCmd --> Encode["Base64 encode"]
  SkipCmd --> Encode
  Base64 --> Encode
  Encode --> VultrAPI["Send to Vultr API"]
Loading

File Walkthrough

Relevant files
Enhancement
driver.go
Add cloud-init file support and UFW control                           

machine/driver/driver.go

  • Add CloudConfig and WriteFile structs for YAML parsing
  • Add new flags: --vultr-cloud-init-from-file and --vultr-disable-ufw
  • Implement file-based cloud-init with UFW disable logic
  • Replace hardcoded base64 with defaultCloudConfig constant
+70/-5   
Dependencies
go.mod
Add YAML parsing dependency                                                           

go.mod

  • Add github.com/goccy/go-yaml v1.18.0 dependency
+1/-0     
go.sum
Update checksums for new dependency                                           

go.sum

  • Add go-yaml module checksums
+2/-0     

@mondragonfx mondragonfx requested a review from happytreees as a code owner June 26, 2025 15:20
@mondragonfx mondragonfx requested a review from happytreees June 30, 2025 17:25
@mondragonfx mondragonfx requested a review from optik-aper July 1, 2025 16:32
Copy link

github-actions bot commented Aug 5, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

YAML Parsing Bug

The code uses strings.TrimPrefix to remove #cloud-config\n from the file content, but this only removes the first occurrence. If the file has leading whitespace or uses CRLF line endings, the prefix won't match and the YAML will fail to parse. Consider using a more robust approach to handle the cloud-config header.

cloudConfigHeader := strings.TrimPrefix(string(data), "#cloud-config\n")
var config CloudConfig
if err := yaml.Unmarshal([]byte(cloudConfigHeader), &config); err != nil {
	return fmt.Errorf("failed to unmarshal cloud config: %w", err)
}
Missing Validation

When vultr-cloud-init-from-file is true, there's no validation that the provided file path actually points to a valid cloud-init YAML file. The error handling only catches file read errors, not YAML structure validation beyond basic unmarshaling.

if cloudInitFromFile {
	data, err := os.ReadFile(cloudInitUserData)
	if err != nil {
		return fmt.Errorf("failed to read cloud-init file: %w", err)
	}
Logic Issue

The UFW disable command is only appended when both cloudInitFromFile is true AND disableUFW is true. However, when using base64 string mode (not from file), the UFW disable logic is completely bypassed regardless of the disableUFW flag value, which seems inconsistent with the flag's purpose.

} else {
	if cloudInitUserData == "" {
		cloudInitUserData = base64.StdEncoding.EncodeToString([]byte(defaultCloudConfig))
	}
	d.RequestPayloads.InstanceCreateReq.UserData = cloudInitUserData
}

Copy link

github-actions bot commented Aug 5, 2025

PR Code Suggestions ✨

No code suggestions found for the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants