Skip to content

Commit

Permalink
fix(manager): expose agent image as flags
Browse files Browse the repository at this point in the history
Signed-off-by: Zespre Schmidt <[email protected]>
  • Loading branch information
starbops committed Nov 17, 2024
1 parent 6b57e54 commit b7e356c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
FROM golang:1.23.2 AS builder
ARG TARGETOS
ARG TARGETARCH
ARG LINKFLAGS

WORKDIR /workspace
# Copy the Go Modules manifests
Expand All @@ -21,7 +22,7 @@ COPY internal/controller/ internal/controller/
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/controller/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -ldflags "${LINKFLAGS}" -o manager cmd/controller/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.virtbmc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
FROM golang:1.23.2 AS builder
ARG TARGETOS
ARG TARGETARCH
ARG LINKFLAGS

WORKDIR /workspace
# Copy the Go Modules manifests
Expand All @@ -20,7 +21,7 @@ COPY pkg/ pkg/
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o virtbmc cmd/virtbmc/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -ldflags "${LINKFLAGS}" -o virtbmc cmd/virtbmc/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
38 changes: 24 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# VERSION and COMMIT are set by the CI/CD pipeline. If not set, they are set to
# the current branch and commit.
VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null || echo "$(shell git rev-parse --abbrev-ref HEAD)-head")
COMMIT ?= $(shell git rev-parse --short HEAD)

REPO ?= starbops

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
MGR_IMG ?= $(REPO)/virtbmc-controller:$(VERSION)
AGT_IMG ?= $(REPO)/virtbmc:$(VERSION)
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.28.0

Expand Down Expand Up @@ -94,10 +101,12 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes

##@ Build

LINKFLAGS := "-X main.AppVersion=$(VERSION) -X main.GitCommit=$(COMMIT)"

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/controller/main.go
go build -o bin/virtbmc cmd/virtbmc/main.go
go build -ldflags $(LINKFLAGS) -o bin/manager cmd/controller/main.go
go build -ldflags $(LINKFLAGS) -o bin/virtbmc cmd/virtbmc/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand All @@ -107,16 +116,13 @@ run: manifests generate fmt vet ## Run a controller from your host.
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .

.PHONY: docker-build-virtbmc
docker-build-virtbmc: ## Builder docker image with the virtbmc binary.
$(CONTAINER_TOOL) build -t ${IMG} --build-arg TARGETARCH=amd64 -f Dockerfile.virtbmc .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
$(CONTAINER_TOOL) push ${IMG}
docker-build: ## Build docker images with the manager and the agent respectively.
$(CONTAINER_TOOL) build -t $(MGR_IMG) --build-arg LINKFLAGS=$(LINKFLAGS) .
$(CONTAINER_TOOL) build -t $(AGT_IMG) --build-arg LINKFLAGS=$(LINKFLAGS) --build-arg TARGETARCH=amd64 -f Dockerfile.virtbmc .
ifeq ($(PUSH),true)
$(CONTAINER_TOOL) push $(MGR_IMG)
$(CONTAINER_TOOL) push $(AGT_IMG)
endif

# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
Expand All @@ -129,11 +135,14 @@ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
docker-buildx: ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile.virtbmc > Dockerfile.virtbmc.cross
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
$(CONTAINER_TOOL) buildx use project-v3-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --build-arg LINKFLAGS=$(LINKFLAGS) --tag $(MGR_IMG) -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --build-arg LINKFLAGS=$(LINKFLAGS) --tag $(AGT_IMG) -f Dockerfile.virtbmc.cross .
- $(CONTAINER_TOOL) buildx rm project-v3-builder
rm Dockerfile.cross
rm Dockerfile.virtbmc.cross

##@ Deployment

Expand All @@ -149,6 +158,7 @@ install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -

IMG ?= $(MGR_IMG)
.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ type VirtualMachineBMCStatus struct {

### Develop

**Build and push the images to the location specified by `IMG`:**
**Build and push the images:**

```sh
make docker-build docker-push IMG=<some-registry>/virtbmc-controller:<tag>
make docker-build-virtbmc docker-push IMG=<some-registry>/virtbmc:<tag>
export PUSH=true
make docker-build
```

> **NOTE:** These images ought to be published in the personal registry you specified. And it is required to have access to pull the images from the working environment. Make sure you have the proper permission to the registry if the above commands don’t work.
Expand Down
31 changes: 26 additions & 5 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -42,6 +43,9 @@ import (
)

var (
AppVersion = "dev"
GitCommit = "none"

scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
Expand All @@ -54,20 +58,35 @@ func init() {
}

func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var (
metricsAddr string
enableLeaderElection bool
probeAddr string

agentImageName string
agentImageTag string
)
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&agentImageName, "agent-image-name", ctlvirtualmachinebmc.VirtBMCImageName,
"The name of the agent image.")
flag.StringVar(&agentImageTag, "agent-image-tag", AppVersion, "The tag of the agent image.")
showVersion := flag.Bool("version", false, "Show version.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()

if *showVersion {
fmt.Println("Version:", AppVersion)
fmt.Println("Git commit:", GitCommit)
os.Exit(0)
}

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Expand All @@ -94,8 +113,10 @@ func main() {
}

if err = (&ctlvirtualmachinebmc.VirtualMachineBMCReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
AgentImageName: agentImageName,
AgentImageTag: agentImageTag,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "VirtualMachineBMC")
os.Exit(1)
Expand Down
18 changes: 18 additions & 0 deletions cmd/virtbmc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"kubevirt.io/kubevirtbmc/pkg/virtbmc"
)

var (
AppVersion = "dev"
GitCommit = "none"
)

func main() {
var options virtbmc.Options

Expand Down Expand Up @@ -39,6 +44,19 @@ func main() {
Usage: "listen on `PORT`",
Destination: &options.Port,
},
&cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "print the version",
Action: func(c *cli.Context, b bool) error {
if b {
fmt.Println("Version:", AppVersion)
fmt.Println("Git commit:", GitCommit)
os.Exit(0)
}
return nil
},
},
},
Action: func(cCtx *cli.Context) error {
ctx := context.WithValue(cCtx.Context, virtbmc.VMNamespaceKey{}, cCtx.Args().Get(0))
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/virtualmachinebmc/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const (
DefaultUsername = "admin"
DefaultPassword = "password"
virtBMCContainerName = "virtbmc"
virtBMCImageName = "starbops/virtbmc"
virtBMCImageTag = "dev"
VirtBMCImageName = "starbops/virtbmc"
ipmiPort = 10623
IPMISvcPort = 623
ipmiPortName = "ipmi"
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/virtualmachinebmc/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import (
type VirtualMachineBMCReconciler struct {
client.Client
Scheme *runtime.Scheme

AgentImageName string
AgentImageTag string
}

var (
Expand All @@ -60,7 +63,7 @@ func (r *VirtualMachineBMCReconciler) constructPodFromVirtualMachineBMC(virtualM
Containers: []corev1.Container{
{
Name: virtBMCContainerName,
Image: fmt.Sprintf("%s:%s", virtBMCImageName, virtBMCImageTag),
Image: fmt.Sprintf("%s:%s", r.AgentImageName, r.AgentImageTag),
Args: []string{
"--address",
"0.0.0.0",
Expand Down

0 comments on commit b7e356c

Please sign in to comment.