Skip to content

Commit

Permalink
Merge pull request #1016 from syself/tg/vendor-installimage
Browse files Browse the repository at this point in the history
✨ use vendored installimage
  • Loading branch information
batistein authored Oct 22, 2023
2 parents ac6392e + c97fd2d commit 84a8470
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,9 @@ tilt-up: env-vars-for-wl-cluster $(ENVSUBST) $(KUBECTL) $(KUSTOMIZE) $(TILT) clu
.PHONY: watch
watch: ## Watch CRDs cluster, machines and Events.
watch -c -n 2 hack/output-for-watch.sh

installimageurl := $(shell curl -sL https://api.github.com/repos/syself/hetzner-installimage/releases/latest | jq -r .assets[].browser_download_url)
.PHONY: create-hetzner-installimage-tgz
create-hetzner-installimage-tgz:
rm -rf data/hetzner-installimage*
cd data; curl -sSLO $(installimageurl)
9 changes: 8 additions & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def set_env_variables():
tilt_dockerfile_header = """
FROM gcr.io/distroless/base:debug as tilt
WORKDIR /
COPY installimage.tgz .
COPY manager .
"""

Expand All @@ -154,6 +157,10 @@ def caph():
yaml = str(encode_yaml_stream(yaml_dict))
yaml = fixup_yaml_empty_arrays(yaml)

# copy things from data directory to .tiltbuild
if not os.path.exists('.tiltbuild/hetzner-installimage.tgz'):
local("cp data/hetzner-install-image-v1.0.0.tgz .tiltbuild/installimage.tgz")

# Set up a local_resource build of the provider's manager binary.

# Forge the build command
Expand Down Expand Up @@ -183,7 +190,7 @@ def caph():
dockerfile_contents = tilt_dockerfile_header,
target = "tilt",
entrypoint = entrypoint,
only = "manager",
only = ["manager", "installimage.tgz"],
live_update = [
sync(".tiltbuild/manager", "/manager"),
],
Expand Down
1 change: 1 addition & 0 deletions controllers/hetznerbaremetalhost_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@ name="eth0" model="Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express
})
sshClient.On("DownloadImage", mock.Anything, mock.Anything).Return(sshclient.Output{})
sshClient.On("CreateAutoSetup", mock.Anything).Return(sshclient.Output{})
sshClient.On("UntarTGZ").Return(sshclient.Output{})
sshClient.On("CreatePostInstallScript", mock.Anything).Return(sshclient.Output{})
sshClient.On("ExecuteInstallImage", mock.Anything).Return(sshclient.Output{})
sshClient.On("Reboot").Return(sshclient.Output{})
Expand Down
2 changes: 2 additions & 0 deletions controllers/hetznerbaremetalremediation_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ var _ = Describe("HetznerBareMetalRemediationReconciler", func() {
osSSHClientAfterInstallImage.On("CloudInitStatus").Return(sshclient.Output{StdOut: "status: done"})
osSSHClientAfterInstallImage.On("CheckCloudInitLogsForSigTerm").Return(sshclient.Output{})
osSSHClientAfterInstallImage.On("ResetKubeadm").Return(sshclient.Output{})
osSSHClientAfterInstallImage.On("GetCloudInitOutput").Return(sshclient.Output{StdOut: "dummy content of /var/log/cloud-init-output.log"})
osSSHClientAfterInstallImage.On("GetHostName").Return(sshclient.Output{
StdOut: infrav1.BareMetalHostNamePrefix + bmMachineName,
StdErr: "",
Expand All @@ -227,6 +228,7 @@ var _ = Describe("HetznerBareMetalRemediationReconciler", func() {
osSSHClientAfterCloudInit.On("CloudInitStatus").Return(sshclient.Output{StdOut: "status: done"})
osSSHClientAfterCloudInit.On("CheckCloudInitLogsForSigTerm").Return(sshclient.Output{})
osSSHClientAfterCloudInit.On("ResetKubeadm").Return(sshclient.Output{})
osSSHClientAfterCloudInit.On("GetCloudInitOutput").Return(sshclient.Output{StdOut: "dummy content of /var/log/cloud-init-output.log"})
})

AfterEach(func() {
Expand Down
Binary file added data/hetzner-install-image-v1.0.0.tgz
Binary file not shown.
2 changes: 1 addition & 1 deletion hack/filter-caph-controller-manager-logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

keys_to_skip = ['controller', 'controllerGroup', 'controllerKind', 'reconcileID',
'HetznerCluster', 'Cluster',
'namespace', 'name', 'Machine']
'namespace', 'name', 'Machine', 'stack', 'stacktrace']

rows_to_skip = [
'controller-runtime.webhook', 'certwatcher/certwatcher', 'Registering a validating webhook',
Expand Down
2 changes: 1 addition & 1 deletion hack/kind-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ local CLUSTER_VERSION=$2
cat <<EOF | ctlptl apply -f -
apiVersion: ctlptl.dev/v1alpha1
kind: Registry
name: ${CLUSTER_NAME}-registry
name: kind-registry
port: 5000
---
apiVersion: ctlptl.dev/v1alpha1
Expand Down
2 changes: 1 addition & 1 deletion hack/output-for-watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ kubectl get hetznerbaremetalmachine -A

print_heading events:

kubectl get events -A --sort-by=lastTimestamp | tail -8
kubectl get events -A --sort-by=lastTimestamp | grep -vP 'LeaderElection' | tail -8

print_heading logs:

Expand Down
1 change: 1 addition & 0 deletions images/caph/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ RUN --mount=type=cache,target=/root/.cache --mount=type=cache,target=/go/pkg \

FROM --platform=${BUILDPLATFORM} gcr.io/distroless/static:nonroot
WORKDIR /
COPY data/hetzner-install-image-v1.0.0.tgz /installimage.tgz
COPY --from=build /src/cluster-api-provider-hetzner/manager .
# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies
USER 65532
Expand Down
41 changes: 41 additions & 0 deletions pkg/services/baremetal/client/mocks/ssh/Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions pkg/services/baremetal/client/ssh/ssh_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ package sshclient
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -92,6 +94,7 @@ type Client interface {
CleanCloudInitLogs() Output
CleanCloudInitInstances() Output
ResetKubeadm() Output
UntarTGZ() Output
}

// Factory is the interface for creating new Client objects.
Expand Down Expand Up @@ -236,9 +239,9 @@ func (c *sshClient) CreatePostInstallScript(data string) Output {
func (c *sshClient) ExecuteInstallImage(hasPostInstallScript bool) Output {
var cmd string
if hasPostInstallScript {
cmd = `/root/.oldroot/nfs/install/installimage -a -c /autosetup -x /root/post-install.sh`
cmd = `/root/hetzner-installimage/installimage -a -c /autosetup -x /root/post-install.sh`
} else {
cmd = `/root/.oldroot/nfs/install/installimage -a -c /autosetup`
cmd = `/root/hetzner-installimage/installimage -a -c /autosetup`
}

out := c.runSSH(fmt.Sprintf(`cat << 'EOF' > /root/install-image-script.sh
Expand Down Expand Up @@ -365,6 +368,17 @@ func (c *sshClient) ResetKubeadm() Output {
return output
}

func (c *sshClient) UntarTGZ() Output {
fileName := "/installimage.tgz"
data, err := os.ReadFile(fileName)
if err != nil {
return Output{Err: fmt.Errorf("ReadInstallimageTgzFailed %s: %w", fileName, err)}
}

return c.runSSH(fmt.Sprintf("echo %s | base64 -d | tar -xzf-",
base64.StdEncoding.EncodeToString(data)))
}

// IsConnectionRefusedError checks whether the ssh error is a connection refused error.
func IsConnectionRefusedError(err error) bool {
return strings.Contains(err.Error(), ErrConnectionRefused.Error())
Expand Down
8 changes: 7 additions & 1 deletion pkg/services/baremetal/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,14 @@ func (s *Service) actionImageInstalling() actionResult {
}
}

out := sshClient.UntarTGZ()
if out.Err != nil {
record.Warnf(s.scope.HetznerBareMetalHost, "UntarInstallimageTgzFailed", "err: %s, stderr: %s", out.Err.Error(), out.StdErr)
return actionError{err: fmt.Errorf("UntarInstallimageTgzFailed: %w", out.Err)}
}

// Execute install image
out := sshClient.ExecuteInstallImage(postInstallScript != "")
out = sshClient.ExecuteInstallImage(postInstallScript != "")
if out.Err != nil {
record.Warnf(s.scope.HetznerBareMetalHost, "ExecuteInstallImageFailed", out.StdOut)
return actionError{err: fmt.Errorf("failed to execute installimage: %w", out.Err)}
Expand Down

0 comments on commit 84a8470

Please sign in to comment.