servicebinding / spec

Specification for binding services to k8s workloads
https://servicebinding.io
Apache License 2.0
92 stars 35 forks source link

Programming language interface for application developers #174

Closed baijum closed 2 years ago

baijum commented 3 years ago

How the application developers consume the binding depends on the programming language of their choice. If there is a document that providers some API for few popular languages would help accelerate the adoption of the spec. Probably we can create some language-specific libraries. For Java, Spring Cloud Bindings library looks great.

This is my attempt to design API for Python and Go.

Python:

class ServiceBindingRootMissingError(Exception):
    pass

class ServiceBinding:

    def __init__(self):
        """
        - raise ServiceBindingRootMissingError if SERVICE_BINDING_ROOT env var not set
        """

    def all_bindings(self) -> list[dict[str, str]]:
        """Get all bindings as a list of dictionaries

        - return an empty list if no bindings found
        """

    def bindings(self, _type: str, provider: typing.Optional[str] = None) -> list[dict[str, str]]:
        """Get filtered bindings as a list of dictionaries

        - return an empty list if no bindings found
        - filter the result with the given _type input
        - if provider input is given, filter bindings using the given type and provider
        """

Go:

type ServiceBinding interface {

    // AllBindings get all bndings as a sice of map[string]string
    // return empty slice if no bindings found
    AllBindings() ([]map[string]string, error)

    // Bindings get the bindings for a given type as a slice of map[string]string
    // return empty slice if no bindings found
    Bindings(_type string) ([]map[string]string, error)

    // BindingsWithProvider get the bindings for a given type and provider as a slice of map[string]string
    // return empty slice if no bindings found
    BindingsWithProvider(_type, provider string) ([]map[string]string, error)
}

Feedback welcome!

baijum commented 3 years ago

I just found a Node.js library.

I also created a Python implementation using the above API.

On a closer look at the Node.js implementation, Java implementation, and my own Python, looks like it is a bit hard to standardize the client library. Every language/framework has its own needs/constraints. Probably we should leave it to the community and see how it evolves?

scothis commented 3 years ago

We should start to collect a list of adopters for services and app libraries that conform to the provisioned service or application projections sections of the spec (respectively).

baijum commented 3 years ago

Another application projection from QUARKUS: https://quarkus.io/guides/deploying-to-kubernetes#service-binding

I don't see much use of provider value. I think we should work on the implementor's guide to explain the usage pattern.

baijum commented 3 years ago

I don't see much use of provider value. I think we should work on the implementor's guide to explain the usage pattern.

I just started working on a draft PR: #175

wtrocki commented 3 years ago

I think we should work on the implementor's guide to explain the usage pattern.

It will be actually much better to have mini sdk for each popular library done as part of the spec. Since spec now enforces large file structure with multiple files I always hear some from of negativism related to lack of single file or env variables. If there would be libraries that cover binding to object for every major language then this will be huge boost for overall binding popularity.

scothis commented 3 years ago

It will be actually much better to have mini sdk for each popular library done as part of the spec.

While I agree that we should encourage and support libraries for every major language, I strongly disagree that we as the spec community should provide them. There's a find line as a spec between fostering a community and being the community; we do not want to be "the" community. By starting to provide "official" libraries, it ends up discouraging third parties from developing their own support as it is hard to compete with the official libraries, even when it is outdated and inferior. Because we don't have expertise in every major language, they will inherently become outdated and inferior.

wtrocki commented 3 years ago

I was copying the experience I had in the GraphQL community which provides "community-supported reference implementations" that are still maintained by volunteers. I was thinking in terms of creating space for building such libraries - like repos and looking if there will be possible maintainers. For us is better to maintain this in the source rather than add some solutions into each open source project etc. Process of creating specifications involves measuring impact, breaking changes on libraries that exist.

The concept of reading files into configuration seems low on investment and maintenance free task that can be achieved without significant development effort.

Reference specification also forces spec proposals to be more practical and down to the earth, meaning that spec will not overgrow into corner cases that will not be widely supported etc. EDIT:

What is also good is that introduction of the different languages can bring a better perspective on how things can be done and provide a feedback loop to the evolution of the spec.

baijum commented 3 years ago

Go package: https://github.com/baijum/servicebinding

baijum commented 2 years ago

C# library: https://www.nuget.org/packages/KubeServiceBinding.DotnetServiceBinding/