operator

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2023 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PGHAConfigInitSetting determines whether or not initialization logic should be run in the
	// crunchy-postgres-ha (or GIS equivilaent) container
	PGHAConfigInitSetting = "init"
	// PGHAConfigReplicaBootstrapRepoType defines an override for the type of repo (local, S3, etc.)
	// that should be utilized when bootstrapping a replica (i.e. it override the
	// PGBACKREST_REPO1_TYPE env var in the environment).  Allows for dynamic changing of the
	// backrest repo type without requiring container restarts (as would be required to update
	// PGBACKREST_REPO1_TYPE).
	PGHAConfigReplicaBootstrapRepoType = "replica-bootstrap-repo-type"
)

the following constants define the settings in the PGHA configMap that is created for each PG cluster

View Source
const PGHAConfigMapSuffix = "pgha-config"

PGHAConfigMapSuffix defines the suffix for the name of the PGHA configMap created for each PG cluster

Variables

View Source
var (
	CRUNCHY_DEBUG bool
	NAMESPACE     string
)
View Source
var (
	InstallationName string
	PgoNamespace     string
	EventTCPAddress  = "localhost:4150"
)
View Source
var ContainerImageOverrides = map[string]string{}

ContainerImageOverrides contains a list of container images that are overridden by the RELATED_IMAGE_* environmental variables that can be set by people deploying the Operator

Functions

func AddBackRestConfigVolumeAndMounts

func AddBackRestConfigVolumeAndMounts(podSpec *v1.PodSpec, clusterName string, projections []v1.VolumeProjection)

AddBackRestConfigVolumeAndMounts modifies podSpec to include pgBackRest configuration. Any projections are included as custom pgBackRest configuration.

func AddWALVolumeAndMountsToBackRest

func AddWALVolumeAndMountsToBackRest(podSpec *core_v1.PodSpec, walVolume StorageResult)

AddWALVolumeAndMountsToBackRest modifies a pgBackRest podSpec to include walVolume.

func AddWALVolumeAndMountsToPostgreSQL

func AddWALVolumeAndMountsToPostgreSQL(podSpec *core_v1.PodSpec, walVolume StorageResult, instanceName string)

AddWALVolumeAndMountsToPostgreSQL modifies a PostgreSQL podSpec to include walVolume.

func CreatePGHAConfigMap

func CreatePGHAConfigMap(clientset kubernetes.Interface, cluster *crv1.Pgcluster,
	namespace string) error

CreatePGHAConfigMap creates a configMap that will be utilized to store configuration settings for a PostgreSQL cluster. Currently this configMap simply defines an "init" setting, which is utilized by the crunchy-postgres-ha container (or GIS equivalent) to determine whether or not initialization logic should be executed when the container is run. This ensures that the original primary in a PostgreSQL cluster does not attempt to run any initialization logic more than once, such as following a restart of the container. In the future this configMap can also be leveraged to manage other configuration settings for the PostgreSQL cluster and its associated containers.

func Failover

func Failover(clientset kubernetes.Interface, restConfig *rest.Config, cluster *crv1.Pgcluster, target string) error

Failover performs a failover to a PostgreSQL cluster, which is effectively a "forced switchover." In other words, failover will force ensure that there is a primary available.

NOTE: This is reserve as the "last resort" case. If you want a controlled failover, you want "Switchover".

A target must be specified. The target should contain the name of the target instances (Deployment), is not empty then we will attempt to locate that target Pod.

The target Pod name, called the candidate is passed into the failover command generation function, and then is ultimately used in the failover.

func GetAnnotations

func GetAnnotations(cluster *crv1.Pgcluster, annotationType crv1.ClusterAnnotationType) string

GetAnnotations returns the annotations in a JSON format can be used by the template. If no annotations are found, returns an empty string

func GetBackrestDeployment

func GetBackrestDeployment(clientset kubernetes.Interface, cluster *crv1.Pgcluster) (*apps_v1.Deployment, error)

GetBackrestDeployment finds the pgBackRest repository Deployments for a PostgreQL cluster

func GetBadgerAddon

func GetBadgerAddon(cluster *crv1.Pgcluster, target string) string

GetBadgerAddon is a legacy method that generates a JSONish string to be used to add a pgBadger sidecar to a PostgreSQL instance

func GetBootstrapNamespace added in v0.2.0

func GetBootstrapNamespace(cluster *crv1.Pgcluster) string

GetBootstrapNamespace returns the proper namespace to use when looking up and/or creating any resources required to bootstrap a PostgreSQL cluster. This includes either using the namespace specified by the user in the PGDataSource, or defaulting to the same namespace as the pgcluster.

func GetConfVolume

func GetConfVolume(clientset kubernetes.Interface, cl *crv1.Pgcluster, namespace string) string

consolidate with cluster.GetConfVolume

func GetExporterAddon

func GetExporterAddon(spec crv1.PgclusterSpec) string

GetExporterAddon returns the template used to create an exporter container for metrics. This is semi-legacy, but updated to match the current way of handling this

func GetInstanceDeployments

func GetInstanceDeployments(clientset kubernetes.Interface, cluster *crv1.Pgcluster) (*apps_v1.DeploymentList, error)

GetInstanceDeployments finds the Deployments that represent PostgreSQL instances

func GetLabelsFromMap

func GetLabelsFromMap(labels map[string]string, trimComma bool) string

needs to be consolidated with cluster.GetLabelsFromMap GetLabelsFromMap ...

func GetNodeAffinity

func GetNodeAffinity(nodeAffinity *v1.NodeAffinity) string

GetNodeAffinity returns any node affinity rules for the Operator in a JSON string. If there is no data or there is an error, it will return an empty string.

func GetPGBackRestRepoPath

func GetPGBackRestRepoPath(cluster *crv1.Pgcluster) string

GetPGBackRestRepoPath is responsible for determining the repo path setting (i.e. 'repo1-path' flag) for use by pgBackRest. If a specific repo path has been defined in the pgcluster CR, then that path will be returned. Otherwise a default path will be returned that is generated from the cluster name

func GetPasswordType added in v0.2.0

func GetPasswordType(cluster *crv1.Pgcluster) string

GetPasswordType returns the specific password type to use as part of Postgres user management. If it's not "scram-sha-256" or "md5", it will just return "" and use the default set in Postgres

func GetPgbackrestBootstrapEnvVars

func GetPgbackrestBootstrapEnvVars(cluster *crv1.Pgcluster,
	restoreFromSecret *v1.Secret) (string, error)

GetPgbackrestBootstrapEnvVars returns a string containing the pgBackRest environment variables for a bootstrap job

func GetPgbackrestBootstrapGCSEnvVars added in v0.2.0

func GetPgbackrestBootstrapGCSEnvVars(pgDataSourceRestoreFrom string,
	restoreFromSecret *v1.Secret) string

GetPgbackrestBootstrapGCSEnvVars retrieves the values for the various configuration settings required to configure pgBackRest for GCS, specifically for a bootstrap job.

func GetPgbackrestBootstrapS3EnvVars

func GetPgbackrestBootstrapS3EnvVars(pgDataSourceRestoreFrom string,
	restoreFromSecret *v1.Secret) string

GetPgbackrestBootstrapS3EnvVars retrieves the values for the various configuration settings required to configure pgBackRest for AWS S3, specifically for a bootstrap job (includes a bucket, endpoint, region, key and key secret. The bucket, endpoint & region are obtained from annotations in the pgbackrest secret from the cluster being restored from during the bootstrap job, while the key and key secret are then obtained from the data in this same secret. Once these values have been obtained, they are used to populate a template containing the various pgBackRest environment variables required to enable S3 support for the boostrap job. After the template has been executed with the proper values, the result is then returned a string for inclusion in the PG and pgBackRest deployments.

func GetPgbackrestEnvVars

func GetPgbackrestEnvVars(cluster *crv1.Pgcluster, depName, port string) string

consolidate with cluster.GetPgbackrestEnvVars

func GetPgbackrestGCSEnvVars added in v0.2.0

func GetPgbackrestGCSEnvVars(clientset kubernetes.Interface, cluster crv1.Pgcluster) string

GetPgbackrestGCSEnvVars retrieves the values for the various configuration settings required to configure pgBackRest for GCS.

func GetPgbackrestS3EnvVars

func GetPgbackrestS3EnvVars(clientset kubernetes.Interface, cluster crv1.Pgcluster) string

GetPgbackrestS3EnvVars retrieves the values for the various configuration settings require to configure pgBackRest for AWS S3, including a bucket, endpoint, region, key and key secret. The bucket, endpoint & region are obtained from the associated parameters in the pgcluster CR, while the key and key secret are obtained from the backrest repository secret. Once these values have been obtained, they are used to populate a template containing the various pgBackRest environment variables required to enable S3 support. After the template has been executed with the proper values, the result is then returned a string for inclusion in the PG and pgBackRest deployments.

func GetPgmonitorEnvVars

func GetPgmonitorEnvVars(cluster *crv1.Pgcluster) string

GetPgmonitorEnvVars populates the pgmonitor env var template, which contains any pgmonitor env vars that need to be included in the Deployment spec for a PG cluster.

func GetPodAntiAffinity

func GetPodAntiAffinity(cluster *crv1.Pgcluster, deploymentType crv1.PodAntiAffinityDeployment, podAntiAffinityType crv1.PodAntiAffinityType) string

GetPodAntiAffinity returns the populated pod anti-affinity json that should be attached to the various pods comprising the pg cluster

func GetPodAntiAffinityType

func GetPodAntiAffinityType(cluster *crv1.Pgcluster, deploymentType crv1.PodAntiAffinityDeployment, podAntiAffinityType crv1.PodAntiAffinityType) crv1.PodAntiAffinityType

GetPodAntiAffinityType returns the type of pod anti-affinity to use. This is based on the deployment type (cluster, pgBackRest, pgBouncer), the value in the cluster spec, and the defaults available in pgo.yaml.

In other words, the pod anti-affinity is determined by this heuristic, in priority order:

  1. If it's pgBackRest/pgBouncer the value set by the user (available in the cluster spec)
  2. If it's pgBackRest/pgBouncer the value set in pgo.yaml
  3. The value set in "Default" in the cluster spec
  4. The value set for PodAntiAffinity in pgo.yaml

func GetPodSecurityContext

func GetPodSecurityContext(supplementalGroups []int64) string

GetPodSecurityContext will generate the security context required for a Deployment by incorporating the standard fsGroup for the user that runs the container (typically the "postgres" user), and adds any supplemental groups that may need to be added, e.g. for NFS storage.

Following the legacy method, this returns a JSON string, which will be modified in the future. Mainly this is transitioning from the legacy function by adding the expected types

func GetRepoType

func GetRepoType(cluster *crv1.Pgcluster) crv1.BackrestStorageType

GetRepoType returns the proper repo type to set in container based on the backrest storage type provided

If there are multiple types, the default returned is "posix". This could change once there is proper multi-repo support, but with proper multi-repo support, this function is likely annhilated.

If there is nothing, the default returned is posix

func GetResourcesJSON

func GetResourcesJSON(resources, limits v1.ResourceList) string

GetResourcesJSON is a pseudo-legacy method that creates JSON that applies the CPU and Memory settings. The settings are only included if: a) they exist b) they are nonzero

func GetS3VerifyTLSSetting

func GetS3VerifyTLSSetting(cluster *crv1.Pgcluster) string

GetS3VerifyTLSSetting parses the configured value as a boolean to ensure a valid option is used, then returns the pgBackRest S3 configuration value to either enable or disable TLS verification as the expected string value.

func GetSyncReplication

func GetSyncReplication(specSyncReplication *bool) bool

GetSyncReplication returns true if synchronous replication has been enabled using either the pgcluster CR specification or the pgo.yaml configuration file. Otherwise, if synchronous mode has not been enabled, it returns false.

func GetTablespaceNamePVCMap

func GetTablespaceNamePVCMap(clusterName string, tablespaceStorageTypeMap map[string]string) map[string]string

GetTablespaceNamePVCMap returns a map of the tablespace name to the PVC name

func GetTablespaceNames

func GetTablespaceNames(tablespaceMounts map[string]crv1.PgStorageSpec) string

GetTablespaceNames generates a comma-separated list of the format "tablespaceName1,tablespceName2" so that the PVC containing a tablespace can be properly mounted in the container, and the tablespace can be referenced by the specified human readable name. We use a comma-separated list to make it "easier" to work with the shell scripts that currently setup the container

func GetTablespacePVCName

func GetTablespacePVCName(clusterName string, tablespaceName string) string

GetTablespacePVCName returns the formatted name that is used for a PVC for a tablespace

func GetTablespaceStorageTypeMap

func GetTablespaceStorageTypeMap(tablespaceMounts map[string]crv1.PgStorageSpec) map[string]string

GetTablespaceStorageTypeMap returns a map of "tablespaceName => storageType"

func GetTablespaceVolumeMountsJSON

func GetTablespaceVolumeMountsJSON(tablespaceStorageTypeMap map[string]string) string

GetTablespaceVolumeMountsJSON Creates an appendable list for the volumeMounts that are used to mount table spacs and returns them in a JSON-ish string

func GetTablespaceVolumeName

func GetTablespaceVolumeName(tablespaceName string) string

GetTableSpaceVolumeName returns the name that is used to identify the volume that is used to mount the tablespace

func GetTablespaceVolumesJSON

func GetTablespaceVolumesJSON(clusterName string, tablespaceStorageTypeMap map[string]string) string

GetTablespaceVolumes Creates an appendable list for the volumes section of a Kubernetes pod

func Initialize

func Initialize(clientset kubernetes.Interface)

func IsLocalAndGCSStorage added in v0.2.0

func IsLocalAndGCSStorage(cluster *crv1.Pgcluster) bool

IsLocalAndGCSStorage a boolean indicating whether or not local and gcs storage should be enabled for pgBackRest based on the backrestStorageType string provided

func IsLocalAndS3Storage

func IsLocalAndS3Storage(cluster *crv1.Pgcluster) bool

IsLocalAndS3Storage a boolean indicating whether or not local and s3 storage should be enabled for pgBackRest based on the backrestStorageType string provided

func NamespaceOperatingMode

func NamespaceOperatingMode() ns.NamespaceOperatingMode

NamespaceOperatingMode returns the namespace operating mode for the current Operator installation, which is stored in the "namespaceOperatingMode" variable

func OverrideClusterContainerImages

func OverrideClusterContainerImages(containers []v1.Container)

OverrideClusterContainerImages is a helper function that provides the appropriate hooks to override any of the container images that might be deployed with a PostgreSQL cluster

func RemovePrimaryOnRoleChangeTag

func RemovePrimaryOnRoleChangeTag(clientset kubernetes.Interface, restconfig *rest.Config,
	clusterName, namespace string) error

RemovePrimaryOnRoleChangeTag sets the 'primary_on_role_change' tag to null in the Patroni DCS, effectively removing the tag. This is accomplished by exec'ing into the primary PG pod, and sending a patch request to update the appropriate data (i.e. the 'primary_on_role_change' tag) in the DCS.

func ScaleDeployment added in v0.2.0

func ScaleDeployment(clientset kubeapi.Interface,
	deployment *appsv1.Deployment, replicas *int32) error

ScaleDeployment scales a deployment to a specified number of replicas. It will also wait to ensure that the Deployment is actually scaled down.

func SetContainerImageOverride

func SetContainerImageOverride(containerImageName string, container *v1.Container)

SetContainerImageOverride determines if there is an override available for a container image, and sets said value on the Kubernetes Container image definition

func SetupNamespaces

func SetupNamespaces(clientset kubernetes.Interface) ([]string, error)

SetupNamespaces is responsible for the initial namespace configuration for the Operator install. This includes setting the proper namespace operating mode, creating and/or updating namespaces as needed (or as permitted by the current operator mode), and returning a valid list of namespaces for the current Operator install.

func Switchover

func Switchover(clientset kubernetes.Interface, restConfig *rest.Config, cluster *crv1.Pgcluster, target string) error

Switchover performs a controlled switchover within a PostgreSQL cluster, i.e. demoting a primary and promoting a replica. There are two types of switchover methods that can be invoked.

Method #1: Automatic Choice

The switchover command invokves Patroni which works as such:

1. The function looks for all available replicas as well as the current primary. We look up the primary for convenience to avoid various API calls

2. We then search over the list to find both a primary and a suitable candidate for promotion. A candidate is suitable if:

  • It is on the latest timeline
  • It has the least amount of replication lag

This is done to limit the risk of data loss.

If either a primary or candidate is **not** found, we do not switch over.

3. If all of the above works successfully, a switchover is attempted.

Method #2: Targeted Choice

1. If the "target" parameter, which should contain the name of the target instances (Deployment), is not empty then we will attempt to locate that target Pod.

2. The target Pod name, called the candidate is passed into the switchover command generation function, and then is ultimately used in the switchover.

func UpdatePGHAConfigInitFlag

func UpdatePGHAConfigInitFlag(clientset kubernetes.Interface, initVal bool, clusterName,
	namespace string) error

UpdatePGHAConfigInitFlag sets the value for the "init" setting in the PGHA configMap for the PG cluster to the value specified via the "initVal" parameter. For instance, following the initialization of a PG cluster this function will be utilized to set the "init" value to false to ensure the primary does not attempt to run initialization logic in the event that it is restarted.

Types

type BootstrapJobTemplateFields

type BootstrapJobTemplateFields struct {
	DeploymentTemplateFields
	// RestoreFrom defines the name of a cluster to restore from when bootstrapping from an
	// existing data source
	RestoreFrom string
	// RestoreOpts defines the command line options that should be passed to the restore utility
	// (e.g. pgBackRest) when bootstrapping the cluster from an existing data source
	RestoreOpts string
}

BootstrapJobTemplateFields defines the fields needed to populate the cluster bootstrap job template

type DeploymentTemplateFields

type DeploymentTemplateFields struct {
	Name             string
	ClusterName      string
	Port             string
	Image            string
	Database         string
	DeploymentLabels string
	// PodAnnotations are user-specified annotations that can be applied to a
	// Pod, e.g. annotations specific to a PostgreSQL instance
	PodAnnotations            string
	PodLabels                 string
	DataPathOverride          string
	PVCName                   string
	RootSecretName            string
	UserSecretName            string
	PrimarySecretName         string
	SecurityContext           string
	ContainerResources        string
	NodeSelector              string
	ConfVolume                string
	ExporterAddon             string
	BadgerAddon               string
	PgbackrestEnvVars         string
	PgbackrestS3EnvVars       string
	PgbackrestGCSEnvVars      string
	PgmonitorEnvVars          string
	ScopeLabel                string
	Replicas                  string
	IsInit                    bool
	ReplicaReinitOnStartFail  bool
	PasswordType              string
	PodAntiAffinity           string
	PodAntiAffinityLabelName  string
	PodAntiAffinityLabelValue string
	SyncReplication           bool
	Standby                   bool
	// A comma-separated list of tablespace names...this could be an array, but
	// given how this would ultimately be interpreted in a shell script somewhere
	// down the line, it's easier for the time being to do it this way. In the
	// future, we should consider having an array
	Tablespaces            string
	TablespaceVolumes      string
	TablespaceVolumeMounts string
	// Tolerations is an optional parameter that provides Pod tolerations that
	// have been transformed into JSON encoding from an actual Tolerations object
	Tolerations string
	// The following fields set the TLS requirements as well as provide
	// information on how to configure TLS in a PostgreSQL cluster
	// TLSEnabled enables TLS in a cluster if set to true. Only works in actuality
	// if CASecret and TLSSecret are set
	TLSEnabled bool
	// TLSOnly is set to true if the PostgreSQL cluster should only accept TLS
	// connections
	TLSOnly bool
	// TLSSecret is the name of the Secret that has the PostgreSQL server's TLS
	// keypair
	TLSSecret string
	// ReplicationTLSSecret is the name of the Secret that has the TLS keypair
	// for performing certificate-based authentication between instances
	ReplicationTLSSecret string
	// CASecret is the name of the Secret that has the trusted CA that the
	// PostgreSQL server is using
	CASecret string
	PGParams string
}

DeploymentTemplateFields ...

type PgbackrestEnvVarsTemplateFields

type PgbackrestEnvVarsTemplateFields struct {
	PgbackrestStanza             string
	PgbackrestDBPath             string
	PgbackrestRepo1Path          string
	PgbackrestRepo1Host          string
	PgbackrestRepo1Type          crv1.BackrestStorageType
	PgbackrestLocalAndGCSStorage bool
	PgbackrestLocalAndS3Storage  bool
	PgbackrestPGPort             string
}

type PgbackrestGCSEnvVarsTemplateFields added in v0.2.0

type PgbackrestGCSEnvVarsTemplateFields struct {
	PgbackrestGCSBucket   string
	PgbackrestGCSEndpoint string
	PgbackrestGCSKeyType  string
}

type PgbackrestS3EnvVarsTemplateFields

type PgbackrestS3EnvVarsTemplateFields struct {
	PgbackrestS3Bucket     string
	PgbackrestS3Endpoint   string
	PgbackrestS3Region     string
	PgbackrestS3Key        string
	PgbackrestS3KeySecret  string
	PgbackrestS3SecretName string
	PgbackrestS3URIStyle   string
	PgbackrestS3VerifyTLS  string
}

type PgmonitorEnvVarsTemplateFields

type PgmonitorEnvVarsTemplateFields struct {
	ExporterSecret string
}

type StorageResult

type StorageResult struct {
	PersistentVolumeClaimName string
	SupplementalGroups        []int64
}

StorageResult is a resolved PgStorageSpec. The zero value is an emptyDir.

func (StorageResult) InlineVolumeSource

func (s StorageResult) InlineVolumeSource() string

InlineVolumeSource returns the key and value of a k8s.io/api/core/v1.VolumeSource.

func (StorageResult) VolumeSource

func (s StorageResult) VolumeSource() v1.VolumeSource

VolumeSource returns the VolumeSource equivalent of s.

Directories

Path Synopsis
Package cluster holds the cluster CRD logic and definitions A cluster is comprised of a primary service, replica service, primary deployment, and replica deployment
Package cluster holds the cluster CRD logic and definitions A cluster is comprised of a primary service, replica service, primary deployment, and replica deployment

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL