Skip to content

Commit

Permalink
[wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
stuggi committed Sep 11, 2023
1 parent c816e73 commit ddd3b7e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 80 deletions.
14 changes: 11 additions & 3 deletions modules/common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ func (s *Service) AddAnnotation(anno map[string]string) {
}

// GetAPIEndpoint - returns the API endpoint URL for the service to register in keystone.
func (s *Service) GetAPIEndpoint(svcOverride *OverrideSpec, protocol *Protocol, path string) (string, error) {
func (s *Service) GetAPIEndpoint(endpointURL *string, protocol *Protocol, path string) (string, error) {
var apiEndpoint *url.URL
var err error
if svcOverride != nil && svcOverride.EndpointURL != nil {
apiEndpoint, err = url.Parse(*svcOverride.EndpointURL)
if endpointURL != nil {
apiEndpoint, err = url.Parse(*endpointURL)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -426,3 +426,11 @@ func EndptProtocol(proto *Protocol) string {

return string(*proto) + "://"
}

// GetOverrideSpec -
func (r *RoutedOverrideSpec) GetOverrideSpec() *OverrideSpec {
return &OverrideSpec{
EmbeddedLabelsAnnotations: r.EmbeddedLabelsAnnotations,
Spec: r.Spec,
}
}
118 changes: 57 additions & 61 deletions modules/common/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,91 +302,87 @@ func TestNewService(t *testing.T) {

func TestGetAPIEndpoint(t *testing.T) {
tests := []struct {
name string
service *corev1.Service
override OverrideSpec
proto Protocol
port string
path string
want string
name string
service *corev1.Service
endpointURL *string
proto Protocol
port string
path string
want string
}{
{
name: "HTTP ClusterIP service default port 80, no override",
service: getServiceWithPort(svcClusterIP, portHTTP),
override: OverrideSpec{},
proto: ProtocolHTTP,
path: "",
want: "http://foo.namespace.svc",
name: "HTTP ClusterIP service default port 80, no override",
service: getServiceWithPort(svcClusterIP, portHTTP),
endpointURL: nil,
proto: ProtocolHTTP,
path: "",
want: "http://foo.namespace.svc",
},
{
name: "HTTP ClusterIP service non default 8080 port, no override",
service: getServiceWithPort(svcClusterIP, portCustom),
override: OverrideSpec{},
proto: ProtocolHTTP,
path: "/path",
want: "http://foo.namespace.svc:8080/path",
name: "HTTP ClusterIP service non default 8080 port, no override",
service: getServiceWithPort(svcClusterIP, portCustom),
endpointURL: nil,
proto: ProtocolHTTP,
path: "/path",
want: "http://foo.namespace.svc:8080/path",
},
{
name: "HTTPS ClusterIP service default 443 port, no override",
service: getServiceWithPort(svcClusterIP, portHTTPS),
override: OverrideSpec{},
proto: ProtocolHTTPS,
path: "/path",
want: "https://foo.namespace.svc/path",
name: "HTTPS ClusterIP service default 443 port, no override",
service: getServiceWithPort(svcClusterIP, portHTTPS),
endpointURL: nil,
proto: ProtocolHTTPS,
path: "/path",
want: "https://foo.namespace.svc/path",
},
{
name: "HTTPS ClusterIP service non default 8080 port, no override",
service: getServiceWithPort(svcClusterIP, portCustom),
override: OverrideSpec{},
proto: ProtocolHTTPS,
path: "/path",
want: "https://foo.namespace.svc:8080/path",
name: "HTTPS ClusterIP service non default 8080 port, no override",
service: getServiceWithPort(svcClusterIP, portCustom),
endpointURL: nil,
proto: ProtocolHTTPS,
path: "/path",
want: "https://foo.namespace.svc:8080/path",
},
{
name: "None ClusterIP service port 80 no override",
service: getServiceWithPort(svcClusterIP, portHTTP),
override: OverrideSpec{},
proto: ProtocolNone,
path: "/path",
want: "foo.namespace.svc:80/path",
name: "None ClusterIP service port 80 no override",
service: getServiceWithPort(svcClusterIP, portHTTP),
endpointURL: nil,
proto: ProtocolNone,
path: "/path",
want: "foo.namespace.svc:80/path",
},
{
name: "None ClusterIP service port 8080 override",
service: getServiceWithPort(svcClusterIP, portCustom),
override: OverrideSpec{},
proto: ProtocolNone,
path: "/path",
want: "foo.namespace.svc:8080/path",
name: "None ClusterIP service port 8080 override",
service: getServiceWithPort(svcClusterIP, portCustom),
endpointURL: nil,
proto: ProtocolNone,
path: "/path",
want: "foo.namespace.svc:8080/path",
},
{
name: "Override EndpointURL with path",
service: getServiceWithPort(svcClusterIP, portCustom),
override: OverrideSpec{
EndpointURL: ptr.To("http://override.me"),
},
proto: ProtocolNone,
path: "/path",
want: "http://override.me/path",
name: "Override EndpointURL with path",
service: getServiceWithPort(svcClusterIP, portCustom),
endpointURL: ptr.To("http://this.url"),
proto: ProtocolNone,
path: "/path",
want: "http://this.url/path",
},
{
name: "Override EndpointURL no path",
service: getServiceWithPort(svcClusterIP, portCustom),
override: OverrideSpec{
EndpointURL: ptr.To("http://override.me"),
},
proto: ProtocolNone,
path: "",
want: "http://override.me",
name: "Override EndpointURL no path",
service: getServiceWithPort(svcClusterIP, portCustom),
endpointURL: ptr.To("http://this.url"),
proto: ProtocolNone,
path: "",
want: "http://this.url",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

service, err := NewService(tt.service, timeout, &tt.override)
service, err := NewService(tt.service, timeout, &OverrideSpec{})
g.Expect(err).ToNot(HaveOccurred())
url, err := service.GetAPIEndpoint(&tt.override, ptr.To(tt.proto), tt.path)
url, err := service.GetAPIEndpoint(tt.endpointURL, ptr.To(tt.proto), tt.path)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(url).To(Equal(tt.want))
})
Expand Down
18 changes: 7 additions & 11 deletions modules/common/service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const (
EndpointPublic Endpoint = "public"
// AnnotationIngressCreateKey -
AnnotationIngressCreateKey = "core.openstack.org/ingress_create"
// AnnotationIngressNameKey -
AnnotationIngressNameKey = "core.openstack.org/ingress_name"
// AnnotationEndpointKey -
AnnotationEndpointKey = "endpoint"
// AnnotationHostnameKey -
Expand Down Expand Up @@ -114,17 +112,15 @@ const (
// OverrideSpec - service override configuration for the Service created to serve traffic to the cluster.
// Allows for the manifest of the created Service to be overwritten with custom configuration.
type OverrideSpec struct {
// EndpointURL to be used to register the service in keystone.
// +optional
EndpointURL *string `json:"endpointURL,omitempty"`

// +optional
*EmbeddedLabelsAnnotations `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
Spec *OverrideServiceSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
}

// Spec defines the behavior of a Service.
// https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
// +optional
Spec *OverrideServiceSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
// RoutedOverrideSpec - a routed service override configuration for the Service created to serve traffic
// to the cluster. Allows for the manifest of the created Service to be overwritten with custom configuration.
type RoutedOverrideSpec struct {
OverrideSpec `json:",inline"`
EndpointURL *string `json:"endpointURL,omitempty"`
}

// EmbeddedLabelsAnnotations is an embedded subset of the fields included in k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta.
Expand Down
26 changes: 21 additions & 5 deletions modules/common/service/zz_generated.deepcopy.go

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

0 comments on commit ddd3b7e

Please sign in to comment.