Skip to content

Commit

Permalink
feat: function name matches KService name (#317)
Browse files Browse the repository at this point in the history
* feat: function name matches KService name

Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>

* fix typo

Signed-off-by: Zbynek Roubalik <zroubali@redhat.com>
  • Loading branch information
zroubalik authored Apr 26, 2021
1 parent 36926ac commit 541e858
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 208 deletions.
2 changes: 0 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ type ListItem struct {
Namespace string `json:"namespace" yaml:"namespace"`
Runtime string `json:"runtime" yaml:"runtime"`
URL string `json:"url" yaml:"url"`
KService string `json:"kservice" yaml:"kservice"`
Ready string `json:"ready" yaml:"ready"`
}

Expand Down Expand Up @@ -108,7 +107,6 @@ type Describer interface {
type Description struct {
Name string `json:"name" yaml:"name"`
Image string `json:"image" yaml:"image"`
KService string `json:"kservice" yaml:"kservice"`
Namespace string `json:"namespace" yaml:"namespace"`
Routes []string `json:"routes" yaml:"routes"`
Subscriptions []Subscription `json:"subscriptions" yaml:"subscriptions"`
Expand Down
21 changes: 18 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ kn func create --trigger events myfunc
}

func runCreate(cmd *cobra.Command, args []string) error {
config := newCreateConfig(args).Prompt()
config := newCreateConfig(args)

if err := utils.ValidateFunctionName(config.Name); err != nil {
return err
}

config = config.Prompt()

function := bosonFunc.Function{
Name: config.Name,
Expand Down Expand Up @@ -131,12 +137,21 @@ func (c createConfig) Prompt() createConfig {
return c
}

derivedName, derivedPath := deriveNameAndAbsolutePathFromPath(prompt.ForString("Project path", c.Path, prompt.WithRequired(true)))
var derivedName, derivedPath string
for {
derivedName, derivedPath = deriveNameAndAbsolutePathFromPath(prompt.ForString("Project path", c.Path, prompt.WithRequired(true)))
err := utils.ValidateFunctionName(derivedName)
if err == nil {
break
}
fmt.Println("Error:", err)
}

return createConfig{
Name: derivedName,
Path: derivedPath,
Runtime: prompt.ForString("Runtime", c.Runtime),
Trigger: prompt.ForString("Trigger", c.Trigger),
// Templates intentiopnally omitted from prompt for being an edge case.
// Templates intentionally omitted from prompt for being an edge case.
}
}
3 changes: 0 additions & 3 deletions cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ func (d description) Human(w io.Writer) error {
fmt.Fprintf(w, " %v\n", d.Name)
fmt.Fprintln(w, "Function is built in image:")
fmt.Fprintf(w, " %v\n", d.Image)
fmt.Fprintln(w, "Function is deployed as Knative Service:")
fmt.Fprintf(w, " %v\n", d.KService)
fmt.Fprintln(w, "Function is deployed in namespace:")
fmt.Fprintf(w, " %v\n", d.Namespace)
fmt.Fprintln(w, "Routes:")
Expand All @@ -138,7 +136,6 @@ func (d description) Human(w io.Writer) error {
func (d description) Plain(w io.Writer) error {
fmt.Fprintf(w, "Name %v\n", d.Name)
fmt.Fprintf(w, "Image %v\n", d.Image)
fmt.Fprintf(w, "Knative Service %v\n", d.KService)
fmt.Fprintf(w, "Namespace %v\n", d.Namespace)

for _, route := range d.Routes {
Expand Down
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ func (items listItems) Plain(w io.Writer) error {
tabWriter := tabwriter.NewWriter(w, 0, 8, 2, ' ', 0)
defer tabWriter.Flush()

fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\t%s\n", "NAME", "NAMESPACE", "RUNTIME", "URL", "KSERVICE", "READY")
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\n", "NAME", "NAMESPACE", "RUNTIME", "URL", "READY")
for _, item := range items {
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\t%s\n", item.Name, item.Namespace, item.Runtime, item.URL, item.KService, item.Ready)
fmt.Fprintf(tabWriter, "%s\t%s\t%s\t%s\t%s\n", item.Name, item.Namespace, item.Runtime, item.URL, item.Ready)
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions docs/guides/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

Creates a new Function project at _`path`_. If _`path`_ is unspecified, assumes the current directory. If _`path`_ does not exist, it will be created. The function name is the name of the leaf directory at path. The user can specify the runtime and trigger with flags.

Function name must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?').

Similar `kn` command: none.

```console
Expand Down
42 changes: 24 additions & 18 deletions docs/guides/developers_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ The unit of deployment in Boson Functions is an [OCI](https://opencontainers.org
container image, typically referred to as a Docker container image.

In order for the `func` CLI to manage these containers, you'll need to be
logged in to a container registry. For example, `docker.io/lanceball`
logged in to a container registry. For example, `docker.io/developer`


```bash
# Typically, this will log you in to docker hub if you
# omit <registry.url>. If you are using a registry
# other than Docker hub, provide that for <registry.url>
docker login -u lanceball -p [redacted] <registry.url>
docker login -u developer -p [redacted] <registry.url>
```

> Note: many of the `func` CLI commands take a `--registry` argument.
Expand All @@ -62,20 +62,21 @@ docker login -u lanceball -p [redacted] <registry.url>
```bash
# This should be set to a registry that you have write permission
# on and you have logged into in the previous step.
export FUNC_REGISTRY=docker.io/lanceball
export FUNC_REGISTRY=docker.io/developer
```

## Creating a Project

With your Knative enabled cluster up and running, you can now create a new
Function Project. Let's start by creating a project directory. Function names
in `func` correspond to URLs at the moment, and there are some finicky cases
at the moment. To ensure that everything works as it should, create a project
directory consisting of three URL parts. Here is a good one.
Function Project. Let's start by creating a project directory. Function name
must consist of lower case alphanumeric characters or '-',
and must start and end with an alphanumeric character
(e.g. 'my-name', or '123-abc', regex used for validation is `[a-z0-9]([-a-z0-9]*[a-z0-9])?`).


```bash
mkdir fn.example.io
cd fn.example.io
mkdir fn-example-io
cd fn-example-io
```

Now, we will create the project files, build a container, and
Expand All @@ -84,7 +85,6 @@ deploy the function as a Knative service.

```bash
func create -l node
func build
func deploy
```

Expand All @@ -93,9 +93,15 @@ all of the defaults inferred from your environment, for example`$FUNC_REGISTRY`.
When the command has completed, you can see the deployed function.

```bash
kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
fn-example-io http://fn-example-io.func.127.0.0.1.nip.io fn-example-io-ngswh-1 24s 3 OK / 3 True
func describe
Function name:
fn-example-io
Function is built in image:
docker.io/developer/fn-example-io:latest
Function is deployed in namespace:
default
Routes:
http://fn-example-io-default.apps.functions.my-cluster.com
```

Clicking on the URL will take you to the running function in your cluster. You
Expand All @@ -108,7 +114,7 @@ should see a simple response.
You can add query parameters to the request to see those echoed in return.

```console
curl "http://fn-example-io.func.127.0.0.1.nip.io?name=tiger"
curl "http://fn-example-io-default.apps.functions.my-cluster.com?name=tiger"
{"query":{"name":"tiger"},"name":"tiger"}
```

Expand Down Expand Up @@ -150,7 +156,7 @@ You might see a message such as this.
Error: remover failed to delete the service: timeout: service 'fn-example-io' not ready after 30 seconds.
```

If you do, just run `kn service list` to see if the function is still deployed.
If you do, just run `func list` to see if the function is still deployed.
It might just take a little time for it to be removed.

Now, let's clean up the current directory.
Expand All @@ -168,9 +174,9 @@ cluster, use the `create` command.
func create -l node -t http
```

You can also create a Quarkus or a Golang project by providing `quarkus` or `go`
respectively to the `-l` flag. To create a project with a template for
CloudEvents, provide `events` to the `-t` flag.
You can also create a Quarkus, SpringBoot, Python or a Golang project by providing
`quarkus`, `springboot`, `python` or `go` respectively to the `-l` flag.
To create a project with a template for CloudEvents, provide `events` to the `-t` flag.

### `func build`

Expand Down
77 changes: 0 additions & 77 deletions k8s/names.go

This file was deleted.

58 changes: 0 additions & 58 deletions k8s/names_test.go

This file was deleted.

Loading

0 comments on commit 541e858

Please sign in to comment.