diff --git a/pkg/apis/mysql/v1alpha1/types.go b/pkg/apis/mysql/v1alpha1/types.go index dcc8735cf..c0275dea5 100644 --- a/pkg/apis/mysql/v1alpha1/types.go +++ b/pkg/apis/mysql/v1alpha1/types.go @@ -75,7 +75,8 @@ type ClusterSpec struct { // set of Nodes a Pod can be scheduled on Tolerations *[]corev1.Toleration `json:"tolerations,omitempty"` // Resources holds ResourceRequirements for the MySQL Agent & Server Containers - Resources *Resources `json:"resources,omitempty"` + Resources *Resources `json:"resources,omitempty"` + PodLabels map[string]string `json:"podLabels,omitempty"` } // ClusterConditionType represents a valid condition of a Cluster. diff --git a/pkg/resources/statefulsets/statefulset.go b/pkg/resources/statefulsets/statefulset.go index b3ae46f3c..d842ac9b8 100644 --- a/pkg/resources/statefulsets/statefulset.go +++ b/pkg/resources/statefulsets/statefulset.go @@ -383,6 +383,12 @@ func NewForCluster(cluster *v1alpha1.Cluster, images operatoropts.Images, servic podLabels[constants.LabelClusterRole] = constants.ClusterRolePrimary } + if cluster.Spec.PodLabels != nil { + for k, v := range cluster.Spec.PodLabels { + podLabels[k] = v + } + } + ss := &apps.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, diff --git a/pkg/resources/statefulsets/statefulset_test.go b/pkg/resources/statefulsets/statefulset_test.go index fc3050e79..ccbb15b2c 100644 --- a/pkg/resources/statefulsets/statefulset_test.go +++ b/pkg/resources/statefulsets/statefulset_test.go @@ -15,6 +15,7 @@ package statefulsets import ( + "github.com/oracle/mysql-operator/pkg/constants" "reflect" "testing" @@ -357,3 +358,23 @@ func TestClusterNotSetGroupExitStateArgs(t *testing.T) { assert.NotContains(t, cmd, "--loose-group-replication-exit-state-action=READ_ONLY") } + +func TestClusterWithPodLabels(t *testing.T) { + cluster := &v1alpha1.Cluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + }, + Spec: v1alpha1.ClusterSpec{ + PodLabels: map[string]string{ + "example.com/testing": "enabled", + }, + }, + } + cluster.EnsureDefaults() + statefulSet := NewForCluster(cluster, mockOperatorConfig().Images, "mycluster") + + // check original labels exist + assert.Equal(t, "test", statefulSet.Spec.Template.Labels[constants.ClusterLabel]) + // check additional labels exist + assert.Equal(t, "enabled", statefulSet.Spec.Template.Labels["example.com/testing"]) +}