Skip to content

Commit dfe3f0b

Browse files
authored
🌱 Fix CI (getting version info), add logging to InstanceMetadata (#67)
1 parent c81a443 commit dfe3f0b

9 files changed

Lines changed: 75 additions & 125 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on: # yamllint disable-line rule:truthy
44
push:
55
branches:
66
- main
7+
pull_request:
8+
types: [opened, synchronize, reopened, ready_for_review]
79
env:
810
IMAGE_NAME: hetzner-cloud-controller-manager-staging
911
REGISTRY: ghcr.io/syself
@@ -47,13 +49,6 @@ jobs:
4749
- name: Install Cosign
4850
uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # v3.7.0
4951

50-
- name: Setup Env
51-
run: |
52-
DOCKER_BUILD_LDFLAGS="$(hack/version.sh)"
53-
echo 'DOCKER_BUILD_LDFLAGS<<EOF' >> $GITHUB_ENV
54-
echo $DOCKER_BUILD_LDFLAGS >> $GITHUB_ENV
55-
echo 'EOF' >> $GITHUB_ENV
56-
5752
- name: Build and push manager image
5853
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
5954
id: docker_build_release
@@ -62,8 +57,6 @@ jobs:
6257
context: .
6358
file: ./images/hetzner-cloud-controller-manager/Dockerfile
6459
push: true
65-
build-args: |
66-
LDFLAGS=${{ env.DOCKER_BUILD_LDFLAGS }}
6760
tags: ${{ steps.meta.outputs.tags }}
6861
labels: ${{ steps.meta.outputs.labels }}
6962
platforms: linux/amd64,linux/arm64

.github/workflows/go-test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Go Test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v5
18+
19+
- name: Run tests
20+
run: go test ./...

.github/workflows/release.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ jobs:
5757
sudo mv ./bom /usr/local/bin/bom
5858
sudo chmod +x /usr/local/bin/bom
5959
60-
- name: Setup Env
61-
run: |
62-
DOCKER_BUILD_LDFLAGS="$(hack/version.sh)"
63-
echo 'DOCKER_BUILD_LDFLAGS<<EOF' >> $GITHUB_ENV
64-
echo $DOCKER_BUILD_LDFLAGS >> $GITHUB_ENV
65-
echo 'EOF' >> $GITHUB_ENV
66-
6760
- name: Build and push manager image
6861
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
6962
id: docker_build_release
@@ -72,8 +65,6 @@ jobs:
7265
context: .
7366
file: ./images/hetzner-cloud-controller-manager/Dockerfile
7467
push: true
75-
build-args: |
76-
LDFLAGS=${{ env.DOCKER_BUILD_LDFLAGS }}
7768
tags: ${{ steps.meta.outputs.tags }}
7869
labels: ${{ steps.meta.outputs.labels }}
7970
platforms: linux/amd64,linux/arm64

hack/version.sh

Lines changed: 0 additions & 90 deletions
This file was deleted.

hcloud/cloud.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,15 @@ const (
7272

7373
var errMissingRobotCredentials = errors.New("missing robot credentials - cannot connect to robot API")
7474

75-
// providerVersion is set by the build process using -ldflags -X.
76-
var providerVersion = "unknown"
75+
// ProviderVersion shows the provider version via debug.ReadBuildInfo(). If this returns the empty
76+
// string, read https://github.com/golang/go/issues/51279.
77+
func ProviderVersion() string {
78+
info, ok := debug.ReadBuildInfo()
79+
if !ok {
80+
return "failed-to-get-version-info"
81+
}
82+
return info.Main.Version
83+
}
7784

7885
type cloud struct {
7986
hcloudClient *hcloud.Client
@@ -120,7 +127,7 @@ func newHcloudClient(rootDir string) (*hcloud.Client, error) {
120127
}
121128
opts := []hcloud.ClientOption{
122129
hcloud.WithToken(token),
123-
hcloud.WithApplication("hetzner-cloud-controller", providerVersion),
130+
hcloud.WithApplication("hetzner-cloud-controller", ProviderVersion()),
124131
}
125132

126133
// start metrics server if enabled (enabled by default)
@@ -214,7 +221,7 @@ func newCloud(_ io.Reader) (cloudprovider.Interface, error) {
214221
return nil, fmt.Errorf("%s: %w", op, err)
215222
}
216223

217-
klog.Infof("Hetzner Cloud k8s cloud controller %s started\n", providerVersion)
224+
klog.Infof("Hetzner Cloud k8s cloud controller %s started\n", ProviderVersion())
218225

219226
lbOpsDefaults.DisableIPv6 = lbDisableIPv6
220227

hcloud/instances.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/syself/hrobot-go/models"
2828
corev1 "k8s.io/api/core/v1"
2929
cloudprovider "k8s.io/cloud-provider"
30+
"k8s.io/klog/v2"
3031
)
3132

3233
type addressFamily int
@@ -130,10 +131,13 @@ func (i *instances) InstanceShutdown(ctx context.Context, node *corev1.Node) (bo
130131
return false, nil
131132
}
132133

133-
func (i *instances) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
134+
func (i *instances) InstanceMetadata(ctx context.Context, node *corev1.Node) (metadata *cloudprovider.InstanceMetadata, reterr error) {
134135
const op = "hcloud/instancesv2.InstanceMetadata"
135136
metrics.OperationCalled.WithLabelValues(op).Inc()
136-
137+
defer func() {
138+
klog.InfoS("InstanceMetadata", "node", node,
139+
"InstanceMetadata", metadata, "err", reterr)
140+
}()
137141
hcloudServer, bmServer, isHCloudServer, err := i.lookupServer(ctx, node)
138142
if err != nil {
139143
return nil, err

images/hetzner-cloud-controller-manager/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ COPY . /src/hetzner-cloud-controller-manager
2020
WORKDIR /src/hetzner-cloud-controller-manager
2121
RUN --mount=type=cache,target=/root/.cache --mount=type=cache,target=/go/pkg \
2222
GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 \
23-
go build -mod=readonly -ldflags "${LDFLAGS} -extldflags '-static'" \
24-
-o manager main.go
23+
go build -mod=readonly -ldflags "-extldflags '-static'" \
24+
-o manager .
2525

2626
FROM --platform=${BUILDPLATFORM} gcr.io/distroless/static:nonroot
2727
WORKDIR /

internal/credentials/hotreload.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ func handleEvent(credentialsDir, baseName string, hcloudClient *hcloud.Client, r
9393

9494
case "hcloud":
9595
// This case is executed, when the process is running on a local machine.
96-
return loadHcloudCredentials(credentialsDir, hcloudClient)
96+
err := loadHcloudCredentials(credentialsDir, hcloudClient)
97+
if err != nil {
98+
return fmt.Errorf("handleEvent hcloud: %w", err)
99+
}
100+
return nil
97101

98102
case "..data":
99103
// This case is executed, when the secrets are mounted in a Kubernetes pod.
@@ -102,14 +106,19 @@ func handleEvent(credentialsDir, baseName string, hcloudClient *hcloud.Client, r
102106
// This means the files/symlinks don't change. When the secrets get changed, then
103107
// a new ..data directory gets created. This is done by Kubernetes to make the
104108
// update of all files atomic.
105-
err = loadHcloudCredentials(credentialsDir, hcloudClient)
106-
if err != nil {
107-
return err
109+
if hcloudClient != nil {
110+
err = loadHcloudCredentials(credentialsDir, hcloudClient)
111+
if err != nil {
112+
return fmt.Errorf("handleEvent ..data loadHcloudCredentials: %w", err)
113+
}
108114
}
109-
if robotClient == nil {
110-
return nil
115+
if robotClient != nil {
116+
err := loadRobotCredentials(credentialsDir, robotClient)
117+
if err != nil {
118+
return fmt.Errorf("handleEvent ..data loadRobotCredentials: %w", err)
119+
}
111120
}
112-
return loadRobotCredentials(credentialsDir, robotClient)
121+
return nil
113122

114123
default:
115124
klog.Infof("Ignoring fsnotify event for file %q: %s", baseName, event.String())
@@ -192,7 +201,7 @@ func loadHcloudCredentials(credentialsDir string, hcloudClient *hcloud.Client) e
192201

193202
token, err := readHcloudCredentials(credentialsDir)
194203
if err != nil {
195-
return err
204+
return fmt.Errorf("loadHcloudCredentials: %w", err)
196205
}
197206

198207
if len(token) != 64 {
@@ -218,7 +227,7 @@ func loadHcloudCredentials(credentialsDir string, hcloudClient *hcloud.Client) e
218227
func GetInitialHcloudCredentialsFromDirectory(credentialsDir string) (string, error) {
219228
token, err := readHcloudCredentials(credentialsDir)
220229
if err != nil {
221-
return "", fmt.Errorf("readHcloudCredentials: %w", err)
230+
return "", fmt.Errorf("Getting initial credentials: readHcloudCredentials: %w", err)
222231
}
223232

224233
// Update global variable

main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ limitations under the License.
2121
package main
2222

2323
import (
24+
"fmt"
2425
"os"
2526

27+
"github.com/spf13/cobra"
2628
"github.com/spf13/pflag"
29+
"github.com/syself/hetzner-cloud-controller-manager/hcloud"
2730
_ "github.com/syself/hetzner-cloud-controller-manager/hcloud"
2831
"k8s.io/apimachinery/pkg/util/wait"
2932
cloudprovider "k8s.io/cloud-provider"
@@ -45,8 +48,21 @@ func main() {
4548
}
4649

4750
fss := cliflag.NamedFlagSets{}
51+
52+
// Add custom option, so that we can see which ccm version gets used.
53+
var printVersion bool
54+
fss.FlagSet("hcloud").BoolVar(&printVersion, "ccm-version", false, "Print CCM version and exit.")
55+
4856
command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, app.DefaultInitFuncConstructors, names.CCMControllerAliases(), fss, wait.NeverStop)
4957

58+
command.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
59+
if printVersion {
60+
fmt.Println(hcloud.ProviderVersion())
61+
os.Exit(0)
62+
}
63+
return nil
64+
}
65+
5066
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
5167
logs.InitLogs()
5268
defer logs.FlushLogs()

0 commit comments

Comments
 (0)