Skip to content

Commit

Permalink
Add env vars and handle sync
Browse files Browse the repository at this point in the history
  • Loading branch information
rafia sabih committed Jan 24, 2024
1 parent 5963890 commit 1fa5bd3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
13 changes: 3 additions & 10 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,17 @@ func New(cfg Config, kubeClient k8sutil.KubernetesClient, pgSpec acidv1.Postgres
if !ok {
passwordEncryption = "scram-sha-256"
}

if pgSpec.Spec.Monitoring != nil {
monitor := pgSpec.Spec.Monitoring
sidecar := &acidv1.Sidecar{
Name: "postgres-exporter",
DockerImage: monitor.Image,
}
pgSpec.Spec.Sidecars = append(pgSpec.Spec.Sidecars, *sidecar) //populate the sidecar spec so that the sidecar is automatically created

flg := acidv1.UserFlags{constants.RoleFlagLogin}
if pgSpec.Spec.Users != nil {
pgSpec.Spec.Users["cpo_exporter"] = flg //TODO: do better than hardcoding this user
pgSpec.Spec.Users[monitorUsername] = flg
} else {
users := make(map[string]acidv1.UserFlags)
pgSpec.Spec.Users = users
pgSpec.Spec.Users["cpo_exporter"] = flg //TODO: do better than hardcoding this user
pgSpec.Spec.Users[monitorUsername] = flg
}
}

cluster := &Cluster{
Config: cfg,
Postgresql: pgSpec,
Expand Down
29 changes: 28 additions & 1 deletion pkg/cluster/k8sres.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
connectionPoolerContainer = "connection-pooler"
pgPort = 5432
operatorPort = 8080
monitorPort = 9187
monitorUsername = "cpo_exporter"
)

type pgUser struct {
Expand Down Expand Up @@ -2634,12 +2636,37 @@ func (c *Cluster) getTDESecretName() string {

func (c *Cluster) getMonitoringSecretName() string {
return c.OpConfig.SecretNameTemplate.Format(
"username", strings.Replace("postgres-exporter", "_", "-", -1),
"username", "cpo-exporter",
"cluster", c.clusterName().Name,
"tprkind", acidv1.PostgresCRDResourceKind,
"tprgroup", acidzalando.GroupName)
}

func (c *Cluster) generateMonitoringEnvVars() []v1.EnvVar {
env := []v1.EnvVar{
{
Name: "DATA_SOURCE_URI",
Value: "localhost:5432/postgres?sslmode=disable",
},
{
Name: "DATA_SOURCE_USER",
Value: monitorUsername,
},
{
Name: "DATA_SOURCE_PASS",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: c.getMonitoringSecretName(),
},
Key: "password",
},
},
},
}
return env
}

func (c *Cluster) getPgbackrestRestoreConfigmapName() (jobName string) {
return fmt.Sprintf("%s-pgbackrest-restore", c.Name)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/cluster/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

acidv1 "github.com/cybertec-postgresql/CYBERTEC-pg-operator/tree/v0.7.0-rc5/pkg/apis/cpo.opensource.cybertec.at/v1"
"github.com/cybertec-postgresql/CYBERTEC-pg-operator/tree/v0.7.0-rc5/pkg/util"
"github.com/cybertec-postgresql/CYBERTEC-pg-operator/tree/v0.7.0-rc5/pkg/util/k8sutil"
"github.com/cybertec-postgresql/CYBERTEC-pg-operator/tree/v0.7.0-rc5/pkg/util/retryutil"
Expand Down Expand Up @@ -79,6 +80,21 @@ func (c *Cluster) createStatefulSet() (*appsv1.StatefulSet, error) {
return nil, fmt.Errorf("sidecar containers specified but disabled in configuration")
}

if c.Spec.Monitoring != nil {
monitor := c.Spec.Monitoring
sidecar := &acidv1.Sidecar{
Name: "postgres-exporter",
DockerImage: monitor.Image,
Ports: []v1.ContainerPort{
{
ContainerPort: monitorPort,
Protocol: v1.ProtocolTCP,
},
},
Env: c.generateMonitoringEnvVars(),
}
c.Spec.Sidecars = append(c.Spec.Sidecars, *sidecar) //populate the sidecar spec so that the sidecar is automatically created
}
statefulSetSpec, err := c.generateStatefulSet(&c.Spec)
if err != nil {
return nil, fmt.Errorf("could not generate statefulset: %v", err)
Expand Down
31 changes: 29 additions & 2 deletions pkg/cluster/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,21 @@ func (c *Cluster) syncStatefulSet() error {
if err != nil {
c.logger.Warnf("could not list pods of the statefulset: %v", err)
}

if c.Spec.Monitoring != nil {
monitor := c.Spec.Monitoring
sidecar := &acidv1.Sidecar{
Name: "postgres-exporter",
DockerImage: monitor.Image,
Ports: []v1.ContainerPort{
{
ContainerPort: monitorPort,
Protocol: v1.ProtocolTCP,
},
},
Env: c.generateMonitoringEnvVars(),
}
c.Spec.Sidecars = append(c.Spec.Sidecars, *sidecar) //populate the sidecar spec so that the sidecar is automatically created
}
// NB: Be careful to consider the codepath that acts on podsRollingUpdateRequired before returning early.
sset, err := c.KubeClient.StatefulSets(c.Namespace).Get(context.TODO(), c.statefulSetName(), metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -997,6 +1011,17 @@ func (c *Cluster) syncRoles() (err error) {
deletedUsers := map[string]string{}
newUsers = make(map[string]spec.PgUser)

if c.Spec.Monitoring != nil {
flg := acidv1.UserFlags{constants.RoleFlagLogin}
if c.Spec.Users != nil {
c.Spec.Users[monitorUsername] = flg
} else {
users := make(map[string]acidv1.UserFlags)
c.Spec.Users = users
c.Spec.Users[monitorUsername] = flg
}
}

// create list of database roles to query
for _, u := range c.pgUsers {
pgRole := u.Name
Expand Down Expand Up @@ -1438,7 +1463,9 @@ func (c *Cluster) createMonitoringSecret() error {
c.Secrets[secret.UID] = secret
c.logger.Debugf("created new secret %s, namespace: %s, uid: %s", util.NameFromMeta(secret.ObjectMeta), generatedSecret.Namespace, secret.UID)
} else {
return fmt.Errorf("could not create secret for TDE %s: in namespace %s: %v", util.NameFromMeta(secret.ObjectMeta), generatedSecret.Namespace, err)
if !k8sutil.ResourceAlreadyExists(err) {
return fmt.Errorf("could not create secret for Monitoring %s: in namespace %s: %v", util.NameFromMeta(secret.ObjectMeta), generatedSecret.Namespace, err)
}
}

return nil
Expand Down

0 comments on commit 1fa5bd3

Please sign in to comment.