# Multi-stage build using official Go image
FROM harbor.mws-team.ru/docker.io/library/ubuntu:22.04 AS builder

# Add Ceph Reef repository
RUN apt-get update && apt-get install -y \
    wget \
    gnupg \
    gcc \
    pkg-config \
    && wget -q -O- 'https://download.ceph.com/keys/release.asc' | apt-key add - \
    && echo "deb https://download.ceph.com/debian-reef/ jammy main" > /etc/apt/sources.list.d/ceph.list

# Install Ceph Reef development libraries
RUN apt-get update && apt-get install -y \
    librbd-dev \
    librados-dev \
    && rm -rf /var/lib/apt/lists/*

# Install Go
ENV GO_VERSION=1.25.5
RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz \
    && tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz \
    && rm go${GO_VERSION}.linux-amd64.tar.gz

# Set Go environment variables
ENV PATH=$PATH:/usr/local/go/bin
ENV GOPATH=/go
ENV PATH=$PATH:$GOPATH/bin

WORKDIR /app

# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download

# Copy source code
COPY . .

# Build the application
RUN CGO_ENABLED=1 GOOS=linux go build -o rbd-exporter .


# Final stage with Ceph Reef runtime libraries
FROM harbor.mws-team.ru/docker.io/library/ubuntu:22.04 AS final

# Add Ceph Reef repository to runtime stage too
RUN apt-get update && apt-get install -y \
    wget \
    gnupg \
    lsb-release \
    ca-certificates \
    && wget -q -O- 'https://download.ceph.com/keys/release.asc' | gpg --dearmor -o /etc/apt/trusted.gpg.d/ceph.gpg \
    && echo "deb https://download.ceph.com/debian-reef/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/ceph.list

# Install Ceph Reef runtime libraries
RUN apt-get update && apt-get install -y \
    librbd-dev \
    librados-dev \
    ceph-common \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /rbd-exporter

# Copy the binary from builder stage
COPY --from=builder /app/rbd-exporter .

# Make binary executable
RUN chmod +x ./rbd-exporter

EXPOSE 9040

CMD ["./rbd-exporter"]