Skip to content

Commit

Permalink
fix(vm): fix panic with nil labelselector (#355)
Browse files Browse the repository at this point in the history
fix panic with nil labelselector

Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat authored Sep 9, 2024
1 parent 5f585c4 commit 8df6c59
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
8 changes: 8 additions & 0 deletions images/virtualization-artifact/Taskfile.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,14 @@ tasks:
desc: "Push image virtualization-controller with dlv"
cmd: ./hack/dlv.sh push controller --image="$DLV_IMAGE"

dlv:build:apiserver:
desc: "Build image virtualization-api with dlv"
cmd: ./hack/dlv.sh build apiserver --image="$DLV_IMAGE"

dlv:build-push:apiserver:
desc: "Push image virtualization-api with dlv"
cmd: ./hack/dlv.sh push apiserver --image="$DLV_IMAGE"

_copy_d8_registry_secret:
internal: true
vars:
Expand Down
31 changes: 31 additions & 0 deletions images/virtualization-artifact/hack/dlv-apiserver.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM golang:1.21.8-bookworm@sha256:ac14cc827536ef1a124cd2f7a03178c3335c1db8ad3807e7fdd57f74096abfa0 AS builder
ARG TARGETOS
ARG TARGETARCH

WORKDIR /app/images/virtualization-artifact
RUN go install github.com/go-delve/delve/cmd/dlv@latest

COPY ./images/virtualization-artifact/go.mod /app/images/virtualization-artifact/
COPY ./images/virtualization-artifact/go.sum /app/images/virtualization-artifact/
COPY ./api/ /app/api/

RUN go mod download

COPY ./images/virtualization-artifact/cmd /app/images/virtualization-artifact/cmd
COPY ./images/virtualization-artifact/pkg /app/images/virtualization-artifact/pkg

ENV GO111MODULE=on
ENV GOOS=${TARGETOS:-linux}
ENV GOARCH=${TARGETARCH:-amd64}
ENV CGO_ENABLED=0

RUN go build -gcflags "all=-N -l" -a -o virtualization-api ./cmd/virtualization-api

FROM busybox:1.36.1-glibc

WORKDIR /app
COPY --from=builder /go/bin/dlv /app/dlv
COPY --from=builder /app/images/virtualization-artifact/virtualization-api /app/virtualization-api
USER 65532:65532

ENTRYPOINT ["./dlv", "--listen=:2345", "--headless=true", "--continue", "--log=true", "--log-output=debugger,debuglineerr,gdbwire,lldbout,rpc", "--accept-multiclient", "--api-version=2", "exec", "./virtualization-api", "--"]
49 changes: 35 additions & 14 deletions images/virtualization-artifact/hack/dlv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function usage {
Usage: $0 COMMAND OPTIONS
Commands:
build <controller> Build docker image with dlv.
push <controller> Build and Push docker image with dlv.
build <controller/apiserver> Build docker image with dlv.
push <controller/apiserver> Build and Push docker image with dlv.
Global Flags:
--image,-i (optional) The name of the image being built.
Expand All @@ -30,7 +30,7 @@ Examples:
# build"
$(basename "$0") build controller --image=myimage:latest
# push"
$(basename "$0") push controller
$(basename "$0") push apiserver
EOF
}

Expand All @@ -42,33 +42,45 @@ function usage_exit {
}

function build_controller {
build "dlv-controller.Dockerfile"
}

function build_apiserver {
build "dlv-apiserver.Dockerfile"
}

function build {
local dockerfile=$1
cd "$ROOT"
docker build -f ./images/virtualization-artifact/hack/dlv-controller.Dockerfile -t "${IMAGE}" .
docker build -f "./images/virtualization-artifact/hack/$dockerfile" -t "${IMAGE}" .
}

function push {
docker push "${IMAGE}"
}

# shellcheck disable=SC2120
function print_patches_controller {
local deployment=$1

cat <<EOF
Run commands:
kubectl -n d8-virtualization scale deployment virtualization-controller --replicas 1
kubectl -n d8-virtualization patch deployment virtualization-controller --type='json' -p '[{"op": "replace", "path": "/spec/template/spec/containers/1/image", "value": "${IMAGE}"}]'
kubectl -n d8-virtualization patch deployment virtualization-controller --type='strategic' -p '{
kubectl -n d8-virtualization scale deployment ${deployment} --replicas 1
kubectl -n d8-virtualization patch deployment ${deployment} --type='strategic' -p '{
"spec": {
"template": {
"spec": {
"containers": [{
"name": "virtualization-controller",
"name": "${deployment}",
"image": "${IMAGE}",
"ports": [{"containerPort": 2345, "name": "dlv"}]
}]
}
}
}
}'
kubectl -n d8-virtualization port-forward deployments/virtualization-controller 2345:2345
kubectl -n d8-virtualization port-forward deployments/${deployment} 2345:2345
EOF
}
Expand Down Expand Up @@ -100,26 +112,35 @@ case "$CMD" in
case "$NAME" in
"controller")
build_controller
echo "Built image ${IMAGE} successfully."
;;
"apiserver")
build_apiserver
;;
*)
usage_exit 1
;;
esac
echo "Built image ${IMAGE} successfully."
;;
"push")
deployment=""
case "$NAME" in
"controller")
build_controller
echo "Built image ${IMAGE} successfully."
push
echo "Push image ${IMAGE} successfully."
print_patches_controller
deployment="virtualization-controller"
;;
"apiserver")
build_apiserver
deployment="virtualization-api"
;;
*)
usage_exit 1
;;
esac
echo "Built image ${IMAGE} successfully."
push
echo "Push image ${IMAGE} successfully."
print_patches_controller "${deployment}"
;;
*)
usage_exit 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ func (store VirtualMachineStorage) List(ctx context.Context, options *internalve
var opts internalversion.ListOptions
if options != nil {
opts = *options
labelSelector = options.LabelSelector
}
if !(opts.LabelSelector == nil || opts.LabelSelector.Empty()) {
labelSelector = opts.LabelSelector
}

name, err := nameFor(opts.FieldSelector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func nameFor(fs fields.Selector) (string, error) {
if fs == nil {
if fs == nil || fs.Empty() {
fs = fields.Everything()
}
name, found := fs.RequiresExactMatch("metadata.name")
Expand Down

0 comments on commit 8df6c59

Please sign in to comment.