-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ClusterIP service through spec
Allow users to create a ClusterIP service by setting the ServiceName field in the spec
- Loading branch information
Showing
15 changed files
with
306 additions
and
24 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2023 - 2024 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" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/crunchydata/postgres-operator/internal/logging" | ||
"github.com/crunchydata/postgres-operator/internal/naming" | ||
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||
) | ||
|
||
// +kubebuilder:rbac:groups="",resources="services",verbs={get} | ||
// +kubebuilder:rbac:groups="",resources="services",verbs={create,delete,patch} | ||
|
||
// reconcilePGAdminService will reconcile a ClusterIP service that points to | ||
// pgAdmin. | ||
func (r *PGAdminReconciler) reconcilePGAdminService( | ||
ctx context.Context, | ||
pgadmin *v1beta1.PGAdmin, | ||
) error { | ||
log := logging.FromContext(ctx) | ||
|
||
// Since spec.Service only accepts a single service name, we shouldn't ever | ||
// have more than one service. However, if the user changes ServiceName, we | ||
// need to delete any existing service(s). At the start of every reconcile | ||
// get all services that match the current pgAdmin labels. | ||
services := corev1.ServiceList{} | ||
if err := r.Client.List(ctx, &services, client.MatchingLabels{ | ||
naming.LabelStandalonePGAdmin: pgadmin.Name, | ||
naming.LabelRole: naming.RolePGAdmin, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
// Delete any controlled and labeled service that is not defined in the spec. | ||
for i := range services.Items { | ||
if services.Items[i].Name != pgadmin.Spec.ServiceName { | ||
log.V(1).Info( | ||
"Deleting service(s) not defined in spec.ServiceName that are owned by pgAdmin", | ||
"serviceName", services.Items[i].Name) | ||
if err := r.deleteControlled(ctx, pgadmin, &services.Items[i]); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
// TODO (jmckulk): check if the requested services exists without our pgAdmin | ||
// as the owner. If this happens, don't take over ownership of the existing svc. | ||
|
||
// At this point only a service defined by spec.ServiceName should exist. | ||
// Update the service or create it if it does not exist | ||
if pgadmin.Spec.ServiceName != "" { | ||
service := service(pgadmin) | ||
if err := errors.WithStack(r.setControllerReference(pgadmin, service)); err != nil { | ||
return err | ||
} | ||
return errors.WithStack(r.apply(ctx, service)) | ||
} | ||
|
||
// If we get here then ServiceName was not provided through the spec | ||
return nil | ||
} | ||
|
||
// Generate a corev1.Service for pgAdmin | ||
func service(pgadmin *v1beta1.PGAdmin) *corev1.Service { | ||
|
||
service := &corev1.Service{} | ||
service.ObjectMeta = metav1.ObjectMeta{ | ||
Name: pgadmin.Spec.ServiceName, | ||
Namespace: pgadmin.Namespace, | ||
} | ||
service.SetGroupVersionKind( | ||
corev1.SchemeGroupVersion.WithKind("Service")) | ||
|
||
service.Annotations = pgadmin.Spec.Metadata.GetAnnotationsOrNil() | ||
service.Labels = naming.Merge( | ||
pgadmin.Spec.Metadata.GetLabelsOrNil(), | ||
naming.StandalonePGAdminLabels(pgadmin.Name)) | ||
|
||
service.Spec.Type = corev1.ServiceTypeClusterIP | ||
service.Spec.Selector = map[string]string{ | ||
naming.LabelStandalonePGAdmin: pgadmin.Name, | ||
} | ||
service.Spec.Ports = []corev1.ServicePort{{ | ||
Name: "pgadmin-port", | ||
Port: pgAdminPort, | ||
Protocol: corev1.ProtocolTCP, | ||
TargetPort: intstr.FromInt(pgAdminPort), | ||
}} | ||
|
||
return service | ||
} |
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,71 @@ | ||
// Copyright 2023 - 2024 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/pkg/apis/postgres-operator.crunchydata.com/v1beta1" | ||
) | ||
|
||
func TestService(t *testing.T) { | ||
pgadmin := new(v1beta1.PGAdmin) | ||
pgadmin.Name = "daisy" | ||
pgadmin.Namespace = "daisy-service-ns" | ||
pgadmin.Spec.ServiceName = "daisy-service" | ||
pgadmin.Spec.Metadata = &v1beta1.Metadata{ | ||
Labels: map[string]string{ | ||
"test-label": "test-label-val", | ||
"postgres-operator.crunchydata.com/pgadmin": "bad-val", | ||
"postgres-operator.crunchydata.com/role": "bad-val", | ||
}, | ||
Annotations: map[string]string{ | ||
"test-annotation": "test-annotation-val", | ||
}, | ||
} | ||
|
||
service := service(pgadmin) | ||
assert.Assert(t, service != nil) | ||
assert.Assert(t, cmp.MarshalMatches(service.TypeMeta, ` | ||
apiVersion: v1 | ||
kind: Service | ||
`)) | ||
|
||
assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta, ` | ||
annotations: | ||
test-annotation: test-annotation-val | ||
creationTimestamp: null | ||
labels: | ||
postgres-operator.crunchydata.com/pgadmin: daisy | ||
postgres-operator.crunchydata.com/role: pgadmin | ||
test-label: test-label-val | ||
name: daisy-service | ||
namespace: daisy-service-ns | ||
`)) | ||
|
||
assert.Assert(t, cmp.MarshalMatches(service.Spec, ` | ||
ports: | ||
- name: pgadmin-port | ||
port: 5050 | ||
protocol: TCP | ||
targetPort: 5050 | ||
selector: | ||
postgres-operator.crunchydata.com/pgadmin: daisy | ||
type: ClusterIP | ||
`)) | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
testing/kuttl/e2e/standalone-pgadmin-service/00--pgadmin.yaml
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,13 @@ | ||
apiVersion: postgres-operator.crunchydata.com/v1beta1 | ||
kind: PGAdmin | ||
metadata: | ||
name: pgadmin | ||
spec: | ||
dataVolumeClaimSpec: | ||
accessModes: | ||
- "ReadWriteOnce" | ||
resources: | ||
requests: | ||
storage: 1Gi | ||
serverGroups: [] | ||
serviceName: pgadmin-service |
16 changes: 16 additions & 0 deletions
16
testing/kuttl/e2e/standalone-pgadmin-service/00-assert.yaml
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,16 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: pgadmin-service | ||
labels: | ||
postgres-operator.crunchydata.com/role: pgadmin | ||
postgres-operator.crunchydata.com/pgadmin: pgadmin | ||
spec: | ||
selector: | ||
postgres-operator.crunchydata.com/pgadmin: pgadmin | ||
ports: | ||
- port: 5050 | ||
targetPort: 5050 | ||
protocol: TCP | ||
name: pgadmin-port | ||
type: ClusterIP |
13 changes: 13 additions & 0 deletions
13
testing/kuttl/e2e/standalone-pgadmin-service/01--update-service.yaml
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,13 @@ | ||
apiVersion: postgres-operator.crunchydata.com/v1beta1 | ||
kind: PGAdmin | ||
metadata: | ||
name: pgadmin | ||
spec: | ||
dataVolumeClaimSpec: | ||
accessModes: | ||
- "ReadWriteOnce" | ||
resources: | ||
requests: | ||
storage: 1Gi | ||
serverGroups: [] | ||
serviceName: pgadmin-service-updated |
16 changes: 16 additions & 0 deletions
16
testing/kuttl/e2e/standalone-pgadmin-service/01-assert.yaml
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,16 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: pgadmin-service-updated | ||
labels: | ||
postgres-operator.crunchydata.com/role: pgadmin | ||
postgres-operator.crunchydata.com/pgadmin: pgadmin | ||
spec: | ||
selector: | ||
postgres-operator.crunchydata.com/pgadmin: pgadmin | ||
ports: | ||
- port: 5050 | ||
targetPort: 5050 | ||
protocol: TCP | ||
name: pgadmin-port | ||
type: ClusterIP |
Oops, something went wrong.