Skip to content

Commit

Permalink
feat: added user-data flags & processing
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Case <[email protected]>
  • Loading branch information
richardcase committed Apr 7, 2022
1 parent 1786792 commit ca67369
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 56 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: release

on:
push:
tags:
- "v*.*.*"

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17.x
- name: Setup Syft
uses: anchore/sbom-action/download-syft@v0
- name: Generate release notes
run: |
gh api repos/{owner}/{repo}/releases/generate-notes -F tag_name=${{ github.ref }} --jq .body > ./changelog.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
version: latest
args: release --release-notes=./changelog.md --skip-validate
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
# vendor/

.vscode/

dist/
36 changes: 36 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
before:
hooks:
- go mod tidy -compat=1.17
- go mod download
builds:
- env:
- CGO_ENABLED=0
main: ./cmd/fl
goos:
- linux
- darwin
goarch:
- amd64
- arm64
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
sboms:
- artifacts: archive
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
release:
draft: true
prerelease: auto
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
3 changes: 3 additions & 0 deletions cmd/fl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"log"
"math/rand"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -11,6 +13,7 @@ import (
)

func main() {
rand.Seed(time.Now().UnixNano())
ctx := context.Background()

cobra.OnInitialize(initConfig)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.10.1
github.com/weaveworks/flintlock/api v0.0.0-20220304105853-8fcb8aa2bafb
github.com/weaveworks/flintlock/client v0.0.0-20220304105853-8fcb8aa2bafb
github.com/yitsushi/macpot v1.0.2
go.uber.org/zap v1.21.0
google.golang.org/grpc v1.44.0
Expand All @@ -27,7 +28,6 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/weaveworks/flintlock/client v0.0.0-20220304105853-8fcb8aa2bafb // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
Expand Down
7 changes: 5 additions & 2 deletions internal/cmd/microvm/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ func newCreateCommand() *cobra.Command {
cmd.Flags().StringVar(&createInput.InitrdFilename, "initrd-filename", "", "name of the file in the image to use for the initial ramdisk")
cmd.Flags().StringSliceVar(&createInput.NetworkInterfaces, "network-interface", nil, "specify the network interfaces to attach. In the following format: name:type:[macaddress]:[ipaddress]")
cmd.Flags().StringSliceVar(&createInput.MetadataFromFile, "metadata-from-file", nil, "specify metadata to be available to your microvm. In the following format key=pathtofile")
cmd.Flags().StringVar(&createInput.Hostname, "hostname", "", "the hostname of the the microvm")
cmd.Flags().StringVar(&createInput.SSHKeyFile, "ssh-key-file", "", "an ssh key to use")
cmd.Flags().StringVar(&createInput.Metadata.Hostname, "metadata-hostname", "", "the hostname of the the microvm")
cmd.Flags().StringVar(&createInput.Metadata.SSHKeyFile, "metadata-ssh-key-file", "", "an ssh key to use")
cmd.Flags().BoolVar(&createInput.Metadata.ResolvdFix, "metadata-resolvd-fix", true, "include a systemd-resolvd fix for container root volumes")
cmd.Flags().StringVar(&createInput.Metadata.Message, "metadata-final-message", "", "set the cloud-init final message")

//TODO: additional command line args for kernel
//TODO: add additional volumes

Expand Down
79 changes: 37 additions & 42 deletions pkg/app/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ import (
"github.com/weaveworks/flintlock/client/cloudinit/userdata"
)

type CreateInput struct {
Host string
Name string
Namespace string
VCPU int
MemoryInMb int
KernelImage string
KernelAddNetConf bool
KernelFileName string
RootImage string
InitrdImage string
InitrdFilename string
NetworkInterfaces []string
MetadataFromFile []string
Hostname string
SSHKeyFile string
}

func (a *app) Create(ctx context.Context, input *CreateInput) error {
a.logger.Debug("creating a microvm")

Expand All @@ -41,21 +23,11 @@ func (a *app) Create(ctx context.Context, input *CreateInput) error {
return fmt.Errorf("creating request: %w", err)
}

sshKey := ""
if input.SSHKeyFile != "" {
data, err := os.ReadFile(input.SSHKeyFile)
if err != nil {
return fmt.Errorf("reading ssh key file %s: %w", input.SSHKeyFile, err)
if !input.Metadata.IsEmpty() {
if metaErr := a.addUserdata(spec, input); metaErr != nil {
return fmt.Errorf("adding user-data: %w", metaErr)
}
sshKey = string(data)
}

vendorData, err := a.createVendorData(input.Hostname, sshKey)
if err != nil {
return fmt.Errorf("creating vendor data for microvm: %w", err)
}
fmt.Println(vendorData)
spec.Metadata["vendor-data"] = vendorData

client, err := a.createFlintlockClient(input.Host)
if err != nil {
Expand All @@ -76,6 +48,16 @@ func (a *app) Create(ctx context.Context, input *CreateInput) error {
return nil
}

func (a *app) addUserdata(spec *flintlocktypes.MicroVMSpec, input *CreateInput) error {
userData, err := a.createUserData(input.Metadata)
if err != nil {
return fmt.Errorf("creating user-data for microvm: %w", err)
}
spec.Metadata["user-data"] = userData

return nil
}

//TODO: this whole thing needs rewriting
func (a *app) convertCreateInputToReq(input *CreateInput) (*flintlocktypes.MicroVMSpec, error) {
req := &flintlocktypes.MicroVMSpec{
Expand Down Expand Up @@ -181,18 +163,32 @@ func (a *app) convertCreateInputToReq(input *CreateInput) (*flintlocktypes.Micro
return req, nil
}

func (a *app) createVendorData(hostname, sshKey string) (string, error) {
vendorUserdata := &userdata.UserData{
func (a *app) createUserData(metadata Metadata) (string, error) {
userMetadata := &userdata.UserData{
FinalMessage: "The fl booted system is good to go after $UPTIME seconds",
BootCommands: []string{
}

if metadata.Message != "" {
userMetadata.FinalMessage = metadata.Message
}

if metadata.ResolvdFix {
userMetadata.BootCommands = []string{
"ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf",
},
}
}
if hostname != "" {
vendorUserdata.HostName = hostname

if metadata.Hostname != "" {
userMetadata.HostName = metadata.Hostname
}

if sshKey != "" {
if metadata.SSHKeyFile != "" {
data, err := os.ReadFile(metadata.SSHKeyFile)
if err != nil {
return "", fmt.Errorf("reading ssh key file %s: %w", metadata.SSHKeyFile, err)
}
sshKey := string(data)

defaultUser := userdata.User{
Name: "ubuntu",
}
Expand All @@ -207,15 +203,14 @@ func (a *app) createVendorData(hostname, sshKey string) (string, error) {
sshKey,
}

vendorUserdata.Users = []userdata.User{defaultUser, rootUser}
userMetadata.Users = []userdata.User{defaultUser, rootUser}
}

data, err := yaml.Marshal(vendorUserdata)
data, err := yaml.Marshal(userMetadata)
if err != nil {
return "", fmt.Errorf("marshalling bootstrap data: %w", err)
}

dataWithHeader := append([]byte("#cloud-config\n"), data...)
dataWithHeader := append([]byte("## template: jinja\n#cloud-config\n\n"), data...)

return base64.StdEncoding.EncodeToString(dataWithHeader), nil
}
5 changes: 0 additions & 5 deletions pkg/app/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import (
flintlockv1 "github.com/weaveworks/flintlock/api/services/microvm/v1alpha1"
)

type DeleteInput struct {
Host string
UID string
}

func (a *app) Delete(ctx context.Context, input *DeleteInput) error {
a.logger.Debugw("deleting microvm", "uid", input.UID, "host", input.Host)

Expand Down
6 changes: 0 additions & 6 deletions pkg/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import (
"gopkg.in/yaml.v2"
)

type GetInput struct {
Host string
Namespace string
UID string
}

func (a *app) Get(ctx context.Context, input *GetInput) error {
if input.UID == "" {
a.logger.Debugw("getting all microvms", "host", input.Host)
Expand Down
53 changes: 53 additions & 0 deletions pkg/app/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package app

type CreateInput struct {
Host string
Name string
Namespace string
VCPU int
MemoryInMb int
KernelImage string
KernelAddNetConf bool
KernelFileName string
RootImage string
InitrdImage string
InitrdFilename string
NetworkInterfaces []string
MetadataFromFile []string
Metadata Metadata
}

type Metadata struct {
Hostname string
SSHKeyFile string
ResolvdFix bool
Message string
}

func (m Metadata) IsEmpty() bool {
if m.Hostname != "" {
return false
}
if m.SSHKeyFile != "" {
return false
}
if m.Message != "" {
return false
}
if m.ResolvdFix {
return false
}

return true
}

type GetInput struct {
Host string
Namespace string
UID string
}

type DeleteInput struct {
Host string
UID string
}

0 comments on commit ca67369

Please sign in to comment.