Skip to content

Commit

Permalink
Allow global labels and annotations to be specified (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegridman authored Feb 28, 2024
1 parent cab4f84 commit a82de9e
Show file tree
Hide file tree
Showing 54 changed files with 1,416 additions and 319 deletions.
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,22 @@ endif
# By default we target amd64 as this is by far the most common local build environment
# We actually build images for amd64 and arm64
# ----------------------------------------------------------------------------------------------------------------------
IMAGE_ARCH ?= amd64
ARCH ?= amd64
UNAME_S = $(shell uname -s)
UNAME_M = $(shell uname -m)
ifeq (x86_64, $(UNAME_M))
IMAGE_ARCH = amd64
ARCH = amd64
else
IMAGE_ARCH = $(UNAME_M)
ARCH = $(UNAME_M)
endif

OS ?= linux
UNAME_S := $(shell uname -s)
GOPROXY ?= https://proxy.golang.org

# ----------------------------------------------------------------------------------------------------------------------
# Set the location of the Operator SDK executable
# ----------------------------------------------------------------------------------------------------------------------
UNAME_S = $(shell uname -s)
UNAME_M = $(shell uname -m)
OPERATOR_SDK_VERSION := v1.9.0

# ----------------------------------------------------------------------------------------------------------------------
Expand Down
49 changes: 38 additions & 11 deletions api/v1/coherence_types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand Down Expand Up @@ -864,13 +864,14 @@ func (in *PersistentStorageSpec) CreatePersistentVolumeClaim(deployment *Coheren
in.PersistentVolumeClaim.DeepCopyInto(&spec)
}

labels := deployment.CreateCommonLabels()
labels := deployment.CreateGlobalLabels()
labels[LabelComponent] = LabelComponentPVC

return &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
Name: name,
Labels: labels,
Annotations: deployment.CreateGlobalAnnotations(),
},
Spec: spec,
}
Expand Down Expand Up @@ -1099,7 +1100,7 @@ func (in *NamedPortSpec) CreateService(deployment CoherenceResource) *corev1.Ser
name, _ := in.GetServiceName(deployment)

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

// The service annotations
var ann map[string]string
ann := deployment.CreateGlobalAnnotations()
if in.Service != nil && in.Service.Annotations != nil {
ann = in.Service.Annotations
if ann == nil {
ann = in.Service.Annotations
} else {
for k, v := range in.Service.Annotations {
ann[k] = v
}
}
}

// Create the Service serviceSpec
Expand Down Expand Up @@ -1181,7 +1188,7 @@ func (in *NamedPortSpec) CreateServiceMonitor(deployment CoherenceResource) *mon
}

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

return &monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: deployment.GetNamespace(),
Labels: labels,
Name: name,
Namespace: deployment.GetNamespace(),
Labels: labels,
Annotations: deployment.CreateGlobalAnnotations(),
},
Spec: spec,
}
Expand Down Expand Up @@ -2979,6 +2987,25 @@ func (in *PersistentVolumeClaimObjectMeta) toObjectMeta() metav1.ObjectMeta {
}
}

// ----- GlobalSpec ---------------------------------------------------------

// GlobalSpec is attributes that will be applied to all resources managed by the Operator.
type GlobalSpec struct {
// Map of string keys and values that can be used to organize and categorize
// (scope and select) objects. May match selectors of replication controllers
// and services.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
// +optional
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"`

// Annotations is an unstructured key value map stored with a resource that may be
// set by external tools to store and retrieve arbitrary metadata. They are not
// queryable and should be preserved when modifying objects.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/
// +optional
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`
}

// ----- helper methods -----------------------------------------------------

// Int32PtrToStringWithDefault converts an int32 pointer to a string using the default if the pointer is nil.
Expand Down
55 changes: 50 additions & 5 deletions api/v1/coherencejobresource_types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/

package v1

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

func (in *CoherenceJob) GetGlobalSpec() *GlobalSpec {
if in == nil {
return nil
}
return in.Spec.Global
}

// GetSpec returns this resource's CoherenceResourceSpec
func (in *CoherenceJob) GetSpec() *CoherenceResourceSpec {
return &in.Spec.CoherenceResourceSpec
Expand Down Expand Up @@ -193,6 +201,25 @@ func (in *CoherenceJob) FindPortServiceName(name string) (string, bool) {
return in.Spec.FindPortServiceName(name, in)
}

// CreateGlobalLabels creates the common label set for all resources.
func (in *CoherenceJob) CreateGlobalLabels() map[string]string {
labels := operator.GetGlobalLabelsNoError()
if labels == nil {
labels = make(map[string]string)
}

globalSpec := in.GetGlobalSpec()
if globalSpec != nil {
for k, v := range globalSpec.Labels {
labels[k] = v
}
}
for k, v := range in.CreateCommonLabels() {
labels[k] = v
}
return labels
}

// CreateCommonLabels creates the deployment's common label set.
func (in *CoherenceJob) CreateCommonLabels() map[string]string {
labels := make(map[string]string)
Expand All @@ -211,6 +238,21 @@ func (in *CoherenceJob) CreateCommonLabels() map[string]string {
return labels
}

// CreateGlobalAnnotations creates the common annotation set for all resources.
func (in *CoherenceJob) CreateGlobalAnnotations() map[string]string {
annotations := operator.GetGlobalAnnotationsNoError()
globalSpec := in.GetGlobalSpec()
if globalSpec != nil && globalSpec.Annotations != nil {
if annotations == nil {
annotations = make(map[string]string)
}
for k, v := range globalSpec.Annotations {
annotations[k] = v
}
}
return annotations
}

// CreateAnnotations returns the annotations to apply to this cluster's
// deployment (StatefulSet).
func (in *CoherenceJob) CreateAnnotations() map[string]string {
Expand Down Expand Up @@ -409,6 +451,8 @@ type CoherenceJobResourceSpec struct {
// Cannot be updated.
// +optional
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
// Global contains attributes that will be applied to all resources managed by the Coherence Operator.
Global *GlobalSpec `json:"global,omitempty"`
}

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

// CreateJobResource creates the deployment's Job resource.
func (in *CoherenceJobResourceSpec) CreateJobResource(deployment CoherenceResource) Resource {
func (in *CoherenceJobResourceSpec) CreateJobResource(deployment *CoherenceJob) Resource {
job := in.CreateJob(deployment)

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

// CreateJob creates the deployment's Job.
func (in *CoherenceJobResourceSpec) CreateJob(deployment CoherenceResource) batchv1.Job {
func (in *CoherenceJobResourceSpec) CreateJob(deployment *CoherenceJob) batchv1.Job {
ann := deployment.CreateGlobalAnnotations()
job := batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Namespace: deployment.GetNamespace(),
Name: deployment.GetName(),
Labels: deployment.CreateCommonLabels(),
Annotations: deployment.CreateAnnotations(),
Labels: deployment.CreateGlobalLabels(),
Annotations: ann,
},
}

Expand Down
8 changes: 7 additions & 1 deletion api/v1/coherenceresource.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand Down Expand Up @@ -43,6 +43,10 @@ type CoherenceResource interface {
FindPortServiceName(name string) (string, bool)
// CreateCommonLabels creates the deployment's common label set.
CreateCommonLabels() map[string]string
// CreateGlobalLabels creates the common label set for all resources.
CreateGlobalLabels() map[string]string
// CreateGlobalAnnotations creates the common annotation set for all resources.
CreateGlobalAnnotations() map[string]string
// CreateAnnotations returns the annotations to apply to this cluster's
// deployment (StatefulSet).
CreateAnnotations() map[string]string
Expand Down Expand Up @@ -92,4 +96,6 @@ type CoherenceResource interface {
IsForceExit() bool
// GetEnvVarFrom returns the array of EnvVarSource configurations
GetEnvVarFrom() []corev1.EnvFromSource
// GetGlobalSpec returns the attributes to be applied to all resources
GetGlobalSpec() *GlobalSpec
}
58 changes: 53 additions & 5 deletions api/v1/coherenceresource_types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023, Oracle and/or its affiliates.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
Expand All @@ -8,6 +8,7 @@ package v1

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

func (in *Coherence) GetGlobalSpec() *GlobalSpec {
if in == nil {
return nil
}
return in.Spec.Global
}

// FindFullyQualifiedPortServiceNames returns a map of the exposed ports of this resource mapped to their Service's
// fully qualified domain name.
func (in *Coherence) FindFullyQualifiedPortServiceNames() map[string]string {
Expand Down Expand Up @@ -226,6 +234,24 @@ func (in *Coherence) FindPortServiceName(name string) (string, bool) {
return in.Spec.FindPortServiceName(name, in)
}

// CreateGlobalLabels creates the common label set for all resources.
func (in *Coherence) CreateGlobalLabels() map[string]string {
labels := operator.GetGlobalLabelsNoError()
if labels == nil {
labels = make(map[string]string)
}
globalSpec := in.GetGlobalSpec()
if globalSpec != nil {
for k, v := range globalSpec.Labels {
labels[k] = v
}
}
for k, v := range in.CreateCommonLabels() {
labels[k] = v
}
return labels
}

// CreateCommonLabels creates the deployment's common label set.
func (in *Coherence) CreateCommonLabels() map[string]string {
labels := make(map[string]string)
Expand All @@ -244,17 +270,37 @@ func (in *Coherence) CreateCommonLabels() map[string]string {
return labels
}

// CreateGlobalAnnotations creates the common annotation set for all resources.
func (in *Coherence) CreateGlobalAnnotations() map[string]string {
annotations := operator.GetGlobalAnnotationsNoError()
globalSpec := in.GetGlobalSpec()
if globalSpec != nil && globalSpec.Annotations != nil {
if annotations == nil {
annotations = make(map[string]string)
}
for k, v := range globalSpec.Annotations {
annotations[k] = v
}
}
return annotations
}

// CreateAnnotations returns the annotations to apply to this cluster's
// deployment (StatefulSet).
func (in *Coherence) CreateAnnotations() map[string]string {
var annotations map[string]string
annotations := in.CreateGlobalAnnotations()

if in.Spec.StatefulSetAnnotations != nil {
annotations = make(map[string]string)
if annotations == nil {
annotations = make(map[string]string)
}
for k, v := range in.Spec.StatefulSetAnnotations {
annotations[k] = v
}
} else if in.Annotations != nil {
annotations = make(map[string]string)
if annotations == nil {
annotations = make(map[string]string)
}
for k, v := range in.Annotations {
annotations[k] = v
}
Expand Down Expand Up @@ -449,6 +495,8 @@ type CoherenceStatefulSetResourceSpec struct {
// Cannot be updated.
// +optional
EnvFrom []corev1.EnvFromSource `json:"envFrom,omitempty"`
// Global contains attributes that will be applied to all resources managed by the Coherence Operator.
Global *GlobalSpec `json:"global,omitempty"`
}

// CreateStatefulSetResource creates the deployment's StatefulSet resource.
Expand All @@ -468,7 +516,7 @@ func (in *CoherenceStatefulSetResourceSpec) CreateStatefulSet(deployment *Cohere
ObjectMeta: metav1.ObjectMeta{
Namespace: deployment.GetNamespace(),
Name: deployment.GetName(),
Labels: deployment.CreateCommonLabels(),
Labels: deployment.CreateGlobalLabels(),
Annotations: deployment.CreateAnnotations(),
},
}
Expand Down
Loading

0 comments on commit a82de9e

Please sign in to comment.