Skip to content

Commit ca67369

Browse files
committed
feat: added user-data flags & processing
Signed-off-by: Richard Case <[email protected]>
1 parent 1786792 commit ca67369

File tree

10 files changed

+175
-56
lines changed

10 files changed

+175
-56
lines changed

.github/workflows/release.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*.*.*"
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
goreleaser:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
with:
18+
fetch-depth: 0
19+
- name: Setup Go
20+
uses: actions/setup-go@v2
21+
with:
22+
go-version: 1.17.x
23+
- name: Setup Syft
24+
uses: anchore/sbom-action/download-syft@v0
25+
- name: Generate release notes
26+
run: |
27+
gh api repos/{owner}/{repo}/releases/generate-notes -F tag_name=${{ github.ref }} --jq .body > ./changelog.md
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
OWNER: ${{ github.repository_owner }}
31+
REPO: ${{ github.event.repository.name }}
32+
- name: Run GoReleaser
33+
uses: goreleaser/goreleaser-action@v1
34+
with:
35+
version: latest
36+
args: release --release-notes=./changelog.md --skip-validate
37+
env:
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
# vendor/
1616

1717
.vscode/
18+
19+
dist/

.goreleaser.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
before:
2+
hooks:
3+
- go mod tidy -compat=1.17
4+
- go mod download
5+
builds:
6+
- env:
7+
- CGO_ENABLED=0
8+
main: ./cmd/fl
9+
goos:
10+
- linux
11+
- darwin
12+
goarch:
13+
- amd64
14+
- arm64
15+
archives:
16+
- replacements:
17+
darwin: Darwin
18+
linux: Linux
19+
windows: Windows
20+
386: i386
21+
amd64: x86_64
22+
sboms:
23+
- artifacts: archive
24+
checksum:
25+
name_template: 'checksums.txt'
26+
snapshot:
27+
name_template: "{{ incpatch .Version }}-next"
28+
release:
29+
draft: true
30+
prerelease: auto
31+
changelog:
32+
sort: asc
33+
filters:
34+
exclude:
35+
- '^docs:'
36+
- '^test:'

cmd/fl/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"context"
55
"log"
6+
"math/rand"
7+
"time"
68

79
"github.com/spf13/cobra"
810
"github.com/spf13/viper"
@@ -11,6 +13,7 @@ import (
1113
)
1214

1315
func main() {
16+
rand.Seed(time.Now().UnixNano())
1417
ctx := context.Background()
1518

1619
cobra.OnInitialize(initConfig)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/spf13/pflag v1.0.5
99
github.com/spf13/viper v1.10.1
1010
github.com/weaveworks/flintlock/api v0.0.0-20220304105853-8fcb8aa2bafb
11+
github.com/weaveworks/flintlock/client v0.0.0-20220304105853-8fcb8aa2bafb
1112
github.com/yitsushi/macpot v1.0.2
1213
go.uber.org/zap v1.21.0
1314
google.golang.org/grpc v1.44.0
@@ -27,7 +28,6 @@ require (
2728
github.com/spf13/cast v1.4.1 // indirect
2829
github.com/spf13/jwalterweatherman v1.1.0 // indirect
2930
github.com/subosito/gotenv v1.2.0 // indirect
30-
github.com/weaveworks/flintlock/client v0.0.0-20220304105853-8fcb8aa2bafb // indirect
3131
go.uber.org/atomic v1.9.0 // indirect
3232
go.uber.org/multierr v1.8.0 // indirect
3333
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect

internal/cmd/microvm/create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ func newCreateCommand() *cobra.Command {
5151
cmd.Flags().StringVar(&createInput.InitrdFilename, "initrd-filename", "", "name of the file in the image to use for the initial ramdisk")
5252
cmd.Flags().StringSliceVar(&createInput.NetworkInterfaces, "network-interface", nil, "specify the network interfaces to attach. In the following format: name:type:[macaddress]:[ipaddress]")
5353
cmd.Flags().StringSliceVar(&createInput.MetadataFromFile, "metadata-from-file", nil, "specify metadata to be available to your microvm. In the following format key=pathtofile")
54-
cmd.Flags().StringVar(&createInput.Hostname, "hostname", "", "the hostname of the the microvm")
55-
cmd.Flags().StringVar(&createInput.SSHKeyFile, "ssh-key-file", "", "an ssh key to use")
54+
cmd.Flags().StringVar(&createInput.Metadata.Hostname, "metadata-hostname", "", "the hostname of the the microvm")
55+
cmd.Flags().StringVar(&createInput.Metadata.SSHKeyFile, "metadata-ssh-key-file", "", "an ssh key to use")
56+
cmd.Flags().BoolVar(&createInput.Metadata.ResolvdFix, "metadata-resolvd-fix", true, "include a systemd-resolvd fix for container root volumes")
57+
cmd.Flags().StringVar(&createInput.Metadata.Message, "metadata-final-message", "", "set the cloud-init final message")
58+
5659
//TODO: additional command line args for kernel
5760
//TODO: add additional volumes
5861

pkg/app/create.go

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@ import (
1515
"github.com/weaveworks/flintlock/client/cloudinit/userdata"
1616
)
1717

18-
type CreateInput struct {
19-
Host string
20-
Name string
21-
Namespace string
22-
VCPU int
23-
MemoryInMb int
24-
KernelImage string
25-
KernelAddNetConf bool
26-
KernelFileName string
27-
RootImage string
28-
InitrdImage string
29-
InitrdFilename string
30-
NetworkInterfaces []string
31-
MetadataFromFile []string
32-
Hostname string
33-
SSHKeyFile string
34-
}
35-
3618
func (a *app) Create(ctx context.Context, input *CreateInput) error {
3719
a.logger.Debug("creating a microvm")
3820

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

44-
sshKey := ""
45-
if input.SSHKeyFile != "" {
46-
data, err := os.ReadFile(input.SSHKeyFile)
47-
if err != nil {
48-
return fmt.Errorf("reading ssh key file %s: %w", input.SSHKeyFile, err)
26+
if !input.Metadata.IsEmpty() {
27+
if metaErr := a.addUserdata(spec, input); metaErr != nil {
28+
return fmt.Errorf("adding user-data: %w", metaErr)
4929
}
50-
sshKey = string(data)
51-
}
52-
53-
vendorData, err := a.createVendorData(input.Hostname, sshKey)
54-
if err != nil {
55-
return fmt.Errorf("creating vendor data for microvm: %w", err)
5630
}
57-
fmt.Println(vendorData)
58-
spec.Metadata["vendor-data"] = vendorData
5931

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

51+
func (a *app) addUserdata(spec *flintlocktypes.MicroVMSpec, input *CreateInput) error {
52+
userData, err := a.createUserData(input.Metadata)
53+
if err != nil {
54+
return fmt.Errorf("creating user-data for microvm: %w", err)
55+
}
56+
spec.Metadata["user-data"] = userData
57+
58+
return nil
59+
}
60+
7961
//TODO: this whole thing needs rewriting
8062
func (a *app) convertCreateInputToReq(input *CreateInput) (*flintlocktypes.MicroVMSpec, error) {
8163
req := &flintlocktypes.MicroVMSpec{
@@ -181,18 +163,32 @@ func (a *app) convertCreateInputToReq(input *CreateInput) (*flintlocktypes.Micro
181163
return req, nil
182164
}
183165

184-
func (a *app) createVendorData(hostname, sshKey string) (string, error) {
185-
vendorUserdata := &userdata.UserData{
166+
func (a *app) createUserData(metadata Metadata) (string, error) {
167+
userMetadata := &userdata.UserData{
186168
FinalMessage: "The fl booted system is good to go after $UPTIME seconds",
187-
BootCommands: []string{
169+
}
170+
171+
if metadata.Message != "" {
172+
userMetadata.FinalMessage = metadata.Message
173+
}
174+
175+
if metadata.ResolvdFix {
176+
userMetadata.BootCommands = []string{
188177
"ln -sf /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf",
189-
},
178+
}
190179
}
191-
if hostname != "" {
192-
vendorUserdata.HostName = hostname
180+
181+
if metadata.Hostname != "" {
182+
userMetadata.HostName = metadata.Hostname
193183
}
194184

195-
if sshKey != "" {
185+
if metadata.SSHKeyFile != "" {
186+
data, err := os.ReadFile(metadata.SSHKeyFile)
187+
if err != nil {
188+
return "", fmt.Errorf("reading ssh key file %s: %w", metadata.SSHKeyFile, err)
189+
}
190+
sshKey := string(data)
191+
196192
defaultUser := userdata.User{
197193
Name: "ubuntu",
198194
}
@@ -207,15 +203,14 @@ func (a *app) createVendorData(hostname, sshKey string) (string, error) {
207203
sshKey,
208204
}
209205

210-
vendorUserdata.Users = []userdata.User{defaultUser, rootUser}
206+
userMetadata.Users = []userdata.User{defaultUser, rootUser}
211207
}
212208

213-
data, err := yaml.Marshal(vendorUserdata)
209+
data, err := yaml.Marshal(userMetadata)
214210
if err != nil {
215211
return "", fmt.Errorf("marshalling bootstrap data: %w", err)
216212
}
217-
218-
dataWithHeader := append([]byte("#cloud-config\n"), data...)
213+
dataWithHeader := append([]byte("## template: jinja\n#cloud-config\n\n"), data...)
219214

220215
return base64.StdEncoding.EncodeToString(dataWithHeader), nil
221216
}

pkg/app/delete.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import (
77
flintlockv1 "github.com/weaveworks/flintlock/api/services/microvm/v1alpha1"
88
)
99

10-
type DeleteInput struct {
11-
Host string
12-
UID string
13-
}
14-
1510
func (a *app) Delete(ctx context.Context, input *DeleteInput) error {
1611
a.logger.Debugw("deleting microvm", "uid", input.UID, "host", input.Host)
1712

pkg/app/get.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ import (
99
"gopkg.in/yaml.v2"
1010
)
1111

12-
type GetInput struct {
13-
Host string
14-
Namespace string
15-
UID string
16-
}
17-
1812
func (a *app) Get(ctx context.Context, input *GetInput) error {
1913
if input.UID == "" {
2014
a.logger.Debugw("getting all microvms", "host", input.Host)

pkg/app/types.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package app
2+
3+
type CreateInput struct {
4+
Host string
5+
Name string
6+
Namespace string
7+
VCPU int
8+
MemoryInMb int
9+
KernelImage string
10+
KernelAddNetConf bool
11+
KernelFileName string
12+
RootImage string
13+
InitrdImage string
14+
InitrdFilename string
15+
NetworkInterfaces []string
16+
MetadataFromFile []string
17+
Metadata Metadata
18+
}
19+
20+
type Metadata struct {
21+
Hostname string
22+
SSHKeyFile string
23+
ResolvdFix bool
24+
Message string
25+
}
26+
27+
func (m Metadata) IsEmpty() bool {
28+
if m.Hostname != "" {
29+
return false
30+
}
31+
if m.SSHKeyFile != "" {
32+
return false
33+
}
34+
if m.Message != "" {
35+
return false
36+
}
37+
if m.ResolvdFix {
38+
return false
39+
}
40+
41+
return true
42+
}
43+
44+
type GetInput struct {
45+
Host string
46+
Namespace string
47+
UID string
48+
}
49+
50+
type DeleteInput struct {
51+
Host string
52+
UID string
53+
}

0 commit comments

Comments
 (0)