Skip to content

K8SPS-466: global labels and annotations #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions api/v1alpha1/perconaservermysql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const (
// +kubebuilder:validation:XValidation:rule="!(self.mysql.clusterType == 'group-replication' && has(self.mysql.size) && self.mysql.size >= 9) || self.unsafeFlags.mysqlSize",message="Invalid configuration: For 'group replication', scaling MySQL replicas above 9 requires 'unsafeFlags.mysqlSize: true'"
// +kubebuilder:validation:XValidation:rule="!(self.mysql.clusterType == 'group-replication' && has(self.mysql.size) && self.mysql.size % 2 == 0) || self.unsafeFlags.mysqlSize",message="Invalid configuration: For 'group replication', using an even number of MySQL replicas requires 'unsafeFlags.mysqlSize: true'"
type PerconaServerMySQLSpec struct {
Metadata *Metadata `json:"metadata,omitempty"`
CRVersion string `json:"crVersion,omitempty"`
Pause bool `json:"pause,omitempty"`
VolumeExpansionEnabled bool `json:"enableVolumeExpansion,omitempty"`
Expand Down Expand Up @@ -197,6 +198,22 @@ type PodSpec struct {
ContainerSpec `json:",inline"`
}

type Metadata 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"`
}

// GetTerminationGracePeriodSeconds returns the configured termination grace period for the Pod.
// If not explicitly set, it returns the default grace period.
func (s PodSpec) GetTerminationGracePeriodSeconds() *int64 {
Expand Down Expand Up @@ -594,6 +611,22 @@ func (cr *PerconaServerMySQL) SetVersion() {
cr.Spec.CRVersion = version.Version()
}

func (cr *PerconaServerMySQL) GlobalLabels() map[string]string {
if cr.Spec.Metadata == nil {
return nil
}

return cr.Spec.Metadata.Labels
}

func (cr *PerconaServerMySQL) GlobalAnnotations() map[string]string {
if cr.Spec.Metadata == nil {
return nil
}

return cr.Spec.Metadata.Annotations
}

func (cr *PerconaServerMySQL) Version() *v.Version {
return v.Must(v.NewVersion(cr.Spec.CRVersion))
}
Expand Down
34 changes: 34 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions config/crd/bases/ps.percona.com_perconaservermysqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2338,6 +2338,17 @@ spec:
type: array
initImage:
type: string
metadata:
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
mysql:
properties:
affinity:
Expand Down
11 changes: 11 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4261,6 +4261,17 @@ spec:
type: array
initImage:
type: string
metadata:
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
mysql:
properties:
affinity:
Expand Down
6 changes: 6 additions & 0 deletions deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ metadata:
# - percona.com/delete-ssl
# - percona.com/delete-mysql-pvc
spec:
# metadata:
# annotations:
# service.beta.kubernetes.io/aws-load-balancer-backend-protocol: tcp
# service.beta.kubernetes.io/aws-load-balancer-type: nlb
# labels:
# rack: rack-22
# unsafeFlags:
# mysqlSize: false
# orchestrator: false
Expand Down
11 changes: 11 additions & 0 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4261,6 +4261,17 @@ spec:
type: array
initImage:
type: string
metadata:
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
mysql:
properties:
affinity:
Expand Down
11 changes: 11 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4261,6 +4261,17 @@ spec:
type: array
initImage:
type: string
metadata:
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
mysql:
properties:
affinity:
Expand Down
11 changes: 6 additions & 5 deletions pkg/binlogserver/binlog_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ConfigSecretName(cr *apiv1alpha1.PerconaServerMySQL) string {
}

func MatchLabels(cr *apiv1alpha1.PerconaServerMySQL) map[string]string {
return util.SSMapMerge(
return util.SSMapMerge(cr.GlobalLabels(),
cr.MySQLSpec().Labels,
cr.Labels(AppName, naming.ComponentPITR),
)
Expand All @@ -61,9 +61,10 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash strin
Kind: "StatefulSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: Name(cr),
Namespace: cr.Namespace,
Labels: labels,
Name: Name(cr),
Namespace: cr.Namespace,
Labels: labels,
Annotations: cr.GlobalAnnotations(),
},
Spec: appsv1.StatefulSetSpec{
Replicas: &spec.Size,
Expand All @@ -73,7 +74,7 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash strin
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: annotations,
Annotations: util.SSMapMerge(cr.GlobalAnnotations(), annotations),
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
Expand Down
18 changes: 12 additions & 6 deletions pkg/controller/ps/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ func (r *PerconaServerMySQLReconciler) ensureSSLByCertManager(ctx context.Contex

caCert := &cm.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: certName,
Namespace: cr.Namespace,
Name: certName,
Namespace: cr.Namespace,
Labels: cr.GlobalLabels(),
Annotations: cr.GlobalAnnotations(),
},
Spec: cm.CertificateSpec{
SecretName: secretName,
Expand Down Expand Up @@ -190,8 +192,10 @@ func (r *PerconaServerMySQLReconciler) ensureSSLByCertManager(ctx context.Contex

kubeCert := &cm.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: certName,
Namespace: cr.Namespace,
Name: certName,
Namespace: cr.Namespace,
Labels: cr.GlobalLabels(),
Annotations: cr.GlobalAnnotations(),
},
Spec: cm.CertificateSpec{
SecretName: cr.Spec.SSLSecretName,
Expand All @@ -216,8 +220,10 @@ func (r *PerconaServerMySQLReconciler) ensureIssuer(ctx context.Context, cr *api
) error {
isr := &cm.Issuer{
ObjectMeta: metav1.ObjectMeta{
Name: issuerName,
Namespace: cr.Namespace,
Name: issuerName,
Namespace: cr.Namespace,
Labels: cr.GlobalLabels(),
Annotations: cr.GlobalAnnotations(),
},
Spec: cm.IssuerSpec{
IssuerConfig: IssuerConf,
Expand Down
13 changes: 7 additions & 6 deletions pkg/haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func ServiceName(cr *apiv1alpha1.PerconaServerMySQL) string {
}

func MatchLabels(cr *apiv1alpha1.PerconaServerMySQL) map[string]string {
return util.SSMapMerge(cr.MySQLSpec().Labels,
return util.SSMapMerge(cr.GlobalLabels(), cr.Spec.Proxy.HAProxy.Labels,
cr.Labels(AppName, naming.ComponentProxy))
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func Service(cr *apiv1alpha1.PerconaServerMySQL, secret *corev1.Secret) *corev1.
Name: ServiceName(cr),
Namespace: cr.Namespace,
Labels: labels,
Annotations: expose.Annotations,
Annotations: util.SSMapMerge(cr.GlobalAnnotations(), expose.Annotations),
},
Spec: corev1.ServiceSpec{
Type: serviceType,
Expand Down Expand Up @@ -142,9 +142,10 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash, tlsH
Kind: "StatefulSet",
},
ObjectMeta: metav1.ObjectMeta{
Name: Name(cr),
Namespace: cr.Namespace,
Labels: labels,
Name: Name(cr),
Namespace: cr.Namespace,
Labels: labels,
Annotations: cr.GlobalAnnotations(),
},
Spec: appsv1.StatefulSetSpec{
Replicas: &cr.Spec.Proxy.HAProxy.Size,
Expand All @@ -156,7 +157,7 @@ func StatefulSet(cr *apiv1alpha1.PerconaServerMySQL, initImage, configHash, tlsH
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: annotations,
Annotations: util.SSMapMerge(cr.GlobalAnnotations(), annotations),
},
Spec: corev1.PodSpec{
NodeSelector: cr.Spec.Proxy.HAProxy.NodeSelector,
Expand Down
6 changes: 4 additions & 2 deletions pkg/k8s/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1"
)

func PVC(name string, spec *apiv1alpha1.VolumeSpec) corev1.PersistentVolumeClaim {
func PVC(cr *apiv1alpha1.PerconaServerMySQL, name string, spec *apiv1alpha1.VolumeSpec) corev1.PersistentVolumeClaim {
return corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: name,
Labels: cr.GlobalLabels(),
Annotations: cr.GlobalAnnotations(),
},
Spec: corev1.PersistentVolumeClaimSpec{
StorageClassName: spec.PersistentVolumeClaim.StorageClassName,
Expand Down
6 changes: 3 additions & 3 deletions pkg/k8s/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package k8s
import (
"testing"

apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1"
)

func TestPVC(t *testing.T) {
Expand All @@ -29,7 +29,7 @@ func TestPVC(t *testing.T) {

pvcName := "pvc-name"

pvc := PVC(pvcName, spec)
pvc := PVC(new(apiv1alpha1.PerconaServerMySQL), pvcName, spec)

assert.Equal(t, pvcName, pvc.Name)
assert.NotNil(t, pvc.Spec.StorageClassName)
Expand Down
Loading
Loading