Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions api/v1alpha/location_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ type LocationSpec struct {
//
// +kubebuilder:validation:Required
Provider LocationProvider `json:"provider"`

// The geographic coordinates of the location, used by consumers that need
// to plot the location on a map.
//
// +kubebuilder:validation:Optional
Coordinates *Coordinates `json:"coordinates,omitempty"`
}

// Coordinates describes a geographic point in decimal degrees (WGS 84).
//
// Latitude and longitude are serialized as strings rather than floats, per
// Kubernetes API convention (float precision/serialization varies across
// client languages).
type Coordinates struct {
// Latitude in decimal degrees, in the range [-90, 90].
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern=`^-?\d{1,2}(\.\d+)?$`
// +kubebuilder:validation:XValidation:message="latitude must be between -90 and 90",rule="double(self) >= -90.0 && double(self) <= 90.0"
Latitude string `json:"latitude"`

// Longitude in decimal degrees, in the range [-180, 180].
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern=`^-?\d{1,3}(\.\d+)?$`
// +kubebuilder:validation:XValidation:message="longitude must be between -180 and 180",rule="double(self) >= -180.0 && double(self) <= 180.0"
Longitude string `json:"longitude"`
}

type LocationProvider struct {
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions config/crd/bases/networking.datumapis.com_locations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ spec:
spec:
description: LocationSpec defines the desired state of Location.
properties:
coordinates:
description: |-
The geographic coordinates of the location, used by consumers that need
to plot the location on a map.
properties:
latitude:
description: Latitude in decimal degrees, in the range [-90, 90].
pattern: ^-?\d{1,2}(\.\d+)?$
type: string
x-kubernetes-validations:
- message: latitude must be between -90 and 90
rule: double(self) >= -90.0 && double(self) <= 90.0
longitude:
description: Longitude in decimal degrees, in the range [-180,
180].
pattern: ^-?\d{1,3}(\.\d+)?$
type: string
x-kubernetes-validations:
- message: longitude must be between -180 and 180
rule: double(self) >= -180.0 && double(self) <= 180.0
required:
- latitude
- longitude
type: object
locationClassName:
description: "The location class that indicates control plane behavior
of entities\nassociated with the location.\n\nValid values are:\n\t-
Expand Down
47 changes: 47 additions & 0 deletions docs/api/locations.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ as:
- topology.datum.net/city-code<br/>
</td>
<td>true</td>
</tr><tr>
<td><b><a href="#locationspeccoordinates">coordinates</a></b></td>
<td>object</td>
<td>
The geographic coordinates of the location, used by consumers that need
to plot the location on a map.<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down Expand Up @@ -189,6 +197,45 @@ namespace.<br/>
</table>


### Location.spec.coordinates
<sup><sup>[↩ Parent](#locationspec)</sup></sup>



The geographic coordinates of the location, used by consumers that need
to plot the location on a map.

<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Required</th>
</tr>
</thead>
<tbody><tr>
<td><b>latitude</b></td>
<td>string</td>
<td>
Latitude in decimal degrees, in the range [-90, 90].<br/>
<br/>
<i>Validations</i>:<li>double(self) >= -90.0 && double(self) <= 90.0: latitude must be between -90 and 90</li>
</td>
<td>true</td>
</tr><tr>
<td><b>longitude</b></td>
<td>string</td>
<td>
Longitude in decimal degrees, in the range [-180, 180].<br/>
<br/>
<i>Validations</i>:<li>double(self) >= -180.0 && double(self) <= 180.0: longitude must be between -180 and 180</li>
</td>
<td>true</td>
</tr></tbody>
</table>


### Location.status
<sup><sup>[↩ Parent](#location)</sup></sup>

Expand Down
116 changes: 116 additions & 0 deletions test/crd/location_coordinates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// SPDX-License-Identifier: AGPL-3.0-only

package crd

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

networkingv1alpha "go.datum.net/network-services-operator/api/v1alpha"
)

func baseLocationSpec() networkingv1alpha.LocationSpec {
return networkingv1alpha.LocationSpec{
LocationClassName: "datum-managed",
Topology: map[string]string{
"topology.datum.net/city-code": "DFW",
},
Provider: networkingv1alpha.LocationProvider{
GCP: &networkingv1alpha.GCPLocationProvider{
ProjectID: "datum-cloud-poc-1",
Region: "us-south1",
Zone: "us-south1-a",
},
},
}
}

// TestLocationCoordinatesOptional asserts a Location can still be created
// without coordinates, so the new field doesn't break existing callers.
func TestLocationCoordinatesOptional(t *testing.T) {
cl := requireEnv(t)
ctx := context.Background()

loc := &networkingv1alpha.Location{
ObjectMeta: metav1.ObjectMeta{Name: "no-coordinates"},
Spec: baseLocationSpec(),
}
require.NoError(t, cl.Create(ctx, loc))
t.Cleanup(func() { _ = cl.Delete(ctx, loc) })

var got networkingv1alpha.Location
require.NoError(t, cl.Get(ctx, client.ObjectKeyFromObject(loc), &got))
assert.Nil(t, got.Spec.Coordinates)
}

// TestLocationCoordinatesValid asserts a Location with well-formed
// coordinates is accepted and round-trips.
func TestLocationCoordinatesValid(t *testing.T) {
cl := requireEnv(t)
ctx := context.Background()

spec := baseLocationSpec()
spec.Coordinates = &networkingv1alpha.Coordinates{
Latitude: "32.8968",
Longitude: "-97.0380",
}
loc := &networkingv1alpha.Location{
ObjectMeta: metav1.ObjectMeta{Name: "valid-coordinates"},
Spec: spec,
}
require.NoError(t, cl.Create(ctx, loc))
t.Cleanup(func() { _ = cl.Delete(ctx, loc) })

var got networkingv1alpha.Location
require.NoError(t, cl.Get(ctx, client.ObjectKeyFromObject(loc), &got))
require.NotNil(t, got.Spec.Coordinates)
assert.Equal(t, "32.8968", got.Spec.Coordinates.Latitude)
assert.Equal(t, "-97.0380", got.Spec.Coordinates.Longitude)
}

// TestLocationRejectsOutOfRangeCoordinates asserts the CEL range rules reject
// a latitude/longitude outside valid Earth coordinates, even though the
// pattern alone would accept the string shape.
func TestLocationRejectsOutOfRangeCoordinates(t *testing.T) {
cl := requireEnv(t)
ctx := context.Background()

spec := baseLocationSpec()
spec.Coordinates = &networkingv1alpha.Coordinates{
Latitude: "95.0",
Longitude: "-97.0380",
}
loc := &networkingv1alpha.Location{
ObjectMeta: metav1.ObjectMeta{Name: "out-of-range-latitude"},
Spec: spec,
}
err := cl.Create(ctx, loc)
require.Error(t, err, "latitude outside [-90, 90] must be rejected")
assert.Truef(t, apierrors.IsInvalid(err), "expected an Invalid error, got %v", err)
}

// TestLocationRejectsMalformedCoordinate asserts the pattern rule rejects a
// non-numeric coordinate string.
func TestLocationRejectsMalformedCoordinate(t *testing.T) {
cl := requireEnv(t)
ctx := context.Background()

spec := baseLocationSpec()
spec.Coordinates = &networkingv1alpha.Coordinates{
Latitude: "not-a-number",
Longitude: "-97.0380",
}
loc := &networkingv1alpha.Location{
ObjectMeta: metav1.ObjectMeta{Name: "malformed-latitude"},
Spec: spec,
}
err := cl.Create(ctx, loc)
require.Error(t, err, "non-numeric latitude must be rejected")
assert.Truef(t, apierrors.IsInvalid(err), "expected an Invalid error, got %v", err)
}
Loading