@@ -77,6 +77,7 @@ const (
7777 MaxSafeGRSize = 9
7878)
7979
80+ // Checks if the provided ClusterType is valid.
8081func (t ClusterType ) isValid () bool {
8182 switch ClusterType (t ) {
8283 case ClusterTypeGR , ClusterTypeAsync :
@@ -98,10 +99,12 @@ type MySQLSpec struct {
9899 PodSpec `json:",inline"`
99100}
100101
102+ // Checks if the MySQL cluster type is asynchronous.
101103func (m MySQLSpec ) IsAsync () bool {
102104 return m .ClusterType == ClusterTypeAsync
103105}
104106
107+ // Checks if the MySQL cluster type is Group Replication (GR).
105108func (m MySQLSpec ) IsGR () bool {
106109 return m .ClusterType == ClusterTypeGR
107110}
@@ -158,6 +161,7 @@ type PodSpec struct {
158161 ContainerSpec `json:",inline"`
159162}
160163
164+ // Retrieves the initialization image for the pod.
161165func (s * PodSpec ) GetInitImage () string {
162166 return s .InitImage
163167}
@@ -185,6 +189,7 @@ type BackupSpec struct {
185189 Storages map [string ]* BackupStorageSpec `json:"storages,omitempty"`
186190}
187191
192+ // Retrieves the initialization image for the backup.
188193func (s * BackupSpec ) GetInitImage () string {
189194 return s .InitImage
190195}
@@ -230,11 +235,13 @@ type BackupStorageS3Spec struct {
230235// BucketWithPrefix contains a bucket name with or without a prefix in a format <bucket>/<prefix>
231236type BucketWithPrefix string
232237
238+ // Extracts the bucket name from a combined bucket with prefix string.
233239func (b BucketWithPrefix ) Bucket () string {
234240 bucket , _ , _ := strings .Cut (string (b ), "/" )
235241 return bucket
236242}
237243
244+ // Extracts the prefix from a combined bucket with prefix string.
238245func (b BucketWithPrefix ) Prefix () string {
239246 _ , prefix , _ := strings .Cut (string (b ), "/" )
240247 return prefix
@@ -331,6 +338,7 @@ type ServiceExpose struct {
331338 ExternalTrafficPolicy corev1.ServiceExternalTrafficPolicyType `json:"externalTrafficPolicy,omitempty"`
332339}
333340
341+ // Determines if both annotations and labels of the service expose are empty.
334342func (e * ServiceExpose ) SaveOldMeta () bool {
335343 return len (e .Annotations ) == 0 && len (e .Labels ) == 0
336344}
@@ -417,18 +425,22 @@ const (
417425 UserXtraBackup SystemUser = "xtrabackup"
418426)
419427
428+ // MySQLSpec returns the MySQL specification from the PerconaServerMySQL custom resource.
420429func (cr * PerconaServerMySQL ) MySQLSpec () * MySQLSpec {
421430 return & cr .Spec .MySQL
422431}
423432
433+ // PMMSpec returns the PMM specification from the PerconaServerMySQL custom resource.
424434func (cr * PerconaServerMySQL ) PMMSpec () * PMMSpec {
425435 return cr .Spec .PMM
426436}
427437
438+ // OrchestratorSpec returns the Orchestrator specification from the PerconaServerMySQL custom resource.
428439func (cr * PerconaServerMySQL ) OrchestratorSpec () * OrchestratorSpec {
429440 return & cr .Spec .Orchestrator
430441}
431442
443+ // SetVersion sets the CRVersion to the version value if it's not already set.
432444func (cr * PerconaServerMySQL ) SetVersion () {
433445 if len (cr .Spec .CRVersion ) > 0 {
434446 return
@@ -437,6 +449,7 @@ func (cr *PerconaServerMySQL) SetVersion() {
437449 cr .Spec .CRVersion = version .Version
438450}
439451
452+ // CheckNSetDefaults validates and sets default values for the PerconaServerMySQL custom resource.
440453func (cr * PerconaServerMySQL ) CheckNSetDefaults (ctx context.Context , serverVersion * platform.ServerVersion ) error {
441454 log := logf .FromContext (ctx ).WithName ("CheckNSetDefaults" )
442455 if len (cr .Spec .MySQL .ClusterType ) == 0 {
@@ -690,6 +703,7 @@ const (
690703 BinVolumePath = "/opt/percona"
691704)
692705
706+ // reconcileVol validates and sets default values for a given VolumeSpec, ensuring it is properly defined.
693707func reconcileVol (v * VolumeSpec ) (* VolumeSpec , error ) {
694708 if v == nil || v .EmptyDir == nil && v .HostPath == nil && v .PersistentVolumeClaim == nil {
695709 return nil , errors .New ("volumeSpec and it's internals should be specified" )
@@ -708,6 +722,7 @@ func reconcileVol(v *VolumeSpec) (*VolumeSpec, error) {
708722 return v , nil
709723}
710724
725+ // defaultPVCSpec sets default access mode for a PersistentVolumeClaimSpec if not already defined.
711726func defaultPVCSpec (pvc * corev1.PersistentVolumeClaimSpec ) {
712727 if pvc == nil {
713728 return
@@ -754,6 +769,7 @@ func (p *PodSpec) reconcileAffinityOpts() {
754769 }
755770}
756771
772+ // GetAffinity derives an Affinity configuration based on the provided PodSpec's affinity settings and labels.
757773func (p * PodSpec ) GetAffinity (labels map [string ]string ) * corev1.Affinity {
758774 if p .Affinity == nil {
759775 return nil
@@ -801,6 +817,7 @@ const (
801817 ExposedLabel = "percona.com/exposed"
802818)
803819
820+ // Labels returns a standardized set of labels for the PerconaServerMySQL custom resource.
804821func (cr * PerconaServerMySQL ) Labels () map [string ]string {
805822 return map [string ]string {
806823 NameLabel : "percona-server" ,
@@ -810,10 +827,13 @@ func (cr *PerconaServerMySQL) Labels() map[string]string {
810827 }
811828}
812829
830+ // ClusterHint generates a unique identifier for the PerconaServerMySQL
831+ // cluster using its name and namespace.
813832func (cr * PerconaServerMySQL ) ClusterHint () string {
814833 return fmt .Sprintf ("%s.%s" , cr .Name , cr .Namespace )
815834}
816835
836+ // GetClusterNameFromObject retrieves the cluster's name from the given client object's labels.
817837func GetClusterNameFromObject (obj client.Object ) (string , error ) {
818838 labels := obj .GetLabels ()
819839 instance , ok := labels [InstanceLabel ]
@@ -823,6 +843,7 @@ func GetClusterNameFromObject(obj client.Object) (string, error) {
823843 return instance , nil
824844}
825845
846+ // FNVHash computes a hash of the provided byte slice using the FNV-1a algorithm.
826847func FNVHash (p []byte ) string {
827848 hash := fnv .New32 ()
828849 hash .Write (p )
@@ -844,17 +865,20 @@ func (cr *PerconaServerMySQL) ClusterHash() string {
844865 return serverIDHash
845866}
846867
868+ // InternalSecretName generates a name for the internal secret based on the PerconaServerMySQL name.
847869func (cr * PerconaServerMySQL ) InternalSecretName () string {
848870 return "internal-" + cr .Name
849871}
850872
873+ // PMMEnabled checks if PMM is enabled and if the provided secret contains PMM-specific data.
851874func (cr * PerconaServerMySQL ) PMMEnabled (secret * corev1.Secret ) bool {
852875 if cr .Spec .PMM != nil && cr .Spec .PMM .Enabled && secret != nil && secret .Data != nil {
853876 return cr .Spec .PMM .HasSecret (secret )
854877 }
855878 return false
856879}
857880
881+ // HasSecret determines if the provided secret contains the necessary PMM server key.
858882func (pmm * PMMSpec ) HasSecret (secret * corev1.Secret ) bool {
859883 if secret .Data != nil {
860884 v , ok := secret .Data [string (UserPMMServerKey )]
@@ -863,6 +887,7 @@ func (pmm *PMMSpec) HasSecret(secret *corev1.Secret) bool {
863887 return false
864888}
865889
890+ // RouterEnabled checks if the router is enabled, considering the MySQL configuration.
866891func (cr * PerconaServerMySQL ) RouterEnabled () bool {
867892 if cr .MySQLSpec ().IsAsync () {
868893 return false
@@ -871,6 +896,7 @@ func (cr *PerconaServerMySQL) RouterEnabled() bool {
871896 return cr .Spec .Proxy .Router != nil && cr .Spec .Proxy .Router .Enabled
872897}
873898
899+ // HAProxyEnabled verifies if HAProxy is enabled based on MySQL configuration and safety settings.
874900func (cr * PerconaServerMySQL ) HAProxyEnabled () bool {
875901 if cr .MySQLSpec ().IsAsync () && ! cr .Spec .AllowUnsafeConfig {
876902 return true
@@ -879,6 +905,8 @@ func (cr *PerconaServerMySQL) HAProxyEnabled() bool {
879905 return cr .Spec .Proxy .HAProxy != nil && cr .Spec .Proxy .HAProxy .Enabled
880906}
881907
908+ // OrchestratorEnabled determines if the orchestrator is enabled,
909+ // considering the MySQL configuration.
882910func (cr * PerconaServerMySQL ) OrchestratorEnabled () bool {
883911 if cr .MySQLSpec ().IsGR () {
884912 return false
@@ -893,10 +921,12 @@ func (cr *PerconaServerMySQL) OrchestratorEnabled() bool {
893921
894922var NonAlphaNumeric = regexp .MustCompile ("[^a-zA-Z0-9_]+" )
895923
924+ // Generates a cluster name by sanitizing the PerconaServerMySQL name.
896925func (cr * PerconaServerMySQL ) InnoDBClusterName () string {
897926 return NonAlphaNumeric .ReplaceAllString (cr .Name , "" )
898927}
899928
929+ // Registers PerconaServerMySQL types with the SchemeBuilder.
900930func init () {
901931 SchemeBuilder .Register (& PerconaServerMySQL {}, & PerconaServerMySQLList {})
902932}
0 commit comments