Skip to content

Commit

Permalink
Change PostgresIdentifier to a type alias
Browse files Browse the repository at this point in the history
The type existed to avoid schema repetition with controller-gen, but
recent versions can do that using aliases. This simplifies a few tests
by eliminating conversions.
  • Loading branch information
cbandy committed Jan 3, 2025
1 parent 4d73723 commit c580e20
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
5 changes: 2 additions & 3 deletions internal/controller/postgrescluster/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,9 @@ func (r *Reconciler) reconcilePostgresUserSecrets(
r.Recorder.Event(cluster, corev1.EventTypeWarning, "InvalidUser",
allErrors.ToAggregate().Error())
} else {
identifier := v1beta1.PostgresIdentifier(cluster.Name)
specUsers = []v1beta1.PostgresUserSpec{{
Name: identifier,
Databases: []v1beta1.PostgresIdentifier{identifier},
Name: cluster.Name,
Databases: []string{cluster.Name},
}}
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/postgrescluster/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestGeneratePostgresUserSecret(t *testing.T) {
}

// Present when specified.
spec.Databases = []v1beta1.PostgresIdentifier{"db1"}
spec.Databases = []string{"db1"}

secret, err = reconciler.generatePostgresUserSecret(cluster, &spec, nil)
assert.NilError(t, err)
Expand All @@ -180,7 +180,7 @@ func TestGeneratePostgresUserSecret(t *testing.T) {
}

// Only the first in the list.
spec.Databases = []v1beta1.PostgresIdentifier{"first", "asdf"}
spec.Databases = []string{"first", "asdf"}

secret, err = reconciler.generatePostgresUserSecret(cluster, &spec, nil)
assert.NilError(t, err)
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestGeneratePostgresUserSecret(t *testing.T) {

// Includes a URI when possible.
spec := *spec
spec.Databases = []v1beta1.PostgresIdentifier{"yes", "no"}
spec.Databases = []string{"yes", "no"}

secret, err = reconciler.generatePostgresUserSecret(cluster, &spec, nil)
assert.NilError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/pgadmin/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ with create_app().app_context():
[]v1beta1.PostgresUserSpec{
{
Name: "user-no-options",
Databases: []v1beta1.PostgresIdentifier{"db1"},
Databases: []string{"db1"},
},
{
Name: "user-no-databases",
Expand Down
10 changes: 5 additions & 5 deletions internal/postgres/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ COMMIT;`))
[]v1beta1.PostgresUserSpec{
{
Name: "user-no-options",
Databases: []v1beta1.PostgresIdentifier{"db1"},
Databases: []string{"db1"},
},
{
Name: "user-no-databases",
Expand Down Expand Up @@ -175,7 +175,7 @@ COMMIT;`))
[]v1beta1.PostgresUserSpec{
{
Name: "postgres",
Databases: []v1beta1.PostgresIdentifier{"all", "ignored"},
Databases: []string{"all", "ignored"},
Options: "NOLOGIN CONNECTION LIMIT 0",
},
},
Expand Down Expand Up @@ -213,18 +213,18 @@ func TestWriteUsersSchemasInPostgreSQL(t *testing.T) {
[]v1beta1.PostgresUserSpec{
{
Name: "user-single-db",
Databases: []v1beta1.PostgresIdentifier{"db1"},
Databases: []string{"db1"},
},
{
Name: "user-no-databases",
},
{
Name: "user-multi-dbs",
Databases: []v1beta1.PostgresIdentifier{"db1", "db2"},
Databases: []string{"db1", "db2"},
},
{
Name: "public",
Databases: []v1beta1.PostgresIdentifier{"db3"},
Databases: []string{"db3"},
},
},
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package v1beta1

// PostgreSQL identifiers are limited in length but may contain any character.
// More info: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
//
// ---
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=63
type PostgresIdentifier string
type PostgresIdentifier = string

type PostgresPasswordSpec struct {
// Type of password to generate. Defaults to ASCII. Valid options are ASCII
Expand All @@ -23,6 +23,7 @@ type PostgresPasswordSpec struct {
//
// +kubebuilder:default=ASCII
// +kubebuilder:validation:Enum={ASCII,AlphaNumeric}
// +required
Type string `json:"type"`
}

Expand All @@ -33,27 +34,29 @@ const (
)

type PostgresUserSpec struct {

// The name of this PostgreSQL user. The value may contain only lowercase
// letters, numbers, and hyphen so that it fits into Kubernetes metadata.
// ---
// This value goes into the name of a corev1.Secret and a label value, so
// it must match both IsDNS1123Subdomain and IsValidLabelValue. The pattern
// below is IsDNS1123Subdomain without any dots, U+002E.

// The name of this PostgreSQL user. The value may contain only lowercase
// letters, numbers, and hyphen so that it fits into Kubernetes metadata.
//
// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$`
// +kubebuilder:validation:Type=string
// +required
Name PostgresIdentifier `json:"name"`

// Databases to which this user can connect and create objects. Removing a
// database from this list does NOT revoke access. This field is ignored for
// the "postgres" user.
// ---
// +listType=set
// +optional
Databases []PostgresIdentifier `json:"databases,omitempty"`

// ALTER ROLE options except for PASSWORD. This field is ignored for the
// "postgres" user.
// More info: https://www.postgresql.org/docs/current/role-attributes.html
// ---
// +kubebuilder:validation:MaxLength=200
// +kubebuilder:validation:Pattern=`^[^;]*$`
// +kubebuilder:validation:XValidation:rule=`!self.matches("(?i:PASSWORD)")`,message="cannot assign password"
Expand All @@ -62,6 +65,7 @@ type PostgresUserSpec struct {
Options string `json:"options,omitempty"`

// Properties of the password generated for this user.
// ---
// +optional
Password *PostgresPasswordSpec `json:"password,omitempty"`
}

0 comments on commit c580e20

Please sign in to comment.