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

[controller] Static linked nsenter, lsblk and lvm utils #30

Merged
merged 16 commits into from
Mar 15, 2024
Merged
2 changes: 2 additions & 0 deletions .github/workflows/build_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ env:
MODULES_MODULE_SOURCE: ${{ vars.DEV_MODULE_SOURCE }}
MODULES_REGISTRY_LOGIN: ${{ vars.DEV_MODULES_REGISTRY_LOGIN }}
MODULES_REGISTRY_PASSWORD: ${{ secrets.DEV_MODULES_REGISTRY_PASSWORD }}
# for ex https://user:[email protected]/group
SOURCE_REPO: "${{ secrets.SOURCE_REPO }}"

on:
pull_request:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ env:
MODULES_REGISTRY_LOGIN: ${{ vars.PROD_MODULES_REGISTRY_LOGIN }}
MODULES_REGISTRY_PASSWORD: ${{ secrets.PROD_MODULES_REGISTRY_PASSWORD }}
MODULES_MODULE_TAG: ${{ github.ref_name }}
# for ex https://user:[email protected]/group
SOURCE_REPO: "${{ secrets.SOURCE_REPO }}"

on:
push:
Expand Down
2 changes: 2 additions & 0 deletions .werf/images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
image: images/{{ $ctx.ImageName }}
context: images/{{ $ctx.ImageName }}
dockerfile: Dockerfile
args:
SOURCE_REPO: {{ env "SOURCE_REPO" | default "https://github.com" }}

{{- /* For werf.inc.yaml render content by providing the ImageName param. */ -}}
{{- else }}
Expand Down
4 changes: 4 additions & 0 deletions crds/lvmvolumegroup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ spec:
properties:
spec:
type: object
required:
- type
- blockDeviceNames
- actualVGNameOnTheNode
properties:
type:
type: string
Expand Down
52 changes: 52 additions & 0 deletions images/agent/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
ARG GOLANG_20_ALPINE_BUILDER=registry.deckhouse.io/base_images/golang:1.20.5-alpine3.18@sha256:51a47fb0851397db2f506c15c426735bc23de31177cbdd962880c0879d1906a4
ARG UBUNTU_UTILS_BUILDER=registry.deckhouse.io/base_images/ubuntu:jammy-20221130@sha256:c14c3b1242536729ce5227ff833144977b4e378723858fb73a4cf40ea6daaf6a
ARG BASE_IMAGE=registry.deckhouse.io/base_images/scratch@sha256:b054705fcc9f2205777d80a558d920c0b4209efdc3163c22b5bfcb5dda1db5fc

#################################
FROM $UBUNTU_UTILS_BUILDER as util-linux-builder
ARG SOURCE_REPO
ARG UTIL_LINUX_VERSION=2.39.3

RUN apt-get update && apt-get install -y \
build-essential \
git \
pkg-config \
autopoint \
autoconf \
bison \
libtool \
automake \
gettext \
flex \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /util-linux

RUN git clone ${SOURCE_REPO}/util-linux/util-linux.git . && \
git checkout v${UTIL_LINUX_VERSION} && \
./autogen.sh && \
./configure LDFLAGS="-static" --enable-static-programs -disable-shared

RUN make LDFLAGS="--static" nsenter

#################################
FROM $GOLANG_20_ALPINE_BUILDER as agent-builder
WORKDIR /go/src

ADD go.mod .
ADD go.sum .

RUN go mod download

COPY . .

WORKDIR /go/src/cmd
RUN GOOS=linux GOARCH=amd64 go build -o sds-node-configurator-agent

#################################
FROM --platform=linux/amd64 $BASE_IMAGE

COPY --from=util-linux-builder /util-linux/nsenter /opt/deckhouse/sds/nsenter.static
COPY --from=agent-builder /go/src/cmd/sds-node-configurator-agent /go/src/cmd/sds-node-configurator-agent

CMD ["/go/src/cmd/sds-node-configurator-agent"]
File renamed without changes.
3 changes: 2 additions & 1 deletion images/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"os/exec"
"sds-node-configurator/internal"
"sds-node-configurator/pkg/logger"
"time"
)
Expand Down Expand Up @@ -80,7 +81,7 @@ func getMachineId() (string, error) {
args := []string{"-m", "-u", "-i", "-n", "-p", "-t", "1", "cat", "/etc/machine-id"}

var stdout bytes.Buffer
cmd := exec.Command("/usr/bin/nsenter", args...)
cmd := exec.Command(internal.NSENTERCmd, args...)
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions images/agent/internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const (
ResizeDelta = "32Mi"
KubernetesApiRequestLimit = 5
KubernetesApiRequestTimeout = 1
NSENTERCmd = "/opt/deckhouse/sds/nsenter.static"
LSBLKCmd = "/opt/deckhouse/sds/lsblk.static"
LVMCmd = "/opt/deckhouse/sds/lvm.static"
)

var (
Expand Down
66 changes: 30 additions & 36 deletions images/agent/pkg/controller/lvm_volume_group_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func RunLVMVolumeGroupWatcherController(
}

createFunc := func(ctx context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) {
log.Info(fmt.Sprintf("[RunLVMVolumeGroupController] event create LVMVolumeGroup, name: %s", e.Object.GetName()))
log.Info(fmt.Sprintf("[RunLVMVolumeGroupController] Get event CREATE for resource LVMVolumeGroup, name: %s", e.Object.GetName()))

request := reconcile.Request{NamespacedName: types.NamespacedName{Namespace: e.Object.GetNamespace(), Name: e.Object.GetName()}}
shouldRequeue, err := ReconcileLVMVG(ctx, metrics, e.Object.GetName(), e.Object.GetNamespace(), cfg.NodeName, log, cl)
Expand All @@ -92,7 +92,7 @@ func RunLVMVolumeGroupWatcherController(
}

updateFunc := func(ctx context.Context, e event.UpdateEvent, q workqueue.RateLimitingInterface) {
log.Info(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] update LVMVolumeGroupn, name: %s", e.ObjectNew.GetName()))
log.Info(fmt.Sprintf("[RunLVMVolumeGroupWatcherController] Get event UPDATE for resource LVMVolumeGroup, name: %s", e.ObjectNew.GetName()))

newLVG, ok := e.ObjectNew.(*v1alpha1.LvmVolumeGroup)
if !ok {
Expand Down Expand Up @@ -198,41 +198,34 @@ func ReconcileLVMVG(
lvg, err := getLVMVolumeGroup(ctx, cl, metrics, objectNameSpace, objectName)
if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error getLVMVolumeGroup, objectname: %s", objectName))
return true, err
return false, err
}
validation, status, err := ValidateLVMGroup(ctx, cl, metrics, lvg, objectNameSpace, nodeName)

if lvg == nil {
err = errors.New("nil pointer detected")
log.Error(err, "[ReconcileLVMVG] requested LVMVG group in nil")
return true, err
}

if status.Health == NonOperational {
health := status.Health
var message string
if err != nil {
message = err.Error()
}

log.Error(err, fmt.Sprintf("[ReconcileLVMVG] ValidateLVMGroup, resource name: %s, message: %s", lvg.Name, message))
err = updateLVMVolumeGroupHealthStatus(ctx, cl, metrics, lvg.Name, lvg.Namespace, message, health)
if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error update LVMVolumeGroup %s", lvg.Name))
return true, err
}
}
isOwnedByNode, status, err := CheckLVMVGNodeOwnership(ctx, cl, metrics, lvg, objectNameSpace, nodeName)

if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] validationLVMGroup failed, resource name: %s", lvg.Name))
return false, err
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error CheckLVMVGNodeOwnership, resource name: %s", lvg.Name))
if status.Health == NonOperational {
health := status.Health
message := status.Message
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] ValidateLVMGroup, resource name: %s, health: %s, phase: %s, message: %s", lvg.Name, health, status.Phase, message))
err = updateLVMVolumeGroupHealthStatus(ctx, cl, metrics, lvg.Name, lvg.Namespace, message, health)
if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error update LVMVolumeGroup %s", lvg.Name))
return true, err
}
}
return true, err
}

if validation == false {
err = errors.New("resource validation failed")
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] validation failed for resource, name: %s", lvg.Name))
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] status.Message = %s", status.Message))
return false, err
if !isOwnedByNode {
log.Debug(fmt.Sprintf("[ReconcileLVMVG] resource is not owned by node, name: %s, skip it", lvg.Name))
return false, nil
}

log.Info("[ReconcileLVMVG] validation passed")
Expand Down Expand Up @@ -272,14 +265,14 @@ func ReconcileLVMVG(
}
log.Info(fmt.Sprintf(`[ReconcileLVMVG] event was created for resource, name: %s`, lvg.Name))

existVG, err := ExistVG(lvg.Spec.ActualVGNameOnTheNode, log, metrics)
isVgExist, vg, err := GetVGFromNode(lvg.Spec.ActualVGNameOnTheNode, log, metrics)
if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error ExistVG, name: %s", lvg.Spec.ActualVGNameOnTheNode))
return true, err
}
if existVG {
log.Debug("[ReconcileLVMVG] tries to update ")
updated, err := UpdateLVMVolumeGroupTagsName(log, metrics, lvg)
if isVgExist {
log.Debug("[ReconcileLVMVG] start UpdateLVMVolumeGroupTagsName for r " + lvg.Name)
updated, err := UpdateLVMVolumeGroupTagsName(log, metrics, vg, lvg)
if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] unable to update VG tags on VG, name: %s", lvg.Spec.ActualVGNameOnTheNode))
return true, err
Expand All @@ -292,16 +285,16 @@ func ReconcileLVMVG(
}

log.Info("[ReconcileLVMVG] validation and choosing the type of operation")
extendPVs, shrinkPVs, err := ValidateTypeLVMGroup(ctx, cl, metrics, lvg, log)
extendPVs, shrinkPVs, err := ValidateOperationTypeLVMGroup(ctx, cl, metrics, lvg, log)
if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error ValidateTypeLVMGroup, name: %s", lvg.Name))
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error ValidateOperationTypeLVMGroup, name: %s", lvg.Name))
return true, err
}

if err == nil && extendPVs == nil && shrinkPVs == nil {
log.Warning("[ReconcileLVMVG] ValidateTypeLVMGroup FAIL")
//todo retry and send message
}
// if err == nil && extendPVs == nil && shrinkPVs == nil {
// log.Warning(fmt.Sprintf("[ReconcileLVMVG] ValidateOperationTypeLVMGroup FAIL for resource %s", lvg.Name))
// //todo retry and send message
// }

log.Debug("----- extendPVs list -----")
for _, pvExt := range extendPVs {
Expand Down Expand Up @@ -449,6 +442,7 @@ func ReconcileLVMVG(
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] error CreateEventLVMVolumeGroup, resource name: %s", lvg.Name))
}

log.Debug("[ReconcileLVMVG] Start CreateVGComplex function for resource " + lvg.Name)
err := CreateVGComplex(ctx, cl, metrics, lvg, log)
if err != nil {
log.Error(err, fmt.Sprintf("[ReconcileLVMVG] unable to CreateVGComplex for resource, name: %s", lvg.Name))
Expand Down
Loading