Skip to content

Commit 990296c

Browse files
committed
chore: review
Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent 100ce14 commit 990296c

File tree

9 files changed

+175
-53
lines changed

9 files changed

+175
-53
lines changed

.wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ObjectStoreStatus
2626
PITR
2727
README
2828
RecoveryWindow
29+
ResourceRequirements
2930
RetentionPolicy
3031
SAS
3132
SFO
@@ -54,6 +55,7 @@ cmctl
5455
cnpg
5556
codebase
5657
containerPort
58+
cpu
5759
creds
5860
csi
5961
customresourcedefinition

api/v1/objectstore_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ type InstanceSidecarConfiguration struct {
3333
// +kubebuilder:default:=1800
3434
// +optional
3535
RetentionPolicyIntervalSeconds int `json:"retentionPolicyIntervalSeconds,omitempty"`
36+
3637
// Resources define cpu/memory requests and limits for the sidecar that runs in the instance pods.
37-
// +optional
38-
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
38+
// +optional
39+
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
3940
}
4041

4142
// ObjectStoreSpec defines the desired state of ObjectStore.

api/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/barmancloud.cnpg.io_objectstores.yaml

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -506,18 +506,41 @@ spec:
506506
- name
507507
type: object
508508
type: array
509-
retentionPolicyIntervalSeconds:
510-
default: 1800
511-
description: |-
512-
The retentionCheckInterval defines the frequency at which the
513-
system checks and enforces retention policies.
514-
type: integer
515509
resources:
516-
description: |-
517-
Plugin's sidecar resources requirements. Please refer to
518-
https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
519-
for more information.
510+
description: Resources define cpu/memory requests and limits for
511+
the sidecar that runs in the instance pods.
520512
properties:
513+
claims:
514+
description: |-
515+
Claims lists the names of resources, defined in spec.resourceClaims,
516+
that are used by this container.
517+
518+
This is an alpha field and requires enabling the
519+
DynamicResourceAllocation feature gate.
520+
521+
This field is immutable. It can only be set for containers.
522+
items:
523+
description: ResourceClaim references one entry in PodSpec.ResourceClaims.
524+
properties:
525+
name:
526+
description: |-
527+
Name must match the name of one entry in pod.spec.resourceClaims of
528+
the Pod where this field is used. It makes that resource available
529+
inside a container.
530+
type: string
531+
request:
532+
description: |-
533+
Request is the name chosen for a request in the referenced claim.
534+
If empty, everything from the claim is made available, otherwise
535+
only the result of this request.
536+
type: string
537+
required:
538+
- name
539+
type: object
540+
type: array
541+
x-kubernetes-list-map-keys:
542+
- name
543+
x-kubernetes-list-type: map
521544
limits:
522545
additionalProperties:
523546
anyOf:
@@ -543,6 +566,12 @@ spec:
543566
More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
544567
type: object
545568
type: object
569+
retentionPolicyIntervalSeconds:
570+
default: 1800
571+
description: |-
572+
The retentionCheckInterval defines the frequency at which the
573+
system checks and enforces retention policies.
574+
type: integer
546575
type: object
547576
retentionPolicy:
548577
description: |-

hack/examples/minio-store.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ spec:
66
retentionPolicy: "1m"
77
instanceSidecarConfiguration:
88
retentionPolicyIntervalSeconds: 30
9+
resources:
10+
requests:
11+
memory: "64Mi"
12+
cpu: "250m"
13+
limits:
14+
memory: "128Mi"
15+
cpu: "500m"
916
configuration:
1017
endpointCA:
1118
name: minio-server-tls

internal/cnpgi/operator/lifecycle.go

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/utils/ptr"
1818
"sigs.k8s.io/controller-runtime/pkg/client"
1919

20+
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
2021
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
2122
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
2223
)
@@ -123,23 +124,36 @@ func (impl LifecycleImplementation) reconcileJob(
123124
return nil, err
124125
}
125126

126-
return reconcileJob(ctx, cluster, request, env, certificates)
127+
resources, err := impl.collectSidecarResources(ctx, pluginConfiguration)
128+
if err != nil {
129+
return nil, err
130+
}
131+
132+
return reconcileJob(ctx, cluster, request, sidecarConfiguration{
133+
env: env,
134+
certificates: certificates,
135+
resources: resources,
136+
})
137+
}
138+
139+
type sidecarConfiguration struct {
140+
env []corev1.EnvVar
141+
certificates []corev1.VolumeProjection
142+
resources corev1.ResourceRequirements
127143
}
128144

129145
func reconcileJob(
130146
ctx context.Context,
131147
cluster *cnpgv1.Cluster,
132148
request *lifecycle.OperatorLifecycleRequest,
133-
env []corev1.EnvVar,
134-
certificates []corev1.VolumeProjection,
149+
config sidecarConfiguration,
135150
) (*lifecycle.OperatorLifecycleResponse, error) {
136151
contextLogger := log.FromContext(ctx).WithName("lifecycle")
137152
if pluginConfig := cluster.GetRecoverySourcePlugin(); pluginConfig == nil || pluginConfig.Name != metadata.PluginName {
138153
contextLogger.Debug("cluster does not use the this plugin for recovery, skipping")
139154
return nil, nil
140155
}
141156

142-
var barmanObjectStore barmancloudv1.ObjectStore
143157
var job batchv1.Job
144158
if err := decoder.DecodeObjectStrict(
145159
request.GetObjectDefinition(),
@@ -170,9 +184,7 @@ func reconcileJob(
170184
corev1.Container{
171185
Args: []string{"restore"},
172186
},
173-
env,
174-
certificates,
175-
barmanObjectStore,
187+
config,
176188
); err != nil {
177189
return nil, fmt.Errorf("while reconciling pod spec for job: %w", err)
178190
}
@@ -204,23 +216,36 @@ func (impl LifecycleImplementation) reconcilePod(
204216
return nil, err
205217
}
206218

219+
resources, err := impl.collectSidecarResources(ctx, pluginConfiguration)
220+
if err != nil {
221+
return nil, err
222+
}
223+
224+
return reconcilePod(ctx, cluster, request, pluginConfiguration, sidecarConfiguration{
225+
env: env,
226+
certificates: certificates,
227+
resources: resources,
228+
})
229+
}
230+
231+
func (impl LifecycleImplementation) collectSidecarResources(
232+
ctx context.Context,
233+
configuration *config.PluginConfiguration,
234+
) (corev1.ResourceRequirements, error) {
207235
var barmanObjectStore barmancloudv1.ObjectStore
208-
configuration := config.NewFromCluster(cluster)
209-
if err := impl.Client.Get(ctx, configuration.GetBarmanObjectKey(), &barmanObjectStore); err != nil {
210-
return nil, err
211-
}
236+
if err := impl.Client.Get(ctx, configuration.GetBarmanObjectKey(), &barmanObjectStore); err != nil {
237+
return corev1.ResourceRequirements{}, err
238+
}
212239

213-
return reconcilePod(ctx, cluster, request, pluginConfiguration, env, certificates, barmanObjectStore)
240+
return barmanObjectStore.Spec.InstanceSidecarConfiguration.Resources, nil
214241
}
215242

216243
func reconcilePod(
217244
ctx context.Context,
218245
cluster *cnpgv1.Cluster,
219246
request *lifecycle.OperatorLifecycleRequest,
220247
pluginConfiguration *config.PluginConfiguration,
221-
env []corev1.EnvVar,
222-
certificates []corev1.VolumeProjection,
223-
barmanObjectStore barmancloudv1.ObjectStore,
248+
config sidecarConfiguration,
224249
) (*lifecycle.OperatorLifecycleResponse, error) {
225250
pod, err := decoder.DecodePodJSON(request.GetObjectDefinition())
226251
if err != nil {
@@ -240,9 +265,7 @@ func reconcilePod(
240265
corev1.Container{
241266
Args: []string{"instance"},
242267
},
243-
env,
244-
certificates,
245-
barmanObjectStore,
268+
config,
246269
); err != nil {
247270
return nil, fmt.Errorf("while reconciling pod spec for pod: %w", err)
248271
}
@@ -265,10 +288,8 @@ func reconcilePodSpec(
265288
cluster *cnpgv1.Cluster,
266289
spec *corev1.PodSpec,
267290
mainContainerName string,
268-
sidecarConfig corev1.Container,
269-
additionalEnvs []corev1.EnvVar,
270-
certificates []corev1.VolumeProjection,
271-
barmanObjectStore barmancloudv1.ObjectStore,
291+
sidecarTemplate corev1.Container,
292+
config sidecarConfiguration,
272293
) error {
273294
envs := []corev1.EnvVar{
274295
{
@@ -295,7 +316,7 @@ func reconcilePodSpec(
295316
},
296317
}
297318

298-
envs = append(envs, additionalEnvs...)
319+
envs = append(envs, config.env...)
299320

300321
baseProbe := &corev1.Probe{
301322
FailureThreshold: 10,
@@ -308,11 +329,11 @@ func reconcilePodSpec(
308329
}
309330

310331
// fixed values
311-
sidecarConfig.Name = "plugin-barman-cloud"
312-
sidecarConfig.Image = viper.GetString("sidecar-image")
313-
sidecarConfig.ImagePullPolicy = cluster.Spec.ImagePullPolicy
314-
sidecarConfig.StartupProbe = baseProbe.DeepCopy()
315-
sidecarConfig.SecurityContext = &corev1.SecurityContext{
332+
sidecarTemplate.Name = "plugin-barman-cloud"
333+
sidecarTemplate.Image = viper.GetString("sidecar-image")
334+
sidecarTemplate.ImagePullPolicy = cluster.Spec.ImagePullPolicy
335+
sidecarTemplate.StartupProbe = baseProbe.DeepCopy()
336+
sidecarTemplate.SecurityContext = &corev1.SecurityContext{
316337
AllowPrivilegeEscalation: ptr.To(false),
317338
RunAsNonRoot: ptr.To(true),
318339
Privileged: ptr.To(false),
@@ -324,21 +345,21 @@ func reconcilePodSpec(
324345
Drop: []corev1.Capability{"ALL"},
325346
},
326347
}
327-
sidecarConfig.Resources = barmanObjectStore.Spec.InstanceSidecarConfiguration.Resources
348+
sidecarTemplate.Resources = config.resources
328349

329350
// merge the main container envs if they aren't already set
330351
for _, container := range spec.Containers {
331352
if container.Name == mainContainerName {
332353
for _, env := range container.Env {
333354
found := false
334-
for _, existingEnv := range sidecarConfig.Env {
355+
for _, existingEnv := range sidecarTemplate.Env {
335356
if existingEnv.Name == env.Name {
336357
found = true
337358
break
338359
}
339360
}
340361
if !found {
341-
sidecarConfig.Env = append(sidecarConfig.Env, env)
362+
sidecarTemplate.Env = append(sidecarTemplate.Env, env)
342363
}
343364
}
344365
break
@@ -348,18 +369,18 @@ func reconcilePodSpec(
348369
// merge the default envs if they aren't already set
349370
for _, env := range envs {
350371
found := false
351-
for _, existingEnv := range sidecarConfig.Env {
372+
for _, existingEnv := range sidecarTemplate.Env {
352373
if existingEnv.Name == env.Name {
353374
found = true
354375
break
355376
}
356377
}
357378
if !found {
358-
sidecarConfig.Env = append(sidecarConfig.Env, env)
379+
sidecarTemplate.Env = append(sidecarTemplate.Env, env)
359380
}
360381
}
361382

362-
if err := injectPluginSidecarPodSpec(spec, &sidecarConfig, mainContainerName); err != nil {
383+
if err := injectPluginSidecarPodSpec(spec, &sidecarTemplate, mainContainerName); err != nil {
363384
return err
364385
}
365386

@@ -369,7 +390,7 @@ func reconcilePodSpec(
369390
Name: barmanCertificatesVolumeName,
370391
VolumeSource: corev1.VolumeSource{
371392
Projected: &corev1.ProjectedVolumeSource{
372-
Sources: certificates,
393+
Sources: config.certificates,
373394
},
374395
},
375396
})

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, nil, nil)
110+
response, err := reconcileJob(ctx, cluster, request, sidecarConfiguration{})
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, nil, nil)
131+
response, err := reconcileJob(ctx, cluster, request, sidecarConfiguration{})
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, nil, nil)
141+
response, err := reconcileJob(ctx, cluster, request, sidecarConfiguration{})
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, nil, nil)
168+
response, err := reconcileJob(ctx, cluster, request, sidecarConfiguration{})
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, nil, nil)
188+
response, err := reconcilePod(ctx, cluster, request, pluginConfiguration, sidecarConfiguration{})
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, nil, nil)
206+
response, err := reconcilePod(ctx, cluster, request, pluginConfiguration, sidecarConfiguration{})
207207
Expect(err).To(HaveOccurred())
208208
Expect(response).To(BeNil())
209209
})

0 commit comments

Comments
 (0)