Skip to content

Commit dd6548c

Browse files
authored
feat: operator plugin and manifests (#18)
Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent 88e8762 commit dd6548c

24 files changed

+506
-201
lines changed

cmd/operator/main.go

Lines changed: 67 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,90 @@
1-
/*
2-
Copyright 2024.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
16-
17-
// Package main contains the implementation of the CNPG-i operator plugin
1+
// Package main is the entrypoint of operator plugin
182
package main
193

204
import (
21-
"crypto/tls"
22-
"flag"
5+
"context"
6+
"fmt"
237
"os"
248

25-
// +kubebuilder:scaffold:imports
26-
"k8s.io/apimachinery/pkg/runtime"
27-
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28-
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
9+
"github.com/cloudnative-pg/machinery/pkg/log"
10+
"github.com/sourcegraph/conc/pool"
11+
"github.com/spf13/cobra"
12+
"github.com/spf13/viper"
2913
ctrl "sigs.k8s.io/controller-runtime"
30-
"sigs.k8s.io/controller-runtime/pkg/healthz"
31-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
32-
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
33-
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
34-
"sigs.k8s.io/controller-runtime/pkg/webhook"
3514

36-
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
3715
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator"
38-
"github.com/cloudnative-pg/plugin-barman-cloud/internal/controller"
39-
40-
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
41-
// to ensure that exec-entrypoint and run can make use of them.
42-
_ "k8s.io/client-go/plugin/pkg/client/auth"
16+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/manager"
4317
)
4418

45-
var (
46-
scheme = runtime.NewScheme()
47-
setupLog = ctrl.Log.WithName("setup")
48-
)
49-
50-
func init() {
51-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
52-
53-
utilruntime.Must(barmancloudv1.AddToScheme(scheme))
54-
// +kubebuilder:scaffold:scheme
55-
}
56-
5719
func main() {
58-
var metricsAddr string
59-
var enableLeaderElection bool
60-
var probeAddr string
61-
var secureMetrics bool
62-
var enableHTTP2 bool
63-
var tlsOpts []func(*tls.Config)
64-
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
65-
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
66-
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
67-
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
68-
"Enable leader election for controller manager. "+
69-
"Enabling this will ensure there is only one active controller manager.")
70-
flag.BoolVar(&secureMetrics, "metrics-secure", true,
71-
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
72-
flag.BoolVar(&enableHTTP2, "enable-http2", false,
73-
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
74-
opts := zap.Options{
75-
Development: true,
76-
}
77-
opts.BindFlags(flag.CommandLine)
78-
flag.Parse()
79-
80-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
81-
82-
// if the enable-http2 flag is false (the default), http/2 should be disabled
83-
// due to its vulnerabilities. More specifically, disabling http/2 will
84-
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
85-
// Rapid Reset CVEs. For more information see:
86-
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
87-
// - https://github.com/advisories/GHSA-4374-p667-p6c8
88-
disableHTTP2 := func(c *tls.Config) {
89-
setupLog.Info("disabling http/2")
90-
c.NextProtos = []string{"http/1.1"}
20+
cobra.EnableTraverseRunHooks = true
21+
22+
logFlags := &log.Flags{}
23+
rootCmd := &cobra.Command{
24+
Use: "plugin-barman-cloud",
25+
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
26+
logFlags.ConfigureLogging()
27+
return nil
28+
},
9129
}
9230

93-
if !enableHTTP2 {
94-
tlsOpts = append(tlsOpts, disableHTTP2)
95-
}
31+
logFlags.AddFlags(rootCmd.PersistentFlags())
32+
rootCmd.AddCommand(newOperatorCommand())
9633

97-
webhookServer := webhook.NewServer(webhook.Options{
98-
TLSOpts: tlsOpts,
99-
})
100-
101-
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
102-
// More info:
103-
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/server
104-
// - https://book.kubebuilder.io/reference/metrics.html
105-
metricsServerOptions := metricsserver.Options{
106-
BindAddress: metricsAddr,
107-
SecureServing: secureMetrics,
108-
// TODO(user): TLSOpts is used to allow configuring the TLS config used for the server. If certificates are
109-
// not provided, self-signed certificates will be generated by default. This option is not recommended for
110-
// production environments as self-signed certificates do not offer the same level of trust and security
111-
// as certificates issued by a trusted Certificate Authority (CA). The primary risk is potentially allowing
112-
// unauthorized access to sensitive metrics data. Consider replacing with CertDir, CertName, and KeyName
113-
// to provide certificates, ensuring the server communicates using trusted and secure certificates.
114-
TLSOpts: tlsOpts,
34+
if err := rootCmd.Execute(); err != nil {
35+
fmt.Println(err)
36+
os.Exit(1)
11537
}
38+
}
11639

117-
if secureMetrics {
118-
// FilterProvider is used to protect the metrics endpoint with authn/authz.
119-
// These configurations ensure that only authorized users and service accounts
120-
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
121-
// https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/metrics/filters#WithAuthenticationAndAuthorization
122-
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
40+
func newOperatorCommand() *cobra.Command {
41+
cmd := operator.NewCommand()
42+
cmd.Use = "operator"
43+
cmd.Short = "Starts the BarmanObjectStore reconciler and the Barman Cloud CNPG-i plugin"
44+
grpcServer := cmd.RunE
45+
46+
cmd.RunE = func(cmd *cobra.Command, args []string) error {
47+
ctrl.SetupSignalHandler()
48+
operatorPool := pool.
49+
New().
50+
WithContext(cmd.Context()).
51+
WithCancelOnError().
52+
WithFirstError()
53+
operatorPool.Go(func(ctx context.Context) error {
54+
cmd.SetContext(ctx)
55+
56+
if len(viper.GetString("sidecar-image")) == 0 {
57+
return fmt.Errorf("missing required SIDECAR_IMAGE environment variable")
58+
}
59+
60+
err := grpcServer(cmd, args)
61+
return err
62+
})
63+
operatorPool.Go(manager.Start)
64+
return operatorPool.Wait()
12365
}
12466

125-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
126-
Scheme: scheme,
127-
Metrics: metricsServerOptions,
128-
WebhookServer: webhookServer,
129-
HealthProbeBindAddress: probeAddr,
130-
LeaderElection: enableLeaderElection,
131-
LeaderElectionID: "822e3f5c.cnpg.io",
132-
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
133-
// when the Manager ends. This requires the binary to immediately end when the
134-
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
135-
// speeds up voluntary leader transitions as the new leader don't have to wait
136-
// LeaseDuration time first.
137-
//
138-
// In the default scaffold provided, the program ends immediately after
139-
// the manager stops, so would be fine to enable this option. However,
140-
// if you are doing or is intended to do any operation such as perform cleanups
141-
// after the manager stops then its usage might be unsafe.
142-
// LeaderElectionReleaseOnCancel: true,
143-
})
144-
if err != nil {
145-
setupLog.Error(err, "unable to start manager")
146-
os.Exit(1)
147-
}
67+
cmd.Flags().String("metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
68+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
69+
_ = viper.BindPFlag("metrics-bind-address", cmd.Flags().Lookup("metrics-bind-address"))
14870

149-
if err = (&controller.ObjectStoreReconciler{
150-
Client: mgr.GetClient(),
151-
Scheme: mgr.GetScheme(),
152-
}).SetupWithManager(mgr); err != nil {
153-
setupLog.Error(err, "unable to create controller", "controller", "ObjectStore")
154-
os.Exit(1)
155-
}
156-
// +kubebuilder:scaffold:builder
71+
cmd.Flags().String("health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
72+
_ = viper.BindPFlag("health-probe-bind-address", cmd.Flags().Lookup("health-probe-bind-address"))
15773

158-
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
159-
setupLog.Error(err, "unable to set up health check")
160-
os.Exit(1)
161-
}
162-
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
163-
setupLog.Error(err, "unable to set up ready check")
164-
os.Exit(1)
165-
}
74+
cmd.Flags().Bool("leader-elect", false,
75+
"Enable leader election for controller manager. "+
76+
"Enabling this will ensure there is only one active controller manager.")
77+
_ = viper.BindPFlag("leader-elect", cmd.Flags().Lookup("leader-elect"))
16678

167-
if err := mgr.Add(&operator.CNPGI{}); err != nil {
168-
setupLog.Error(err, "unable to create CNPGI webserver")
169-
os.Exit(1)
170-
}
79+
cmd.Flags().Bool("metrics-secure", true,
80+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
81+
_ = viper.BindPFlag("metrics-secure", cmd.Flags().Lookup("metrics-secure"))
17182

172-
setupLog.Info("starting manager")
173-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
174-
setupLog.Error(err, "problem running manager")
175-
os.Exit(1)
176-
}
83+
cmd.Flags().Bool("enable-http2", false,
84+
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
85+
_ = viper.BindPFlag("enable-http2", cmd.Flags().Lookup("enable-http2"))
86+
87+
_ = viper.BindEnv("sidecar-image", "SIDECAR_IMAGE")
88+
89+
return cmd
17790
}

config/manager/manager.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,5 @@ spec:
9191
requests:
9292
cpu: 10m
9393
memory: 64Mi
94-
serviceAccountName: controller-manager
94+
serviceAccountName: plugin-barman-cloud
9595
terminationGracePeriodSeconds: 10

config/rbac/leader_election_role_binding.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ roleRef:
1111
name: leader-election-role
1212
subjects:
1313
- kind: ServiceAccount
14-
name: controller-manager
15-
namespace: system
14+
name: plugin-barman-cloud
15+
namespace: cnpg-system

config/rbac/metrics_auth_role_binding.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ roleRef:
88
name: metrics-auth-role
99
subjects:
1010
- kind: ServiceAccount
11-
name: controller-manager
12-
namespace: system
11+
name: plugin-barman-cloud
12+
namespace: cnpg-system

config/rbac/role_binding.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ roleRef:
1111
name: manager-role
1212
subjects:
1313
- kind: ServiceAccount
14-
name: controller-manager
15-
namespace: system
14+
name: plugin-barman-cloud
15+
namespace: cnpg-system

config/rbac/service_account.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ metadata:
44
labels:
55
app.kubernetes.io/name: plugin-barman-cloud
66
app.kubernetes.io/managed-by: kustomize
7-
name: controller-manager
8-
namespace: system
7+
name: plugin-barman-cloud

docs/examples/cluster-example.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: postgresql.cnpg.io/v1
2+
kind: Cluster
3+
metadata:
4+
name: cluster-example
5+
spec:
6+
instances: 3
7+
8+
plugins:
9+
- name: barman-cloud.cloudnative-pg.io
10+
11+
storage:
12+
size: 1Gi

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ require (
1010
github.com/cloudnative-pg/machinery v0.0.0-20241001075747-34c8797af80f
1111
github.com/onsi/ginkgo/v2 v2.20.2
1212
github.com/onsi/gomega v1.34.2
13+
github.com/sourcegraph/conc v0.3.0
14+
github.com/spf13/cobra v1.8.1
15+
github.com/spf13/viper v1.19.0
1316
google.golang.org/grpc v1.67.1
17+
k8s.io/api v0.31.1
1418
k8s.io/apimachinery v0.31.1
1519
k8s.io/client-go v0.31.1
1620
sigs.k8s.io/controller-runtime v0.19.0
@@ -75,12 +79,10 @@ require (
7579
github.com/robfig/cron v1.2.0 // indirect
7680
github.com/sagikazarmark/locafero v0.4.0 // indirect
7781
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
78-
github.com/sourcegraph/conc v0.3.0 // indirect
82+
github.com/snorwin/jsonpatch v1.5.0 // indirect
7983
github.com/spf13/afero v1.11.0 // indirect
8084
github.com/spf13/cast v1.6.0 // indirect
81-
github.com/spf13/cobra v1.8.1 // indirect
8285
github.com/spf13/pflag v1.0.5 // indirect
83-
github.com/spf13/viper v1.19.0 // indirect
8486
github.com/stoewer/go-strcase v1.3.0 // indirect
8587
github.com/subosito/gotenv v1.6.0 // indirect
8688
github.com/thoas/go-funk v0.9.3 // indirect
@@ -112,7 +114,6 @@ require (
112114
gopkg.in/ini.v1 v1.67.0 // indirect
113115
gopkg.in/yaml.v2 v2.4.0 // indirect
114116
gopkg.in/yaml.v3 v3.0.1 // indirect
115-
k8s.io/api v0.31.1 // indirect
116117
k8s.io/apiextensions-apiserver v0.31.0 // indirect
117118
k8s.io/apiserver v0.31.0 // indirect
118119
k8s.io/component-base v0.31.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
4343
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
4444
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
4545
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
46+
github.com/go-faker/faker/v4 v4.4.1 h1:LY1jDgjVkBZWIhATCt+gkl0x9i/7wC61gZx73GTFb+Q=
47+
github.com/go-faker/faker/v4 v4.4.1/go.mod h1:HRLrjis+tYsbFtIHufEPTAIzcZiRu0rS9EYl2Ccwme4=
4648
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
4749
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
4850
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
@@ -155,6 +157,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
155157
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
156158
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
157159
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
160+
github.com/snorwin/jsonpatch v1.5.0 h1:0m56YSt9cHiJOn8U+OcqdPGcDQZmhPM/zsG7Dv5QQP0=
161+
github.com/snorwin/jsonpatch v1.5.0/go.mod h1:e0IDKlyFBLTFPqM0wa79dnMwjMs3XFvmKcrgCRpDqok=
158162
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
159163
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
160164
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=

internal/cnpgi/instance/identity.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/client"
99

1010
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
11+
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
1112
)
1213

1314
// IdentityImplementation implements IdentityServer
@@ -22,7 +23,7 @@ func (i IdentityImplementation) GetPluginMetadata(
2223
_ context.Context,
2324
_ *identity.GetPluginMetadataRequest,
2425
) (*identity.GetPluginMetadataResponse, error) {
25-
return &Data, nil
26+
return &metadata.Data, nil
2627
}
2728

2829
// GetPluginCapabilities implements IdentityServer

0 commit comments

Comments
 (0)