Skip to content

Commit a82de9e

Browse files
authored
Allow global labels and annotations to be specified (#643)
1 parent cab4f84 commit a82de9e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1416
-319
lines changed

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,22 @@ endif
7171
# By default we target amd64 as this is by far the most common local build environment
7272
# We actually build images for amd64 and arm64
7373
# ----------------------------------------------------------------------------------------------------------------------
74-
IMAGE_ARCH ?= amd64
75-
ARCH ?= amd64
74+
UNAME_S = $(shell uname -s)
75+
UNAME_M = $(shell uname -m)
76+
ifeq (x86_64, $(UNAME_M))
77+
IMAGE_ARCH = amd64
78+
ARCH = amd64
79+
else
80+
IMAGE_ARCH = $(UNAME_M)
81+
ARCH = $(UNAME_M)
82+
endif
83+
7684
OS ?= linux
77-
UNAME_S := $(shell uname -s)
7885
GOPROXY ?= https://proxy.golang.org
7986

8087
# ----------------------------------------------------------------------------------------------------------------------
8188
# Set the location of the Operator SDK executable
8289
# ----------------------------------------------------------------------------------------------------------------------
83-
UNAME_S = $(shell uname -s)
84-
UNAME_M = $(shell uname -m)
8590
OPERATOR_SDK_VERSION := v1.9.0
8691

8792
# ----------------------------------------------------------------------------------------------------------------------

api/v1/coherence_types.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -864,13 +864,14 @@ func (in *PersistentStorageSpec) CreatePersistentVolumeClaim(deployment *Coheren
864864
in.PersistentVolumeClaim.DeepCopyInto(&spec)
865865
}
866866

867-
labels := deployment.CreateCommonLabels()
867+
labels := deployment.CreateGlobalLabels()
868868
labels[LabelComponent] = LabelComponentPVC
869869

870870
return &corev1.PersistentVolumeClaim{
871871
ObjectMeta: metav1.ObjectMeta{
872-
Name: name,
873-
Labels: labels,
872+
Name: name,
873+
Labels: labels,
874+
Annotations: deployment.CreateGlobalAnnotations(),
874875
},
875876
Spec: spec,
876877
}
@@ -1099,7 +1100,7 @@ func (in *NamedPortSpec) CreateService(deployment CoherenceResource) *corev1.Ser
10991100
name, _ := in.GetServiceName(deployment)
11001101

11011102
// The labels for the service
1102-
svcLabels := deployment.CreateCommonLabels()
1103+
svcLabels := deployment.CreateGlobalLabels()
11031104
svcLabels[LabelComponent] = LabelComponentPortService
11041105
svcLabels[LabelPort] = in.Name
11051106
if in.Service != nil {
@@ -1109,9 +1110,15 @@ func (in *NamedPortSpec) CreateService(deployment CoherenceResource) *corev1.Ser
11091110
}
11101111

11111112
// The service annotations
1112-
var ann map[string]string
1113+
ann := deployment.CreateGlobalAnnotations()
11131114
if in.Service != nil && in.Service.Annotations != nil {
1114-
ann = in.Service.Annotations
1115+
if ann == nil {
1116+
ann = in.Service.Annotations
1117+
} else {
1118+
for k, v := range in.Service.Annotations {
1119+
ann[k] = v
1120+
}
1121+
}
11151122
}
11161123

11171124
// Create the Service serviceSpec
@@ -1181,7 +1188,7 @@ func (in *NamedPortSpec) CreateServiceMonitor(deployment CoherenceResource) *mon
11811188
}
11821189

11831190
// The labels for the ServiceMonitor
1184-
labels := deployment.CreateCommonLabels()
1191+
labels := deployment.CreateGlobalLabels()
11851192
labels[LabelComponent] = LabelComponentPortServiceMonitor
11861193
for k, v := range in.ServiceMonitor.Labels {
11871194
labels[k] = v
@@ -1206,9 +1213,10 @@ func (in *NamedPortSpec) CreateServiceMonitor(deployment CoherenceResource) *mon
12061213

12071214
return &monitoringv1.ServiceMonitor{
12081215
ObjectMeta: metav1.ObjectMeta{
1209-
Name: name,
1210-
Namespace: deployment.GetNamespace(),
1211-
Labels: labels,
1216+
Name: name,
1217+
Namespace: deployment.GetNamespace(),
1218+
Labels: labels,
1219+
Annotations: deployment.CreateGlobalAnnotations(),
12121220
},
12131221
Spec: spec,
12141222
}
@@ -2979,6 +2987,25 @@ func (in *PersistentVolumeClaimObjectMeta) toObjectMeta() metav1.ObjectMeta {
29792987
}
29802988
}
29812989

2990+
// ----- GlobalSpec ---------------------------------------------------------
2991+
2992+
// GlobalSpec is attributes that will be applied to all resources managed by the Operator.
2993+
type GlobalSpec struct {
2994+
// Map of string keys and values that can be used to organize and categorize
2995+
// (scope and select) objects. May match selectors of replication controllers
2996+
// and services.
2997+
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
2998+
// +optional
2999+
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"`
3000+
3001+
// Annotations is an unstructured key value map stored with a resource that may be
3002+
// set by external tools to store and retrieve arbitrary metadata. They are not
3003+
// queryable and should be preserved when modifying objects.
3004+
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
3005+
// +optional
3006+
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`
3007+
}
3008+
29823009
// ----- helper methods -----------------------------------------------------
29833010

29843011
// Int32PtrToStringWithDefault converts an int32 pointer to a string using the default if the pointer is nil.

api/v1/coherencejobresource_types.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
66

77
package v1
88

99
import (
10+
"github.com/oracle/coherence-operator/pkg/operator"
1011
"golang.org/x/mod/semver"
1112
batchv1 "k8s.io/api/batch/v1"
1213
corev1 "k8s.io/api/core/v1"
@@ -68,6 +69,13 @@ func (in *CoherenceJob) GetEnvVarFrom() []corev1.EnvFromSource {
6869
return in.Spec.EnvFrom
6970
}
7071

72+
func (in *CoherenceJob) GetGlobalSpec() *GlobalSpec {
73+
if in == nil {
74+
return nil
75+
}
76+
return in.Spec.Global
77+
}
78+
7179
// GetSpec returns this resource's CoherenceResourceSpec
7280
func (in *CoherenceJob) GetSpec() *CoherenceResourceSpec {
7381
return &in.Spec.CoherenceResourceSpec
@@ -193,6 +201,25 @@ func (in *CoherenceJob) FindPortServiceName(name string) (string, bool) {
193201
return in.Spec.FindPortServiceName(name, in)
194202
}
195203

204+
// CreateGlobalLabels creates the common label set for all resources.
205+
func (in *CoherenceJob) CreateGlobalLabels() map[string]string {
206+
labels := operator.GetGlobalLabelsNoError()
207+
if labels == nil {
208+
labels = make(map[string]string)
209+
}
210+
211+
globalSpec := in.GetGlobalSpec()
212+
if globalSpec != nil {
213+
for k, v := range globalSpec.Labels {
214+
labels[k] = v
215+
}
216+
}
217+
for k, v := range in.CreateCommonLabels() {
218+
labels[k] = v
219+
}
220+
return labels
221+
}
222+
196223
// CreateCommonLabels creates the deployment's common label set.
197224
func (in *CoherenceJob) CreateCommonLabels() map[string]string {
198225
labels := make(map[string]string)
@@ -211,6 +238,21 @@ func (in *CoherenceJob) CreateCommonLabels() map[string]string {
211238
return labels
212239
}
213240

241+
// CreateGlobalAnnotations creates the common annotation set for all resources.
242+
func (in *CoherenceJob) CreateGlobalAnnotations() map[string]string {
243+
annotations := operator.GetGlobalAnnotationsNoError()
244+
globalSpec := in.GetGlobalSpec()
245+
if globalSpec != nil && globalSpec.Annotations != nil {
246+
if annotations == nil {
247+
annotations = make(map[string]string)
248+
}
249+
for k, v := range globalSpec.Annotations {
250+
annotations[k] = v
251+
}
252+
}
253+
return annotations
254+
}
255+
214256
// CreateAnnotations returns the annotations to apply to this cluster's
215257
// deployment (StatefulSet).
216258
func (in *CoherenceJob) CreateAnnotations() map[string]string {
@@ -409,6 +451,8 @@ type CoherenceJobResourceSpec struct {
409451
// Cannot be updated.
410452
// +optional
411453
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
454+
// Global contains attributes that will be applied to all resources managed by the Coherence Operator.
455+
Global *GlobalSpec `json:"global,omitempty"`
412456
}
413457

414458
// GetRestartPolicy returns the name of the application image to use
@@ -458,7 +502,7 @@ func (in *CoherenceJobResourceSpec) IsSyncCompletions() bool {
458502
}
459503

460504
// CreateJobResource creates the deployment's Job resource.
461-
func (in *CoherenceJobResourceSpec) CreateJobResource(deployment CoherenceResource) Resource {
505+
func (in *CoherenceJobResourceSpec) CreateJobResource(deployment *CoherenceJob) Resource {
462506
job := in.CreateJob(deployment)
463507

464508
return Resource{
@@ -469,13 +513,14 @@ func (in *CoherenceJobResourceSpec) CreateJobResource(deployment CoherenceResour
469513
}
470514

471515
// CreateJob creates the deployment's Job.
472-
func (in *CoherenceJobResourceSpec) CreateJob(deployment CoherenceResource) batchv1.Job {
516+
func (in *CoherenceJobResourceSpec) CreateJob(deployment *CoherenceJob) batchv1.Job {
517+
ann := deployment.CreateGlobalAnnotations()
473518
job := batchv1.Job{
474519
ObjectMeta: metav1.ObjectMeta{
475520
Namespace: deployment.GetNamespace(),
476521
Name: deployment.GetName(),
477-
Labels: deployment.CreateCommonLabels(),
478-
Annotations: deployment.CreateAnnotations(),
522+
Labels: deployment.CreateGlobalLabels(),
523+
Annotations: ann,
479524
},
480525
}
481526

api/v1/coherenceresource.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -43,6 +43,10 @@ type CoherenceResource interface {
4343
FindPortServiceName(name string) (string, bool)
4444
// CreateCommonLabels creates the deployment's common label set.
4545
CreateCommonLabels() map[string]string
46+
// CreateGlobalLabels creates the common label set for all resources.
47+
CreateGlobalLabels() map[string]string
48+
// CreateGlobalAnnotations creates the common annotation set for all resources.
49+
CreateGlobalAnnotations() map[string]string
4650
// CreateAnnotations returns the annotations to apply to this cluster's
4751
// deployment (StatefulSet).
4852
CreateAnnotations() map[string]string
@@ -92,4 +96,6 @@ type CoherenceResource interface {
9296
IsForceExit() bool
9397
// GetEnvVarFrom returns the array of EnvVarSource configurations
9498
GetEnvVarFrom() []corev1.EnvFromSource
99+
// GetGlobalSpec returns the attributes to be applied to all resources
100+
GetGlobalSpec() *GlobalSpec
95101
}

api/v1/coherenceresource_types.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at
44
* http://oss.oracle.com/licenses/upl.
55
*/
@@ -8,6 +8,7 @@ package v1
88

99
import (
1010
"fmt"
11+
"github.com/oracle/coherence-operator/pkg/operator"
1112
"golang.org/x/mod/semver"
1213
appsv1 "k8s.io/api/apps/v1"
1314
batchv1 "k8s.io/api/batch/v1"
@@ -186,6 +187,13 @@ func (in *Coherence) GetEnvVarFrom() []corev1.EnvFromSource {
186187
return in.Spec.EnvFrom
187188
}
188189

190+
func (in *Coherence) GetGlobalSpec() *GlobalSpec {
191+
if in == nil {
192+
return nil
193+
}
194+
return in.Spec.Global
195+
}
196+
189197
// FindFullyQualifiedPortServiceNames returns a map of the exposed ports of this resource mapped to their Service's
190198
// fully qualified domain name.
191199
func (in *Coherence) FindFullyQualifiedPortServiceNames() map[string]string {
@@ -226,6 +234,24 @@ func (in *Coherence) FindPortServiceName(name string) (string, bool) {
226234
return in.Spec.FindPortServiceName(name, in)
227235
}
228236

237+
// CreateGlobalLabels creates the common label set for all resources.
238+
func (in *Coherence) CreateGlobalLabels() map[string]string {
239+
labels := operator.GetGlobalLabelsNoError()
240+
if labels == nil {
241+
labels = make(map[string]string)
242+
}
243+
globalSpec := in.GetGlobalSpec()
244+
if globalSpec != nil {
245+
for k, v := range globalSpec.Labels {
246+
labels[k] = v
247+
}
248+
}
249+
for k, v := range in.CreateCommonLabels() {
250+
labels[k] = v
251+
}
252+
return labels
253+
}
254+
229255
// CreateCommonLabels creates the deployment's common label set.
230256
func (in *Coherence) CreateCommonLabels() map[string]string {
231257
labels := make(map[string]string)
@@ -244,17 +270,37 @@ func (in *Coherence) CreateCommonLabels() map[string]string {
244270
return labels
245271
}
246272

273+
// CreateGlobalAnnotations creates the common annotation set for all resources.
274+
func (in *Coherence) CreateGlobalAnnotations() map[string]string {
275+
annotations := operator.GetGlobalAnnotationsNoError()
276+
globalSpec := in.GetGlobalSpec()
277+
if globalSpec != nil && globalSpec.Annotations != nil {
278+
if annotations == nil {
279+
annotations = make(map[string]string)
280+
}
281+
for k, v := range globalSpec.Annotations {
282+
annotations[k] = v
283+
}
284+
}
285+
return annotations
286+
}
287+
247288
// CreateAnnotations returns the annotations to apply to this cluster's
248289
// deployment (StatefulSet).
249290
func (in *Coherence) CreateAnnotations() map[string]string {
250-
var annotations map[string]string
291+
annotations := in.CreateGlobalAnnotations()
292+
251293
if in.Spec.StatefulSetAnnotations != nil {
252-
annotations = make(map[string]string)
294+
if annotations == nil {
295+
annotations = make(map[string]string)
296+
}
253297
for k, v := range in.Spec.StatefulSetAnnotations {
254298
annotations[k] = v
255299
}
256300
} else if in.Annotations != nil {
257-
annotations = make(map[string]string)
301+
if annotations == nil {
302+
annotations = make(map[string]string)
303+
}
258304
for k, v := range in.Annotations {
259305
annotations[k] = v
260306
}
@@ -449,6 +495,8 @@ type CoherenceStatefulSetResourceSpec struct {
449495
// Cannot be updated.
450496
// +optional
451497
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
498+
// Global contains attributes that will be applied to all resources managed by the Coherence Operator.
499+
Global *GlobalSpec `json:"global,omitempty"`
452500
}
453501

454502
// CreateStatefulSetResource creates the deployment's StatefulSet resource.
@@ -468,7 +516,7 @@ func (in *CoherenceStatefulSetResourceSpec) CreateStatefulSet(deployment *Cohere
468516
ObjectMeta: metav1.ObjectMeta{
469517
Namespace: deployment.GetNamespace(),
470518
Name: deployment.GetName(),
471-
Labels: deployment.CreateCommonLabels(),
519+
Labels: deployment.CreateGlobalLabels(),
472520
Annotations: deployment.CreateAnnotations(),
473521
},
474522
}

0 commit comments

Comments
 (0)