Skip to content

Commit 1c86ff6

Browse files
armruleonardoce
andauthored
feat(spike): wal-archive and wal-restore methods (#4)
Signed-off-by: Armando Ruocco <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]>
1 parent 76486c2 commit 1c86ff6

File tree

11 files changed

+739
-59
lines changed

11 files changed

+739
-59
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ COPY go.sum go.sum
1212
RUN go mod download
1313

1414
# Copy the go source
15-
COPY cmd/main.go cmd/main.go
15+
COPY cmd/instance/main.go cmd/instance/main.go
1616
COPY api/ api/
1717
COPY internal/ internal/
1818

@@ -21,7 +21,7 @@ COPY internal/ internal/
2121
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2222
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2323
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
24+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/instance/main.go
2525

2626
# Use distroless as minimal base image to package the manager binary
2727
# Refer to https://github.com/GoogleContainerTools/distroless for more details

cmd/instance/main.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"os"
6+
7+
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
8+
"k8s.io/apimachinery/pkg/fields"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
11+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
12+
controllerruntime "sigs.k8s.io/controller-runtime"
13+
ctrl "sigs.k8s.io/controller-runtime"
14+
"sigs.k8s.io/controller-runtime/pkg/cache"
15+
"sigs.k8s.io/controller-runtime/pkg/client"
16+
17+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
18+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/instance"
19+
)
20+
21+
var (
22+
scheme = runtime.NewScheme()
23+
setupLog = ctrl.Log.WithName("setup")
24+
)
25+
26+
func init() {
27+
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
28+
29+
utilruntime.Must(barmancloudv1.AddToScheme(scheme))
30+
// +kubebuilder:scaffold:scheme
31+
}
32+
33+
func main() {
34+
setupLog.Info("Starting barman cloud instance plugin")
35+
namespace := mustGetEnv("NAMESPACE")
36+
boName := mustGetEnv("BARMAN_OBJECT_NAME")
37+
clusterName := mustGetEnv("CLUSTER_NAME")
38+
instanceName := mustGetEnv("INSTANCE_NAME")
39+
40+
mgr, err := controllerruntime.NewManager(controllerruntime.GetConfigOrDie(), controllerruntime.Options{
41+
Scheme: scheme,
42+
Cache: cache.Options{
43+
ByObject: map[client.Object]cache.ByObject{
44+
&barmancloudv1.ObjectStore{}: {
45+
Field: fields.OneTermEqualSelector("metadata.name", boName),
46+
Namespaces: map[string]cache.Config{
47+
namespace: {},
48+
},
49+
},
50+
&cnpgv1.Cluster{}: {
51+
Field: fields.OneTermEqualSelector("metadata.name", clusterName),
52+
Namespaces: map[string]cache.Config{
53+
namespace: {},
54+
},
55+
},
56+
},
57+
},
58+
})
59+
if err != nil {
60+
setupLog.Error(err, "unable to start manager")
61+
os.Exit(1)
62+
}
63+
64+
if err := mgr.Add(&instance.CNPGI{
65+
Client: mgr.GetClient(),
66+
ClusterObjectKey: client.ObjectKey{
67+
Namespace: namespace,
68+
Name: clusterName,
69+
},
70+
BarmanObjectKey: client.ObjectKey{
71+
Namespace: namespace,
72+
Name: boName,
73+
},
74+
InstanceName: instanceName,
75+
// TODO: improve
76+
PGDataPath: mustGetEnv("PGDATA"),
77+
PGWALPath: mustGetEnv("PGWAL"),
78+
SpoolDirectory: mustGetEnv("SPOOL_DIRECTORY"),
79+
ServerCertPath: mustGetEnv("SERVER_CERT"),
80+
ServerKeyPath: mustGetEnv("SERVER_KEY"),
81+
ClientCertPath: mustGetEnv("CLIENT_CERT"),
82+
ServerAddress: mustGetEnv("SERVER_ADDRESS"),
83+
PluginPath: mustGetEnv("PLUGIN_PATH"),
84+
}); err != nil {
85+
setupLog.Error(err, "unable to create CNPGI runnable")
86+
os.Exit(1)
87+
}
88+
89+
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
90+
setupLog.Error(err, "problem running manager")
91+
os.Exit(1)
92+
}
93+
}
94+
95+
func mustGetEnv(envName string) string {
96+
value := os.Getenv(envName)
97+
if value == "" {
98+
setupLog.Error(
99+
errors.New("missing required env variable"),
100+
"while fetching env variables",
101+
"name",
102+
envName,
103+
)
104+
os.Exit(1)
105+
}
106+
return value
107+
}
File renamed without changes.

go.mod

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@ go 1.22.0
44

55
require (
66
github.com/cloudnative-pg/barman-cloud v0.0.0-20240924124724-92831d48562a
7-
github.com/cloudnative-pg/cnpg-i v0.0.0-20240924030516-c5636170f248
8-
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20240926153929-09e2c6f6689b
7+
github.com/cloudnative-pg/cloudnative-pg v1.24.0
8+
github.com/cloudnative-pg/cnpg-i v0.0.0-20240902182059-c9f193bf825b
9+
github.com/cloudnative-pg/cnpg-i-machinery v0.0.0-20240926095718-27da985944d4
10+
github.com/cloudnative-pg/machinery v0.0.0-20240919131343-9dd62b9257c7
911
github.com/onsi/ginkgo/v2 v2.20.2
1012
github.com/onsi/gomega v1.34.2
11-
github.com/spf13/cobra v1.8.1
12-
google.golang.org/grpc v1.67.0
13+
google.golang.org/grpc v1.66.0
1314
k8s.io/apimachinery v0.31.1
14-
k8s.io/client-go v0.31.1
15+
k8s.io/client-go v0.31.0
1516
sigs.k8s.io/controller-runtime v0.19.0
1617
)
1718

1819
require (
1920
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
2021
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
2122
github.com/beorn7/perks v1.0.1 // indirect
23+
github.com/blang/semver v3.5.1+incompatible // indirect
2224
github.com/blang/semver/v4 v4.0.0 // indirect
2325
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
2426
github.com/cespare/xxhash/v2 v2.3.0 // indirect
25-
github.com/cloudnative-pg/machinery v0.0.0-20240919131343-9dd62b9257c7 // indirect
2627
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2728
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
2829
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
@@ -45,6 +46,7 @@ require (
4546
github.com/google/gofuzz v1.2.0 // indirect
4647
github.com/google/pprof v0.0.0-20240910150728-a0b0bb1d4134 // indirect
4748
github.com/google/uuid v1.6.0 // indirect
49+
github.com/gorilla/websocket v1.5.0 // indirect
4850
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
4951
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
5052
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -53,27 +55,35 @@ require (
5355
github.com/josharian/intern v1.0.0 // indirect
5456
github.com/json-iterator/go v1.1.12 // indirect
5557
github.com/klauspost/compress v1.17.9 // indirect
58+
github.com/kubernetes-csi/external-snapshotter/client/v8 v8.0.0 // indirect
59+
github.com/lib/pq v1.10.9 // indirect
5660
github.com/magiconair/properties v1.8.7 // indirect
5761
github.com/mailru/easyjson v0.7.7 // indirect
5862
github.com/mitchellh/mapstructure v1.5.0 // indirect
63+
github.com/moby/spdystream v0.4.0 // indirect
5964
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6065
github.com/modern-go/reflect2 v1.0.2 // indirect
6166
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
67+
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
6268
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
6369
github.com/pkg/errors v0.9.1 // indirect
70+
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.75.2 // indirect
6471
github.com/prometheus/client_golang v1.20.3 // indirect
6572
github.com/prometheus/client_model v0.6.1 // indirect
6673
github.com/prometheus/common v0.59.1 // indirect
6774
github.com/prometheus/procfs v0.15.1 // indirect
75+
github.com/robfig/cron v1.2.0 // indirect
6876
github.com/sagikazarmark/locafero v0.4.0 // indirect
6977
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
7078
github.com/sourcegraph/conc v0.3.0 // indirect
7179
github.com/spf13/afero v1.11.0 // indirect
7280
github.com/spf13/cast v1.6.0 // indirect
81+
github.com/spf13/cobra v1.8.1 // indirect
7382
github.com/spf13/pflag v1.0.5 // indirect
7483
github.com/spf13/viper v1.19.0 // indirect
7584
github.com/stoewer/go-strcase v1.3.0 // indirect
7685
github.com/subosito/gotenv v1.6.0 // indirect
86+
github.com/thoas/go-funk v0.9.3 // indirect
7787
github.com/x448/float16 v0.8.4 // indirect
7888
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
7989
go.opentelemetry.io/otel v1.28.0 // indirect

0 commit comments

Comments
 (0)