Skip to content

Commit

Permalink
feat: pv/pvc cli helper
Browse files Browse the repository at this point in the history
Implement very common tools to work with Proxmox CSI disks.
Like:
  * migrate - helps to move block devices from one Proxmox node to another.
  * rename  - helps to change name of pvc.

Signed-off-by: Serge Logvinov <[email protected]>
  • Loading branch information
sergelogvinov committed Feb 15, 2024
1 parent ffad744 commit d97bc32
Show file tree
Hide file tree
Showing 15 changed files with 1,168 additions and 16 deletions.
51 changes: 51 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
project_name: pvecsi-mutate

before:
hooks:
- go mod download
- make release-update

dist: bin
builds:
- dir: cmd/pvecsi-mutate
binary: pvecsi-mutate-{{ .Os }}-{{ .Arch }}
no_unique_dist_dir: true
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
goarch:
- amd64
- arm64

checksum:
name_template: "checksums.txt"

snapshot:
name_template: edge

dockers:
- use: buildx
image_templates:
- ghcr.io/sergelogvinov/pvecsi-mutate:{{ .Version }}-amd64
goos: linux
goarch: amd64
build_flag_templates:
- "--label=org.opencontainers.image.version={{.Version}}"
- "--target=pvecsi-mutate-goreleaser"
- "--platform=linux/amd64"
- use: buildx
image_templates:
- ghcr.io/sergelogvinov/pvecsi-mutate:{{ .Version }}-arm64
goos: linux
goarch: arm64
build_flag_templates:
- "--label=org.opencontainers.image.version={{.Version}}"
- "--target=pvecsi-mutate-goreleaser"
- "--platform=linux/arm64"
docker_manifests:
- name_template: ghcr.io/sergelogvinov/{{ .ProjectName }}:{{ .Version }}
image_templates:
- ghcr.io/sergelogvinov/{{ .ProjectName }}:{{ .Version }}-amd64
- ghcr.io/sergelogvinov/{{ .ProjectName }}:{{ .Version }}-arm64
30 changes: 27 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN go mod download

########################################

FROM --platform=${BUILDPLATFORM} golang:1.21.4-alpine3.18 AS builder
FROM --platform=${BUILDPLATFORM} golang:1.21.6-alpine3.18 AS builder
RUN apk update && apk add --no-cache make
ENV GO111MODULE on
WORKDIR /src
Expand All @@ -24,7 +24,7 @@ RUN make build-all-archs

########################################

FROM --platform=${TARGETARCH} scratch AS controller
FROM --platform=${TARGETARCH} scratch AS proxmox-csi-controller
LABEL org.opencontainers.image.source="https://github.com/sergelogvinov/proxmox-csi-plugin" \
org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.description="Proxmox VE CSI plugin"
Expand Down Expand Up @@ -65,7 +65,7 @@ RUN /tools/deps-check.sh

########################################

FROM --platform=${TARGETARCH} scratch AS node
FROM --platform=${TARGETARCH} scratch AS proxmox-csi-node
LABEL org.opencontainers.image.source="https://github.com/sergelogvinov/proxmox-csi-plugin" \
org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.description="Proxmox VE CSI plugin"
Expand All @@ -77,3 +77,27 @@ ARG TARGETARCH
COPY --from=builder /src/bin/proxmox-csi-node-${TARGETARCH} /bin/proxmox-csi-node

ENTRYPOINT ["/bin/proxmox-csi-node"]

########################################

FROM alpine:3.18 AS pvecsi-mutate
LABEL org.opencontainers.image.source="https://github.com/sergelogvinov/proxmox-csi-plugin" \
org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.description="Proxmox VE CSI tools"

ARG TARGETARCH
COPY --from=builder /src/bin/pvecsi-mutate-${TARGETARCH} /bin/pvecsi-mutate

ENTRYPOINT ["/bin/pvecsi-mutate"]

########################################

FROM alpine:3.18 AS pvecsi-mutate-goreleaser
LABEL org.opencontainers.image.source="https://github.com/sergelogvinov/proxmox-csi-plugin" \
org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.description="Proxmox VE CSI tools"

ARG TARGETARCH
COPY pvecsi-mutate-linux-${TARGETARCH} /bin/pvecsi-mutate

ENTRYPOINT ["/bin/pvecsi-mutate"]
29 changes: 17 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
REGISTRY ?= ghcr.io
USERNAME ?= sergelogvinov
PROJECT ?= proxmox-csi
IMAGE ?= $(REGISTRY)/$(USERNAME)/$(PROJECT)
OCIREPO ?= $(REGISTRY)/$(USERNAME)
HELMREPO ?= $(REGISTRY)/$(USERNAME)/charts
PLATFORM ?= linux/arm64,linux/amd64
PUSH ?= false

SHA ?= $(shell git describe --match=none --always --abbrev=8 --dirty)
SHA ?= $(shell git describe --match=none --always --abbrev=7 --dirty)
TAG ?= $(shell git describe --tag --always --match v[0-9]\*)
GO_LDFLAGS := -ldflags "-w -s -X main.version=$(SHA)"
GO_LDFLAGS := -ldflags "-w -s -X main.version=$(TAG) -X main.commit=$(SHA)"

OS ?= $(shell go env GOOS)
ARCH ?= $(shell go env GOARCH)
ARCHS = amd64 arm64

BUILD_ARGS := --platform=$(PLATFORM)
ifeq ($(PUSH),true)
BUILD_ARGS += --push=$(PUSH)
BUILD_ARGS += --push=$(PUSH) --output type=image,annotation-index.org.opencontainers.image.source="https://github.com/$(USERNAME)/proxmox-csi-plugin"
else
BUILD_ARGS += --output type=docker
endif
Expand Down Expand Up @@ -57,12 +56,16 @@ build-all-archs:
clean: ## Clean
rm -rf bin .cache

build-pvecsi-mutate:
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build $(GO_LDFLAGS) \
-o bin/pvecsi-mutate-$(ARCH) ./cmd/pvecsi-mutate

build-%:
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build $(GO_LDFLAGS) \
-o bin/proxmox-csi-$*-$(ARCH) ./cmd/$*

.PHONY: build
build: build-controller build-node ## Build
build: build-controller build-node build-pvecsi-mutate ## Build

.PHONY: run
run: build-controller ## Run
Expand Down Expand Up @@ -133,19 +136,21 @@ image-%:
docker buildx build $(BUILD_ARGS) \
--build-arg TAG=$(TAG) \
--build-arg SHA=$(SHA) \
-t $(IMAGE)-$*:$(TAG) \
-t $(OCIREPO)/$*:$(TAG) \
--target $* \
-f Dockerfile .

.PHONY: images-checks
images-checks: images image-tools-check
trivy image --exit-code 1 --ignore-unfixed --severity HIGH,CRITICAL --no-progress $(IMAGE)-controller:$(TAG)
trivy image --exit-code 1 --ignore-unfixed --severity HIGH,CRITICAL --no-progress $(IMAGE)-node:$(TAG)
trivy image --exit-code 1 --ignore-unfixed --severity HIGH,CRITICAL --no-progress $(OCIREPO)/proxmox-csi-controller:$(TAG)
trivy image --exit-code 1 --ignore-unfixed --severity HIGH,CRITICAL --no-progress $(OCIREPO)/proxmox-csi-node:$(TAG)
trivy image --exit-code 1 --ignore-unfixed --severity HIGH,CRITICAL --no-progress $(OCIREPO)/pvecsi-mutate:$(TAG)

.PHONY: images-cosign
images-cosign:
@cosign sign --yes $(COSING_ARGS) --recursive $(IMAGE)-controller:$(TAG)
@cosign sign --yes $(COSING_ARGS) --recursive $(IMAGE)-node:$(TAG)
@cosign sign --yes $(COSING_ARGS) --recursive $(OCIREPO)/proxmox-csi-controller:$(TAG)
@cosign sign --yes $(COSING_ARGS) --recursive $(OCIREPO)/proxmox-csi-node:$(TAG)
@cosign sign --yes $(COSING_ARGS) --recursive $(OCIREPO)/pvecsi-mutate:$(TAG)

.PHONY: images
images: image-controller image-node ## Build images
images: image-proxmox-csi-controller image-proxmox-csi-node image-pvecsi-mutate ## Build images
103 changes: 103 additions & 0 deletions cmd/pvecsi-mutate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Proxmox PV Migrate utility
package main

import (
"context"
"fmt"
"os"
"strings"

log "github.com/sirupsen/logrus"
cobra "github.com/spf13/cobra"

clilog "github.com/sergelogvinov/proxmox-csi-plugin/pkg/log"
)

var (
command = "pvecsi-mutate"
version = "v0.0.0"
commit = "none"

cloudconfig string
kubeconfig string

flagLogLevel = "log-level"

flagProxmoxConfig = "config"
flagKubeConfig = "kubeconfig"

logger *log.Entry
)

func main() {
if exitCode := run(); exitCode != 0 {
os.Exit(exitCode)
}
}

func run() int {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

l := log.New()
l.SetOutput(os.Stdout)
l.SetLevel(log.InfoLevel)

logger = l.WithContext(ctx)

cmd := cobra.Command{
Use: command,
Version: fmt.Sprintf("%s (commit: %s)", version, commit),
Short: "A command-line utility to manipulate PersistentVolume/PersistentVolumeClaim on Proxmox VE",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
f := cmd.Flags()
loglvl, _ := f.GetString(flagLogLevel) //nolint: errcheck

clilog.Configure(logger, loglvl)

return nil
},
SilenceUsage: true,
SilenceErrors: true,
}

cmd.PersistentFlags().String(flagLogLevel, clilog.LevelInfo,
fmt.Sprintf("log level, must be one of: %s", strings.Join(clilog.Levels, ", ")))

cmd.PersistentFlags().StringVar(&cloudconfig, flagProxmoxConfig, "", "proxmox cluster config file")
cmd.PersistentFlags().StringVar(&kubeconfig, flagKubeConfig, "", "kubernetes config file")

cmd.AddCommand(buildMigrateCmd())
cmd.AddCommand(buildRenameCmd())

err := cmd.ExecuteContext(ctx)
if err != nil {
errorString := err.Error()
if strings.Contains(errorString, "arg(s)") || strings.Contains(errorString, "flag") || strings.Contains(errorString, "command") {
fmt.Fprintf(os.Stderr, "Error: %s\n\n", errorString)
fmt.Fprintln(os.Stderr, cmd.UsageString())
} else {
logger.Errorf("Error: %s\n", errorString)
}

return 1
}

return 0
}
Loading

0 comments on commit d97bc32

Please sign in to comment.