Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Improvements #1

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/Realease.yaml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a big deal, but typo in the file name if it's meant to be "release.yaml".

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Release with goreleaser

on:
push:
tags:
- v*

permissions: write-all # Necessary for the generate-build-provenance action with containers

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up latest stable Go
uses: actions/setup-go@v5
with:
go-version: stable
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Checkout
uses: actions/checkout@v4
with:
fetch-tags: 1
fetch-depth: 1

# Set environment variables required by GoReleaser
- name: Set build environment variables
run: |
echo "GIT_STATE=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)" >> $GITHUB_ENV
echo "BUILD_HOST=$(hostname)" >> $GITHUB_ENV
echo "GO_VERSION=$(go version | awk '{print $3}')" >> $GITHUB_ENV
echo "BUILD_USER=$(whoami)" >> $GITHUB_ENV
echo "CGO_ENABLED=1" >> $GITHUB_ENV
- name: Docker Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Release with goreleaser
uses: goreleaser/goreleaser-action@v6
env:
GITHUB_TOKEN: ${{ github.token }}
with:
version: '~> v2'
args: release --clean
id: goreleaser

- name: Process goreleaser output
id: process_goreleaser_output
run: |
echo "const fs = require('fs');" > process.js
echo 'const artifacts = ${{ steps.goreleaser.outputs.artifacts }}' >> process.js
echo "const firstNonNullDigest = artifacts.find(artifact => artifact.extra && artifact.extra.Digest != null)?.extra.Digest;" >> process.js
echo "console.log(firstNonNullDigest);" >> process.js
echo "fs.writeFileSync('digest.txt', firstNonNullDigest);" >> process.js
node process.js
echo "digest=$(cat digest.txt)" >> $GITHUB_OUTPUT

- name: Attest coredhcp binary linux_amd64
uses: actions/attest-build-provenance@v1
with:
subject-path: dist/coredhcp_linux_amd64_v3/coredhcp

- name: Attest coredhcp binary linux_arm64
uses: actions/attest-build-provenance@v1
with:
subject-path: dist/coredhcp_linux_arm64/coredhcp

- name: generate build provenance of docker container
uses: actions/attest-build-provenance@v1
with:
subject-name: ghcr.io/openchami/coredhcp
subject-digest: ${{ steps.process_goreleaser_output.outputs.digest }}
push-to-registry: true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.tgz
*.swp
.vscode
.idea
dist/
97 changes: 97 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
version: 2

project_name: coredhcp
before:
hooks:
- go install github.com/coredhcp/coredhcp/cmds/coredhcp-generator@latest
- mkdir -p cmd/
- coredhcp-generator -t generator/coredhcp.go.template -f generator/plugins.txt github.com/OpenCHAMI/coresmd/coresmd github.com/OpenCHAMI/coresmd/bootloop -o cmd/coredhcp.go
- go mod tidy

builds:
- id: coredhcp
main: ./cmd/
goos:
- linux
goarch:
- amd64
- arm64
goamd64:
- v3

# export GIT_STATE=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be removed since the vars are defined in Realease.yaml?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it in as a comment so that people who are using goreleaser locally have a guide to follow.

# export BUILD_HOST=$(hostname)
# export GO_VERSION=$(go version | awk '{print $3}')
# export BUILD_USER=$(whoami)
ldflags:
- "-s -w -X main.GitCommit={{.Commit}} \
-X main.BuildTime={{.Timestamp}} \
-X main.Version={{.Version}} \
-X main.GitBranch={{.Branch}} \
-X main.GitTag={{.Tag}} \
-X main.GitState={{ .Env.GIT_STATE }} \
-X main.BuildHost={{ .Env.BUILD_HOST }} \
-X main.GoVersion={{ .Env.GO_VERSION }} \
-X main.BuildUser={{ .Env.BUILD_USER }} "
binary: coredhcp
env:
- CGO_ENABLED=0


dockers:
- image_templates:
- &amd64_linux_image ghcr.io/openchami/{{.ProjectName}}:{{ .Tag }}-amd64
- ghcr.io/openchami/{{.ProjectName}}:{{ .Major }}-amd64
- ghcr.io/openchami/{{.ProjectName}}:{{ .Major }}.{{ .Minor }}-amd64
use: buildx
build_flag_templates:
- "--pull"
- "--platform=linux/amd64"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
goarch: amd64
goamd64: v3
extra_files:
- LICENSE
- README.md

- image_templates:
- &arm64v8_linux_image ghcr.io/openchami/{{.ProjectName}}:{{ .Tag }}-arm64
- ghcr.io/openchami/{{.ProjectName}}:{{ .Major }}-arm64
- ghcr.io/openchami/{{.ProjectName}}:{{ .Major }}.{{ .Minor }}-arm64
use: buildx
build_flag_templates:
- "--pull"
- "--platform=linux/arm64"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
extra_files:
- README.md
- LICENSE
goarch: arm64

docker_manifests:
- name_template: "ghcr.io/openchami/{{.ProjectName}}:latest"
image_templates:
- *amd64_linux_image
- *arm64v8_linux_image

- name_template: "ghcr.io/openchami/{{.ProjectName}}:{{ .Tag }}"
image_templates:
- *amd64_linux_image
- *arm64v8_linux_image

- name_template: "ghcr.io/openchami/{{.ProjectName}}:{{ .Major }}"
image_templates:
- *amd64_linux_image
- *arm64v8_linux_image

- name_template: "ghcr.io/openchami/{{.ProjectName}}:{{ .Major }}.{{ .Minor }}"
image_templates:
- *amd64_linux_image
- *arm64v8_linux_image

71 changes: 13 additions & 58 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,65 +1,20 @@
################################################################################
# STAGE 1: Build CoreDHCP
################################################################################
FROM chainguard/wolfi-base:latest

FROM golang:1.21 AS builder
ARG CGO_ENABLED=1
RUN apk add --no-cache tini

#
# STEP 1: Clone coredhcp and build coredhcp-generator
#
# Include curl in the final image.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean tini instead of curl?

RUN set -ex \
&& apk update \
&& apk add --no-cache curl tini \
&& rm -rf /var/cache/apk/* \
&& rm -rf /tmp/*

RUN git clone https://github.com/coredhcp/coredhcp /coredhcp
WORKDIR /coredhcp
COPY coredhcp /coredhcp

RUN go mod download
RUN go build ./cmds/coredhcp-generator

#
# STEP 2: Copy source tree and generate CoreDHCP main.go
#
# nobody 65534:65534
USER 65534:65534

WORKDIR /coresmd
COPY go.mod go.sum ./
RUN go mod edit -replace=github.com/OpenCHAMI/coresmd=/coresmd
RUN go mod download
COPY . .
RUN ./gen_version.bash
CMD [ "/coredhcp" ]

RUN mkdir /coredhcp-coresmd
WORKDIR /coredhcp-coresmd

RUN /coredhcp/coredhcp-generator \
-t /coredhcp/cmds/coredhcp-generator/coredhcp.go.template \
-f /coredhcp/cmds/coredhcp-generator/core-plugins.txt \
-o /coredhcp-coresmd/coredhcp.go \
github.com/OpenCHAMI/coresmd/coresmd \
github.com/OpenCHAMI/coresmd/bootloop

#
# STEP 3: Build CoreDHCP
#

RUN go mod init coredhcp
RUN go mod edit -replace=github.com/coredhcp/coredhcp=/coredhcp
RUN go mod edit -replace=github.com/OpenCHAMI/coresmd=/coresmd
RUN go mod tidy
RUN go build -o coredhcp

################################################################################
# STAGE 2: Copy CoreDHCP to final location
################################################################################

FROM cgr.dev/chainguard/wolfi-base

#RUN apk add --no-cache tini

COPY --from=builder /coredhcp-coresmd/coredhcp /bin/coredhcp

EXPOSE 67 67/udp

# Make dir for config file
RUN mkdir -p /etc/coredhcp
VOLUME /etc/coredhcp

ENTRYPOINT [ "/bin/coredhcp" ]
ENTRYPOINT [ "/sbin/tini", "--" ]
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
MIT License

Copyright © 2024 Triad National Security, LLC. All rights reserved.
This program was produced under U.S. Government contract 89233218CNA000001
for Los Alamos National Laboratory (LANL), which is operated by Triad
National Security, LLC for the U.S. Department of Energy/National Nuclear
Security Administration.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@ This is meant to be built statically into
[CoreDHCP](https://github.com/coredhcp/coredhcp) using the
[coredhcp-generator](https://github.com/coredhcp/coredhcp/blob/master/cmds/coredhcp-generator).


## Build/Install with goreleaser

This project uses [GoReleaser](https://goreleaser.com/) to automate releases and include additional build metadata such as commit info, build time, and versioning. Below is a guide on how to set up and build the project locally using GoReleaser.

### Environment Variables

To include detailed build metadata, ensure the following environment variables are set:

* __GIT_STATE__: Indicates whether there are uncommitted changes in the working directory. Set to clean if the repository is clean, or dirty if there are uncommitted changes.
* __BUILD_HOST__: The hostname of the machine where the build is being performed.
* __GO_VERSION__: The version of Go used for the build. GoReleaser uses this to ensure consistent Go versioning information.
* __BUILD_USER__: The username of the person or system performing the build.

Set all the environment variables with:
```bash
export GIT_STATE=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)
export BUILD_HOST=$(hostname)
export GO_VERSION=$(go version | awk '{print $3}')
export BUILD_USER=$(whoami)
```

### Building Locally with GoReleaser

Once the environment variables are set, you can build the project locally using GoReleaser in snapshot mode (to avoid publishing).


Follow the installation instructions from [GoReleaser’s documentation](https://goreleaser.com/install/).

1. Run GoReleaser in snapshot mode with the --snapshot and --skip-publish flags to create a local build without attempting to release it:
```bash
goreleaser release --snapshot --skip-publish --clean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--skip-publish should be --skip publish.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on cluster and --skip-publish wasn't recognized as an option but --skip publush was.

```
2. Check the dist/ directory for the built binaries, which will include the metadata from the environment variables. You can inspect the binary output to confirm that the metadata was correctly embedded.


### Container

This repository includes a Dockerfile that builds CoreDHCP with its core plugins
Expand Down
16 changes: 8 additions & 8 deletions bootloop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ import (
"sync"
"time"

"github.com/OpenCHAMI/coresmd/internal/debug"
"github.com/OpenCHAMI/coresmd/internal/ipxe"
"github.com/OpenCHAMI/coresmd/internal/version"
"github.com/coredhcp/coredhcp/handler"
"github.com/coredhcp/coredhcp/logger"
"github.com/coredhcp/coredhcp/plugins"
"github.com/coredhcp/coredhcp/plugins/allocators"
"github.com/coredhcp/coredhcp/plugins/allocators/bitmap"
"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/OpenCHAMI/coresmd/internal/debug"
"github.com/OpenCHAMI/coresmd/internal/ipxe"
"github.com/OpenCHAMI/coresmd/internal/version"
)

// Record holds an IP lease record
type Record struct {
IP net.IP
expires int
IP net.IP
expires int
hostname string
}

Expand Down Expand Up @@ -58,7 +58,7 @@ func setup6(args ...string) (handler.Handler6, error) {
}

func setup4(args ...string) (handler.Handler4, error) {
log.Infof("initializing coresmd/bootloop %s (%s), built %s", version.Version, version.Commit, version.Date)
log.Infof("initializing coresmd/bootloop %s (%s), built %s", version.Version, version.GitCommit, version.BuildTime)

// Ensure all required args were passed
if len(args) != 4 {
Expand Down Expand Up @@ -149,8 +149,8 @@ func (p *PluginState) Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool)
return nil, true
}
rec := Record{
IP: ip.IP.To4(),
expires: int(time.Now().Add(p.LeaseTime).Unix()),
IP: ip.IP.To4(),
expires: int(time.Now().Add(p.LeaseTime).Unix()),
hostname: hostname,
}
err = p.saveIPAddress(req.ClientHWAddr, &rec)
Expand Down
Loading