Skip to content

Commit be9bc73

Browse files
authored
Allow resource limits to be set for the Operator's init container (#647)
* Allow resource limits to be set for the Operator's init container * Add label to all rbac roles
1 parent 6f929b5 commit be9bc73

13 files changed

+116
-4
lines changed

api/v1/coherencejobresource_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ func (in *CoherenceJob) GetGlobalSpec() *GlobalSpec {
7676
return in.Spec.Global
7777
}
7878

79+
func (in *CoherenceJob) GetInitResources() *corev1.ResourceRequirements {
80+
if in == nil {
81+
return nil
82+
}
83+
return in.Spec.InitResources
84+
}
85+
7986
// GetSpec returns this resource's CoherenceResourceSpec
8087
func (in *CoherenceJob) GetSpec() *CoherenceResourceSpec {
8188
return &in.Spec.CoherenceResourceSpec
@@ -453,6 +460,12 @@ type CoherenceJobResourceSpec struct {
453460
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
454461
// Global contains attributes that will be applied to all resources managed by the Coherence Operator.
455462
Global *GlobalSpec `json:"global,omitempty"`
463+
// InitResources is the optional resource requests and limits for the init-container that the Operator
464+
// adds to the Pod.
465+
// ref: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
466+
// The Coherence operator does not apply any default resources.
467+
// +optional
468+
InitResources *corev1.ResourceRequirements `json:"initResources,omitempty"`
456469
}
457470

458471
// GetRestartPolicy returns the name of the application image to use

api/v1/coherenceresource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,6 @@ type CoherenceResource interface {
9898
GetEnvVarFrom() []corev1.EnvFromSource
9999
// GetGlobalSpec returns the attributes to be applied to all resources
100100
GetGlobalSpec() *GlobalSpec
101+
// GetInitResources returns the optional resource requirements for the init container
102+
GetInitResources() *corev1.ResourceRequirements
101103
}

api/v1/coherenceresource_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ func (in *Coherence) GetGlobalSpec() *GlobalSpec {
194194
return in.Spec.Global
195195
}
196196

197+
func (in *Coherence) GetInitResources() *corev1.ResourceRequirements {
198+
if in == nil {
199+
return nil
200+
}
201+
return in.Spec.InitResources
202+
}
203+
197204
// FindFullyQualifiedPortServiceNames returns a map of the exposed ports of this resource mapped to their Service's
198205
// fully qualified domain name.
199206
func (in *Coherence) FindFullyQualifiedPortServiceNames() map[string]string {
@@ -497,6 +504,12 @@ type CoherenceStatefulSetResourceSpec struct {
497504
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
498505
// Global contains attributes that will be applied to all resources managed by the Coherence Operator.
499506
Global *GlobalSpec `json:"global,omitempty"`
507+
// InitResources is the optional resource requests and limits for the init-container that the Operator
508+
// adds to the Pod.
509+
// ref: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/
510+
// The Coherence operator does not apply any default resources.
511+
// +optional
512+
InitResources *corev1.ResourceRequirements `json:"initResources,omitempty"`
500513
}
501514

502515
// CreateStatefulSetResource creates the deployment's StatefulSet resource.

api/v1/coherenceresourcespec_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,12 @@ func (in *CoherenceResourceSpec) CreateOperatorInitContainer(deployment Coherenc
10461046
// set the persistence volume mounts if required
10471047
in.Coherence.AddPersistenceVolumeMounts(&c)
10481048

1049+
// set the container resources if specified
1050+
r := deployment.GetInitResources()
1051+
if r != nil {
1052+
c.Resources = *r
1053+
}
1054+
10491055
return c
10501056
}
10511057

api/v1/create_statefulset_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,31 @@ func TestCreateStatefulSetWithPodAnnotations(t *testing.T) {
280280
assertStatefulSetCreation(t, deployment, stsExpected)
281281
}
282282

283+
func TestCreateStatefulSetWithInitContainerResources(t *testing.T) {
284+
res := corev1.ResourceRequirements{
285+
Limits: map[corev1.ResourceName]resource.Quantity{
286+
corev1.ResourceCPU: resource.MustParse("8"),
287+
},
288+
Requests: map[corev1.ResourceName]resource.Quantity{
289+
corev1.ResourceCPU: resource.MustParse("4"),
290+
},
291+
}
292+
293+
spec := coh.CoherenceStatefulSetResourceSpec{
294+
InitResources: &res,
295+
}
296+
297+
// Create the test deployment
298+
deployment := createTestCoherenceDeployment(spec)
299+
300+
// Create expected StatefulSet
301+
stsExpected := createMinimalExpectedStatefulSet(deployment)
302+
stsExpected.Spec.Template.Spec.InitContainers[0].Resources = res
303+
304+
// assert that the StatefulSet is as expected
305+
assertStatefulSetCreation(t, deployment, stsExpected)
306+
}
307+
283308
func TestCreateStatefulSetWithResources(t *testing.T) {
284309
res := corev1.ResourceRequirements{
285310
Limits: map[corev1.ResourceName]resource.Quantity{

api/v1/zz_generated.deepcopy.go

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

config/manager/webhook-secret.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ kind: Secret
77
metadata:
88
name: coherence-webhook-server-cert
99
namespace: coherence
10+
labels:
11+
control-plane: coherence

config/rbac/leader_election_role.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ apiVersion: rbac.authorization.k8s.io/v1
33
kind: Role
44
metadata:
55
name: leader-election-role
6+
labels:
7+
control-plane: coherence
68
rules:
79
- apiGroups:
810
- ""

config/rbac/leader_election_role_binding.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ apiVersion: rbac.authorization.k8s.io/v1
22
kind: RoleBinding
33
metadata:
44
name: leader-election-rolebinding
5+
labels:
6+
control-plane: coherence
57
roleRef:
68
apiGroup: rbac.authorization.k8s.io
79
kind: Role

config/rbac/role.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
apiVersion: rbac.authorization.k8s.io/v1
33
kind: ClusterRole
44
metadata:
5-
creationTimestamp: null
65
name: manager-role
6+
labels:
7+
control-plane: coherence
78
rules:
89
- apiGroups:
910
- ""

0 commit comments

Comments
 (0)