Skip to content
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

PGBouncer provisioning improvements #1068

Merged
merged 3 commits into from
Aug 19, 2024
Merged
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
6 changes: 3 additions & 3 deletions cmd/cloud/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"github.com/pkg/errors"
)

func parseEnvVarInput(rawInput []string, clear bool) (model.EnvVarMap, error) {
if len(rawInput) != 0 && clear {
func parseEnvVarInput(rawInput []string, clearEnv bool) (model.EnvVarMap, error) {
if len(rawInput) != 0 && clearEnv {
return nil, errors.New("both mattermost-env and mattermost-env-clear were set; use one or the other")
}
if clear {
if clearEnv {
// An empty non-nil map is what the API expects for a full env wipe.
return make(model.EnvVarMap), nil
}
Expand Down
10 changes: 5 additions & 5 deletions internal/api/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,7 @@ func TestResizeCluster(t *testing.T) {
errTest := sqlStore.UpdateCluster(cluster1.Cluster)
require.NoError(t, errTest)

max := int64(1)
clusterResp, errTest := client.ResizeCluster(cluster1.ID, &model.PatchClusterSizeRequest{NodeMaxCount: &max})
clusterResp, errTest := client.ResizeCluster(cluster1.ID, &model.PatchClusterSizeRequest{NodeMaxCount: util.IToP(1)})
require.EqualError(t, errTest, "failed with status code 400")
assert.Nil(t, clusterResp)
})
Expand All @@ -1079,9 +1078,10 @@ func TestResizeCluster(t *testing.T) {
errTest := sqlStore.UpdateCluster(cluster1.Cluster)
require.NoError(t, errTest)

min := int64(10)
max := int64(5)
clusterResp, errTest := client.ResizeCluster(cluster1.ID, &model.PatchClusterSizeRequest{NodeMinCount: &min, NodeMaxCount: &max})
clusterResp, errTest := client.ResizeCluster(cluster1.ID, &model.PatchClusterSizeRequest{
NodeMinCount: util.IToP(10),
NodeMaxCount: util.IToP(5),
})
require.EqualError(t, errTest, "failed with status code 400")
assert.Nil(t, clusterResp)
})
Expand Down
11 changes: 4 additions & 7 deletions internal/provisioner/cluster_provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,11 @@ func provisionCluster(
}

// Sync PGBouncer configmap if there is any change
var vpc string
if cluster.Provisioner == model.ProvisionerKops {
vpc = cluster.ProvisionerMetadataKops.VPC
} else if cluster.Provisioner == model.ProvisionerEKS {
vpc = cluster.ProvisionerMetadataEKS.VPC
} else {
return errors.New("cannot get metadata from unknown provisioner")
vpc := cluster.VpcID()
if vpc == "" {
return errors.New("cluster metadata returned an empty VPC ID")
}

ctx, cancel = context.WithTimeout(context.Background(), time.Duration(10)*time.Second)
defer cancel()
err = pgbouncer.UpdatePGBouncerConfigMap(ctx, vpc, store, cluster.PgBouncerConfig, k8sClient, logger)
Expand Down
3 changes: 1 addition & 2 deletions internal/provisioner/eks_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,7 @@ func (provisioner *EKSProvisioner) isMigrationRequired(oldNodeGroup *eksTypes.No
return true
}

if oldNodeGroup.InstanceTypes != nil && len(oldNodeGroup.InstanceTypes) > 0 &&
oldNodeGroup.InstanceTypes[0] != ngChangeRequest.InstanceType {
if len(oldNodeGroup.InstanceTypes) > 0 && oldNodeGroup.InstanceTypes[0] != ngChangeRequest.InstanceType {
return true
}

Expand Down
20 changes: 18 additions & 2 deletions internal/provisioner/external_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"

"github.com/mattermost/mattermost-cloud/internal/provisioner/utility"
"github.com/mattermost/mattermost-cloud/internal/store"
"github.com/mattermost/mattermost-cloud/internal/supervisor"
"github.com/mattermost/mattermost-cloud/internal/tools/aws"
Expand Down Expand Up @@ -105,9 +106,24 @@ func (provisioner *ExternalProvisioner) DeleteNodegroups(cluster *model.Cluster)
return nil
}

// ProvisionCluster is no-op for external clusters.
// ProvisionCluster runs provisioning tasks for external clusters.
func (provisioner *ExternalProvisioner) ProvisionCluster(cluster *model.Cluster) error {
provisioner.logger.WithField("cluster", cluster.ID).Info("Cluster is managed externally; skipping provision...")
logger := provisioner.logger.WithField("cluster", cluster.ID)

logger.Info("Provisioning cluster")

k8sClient, err := provisioner.getKubeClient(cluster)
if err != nil {
return err
}
if cluster.HasAWSInfrastructure() {
logger.Info("Provisioning resources for AWS infrastructure")

err = utility.DeployPgbouncerManifests(k8sClient, logger)
if err != nil {
return errors.Wrap(err, "failed to deploy pgbouncer manifests")
}
}

return nil
}
Expand Down
13 changes: 11 additions & 2 deletions internal/provisioner/pgbouncer/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const pgbouncerConfigmapDataKey = "pgbouncer.ini"

const baseIni = `
[pgbouncer]
listen_addr = *
Expand Down Expand Up @@ -125,10 +127,17 @@ func UpdatePGBouncerConfigMap(ctx context.Context, vpc string, store model.Clust
if err != nil {
return errors.Wrap(err, "failed to get configmap for pgbouncer-configmap")
}
if configMap.Data["pgbouncer.ini"] != ini {
if _, ok := configMap.Data[pgbouncerConfigmapDataKey]; !ok {
logger.Warnf("No configmap key %s found for pgbouncer configmap; setting key with blank value", pgbouncerConfigmapDataKey)
if configMap.Data == nil {
configMap.Data = make(map[string]string)
}
configMap.Data[pgbouncerConfigmapDataKey] = ""
}
if configMap.Data[pgbouncerConfigmapDataKey] != ini {
logger.Debug("Updating pgbouncer.ini with new database configuration")

configMap.Data["pgbouncer.ini"] = ini
configMap.Data[pgbouncerConfigmapDataKey] = ini
_, err = k8sClient.Clientset.CoreV1().ConfigMaps("pgbouncer").Update(ctx, configMap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update configmap pgbouncer-configmap")
Expand Down
9 changes: 5 additions & 4 deletions internal/provisioner/utility/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (p *pgbouncer) CreateOrUpgrade() error {
return errors.Wrap(err, "failed to set up the k8s client")
}

err = deployManifests(k8sClient, p.logger)
err = DeployPgbouncerManifests(k8sClient, p.logger)
if err != nil {
return err
}
Expand Down Expand Up @@ -144,8 +144,9 @@ func (p *pgbouncer) Name() string {
return model.PgbouncerCanonicalName
}

// deployManifests deploy pgbouncer manifests if they don't exist: pgbouncer-configmap and pgbouncer-userlist-secret
func deployManifests(k8sClient *k8s.KubeClient, logger log.FieldLogger) error {
// DeployManifests deploy pgbouncer manifests if they don't exist:
// pgbouncer-configmap and pgbouncer-userlist-secret
func DeployPgbouncerManifests(k8sClient *k8s.KubeClient, logger log.FieldLogger) error {
logger = logger.WithField("pgbouncer-action", "create-manifests")

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(180)*time.Second)
Expand All @@ -168,7 +169,7 @@ func deployManifests(k8sClient *k8s.KubeClient, logger log.FieldLogger) error {
}
err = k8sClient.CreateFromFile(file, "")
if err != nil {
return err
return errors.Wrap(err, "failed to create pgbouncer-configmap")
}
} else if err != nil {
return errors.Wrap(err, "failed to get configmap for pgbouncer-configmap")
Expand Down
2 changes: 1 addition & 1 deletion internal/provisioner/utility/unmanaged.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (u *unmanaged) CreateOrUpgrade() error {

switch u.Name() {
case model.PgbouncerCanonicalName:
err = deployManifests(k8sClient, u.logger)
err = DeployPgbouncerManifests(k8sClient, u.logger)
if err != nil {
return err
}
Expand Down
Loading