forked from CrunchyData/postgres-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reconcile a pgAdmin StatefulSet, Pod PVC and ConfigMap
Add the reconciliation logic for the main initial elements for pgAdmin. Includes initial configuration options for the StatefulSet and example implementations for the ConfigMap, PVC and Status block
- Loading branch information
Showing
23 changed files
with
3,070 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
527 changes: 527 additions & 0 deletions
527
config/crd/bases/postgres-operator.crunchydata.com_pgadmins.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
apiVersion: postgres-operator.crunchydata.com/v1beta1 | ||
kind: PGAdmin | ||
metadata: | ||
name: pgadmin | ||
spec: {} | ||
name: rhino | ||
spec: | ||
dataVolumeClaimSpec: | ||
accessModes: | ||
- "ReadWriteOnce" | ||
resources: | ||
requests: | ||
storage: 1Gi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2023 Crunchy Data Solutions, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package standalone_pgadmin | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// patch sends patch to object's endpoint in the Kubernetes API and updates | ||
// object with any returned content. The fieldManager is set to r.Owner, but | ||
// can be overridden in options. | ||
// - https://docs.k8s.io/reference/using-api/server-side-apply/#managers | ||
// | ||
// TODO(tjmoore4): This function is duplicated from a version that takes a PostgresCluster object. | ||
func (r *PGAdminReconciler) patch( | ||
ctx context.Context, object client.Object, | ||
patch client.Patch, options ...client.PatchOption, | ||
) error { | ||
options = append([]client.PatchOption{r.Owner}, options...) | ||
return r.Client.Patch(ctx, object, patch, options...) | ||
} | ||
|
||
// apply sends an apply patch to object's endpoint in the Kubernetes API and | ||
// updates object with any returned content. The fieldManager is set to | ||
// r.Owner and the force parameter is true. | ||
// - https://docs.k8s.io/reference/using-api/server-side-apply/#managers | ||
// - https://docs.k8s.io/reference/using-api/server-side-apply/#conflicts | ||
// | ||
// TODO(tjmoore4): This function is duplicated from a version that takes a PostgresCluster object. | ||
func (r *PGAdminReconciler) apply(ctx context.Context, object client.Object) error { | ||
// Generate an apply-patch by comparing the object to its zero value. | ||
zero := reflect.New(reflect.TypeOf(object).Elem()).Interface() | ||
data, err := client.MergeFrom(zero.(client.Object)).Data(object) | ||
apply := client.RawPatch(client.Apply.Type(), data) | ||
|
||
// Send the apply-patch with force=true. | ||
if err == nil { | ||
err = r.patch(ctx, object, apply, client.ForceOwnership) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2023 Crunchy Data Solutions, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package standalone_pgadmin | ||
|
||
const ( | ||
// key for standalone pgAdmin settings | ||
settingsConfigMapKey = "pgadmin-settings.json" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 Crunchy Data Solutions, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package standalone_pgadmin | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/crunchydata/postgres-operator/internal/initialize" | ||
"github.com/crunchydata/postgres-operator/internal/naming" | ||
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||
) | ||
|
||
// +kubebuilder:rbac:groups="",resources="configmaps",verbs={get} | ||
// +kubebuilder:rbac:groups="",resources="configmaps",verbs={create,delete,patch} | ||
|
||
// reconcilePGAdminConfigMap writes the ConfigMap for pgAdmin. | ||
func (r *PGAdminReconciler) reconcilePGAdminConfigMap( | ||
ctx context.Context, pgadmin *v1beta1.PGAdmin, | ||
) (*corev1.ConfigMap, error) { | ||
configmap := configmap(pgadmin) | ||
|
||
err := errors.WithStack(r.setControllerReference(pgadmin, configmap)) | ||
|
||
if err == nil { | ||
err = errors.WithStack(r.apply(ctx, configmap)) | ||
} | ||
|
||
return configmap, err | ||
} | ||
|
||
// configmap returns a v1.ConfigMap for pgAdmin. | ||
func configmap(pgadmin *v1beta1.PGAdmin) *corev1.ConfigMap { | ||
configmap := &corev1.ConfigMap{ObjectMeta: naming.StandalonePGAdmin(pgadmin)} | ||
configmap.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap")) | ||
|
||
configmap.Annotations = pgadmin.Spec.Metadata.GetAnnotationsOrNil() | ||
configmap.Labels = naming.Merge( | ||
pgadmin.Spec.Metadata.GetLabelsOrNil(), | ||
map[string]string{ | ||
naming.LabelStandalonePGAdmin: pgadmin.Name, | ||
naming.LabelRole: naming.RolePGAdmin, | ||
}) | ||
|
||
// TODO(tjmoore4): Populate configuration details. | ||
initialize.StringMap(&configmap.Data) | ||
configmap.Data[settingsConfigMapKey] = "config data" | ||
|
||
return configmap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 Crunchy Data Solutions, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package standalone_pgadmin | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
|
||
"github.com/crunchydata/postgres-operator/internal/testing/cmp" | ||
"github.com/crunchydata/postgres-operator/internal/testing/require" | ||
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||
) | ||
|
||
func TestGeneratePGAdminConfigMap(t *testing.T) { | ||
require.ParallelCapacity(t, 0) | ||
|
||
pgadmin := new(v1beta1.PGAdmin) | ||
pgadmin.Namespace = "some-ns" | ||
pgadmin.Name = "pg1" | ||
|
||
t.Run("Data,ObjectMeta,TypeMeta", func(t *testing.T) { | ||
pgadmin := pgadmin.DeepCopy() | ||
|
||
configmap := configmap(pgadmin) | ||
|
||
assert.Assert(t, cmp.MarshalMatches(configmap.TypeMeta, ` | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
`)) | ||
assert.Assert(t, cmp.MarshalMatches(configmap.ObjectMeta, ` | ||
creationTimestamp: null | ||
labels: | ||
postgres-operator.crunchydata.com/role: pgadmin | ||
postgres-operator.crunchydata.com/standalone-pgadmin: pg1 | ||
name: pg1-standalone-pgadmin | ||
namespace: some-ns | ||
`)) | ||
|
||
assert.Assert(t, len(configmap.Data) > 0, "expected some configuration") | ||
}) | ||
|
||
t.Run("Annotations,Labels", func(t *testing.T) { | ||
pgadmin := pgadmin.DeepCopy() | ||
pgadmin.Spec.Metadata = &v1beta1.Metadata{ | ||
Annotations: map[string]string{"a": "v1", "b": "v2"}, | ||
Labels: map[string]string{"c": "v3", "d": "v4"}, | ||
} | ||
|
||
configmap := configmap(pgadmin) | ||
|
||
// Annotations present in the metadata. | ||
assert.DeepEqual(t, configmap.ObjectMeta.Annotations, map[string]string{ | ||
"a": "v1", "b": "v2", | ||
}) | ||
|
||
// Labels present in the metadata. | ||
assert.DeepEqual(t, configmap.ObjectMeta.Labels, map[string]string{ | ||
"c": "v3", "d": "v4", | ||
"postgres-operator.crunchydata.com/standalone-pgadmin": "pg1", | ||
"postgres-operator.crunchydata.com/role": "pgadmin", | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.