Skip to content

Commit

Permalink
Add flag -use-machined for Talos system extension
Browse files Browse the repository at this point in the history
Flag '-use-machined' switches from using the TALOS_CONFIG_PATH and
TALOS_HOST, to the Talos constants.MachineSocketPath at
/system/run/machined/machine.sock. This is in support of creating a
Talos system extension docker image to run talos-vmtoolsd as early in
the boot process as possible.

Thanks to @flyik for the code inspiration.
  • Loading branch information
Brandon Nason authored and bnason committed Mar 27, 2024
1 parent 8b7ba37 commit 347c1b7
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 94 deletions.
21 changes: 16 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
# For development only.
# This Makefile is not being used by Dockerfile or GitHub actions.

REGISTRY ?= ghcr.io
USERNAME ?= sidereolabs
SHA ?= $(shell git describe --match=none --always --abbrev=8 --dirty)
TAG ?= $(shell git describe --tag --always --dirty --match v[0-9]\*)
ABBREV_TAG ?= $(shell git describe --tag --always --match v[0-9]\* --abbrev=0 )
TAG_SUFFIX ?=
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct)
IMAGE_REGISTRY ?= $(REGISTRY)
IMAGE_NAME = talos-vmtoolsd
IMAGE_TAG ?= $(TAG)$(TAG_SUFFIX)
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
REGISTRY_AND_USERNAME := $(IMAGE_REGISTRY)/$(USERNAME)

talos-vmtoolsd:
go build -ldflags="-s -w" -trimpath -o $@ ./cmd/$@

docker-build:
docker buildx build . --tag talos-vmtoolsd:$(SHA) --file Dockerfile
docker buildx build . --tag $(REGISTRY_AND_USERNAME)/${IMAGE_NAME}:$(IMAGE_TAG) --file Dockerfile

docker-build-extension:
docker buildx build . --tag $(REGISTRY_AND_USERNAME)/${IMAGE_NAME}-system-extension:$(IMAGE_TAG) --file system-extension/Dockerfile

.PHONY: talos-vmtoolsd docker-build
.PHONY: talos-vmtoolsd docker-build docker-build-extension
52 changes: 35 additions & 17 deletions cmd/talos-vmtoolsd/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"

vmtoolsd "github.com/siderolabs/talos-vmtoolsd"
"github.com/siderolabs/talos-vmtoolsd/internal/nanotoolbox"
"github.com/siderolabs/talos-vmtoolsd/internal/talosapi"
"github.com/siderolabs/talos-vmtoolsd/internal/tboxcmds"
"github.com/sirupsen/logrus"
vmguestmsg "github.com/vmware/vmw-guestinfo/message"
"github.com/vmware/vmw-guestinfo/vmcheck"
"os"
"os/signal"
"syscall"
)

func main() {
Expand All @@ -23,8 +25,10 @@ func main() {
})

// Debug flags
talosTestQuery := ""
var talosTestQuery string
var useMachinedSocket bool
flag.StringVar(&talosTestQuery, "test-apid-query", "", "query apid")
flag.BoolVar(&useMachinedSocket, "use-machined", false, "use machined unix socket")
flag.Parse()

// Apply log level, default to "info"
Expand Down Expand Up @@ -52,21 +56,34 @@ func main() {
select {}
}

// Our spec file passes the secret path and K8s host IP via env vars.
configPath := os.Getenv("TALOS_CONFIG_PATH")
if len(configPath) == 0 {
l.Fatal("error: TALOS_CONFIG_PATH is a required path to a Talos configuration file")
}
k8sHost := os.Getenv("TALOS_HOST")
if len(k8sHost) == 0 {
l.Fatal("error: TALOS_HOST is required to point to a node's internal IP")
}
ctx, ctxCancel := context.WithCancel(context.Background())

var api *talosapi.LocalClient
var err error
if !useMachinedSocket {
// Our spec file passes the secret path and K8s host IP via env vars.
configPath := os.Getenv("TALOS_CONFIG_PATH")
if len(configPath) == 0 {
l.Fatal("error: TALOS_CONFIG_PATH is a required path to a Talos configuration file")
}
k8sHost := os.Getenv("TALOS_HOST")
if len(k8sHost) == 0 {
l.Fatal("error: TALOS_HOST is required to point to a node's internal IP")
}

// Connect to Talos apid
api, err := talosapi.NewLocalClient(l, configPath, k8sHost)
if err != nil {
l.WithError(err).Fatal("could not connect to apid")
// Connect to Talos apid
api, err = talosapi.NewLocalClient(ctx, l, configPath, k8sHost)
if err != nil {
l.WithError(err).Fatal("could not connect to apid")
}
} else {
// Connect to Talos machined
api, err = talosapi.NewLocalSocketClient(ctx, l)
if err != nil {
l.WithError(err).Fatal("could not connect to machined socket")
}
}

defer func() {
if err := api.Close(); err != nil {
l.WithError(err).Warn("failed to close API client during process shutdown")
Expand Down Expand Up @@ -101,6 +118,7 @@ func main() {
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
l.Debugf("signal: %s", <-sig)
ctxCancel()
svc.Stop()
}()
svc.Wait()
Expand Down
40 changes: 21 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,56 @@ go 1.22.1
require (
github.com/cosi-project/runtime v0.3.20
github.com/golang/protobuf v1.5.4
github.com/siderolabs/talos v1.6.7
github.com/siderolabs/talos/pkg/machinery v1.6.7
github.com/sirupsen/logrus v1.9.3
github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2
github.com/vmware/govmomi v0.36.1
github.com/vmware/govmomi v0.36.2
github.com/vmware/vmw-guestinfo v0.0.0-20220317130741-510905f0efa3
google.golang.org/grpc v1.62.1
)

require (
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 // indirect
github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f // indirect
github.com/ProtonMail/gopenpgp/v2 v2.7.5 // indirect
github.com/ProtonMail/gopenpgp/v2 v2.7.4 // indirect
github.com/adrg/xdg v0.4.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/go-cni v1.1.9 // indirect
github.com/containernetworking/cni v1.1.2 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gertd/go-pluralize v0.2.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/jsimonetti/rtnetlink v1.4.1 // indirect
github.com/jsimonetti/rtnetlink v1.4.0 // indirect
github.com/mdlayher/ethtool v0.1.0 // indirect
github.com/mdlayher/genetlink v1.3.2 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
github.com/opencontainers/runtime-spec v1.2.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/siderolabs/crypto v0.4.4 // indirect
github.com/siderolabs/gen v0.4.8 // indirect
github.com/siderolabs/go-api-signature v0.3.2 // indirect
github.com/siderolabs/crypto v0.4.1 // indirect
github.com/siderolabs/gen v0.4.7 // indirect
github.com/siderolabs/go-api-signature v0.3.1 // indirect
github.com/siderolabs/go-blockdevice v0.4.7 // indirect
github.com/siderolabs/go-pointer v1.0.0 // indirect
github.com/siderolabs/net v0.4.0 // indirect
github.com/siderolabs/protoenc v0.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 347c1b7

Please sign in to comment.