Skip to content

Commit

Permalink
K8SPG-375: Support installing custom extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
egegunes committed Oct 12, 2023
1 parent 8490610 commit 1d14e19
Show file tree
Hide file tree
Showing 28 changed files with 43,728 additions and 25,054 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ envtest: ## Download envtest-setup locally if necessary.
build-docker-image:
ROOT_REPO=$(ROOT_REPO) VERSION=$(VERSION) IMAGE=$(IMAGE) $(ROOT_REPO)/e2e-tests/build

build-extension-installer-image:
ROOT_REPO=$(ROOT_REPO) VERSION=$(VERSION) IMAGE=$(IMAGE)-ext-installer COMPONENT=extension-installer $(ROOT_REPO)/e2e-tests/build

generate: kustomize generate-crd generate-deepcopy generate-rbac generate-manager generate-bundle generate-cw

generate-crd: generate-crunchy-crd generate-percona-crd
Expand Down

Large diffs are not rendered by default.

1,785 changes: 1,608 additions & 177 deletions build/crd/percona/generated/pgv2.percona.com_perconapgclusters.yaml

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions build/extension-installer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM golang:1.20 AS go_builder
WORKDIR /go/src/github.com/percona/percona-postgresql-operator

COPY . .
RUN go mod download

ARG GIT_COMMIT
ARG GIT_BRANCH
ARG GO_LDFLAGS
ARG GOOS=linux
ARG GOARCH=amd64
ARG CGO_ENABLED=0

RUN mkdir -p build/_output/bin \
&& CGO_ENABLED=$CGO_ENABLED GOOS=$GOOS GOARCH=$GOARCH GO_LDFLAGS=$GO_LDFLAGS \
go build -ldflags "-w -s -X main.GitCommit=$GIT_COMMIT -X main.GitBranch=$GIT_BRANCH" \
-o build/_output/bin/extension-installer \
./cmd/extension-installer \
&& cp -r build/_output/bin/extension-installer /usr/local/bin/extension-installer


RUN ./bin/license_aggregator.sh ./cmd/...; \
cp -r ./licenses /licenses

FROM registry.access.redhat.com/ubi9/ubi-minimal AS ubi9

LABEL name="Percona Postgres Operator Custom Extension Installer" \
vendor="Percona" \
summary="Percona Postgres Operator performs Postgres database creation, adjustment and maintenance inside Kubernetes cluster" \
description="Percona Postgres Operator simplifies and automates all essential actions for stable and continuous database operation during the whole database lifecycle" \
maintainer="Percona Development <[email protected]>"

RUN microdnf update -y && microdnf clean all -y

COPY licenses /licenses

COPY --from=go_builder /usr/local/bin/extension-installer /usr/local/bin
COPY --from=go_builder /licenses /licenses

USER 26
78 changes: 78 additions & 0 deletions cmd/extension-installer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"flag"
"io"
"log"
"os"
"path"

"github.com/percona/percona-postgresql-operator/percona/extensions"
)

func main() {
var storageType, region, bucket, key string
var install, uninstall bool

flag.StringVar(&storageType, "type", "", "Storage type")
flag.StringVar(&region, "region", "", "Storage region")
flag.StringVar(&bucket, "bucket", "", "Storage bucket")
flag.StringVar(&key, "key", "", "Extension archive key")

flag.BoolVar(&install, "install", false, "Install extension")
flag.BoolVar(&uninstall, "uninstall", false, "Uninstall extension")
flag.Parse()

if (install && uninstall) || (!install && !uninstall) {
log.Fatalf("ERROR: set either -install or -uninstall")
}

log.Printf("starting extension installer for %s/%s (%s) in %s", bucket, key, storageType, region)

storage := initStorage(extensions.StorageType(storageType), bucket, region)

object, err := storage.Get(key)
if err != nil {
log.Fatalf("ERROR: failed to get object: %v", err)
}

archivePath := path.Join("/tmp", key)

// /tmp is not a good place due to its size limitation.
dst, err := os.Create(archivePath)
if err != nil {
log.Fatalf("ERROR: failed to create file: %v", err)
}
defer dst.Close()

written, err := io.Copy(dst, object)
if err != nil {
log.Fatalf("ERROR: failed to copy object: %v", err)
}

log.Printf("copied %d bytes to %s", written, dst.Name())

switch {
case install:
log.Printf("installing extension %s", archivePath)
if err := extensions.Install(archivePath); err != nil {
log.Fatalf("ERROR: failed to install extension: %v", err)
}
case uninstall:
log.Printf("uninstalling extension %s", archivePath)
if err := extensions.Uninstall(archivePath); err != nil {
log.Fatalf("ERROR: failed to uninstall extension: %v", err)
}
}
}

func initStorage(storageType extensions.StorageType, bucket, region string) extensions.ObjectGetter {
switch storageType {
case extensions.StorageTypeS3:
return extensions.NewS3(region, bucket)
default:
log.Fatalf("unknown storage type: %s", os.Getenv("STORAGE_TYPE"))
}

return nil
}
Loading

0 comments on commit 1d14e19

Please sign in to comment.