Skip to content

Commit b038e2e

Browse files
committed
feat: additional environment variables
Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent e30edd2 commit b038e2e

File tree

4 files changed

+115
-12
lines changed

4 files changed

+115
-12
lines changed

api/v1/objectstore_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package v1
1818

1919
import (
2020
barmanapi "github.com/cloudnative-pg/barman-cloud/pkg/api"
21+
22+
corev1 "k8s.io/api/core/v1"
2123
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224
)
2325

@@ -29,6 +31,10 @@ type InstanceSidecarConfiguration struct {
2931
// +kubebuilder:validation:Maximum=3600
3032
// +kubebuilder:default=180
3133
CacheTTL *int `json:"cacheTTL,omitempty"`
34+
35+
// The environment to be explicitely passed to the sidecar
36+
// +optional
37+
Env []corev1.EnvVar `json:"env,omitempty"`
3238
}
3339

3440
// GetCacheTTL returns the cache TTL value, defaulting to 180 seconds if not set.

internal/cnpgi/operator/lifecycle.go

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ import (
1414
"github.com/spf13/viper"
1515
batchv1 "k8s.io/api/batch/v1"
1616
corev1 "k8s.io/api/core/v1"
17+
"k8s.io/apimachinery/pkg/types"
1718
"k8s.io/utils/ptr"
19+
"sigs.k8s.io/controller-runtime/pkg/client"
1820

21+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
1922
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
2023
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
2124
)
2225

2326
// LifecycleImplementation is the implementation of the lifecycle handler
2427
type LifecycleImplementation struct {
2528
lifecycle.UnimplementedOperatorLifecycleServer
29+
Client client.Client
2630
}
2731

2832
// GetCapabilities exposes the lifecycle capabilities
@@ -94,20 +98,85 @@ func (impl LifecycleImplementation) LifecycleHook(
9498
switch kind {
9599
case "Pod":
96100
contextLogger.Info("Reconciling pod")
97-
return reconcilePod(ctx, &cluster, request, pluginConfiguration)
101+
return impl.reconcilePod(ctx, &cluster, request, pluginConfiguration)
98102
case "Job":
99103
contextLogger.Info("Reconciling job")
100-
return reconcileJob(ctx, &cluster, request, pluginConfiguration)
104+
return impl.reconcileJob(ctx, &cluster, request, pluginConfiguration)
101105
default:
102106
return nil, fmt.Errorf("unsupported kind: %s", kind)
103107
}
104108
}
105109

110+
func (impl LifecycleImplementation) collectAdditionalEnvs(
111+
ctx context.Context,
112+
namespace string,
113+
pluginConfiguration *config.PluginConfiguration,
114+
) ([]corev1.EnvVar, error) {
115+
var result []corev1.EnvVar
116+
117+
if len(pluginConfiguration.BarmanObjectName) > 0 {
118+
envs, err := impl.collectObjectStoreEnvs(
119+
ctx,
120+
types.NamespacedName{
121+
Name: pluginConfiguration.BarmanObjectName,
122+
Namespace: namespace,
123+
},
124+
)
125+
if err != nil {
126+
return nil, err
127+
}
128+
result = append(result, envs...)
129+
}
130+
131+
if len(pluginConfiguration.RecoveryBarmanObjectName) > 0 {
132+
envs, err := impl.collectObjectStoreEnvs(
133+
ctx,
134+
types.NamespacedName{
135+
Name: pluginConfiguration.RecoveryBarmanObjectName,
136+
Namespace: namespace,
137+
},
138+
)
139+
if err != nil {
140+
return nil, err
141+
}
142+
result = append(result, envs...)
143+
}
144+
145+
return result, nil
146+
}
147+
148+
func (impl LifecycleImplementation) collectObjectStoreEnvs(
149+
ctx context.Context,
150+
barmanObjectKey types.NamespacedName,
151+
) ([]corev1.EnvVar, error) {
152+
var objectStore barmancloudv1.ObjectStore
153+
if err := impl.Client.Get(ctx, barmanObjectKey, &objectStore); err != nil {
154+
return nil, err
155+
}
156+
157+
return objectStore.Spec.InstanceSidecarConfiguration.Env, nil
158+
}
159+
160+
func (impl LifecycleImplementation) reconcileJob(
161+
ctx context.Context,
162+
cluster *cnpgv1.Cluster,
163+
request *lifecycle.OperatorLifecycleRequest,
164+
pluginConfiguration *config.PluginConfiguration,
165+
) (*lifecycle.OperatorLifecycleResponse, error) {
166+
env, err := impl.collectAdditionalEnvs(ctx, cluster.Namespace, pluginConfiguration)
167+
if err != nil {
168+
return nil, nil
169+
}
170+
171+
return reconcileJob(ctx, cluster, request, pluginConfiguration, env)
172+
}
173+
106174
func reconcileJob(
107175
ctx context.Context,
108176
cluster *cnpgv1.Cluster,
109177
request *lifecycle.OperatorLifecycleRequest,
110178
pluginConfiguration *config.PluginConfiguration,
179+
env []corev1.EnvVar,
111180
) (*lifecycle.OperatorLifecycleResponse, error) {
112181
contextLogger := log.FromContext(ctx).WithName("lifecycle")
113182
if pluginConfig := cluster.GetRecoverySourcePlugin(); pluginConfig == nil || pluginConfig.Name != metadata.PluginName {
@@ -144,6 +213,7 @@ func reconcileJob(
144213
corev1.Container{
145214
Args: []string{"restore"},
146215
},
216+
env,
147217
); err != nil {
148218
return nil, fmt.Errorf("while reconciling pod spec for job: %w", err)
149219
}
@@ -159,11 +229,26 @@ func reconcileJob(
159229
}, nil
160230
}
161231

232+
func (impl LifecycleImplementation) reconcilePod(
233+
ctx context.Context,
234+
cluster *cnpgv1.Cluster,
235+
request *lifecycle.OperatorLifecycleRequest,
236+
pluginConfiguration *config.PluginConfiguration,
237+
) (*lifecycle.OperatorLifecycleResponse, error) {
238+
env, err := impl.collectAdditionalEnvs(ctx, cluster.Namespace, pluginConfiguration)
239+
if err != nil {
240+
return nil, nil
241+
}
242+
243+
return reconcilePod(ctx, cluster, request, pluginConfiguration, env)
244+
}
245+
162246
func reconcilePod(
163247
ctx context.Context,
164248
cluster *cnpgv1.Cluster,
165249
request *lifecycle.OperatorLifecycleRequest,
166250
pluginConfiguration *config.PluginConfiguration,
251+
env []corev1.EnvVar,
167252
) (*lifecycle.OperatorLifecycleResponse, error) {
168253
pod, err := decoder.DecodePodJSON(request.GetObjectDefinition())
169254
if err != nil {
@@ -176,9 +261,16 @@ func reconcilePod(
176261
mutatedPod := pod.DeepCopy()
177262

178263
if len(pluginConfiguration.BarmanObjectName) != 0 {
179-
if err := reconcilePodSpec(pluginConfiguration, cluster, &mutatedPod.Spec, "postgres", corev1.Container{
180-
Args: []string{"instance"},
181-
}); err != nil {
264+
if err := reconcilePodSpec(
265+
pluginConfiguration,
266+
cluster,
267+
&mutatedPod.Spec,
268+
"postgres",
269+
corev1.Container{
270+
Args: []string{"instance"},
271+
},
272+
env,
273+
); err != nil {
182274
return nil, fmt.Errorf("while reconciling pod spec for pod: %w", err)
183275
}
184276
} else {
@@ -202,6 +294,7 @@ func reconcilePodSpec(
202294
spec *corev1.PodSpec,
203295
mainContainerName string,
204296
sidecarConfig corev1.Container,
297+
additionalEnvs []corev1.EnvVar,
205298
) error {
206299
envs := []corev1.EnvVar{
207300
{
@@ -246,6 +339,8 @@ func reconcilePodSpec(
246339
)
247340
}
248341

342+
envs = append(envs, additionalEnvs...)
343+
249344
baseProbe := &corev1.Probe{
250345
FailureThreshold: 3,
251346
ProbeHandler: corev1.ProbeHandler{

internal/cnpgi/operator/lifecycle_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var _ = Describe("LifecycleImplementation", func() {
107107
ObjectDefinition: jobJSON,
108108
}
109109

110-
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration)
110+
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration, nil)
111111
Expect(err).NotTo(HaveOccurred())
112112
Expect(response).NotTo(BeNil())
113113
Expect(response.JsonPatch).NotTo(BeEmpty())
@@ -128,7 +128,7 @@ var _ = Describe("LifecycleImplementation", func() {
128128
ObjectDefinition: jobJSON,
129129
}
130130

131-
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration)
131+
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration, nil)
132132
Expect(err).NotTo(HaveOccurred())
133133
Expect(response).To(BeNil())
134134
})
@@ -138,7 +138,7 @@ var _ = Describe("LifecycleImplementation", func() {
138138
ObjectDefinition: []byte("invalid-json"),
139139
}
140140

141-
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration)
141+
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration, nil)
142142
Expect(err).To(HaveOccurred())
143143
Expect(response).To(BeNil())
144144
})
@@ -165,7 +165,7 @@ var _ = Describe("LifecycleImplementation", func() {
165165
ObjectDefinition: jobJSON,
166166
}
167167

168-
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration)
168+
response, err := reconcileJob(ctx, cluster, request, pluginConfiguration, nil)
169169
Expect(err).NotTo(HaveOccurred())
170170
Expect(response).To(BeNil())
171171
})
@@ -185,7 +185,7 @@ var _ = Describe("LifecycleImplementation", func() {
185185
ObjectDefinition: podJSON,
186186
}
187187

188-
response, err := reconcilePod(ctx, cluster, request, pluginConfiguration)
188+
response, err := reconcilePod(ctx, cluster, request, pluginConfiguration, nil)
189189
Expect(err).NotTo(HaveOccurred())
190190
Expect(response).NotTo(BeNil())
191191
Expect(response.JsonPatch).NotTo(BeEmpty())
@@ -203,7 +203,7 @@ var _ = Describe("LifecycleImplementation", func() {
203203
ObjectDefinition: []byte("invalid-json"),
204204
}
205205

206-
response, err := reconcilePod(ctx, cluster, request, pluginConfiguration)
206+
response, err := reconcilePod(ctx, cluster, request, pluginConfiguration, nil)
207207
Expect(err).To(HaveOccurred())
208208
Expect(response).To(BeNil())
209209
})

internal/cnpgi/operator/start.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func (c *CNPGI) Start(ctx context.Context) error {
2727
reconciler.RegisterReconcilerHooksServer(server, ReconcilerImplementation{
2828
Client: c.Client,
2929
})
30-
lifecycle.RegisterOperatorLifecycleServer(server, LifecycleImplementation{})
30+
lifecycle.RegisterOperatorLifecycleServer(server, LifecycleImplementation{
31+
Client: c.Client,
32+
})
3133
return nil
3234
}
3335

0 commit comments

Comments
 (0)