- include namespace in secret name only when namespace is provided
- use username.namespace as key to pgUsers only when namespace is
  provided
- avoid conflict in the role creation in db by checking namespace
  alongwith the username
This commit is contained in:
Rafia Sabih 2021-05-20 19:22:00 +02:00
parent af719c07fe
commit 43154baf39
5 changed files with 20 additions and 12 deletions

View File

@ -1122,7 +1122,11 @@ func (c *Cluster) initRobotUsers() error {
AdminRole: adminRole, AdminRole: adminRole,
} }
if currentRole, present := c.pgUsers[username]; present { if currentRole, present := c.pgUsers[username]; present {
if namespace == c.pgUsers[username].Namespace {
c.pgUsers[username] = c.resolveNameConflict(&currentRole, &newRole) c.pgUsers[username] = c.resolveNameConflict(&currentRole, &newRole)
} else {
c.pgUsers[username+"."+namespace] = newRole
}
} else { } else {
c.pgUsers[username] = newRole c.pgUsers[username] = newRole
} }

View File

@ -1581,10 +1581,13 @@ func (c *Cluster) generateSingleUserSecret(namespace string, pgUser spec.PgUser)
if username == constants.ConnectionPoolerUserName { if username == constants.ConnectionPoolerUserName {
lbls = c.connectionPoolerLabels("", false).MatchLabels lbls = c.connectionPoolerLabels("", false).MatchLabels
} }
secret_name := username
if pgUser.Namespace != c.Namespace {
secret_name = username + "." + pgUser.Namespace
}
secret := v1.Secret{ secret := v1.Secret{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: c.credentialSecretName(username), Name: c.credentialSecretName(secret_name),
Namespace: pgUser.Namespace, Namespace: pgUser.Namespace,
Labels: lbls, Labels: lbls,
Annotations: c.annotationsSet(nil), Annotations: c.annotationsSet(nil),

View File

@ -32,7 +32,7 @@ func (c *Cluster) listResources() error {
} }
for _, obj := range c.Secrets { for _, obj := range c.Secrets {
c.logger.Infof("found secret: %q (uid: %q)", util.NameFromMeta(obj.ObjectMeta), obj.UID) c.logger.Infof("found secret: %q (uid: %q) namesapce: %s", util.NameFromMeta(obj.ObjectMeta), obj.UID, obj.ObjectMeta.Namespace)
} }
for role, endpoint := range c.Endpoints { for role, endpoint := range c.Endpoints {

View File

@ -481,12 +481,9 @@ func (c *Cluster) syncSecrets() error {
secrets := c.generateUserSecrets() secrets := c.generateUserSecrets()
for secretUsername, secretSpec := range secrets { for secretUsername, secretSpec := range secrets {
if len(secretSpec.Namespace) < 0 {
c.logger.Warningf("found empty namespace for user %s", secretUsername)
}
if secret, err = c.KubeClient.Secrets(secretSpec.Namespace).Create(context.TODO(), secretSpec, metav1.CreateOptions{}); err == nil { if secret, err = c.KubeClient.Secrets(secretSpec.Namespace).Create(context.TODO(), secretSpec, metav1.CreateOptions{}); err == nil {
c.Secrets[secret.UID] = secret c.Secrets[secret.UID] = secret
c.logger.Debugf("created new secret %s, uid: %s", util.NameFromMeta(secret.ObjectMeta), secret.UID) c.logger.Debugf("created new secret %s, namespace: %s, uid: %s", util.NameFromMeta(secret.ObjectMeta), secretSpec.Namespace, secret.UID)
continue continue
} }
if k8sutil.ResourceAlreadyExists(err) { if k8sutil.ResourceAlreadyExists(err) {
@ -555,8 +552,12 @@ func (c *Cluster) syncRoles() (err error) {
}() }()
for _, u := range c.pgUsers { for _, u := range c.pgUsers {
if u.Namespace != c.Namespace {
userNames = append(userNames, u.Name+"."+"u.Namespace")
} else {
userNames = append(userNames, u.Name) userNames = append(userNames, u.Name)
} }
}
if needMasterConnectionPooler(&c.Spec) || needReplicaConnectionPooler(&c.Spec) { if needMasterConnectionPooler(&c.Spec) || needReplicaConnectionPooler(&c.Spec) {
connectionPoolerUser := c.systemUsers[constants.ConnectionPoolerUserKeyName] connectionPoolerUser := c.systemUsers[constants.ConnectionPoolerUserKeyName]

View File

@ -48,7 +48,7 @@ const (
type PgUser struct { type PgUser struct {
Origin RoleOrigin `yaml:"-"` Origin RoleOrigin `yaml:"-"`
Name string `yaml:"-"` Name string `yaml:"-"`
Namespace string `yaml:"."` Namespace string
Password string `yaml:"-"` Password string `yaml:"-"`
Flags []string `yaml:"user_flags"` Flags []string `yaml:"user_flags"`
MemberOf []string `yaml:"inrole"` MemberOf []string `yaml:"inrole"`