Skip to content

Commit

Permalink
chore: allow building with debug handlers
Browse files Browse the repository at this point in the history
Refs siderolabs/talos#3534.

Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
  • Loading branch information
AlekSi authored and talos-bot committed May 13, 2021
1 parent 2cdde00 commit db28ed3
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 19 deletions.
20 changes: 13 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ RUN protoc -I/src/app/metal-controller-manager/internal/api \
--go_out=paths=source_relative:/src/app/metal-controller-manager/internal/api --go-grpc_out=paths=source_relative:/src/app/metal-controller-manager/internal/api \
api.proto
RUN --mount=type=cache,target=/.cache controller-gen object:headerFile="./hack/boilerplate.go.txt" paths="./..."
RUN --mount=type=cache,target=/.cache conversion-gen --input-dirs="./app/cluster-api-provider-sidero/api/v1alpha2" --output-base ./ --output-file-base="zz_generated.conversion" --go-header-file="./hack/boilerplate.go.txt"
RUN --mount=type=cache,target=/.cache conversion-gen --input-dirs="./app/cluster-api-provider-sidero/api/v1alpha2" --output-base ./ --output-file-base="zz_generated.conversion" --go-header-file="./hack/boilerplate.go.txt"
ARG MODULE
RUN --mount=type=cache,target=/.cache gofumports -w -local ${MODULE} .

Expand Down Expand Up @@ -108,7 +108,8 @@ COPY --from=release-build /cluster-template.yaml /infrastructure-sidero/${TAG}/c

FROM base AS build-cluster-api-provider-sidero
ARG TARGETARCH
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o /manager ./app/cluster-api-provider-sidero
ARG GO_BUILDFLAGS
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build ${GO_BUILDFLAGS} -ldflags "-s -w" -o /manager ./app/cluster-api-provider-sidero
RUN chmod +x /manager

## TODO(rsmitty): make bmc pkg and move to talos-systems image
Expand All @@ -124,15 +125,18 @@ ENTRYPOINT [ "/manager" ]

FROM base AS build-metal-controller-manager
ARG TARGETARCH
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o /manager ./app/metal-controller-manager
ARG GO_BUILDFLAGS
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build ${GO_BUILDFLAGS} -ldflags "-s -w" -o /manager ./app/metal-controller-manager
RUN chmod +x /manager

FROM base AS agent-build-amd64
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o /agent ./app/metal-controller-manager/cmd/agent
ARG GO_BUILDFLAGS
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=amd64 go build ${GO_BUILDFLAGS} -ldflags "-s -w" -o /agent ./app/metal-controller-manager/cmd/agent
RUN chmod +x /agent

FROM base AS agent-build-arm64
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=arm64 go build -ldflags "-s -w" -o /agent ./app/metal-controller-manager/cmd/agent
ARG GO_BUILDFLAGS
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=arm64 go build ${GO_BUILDFLAGS} -ldflags "-s -w" -o /agent ./app/metal-controller-manager/cmd/agent
RUN chmod +x /agent

FROM base AS initramfs-archive-amd64
Expand Down Expand Up @@ -179,7 +183,8 @@ ENTRYPOINT [ "/manager" ]

FROM base AS build-metal-metadata-server
ARG TARGETARCH
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o /metal-metadata-server ./app/metal-metadata-server
ARG GO_BUILDFLAGS
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build ${GO_BUILDFLAGS} -ldflags "-s -w" -o /metal-metadata-server ./app/metal-metadata-server
RUN chmod +x /metal-metadata-server

FROM scratch AS metal-metadata-server
Expand Down Expand Up @@ -255,7 +260,8 @@ FROM sfyra-base AS sfyra-build
WORKDIR /src/sfyra/cmd/sfyra
ARG TALOS_RELEASE
ARG SFYRA_CMD_PKG=github.com/talos-systems/sidero/sfyra/cmd/sfyra/cmd
RUN --mount=type=cache,target=/.cache GOOS=linux go build -ldflags "-s -w -X ${SFYRA_CMD_PKG}.TalosRelease=${TALOS_RELEASE}" -o /sfyra
ARG GO_BUILDFLAGS
RUN --mount=type=cache,target=/.cache GOOS=linux go build ${GO_BUILDFLAGS} -ldflags "-s -w -X ${SFYRA_CMD_PKG}.TalosRelease=${TALOS_RELEASE}" -o /sfyra
RUN chmod +x /sfyra

FROM scratch AS sfyra
Expand Down
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ PKGS ?= v0.5.0-8-gb0d9cd2

SFYRA_CLUSTERCTL_CONFIG ?= $(HOME)/.cluster-api/clusterctl.sfyra.yaml

GO_BUILDFLAGS ?=

WITH_DEBUG ?=

ifeq ($(WITH_DEBUG), true)
GO_BUILDFLAGS += -tags sidero.debug
endif

BUILD := docker buildx build
PLATFORM ?= linux/amd64
PROGRESS ?= auto
Expand All @@ -30,6 +38,7 @@ COMMON_ARGS += --build-arg=TEST_PKGS=$(TEST_PKGS)
COMMON_ARGS += --build-arg=PKGS=$(PKGS)
COMMON_ARGS += --build-arg=TOOLS=$(TOOLS)
COMMON_ARGS += --build-arg=TALOS_RELEASE=$(TALOS_RELEASE)
COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)"

all: manifests generate cluster-api-provider-sidero metal-controller-manager metal-metadata-server sfyra

Expand Down
16 changes: 16 additions & 0 deletions app/cluster-api-provider-sidero/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
package main

import (
"context"
"flag"
"os"

debug "github.com/talos-systems/go-debug"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
Expand All @@ -26,6 +28,10 @@ import (
// +kubebuilder:scaffold:imports
)

const (
debugAddr = ":9994"
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
Expand Down Expand Up @@ -58,6 +64,16 @@ func main() {
o.Development = true
}))

go func() {
debugLogFunc := func(msg string) {
setupLog.Info(msg)
}
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
setupLog.Error(err, "failed to start debug server")
os.Exit(1)
}
}()

// Machine and cluster operations can create enough events to trigger the event recorder spam filter
// Setting the burst size higher ensures all events will be recorded and submitted to the API
broadcaster := cgrecord.NewBroadcasterWithCorrelatorOptions(cgrecord.CorrelatorOptions{
Expand Down
14 changes: 14 additions & 0 deletions app/metal-controller-manager/cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/talos-systems/go-blockdevice/blockdevice"
"github.com/talos-systems/go-blockdevice/blockdevice/util"
debug "github.com/talos-systems/go-debug"
kmsg "github.com/talos-systems/go-kmsg"
"github.com/talos-systems/go-procfs/procfs"
"github.com/talos-systems/go-retry/retry"
Expand All @@ -33,6 +34,10 @@ import (
"github.com/talos-systems/sidero/app/metal-controller-manager/pkg/constants"
)

const (
debugAddr = ":9991"
)

func setup() error {
if err := os.MkdirAll("/etc", 0o777); err != nil {
return err
Expand Down Expand Up @@ -209,6 +214,15 @@ func mainFunc() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
debugLogFunc := func(msg string) {
log.Print(msg)
}
if err := debug.ListenAndServe(ctx, debugAddr, debugLogFunc); err != nil {
log.Fatalf("failed to start debug server: %s", err)
}
}()

if err := setup(); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ spec:
description: Source for the password value. Cannot be used if Pass is not empty.
properties:
secretKeyRef:
description: SecretKeyRef defines a ref to a given key within a secret
description: SecretKeyRef defines a ref to a given key within a secret.
properties:
key:
description: Key to select
Expand All @@ -94,7 +94,7 @@ spec:
description: Source for the user value. Cannot be used if User is not empty.
properties:
secretKeyRef:
description: SecretKeyRef defines a ref to a given key within a secret
description: SecretKeyRef defines a ref to a given key within a secret.
properties:
key:
description: Key to select
Expand Down
18 changes: 15 additions & 3 deletions app/metal-controller-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"time"

debug "github.com/talos-systems/go-debug"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
Expand All @@ -36,6 +37,7 @@ import (

const (
defaultMaxConcurrentReconciles = 10
debugAddr = ":9992"
)

var (
Expand Down Expand Up @@ -89,13 +91,23 @@ func main() {
apiEndpoint = ""
}

// only for testing, doesn't affect production, default values simulate no failures
api.DefaultDice = api.NewFailureDice(testPowerSimulatedExplicitFailureProb, testPowerSimulatedSilentFailureProb)

ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
}))

go func() {
debugLogFunc := func(msg string) {
setupLog.Info(msg)
}
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
setupLog.Error(err, "failed to start debug server")
os.Exit(1)
}
}()

// only for testing, doesn't affect production, default values simulate no failures
api.DefaultDice = api.NewFailureDice(testPowerSimulatedExplicitFailureProb, testPowerSimulatedSilentFailureProb)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Expand Down
19 changes: 17 additions & 2 deletions app/metal-metadata-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

jsonpatch "github.com/evanphx/json-patch"
"github.com/ghodss/yaml"
debug "github.com/talos-systems/go-debug"
"github.com/talos-systems/talos/pkg/machinery/config/configloader"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
v1 "k8s.io/api/core/v1"
Expand All @@ -27,6 +28,10 @@ import (
"github.com/talos-systems/sidero/internal/client"
)

const (
debugAddr = ":9993"
)

var (
kubeconfigPath *string
port *string
Expand All @@ -51,6 +56,15 @@ func main() {
port = flag.String("port", "8080", "port to use for serving metadata")
flag.Parse()

go func() {
debugLogFunc := func(msg string) {
log.Print(msg)
}
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
log.Fatalf("failed to start debug server: %s", err)
}
}()

k8sClient, err := client.NewClient(kubeconfigPath)
if err != nil {
log.Fatal(fmt.Errorf("failure talking to kubernetes: %s", err))
Expand All @@ -60,8 +74,9 @@ func main() {
client: k8sClient,
}

http.HandleFunc("/configdata", mm.FetchConfig)
log.Fatal(http.ListenAndServe(":"+*port, nil))
mux := http.NewServeMux()
mux.HandleFunc("/configdata", mm.FetchConfig)
log.Fatal(http.ListenAndServe(":"+*port, mux))
}

func (m *metadataConfigs) FetchConfig(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/talos-systems/cluster-api-bootstrap-provider-talos v0.2.0-alpha.11
github.com/talos-systems/cluster-api-control-plane-provider-talos v0.1.0-alpha.11
github.com/talos-systems/go-blockdevice v0.1.1-0.20201218174450-f2728a581972
github.com/talos-systems/go-debug v0.1.0
github.com/talos-systems/go-kmsg v0.1.0
github.com/talos-systems/go-procfs v0.0.0-20210108152626-8cbc42d3dc24
github.com/talos-systems/go-retry v0.2.0
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ github.com/talos-systems/crypto v0.2.1-0.20210202170911-39584f1b6e54 h1:2IGs3f0q
github.com/talos-systems/crypto v0.2.1-0.20210202170911-39584f1b6e54/go.mod h1:OXCK52Q0dzm88YRG4VdTBdidkPUtqrCxCyW7bUs4DAw=
github.com/talos-systems/go-blockdevice v0.1.1-0.20201218174450-f2728a581972 h1:/yEPl6h6+pK9XIfQEiZ989GDuw6dCUBeinzUcCu6dyY=
github.com/talos-systems/go-blockdevice v0.1.1-0.20201218174450-f2728a581972/go.mod h1:efEE9wjtgxiovqsZAV39xlOd/AOI/0sLuZqb5jEgeqo=
github.com/talos-systems/go-debug v0.1.0 h1:lURdF1U2UwB0by/CiunePULUHBR5BXdsG8I4Mjv+uUw=
github.com/talos-systems/go-debug v0.1.0/go.mod h1:pR4NjsZQNFqGx3n4qkD4MIj1F2CxyIF8DCiO1+05JO0=
github.com/talos-systems/go-kmsg v0.1.0 h1:juoZn+XioduYvtie6nqi/miKGJPLYSBNXRv5jRe6+lE=
github.com/talos-systems/go-kmsg v0.1.0/go.mod h1:dppwQn+/mrdvsziGMbXjzfc4E+75oZhr39UIP6LgL0w=
github.com/talos-systems/go-procfs v0.0.0-20210108152626-8cbc42d3dc24 h1:fN8vYvlB9XBQ5aImb1vLgR0ZaDwvfZfBMptqkpi3aEg=
Expand Down Expand Up @@ -619,8 +621,9 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210505214959-0714010a04ed h1:V9kAVxLvz1lkufatrpHuUVyJ/5tR3Ms7rk951P4mI98=
golang.org/x/net v0.0.0-20210505214959-0714010a04ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -677,6 +680,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201130171929-760e229fe7c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6 h1:cdsMqa2nXzqlgs183pHxtvoVwU7CyzaCTAUOg94af4c=
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
Expand All @@ -688,8 +692,9 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
metal "github.com/talos-systems/sidero/app/metal-controller-manager/api/v1alpha1"
)

// NewClient is responsible for creating a controller-runtime k8s client for use by the metadata server.
// NewClient is responsible for creating a controller-runtime k8s client with all schemas.
func NewClient(kubeconfig *string) (client.Client, error) {
// Build rest config based on whether we've got a kubeconfig
var (
Expand Down
16 changes: 16 additions & 0 deletions sfyra/cmd/sfyra/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
package cmd

import (
"context"
"fmt"
"log"
"os"

"github.com/spf13/cobra"
debug "github.com/talos-systems/go-debug"
)

const (
debugAddr = ":9995"
)

var rootCmd = &cobra.Command{
Expand All @@ -19,6 +26,15 @@ var rootCmd = &cobra.Command{

// Execute root command.
func Execute() {
go func() {
debugLogFunc := func(msg string) {
log.Print(msg)
}
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
log.Fatalf("failed to start debug server: %s", err)
}
}()

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions sfyra/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/stretchr/testify v1.7.0
github.com/talos-systems/cluster-api-bootstrap-provider-talos v0.2.0-alpha.11
github.com/talos-systems/cluster-api-control-plane-provider-talos v0.1.0-alpha.11
github.com/talos-systems/go-debug v0.1.0
github.com/talos-systems/go-loadbalancer v0.1.1
github.com/talos-systems/go-procfs v0.0.0-20210108152626-8cbc42d3dc24
github.com/talos-systems/go-retry v0.2.1-0.20210119124456-b9dc1a990133
Expand Down
Loading

0 comments on commit db28ed3

Please sign in to comment.