Skip to content

Commit db28ed3

Browse files
AlekSitalos-bot
authored andcommitted
chore: allow building with debug handlers
Refs siderolabs/talos#3534. Signed-off-by: Alexey Palazhchenko <alexey.palazhchenko@gmail.com>
1 parent 2cdde00 commit db28ed3

File tree

13 files changed

+119
-19
lines changed

13 files changed

+119
-19
lines changed

Dockerfile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ RUN protoc -I/src/app/metal-controller-manager/internal/api \
7171
--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 \
7272
api.proto
7373
RUN --mount=type=cache,target=/.cache controller-gen object:headerFile="./hack/boilerplate.go.txt" paths="./..."
74-
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"
74+
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"
7575
ARG MODULE
7676
RUN --mount=type=cache,target=/.cache gofumports -w -local ${MODULE} .
7777

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

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

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

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

130132
FROM base AS agent-build-amd64
131-
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o /agent ./app/metal-controller-manager/cmd/agent
133+
ARG GO_BUILDFLAGS
134+
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
132135
RUN chmod +x /agent
133136

134137
FROM base AS agent-build-arm64
135-
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=arm64 go build -ldflags "-s -w" -o /agent ./app/metal-controller-manager/cmd/agent
138+
ARG GO_BUILDFLAGS
139+
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
136140
RUN chmod +x /agent
137141

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

180184
FROM base AS build-metal-metadata-server
181185
ARG TARGETARCH
182-
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o /metal-metadata-server ./app/metal-metadata-server
186+
ARG GO_BUILDFLAGS
187+
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
183188
RUN chmod +x /metal-metadata-server
184189

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

261267
FROM scratch AS sfyra

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ PKGS ?= v0.5.0-8-gb0d9cd2
1616

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

19+
GO_BUILDFLAGS ?=
20+
21+
WITH_DEBUG ?=
22+
23+
ifeq ($(WITH_DEBUG), true)
24+
GO_BUILDFLAGS += -tags sidero.debug
25+
endif
26+
1927
BUILD := docker buildx build
2028
PLATFORM ?= linux/amd64
2129
PROGRESS ?= auto
@@ -30,6 +38,7 @@ COMMON_ARGS += --build-arg=TEST_PKGS=$(TEST_PKGS)
3038
COMMON_ARGS += --build-arg=PKGS=$(PKGS)
3139
COMMON_ARGS += --build-arg=TOOLS=$(TOOLS)
3240
COMMON_ARGS += --build-arg=TALOS_RELEASE=$(TALOS_RELEASE)
41+
COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)"
3342

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

app/cluster-api-provider-sidero/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
package main
66

77
import (
8+
"context"
89
"flag"
910
"os"
1011

12+
debug "github.com/talos-systems/go-debug"
1113
corev1 "k8s.io/api/core/v1"
1214
"k8s.io/apimachinery/pkg/runtime"
1315
"k8s.io/client-go/kubernetes"
@@ -26,6 +28,10 @@ import (
2628
// +kubebuilder:scaffold:imports
2729
)
2830

31+
const (
32+
debugAddr = ":9994"
33+
)
34+
2935
var (
3036
scheme = runtime.NewScheme()
3137
setupLog = ctrl.Log.WithName("setup")
@@ -58,6 +64,16 @@ func main() {
5864
o.Development = true
5965
}))
6066

67+
go func() {
68+
debugLogFunc := func(msg string) {
69+
setupLog.Info(msg)
70+
}
71+
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
72+
setupLog.Error(err, "failed to start debug server")
73+
os.Exit(1)
74+
}
75+
}()
76+
6177
// Machine and cluster operations can create enough events to trigger the event recorder spam filter
6278
// Setting the burst size higher ensures all events will be recorded and submitted to the API
6379
broadcaster := cgrecord.NewBroadcasterWithCorrelatorOptions(cgrecord.CorrelatorOptions{

app/metal-controller-manager/cmd/agent/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/talos-systems/go-blockdevice/blockdevice"
2020
"github.com/talos-systems/go-blockdevice/blockdevice/util"
21+
debug "github.com/talos-systems/go-debug"
2122
kmsg "github.com/talos-systems/go-kmsg"
2223
"github.com/talos-systems/go-procfs/procfs"
2324
"github.com/talos-systems/go-retry/retry"
@@ -33,6 +34,10 @@ import (
3334
"github.com/talos-systems/sidero/app/metal-controller-manager/pkg/constants"
3435
)
3536

37+
const (
38+
debugAddr = ":9991"
39+
)
40+
3641
func setup() error {
3742
if err := os.MkdirAll("/etc", 0o777); err != nil {
3843
return err
@@ -209,6 +214,15 @@ func mainFunc() error {
209214
ctx, cancel := context.WithCancel(context.Background())
210215
defer cancel()
211216

217+
go func() {
218+
debugLogFunc := func(msg string) {
219+
log.Print(msg)
220+
}
221+
if err := debug.ListenAndServe(ctx, debugAddr, debugLogFunc); err != nil {
222+
log.Fatalf("failed to start debug server: %s", err)
223+
}
224+
}()
225+
212226
if err := setup(); err != nil {
213227
return err
214228
}

app/metal-controller-manager/config/crd/bases/metal.sidero.dev_servers.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ spec:
7171
description: Source for the password value. Cannot be used if Pass is not empty.
7272
properties:
7373
secretKeyRef:
74-
description: SecretKeyRef defines a ref to a given key within a secret
74+
description: SecretKeyRef defines a ref to a given key within a secret.
7575
properties:
7676
key:
7777
description: Key to select
@@ -94,7 +94,7 @@ spec:
9494
description: Source for the user value. Cannot be used if User is not empty.
9595
properties:
9696
secretKeyRef:
97-
description: SecretKeyRef defines a ref to a given key within a secret
97+
description: SecretKeyRef defines a ref to a given key within a secret.
9898
properties:
9999
key:
100100
description: Key to select

app/metal-controller-manager/main.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"time"
1313

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

3738
const (
3839
defaultMaxConcurrentReconciles = 10
40+
debugAddr = ":9992"
3941
)
4042

4143
var (
@@ -89,13 +91,23 @@ func main() {
8991
apiEndpoint = ""
9092
}
9193

92-
// only for testing, doesn't affect production, default values simulate no failures
93-
api.DefaultDice = api.NewFailureDice(testPowerSimulatedExplicitFailureProb, testPowerSimulatedSilentFailureProb)
94-
9594
ctrl.SetLogger(zap.New(func(o *zap.Options) {
9695
o.Development = true
9796
}))
9897

98+
go func() {
99+
debugLogFunc := func(msg string) {
100+
setupLog.Info(msg)
101+
}
102+
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
103+
setupLog.Error(err, "failed to start debug server")
104+
os.Exit(1)
105+
}
106+
}()
107+
108+
// only for testing, doesn't affect production, default values simulate no failures
109+
api.DefaultDice = api.NewFailureDice(testPowerSimulatedExplicitFailureProb, testPowerSimulatedSilentFailureProb)
110+
99111
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
100112
Scheme: scheme,
101113
MetricsBindAddress: metricsAddr,

app/metal-metadata-server/main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
jsonpatch "github.com/evanphx/json-patch"
1616
"github.com/ghodss/yaml"
17+
debug "github.com/talos-systems/go-debug"
1718
"github.com/talos-systems/talos/pkg/machinery/config/configloader"
1819
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
1920
v1 "k8s.io/api/core/v1"
@@ -27,6 +28,10 @@ import (
2728
"github.com/talos-systems/sidero/internal/client"
2829
)
2930

31+
const (
32+
debugAddr = ":9993"
33+
)
34+
3035
var (
3136
kubeconfigPath *string
3237
port *string
@@ -51,6 +56,15 @@ func main() {
5156
port = flag.String("port", "8080", "port to use for serving metadata")
5257
flag.Parse()
5358

59+
go func() {
60+
debugLogFunc := func(msg string) {
61+
log.Print(msg)
62+
}
63+
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
64+
log.Fatalf("failed to start debug server: %s", err)
65+
}
66+
}()
67+
5468
k8sClient, err := client.NewClient(kubeconfigPath)
5569
if err != nil {
5670
log.Fatal(fmt.Errorf("failure talking to kubernetes: %s", err))
@@ -60,8 +74,9 @@ func main() {
6074
client: k8sClient,
6175
}
6276

63-
http.HandleFunc("/configdata", mm.FetchConfig)
64-
log.Fatal(http.ListenAndServe(":"+*port, nil))
77+
mux := http.NewServeMux()
78+
mux.HandleFunc("/configdata", mm.FetchConfig)
79+
log.Fatal(http.ListenAndServe(":"+*port, mux))
6580
}
6681

6782
func (m *metadataConfigs) FetchConfig(w http.ResponseWriter, r *http.Request) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/talos-systems/cluster-api-bootstrap-provider-talos v0.2.0-alpha.11
2424
github.com/talos-systems/cluster-api-control-plane-provider-talos v0.1.0-alpha.11
2525
github.com/talos-systems/go-blockdevice v0.1.1-0.20201218174450-f2728a581972
26+
github.com/talos-systems/go-debug v0.1.0
2627
github.com/talos-systems/go-kmsg v0.1.0
2728
github.com/talos-systems/go-procfs v0.0.0-20210108152626-8cbc42d3dc24
2829
github.com/talos-systems/go-retry v0.2.0

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ github.com/talos-systems/crypto v0.2.1-0.20210202170911-39584f1b6e54 h1:2IGs3f0q
488488
github.com/talos-systems/crypto v0.2.1-0.20210202170911-39584f1b6e54/go.mod h1:OXCK52Q0dzm88YRG4VdTBdidkPUtqrCxCyW7bUs4DAw=
489489
github.com/talos-systems/go-blockdevice v0.1.1-0.20201218174450-f2728a581972 h1:/yEPl6h6+pK9XIfQEiZ989GDuw6dCUBeinzUcCu6dyY=
490490
github.com/talos-systems/go-blockdevice v0.1.1-0.20201218174450-f2728a581972/go.mod h1:efEE9wjtgxiovqsZAV39xlOd/AOI/0sLuZqb5jEgeqo=
491+
github.com/talos-systems/go-debug v0.1.0 h1:lURdF1U2UwB0by/CiunePULUHBR5BXdsG8I4Mjv+uUw=
492+
github.com/talos-systems/go-debug v0.1.0/go.mod h1:pR4NjsZQNFqGx3n4qkD4MIj1F2CxyIF8DCiO1+05JO0=
491493
github.com/talos-systems/go-kmsg v0.1.0 h1:juoZn+XioduYvtie6nqi/miKGJPLYSBNXRv5jRe6+lE=
492494
github.com/talos-systems/go-kmsg v0.1.0/go.mod h1:dppwQn+/mrdvsziGMbXjzfc4E+75oZhr39UIP6LgL0w=
493495
github.com/talos-systems/go-procfs v0.0.0-20210108152626-8cbc42d3dc24 h1:fN8vYvlB9XBQ5aImb1vLgR0ZaDwvfZfBMptqkpi3aEg=
@@ -619,8 +621,9 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/
619621
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
620622
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
621623
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
622-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
623624
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
625+
golang.org/x/net v0.0.0-20210505214959-0714010a04ed h1:V9kAVxLvz1lkufatrpHuUVyJ/5tR3Ms7rk951P4mI98=
626+
golang.org/x/net v0.0.0-20210505214959-0714010a04ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
624627
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
625628
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
626629
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -677,6 +680,7 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
677680
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
678681
golang.org/x/sys v0.0.0-20201130171929-760e229fe7c5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
679682
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
683+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
680684
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6 h1:cdsMqa2nXzqlgs183pHxtvoVwU7CyzaCTAUOg94af4c=
681685
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
682686
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
@@ -688,8 +692,9 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3
688692
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
689693
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
690694
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
691-
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
692695
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
696+
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
697+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
693698
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
694699
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
695700
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=

internal/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
metal "github.com/talos-systems/sidero/app/metal-controller-manager/api/v1alpha1"
2020
)
2121

22-
// NewClient is responsible for creating a controller-runtime k8s client for use by the metadata server.
22+
// NewClient is responsible for creating a controller-runtime k8s client with all schemas.
2323
func NewClient(kubeconfig *string) (client.Client, error) {
2424
// Build rest config based on whether we've got a kubeconfig
2525
var (

0 commit comments

Comments
 (0)