Skip to content

Commit

Permalink
Add DomainMapping CRUD commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimansk committed Mar 18, 2021
1 parent 6a4f308 commit ff5f028
Show file tree
Hide file tree
Showing 38 changed files with 2,425 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
| 🎁
| Add S column to specify built-in sources in kn source list-types
| https://github.com/knative/client/pull/1246[#1246]

| 🎁
| Add `kn domain` commands to manage DomainMapping
| https://github.com/knative/client/pull/1267[#1267]

|===

## v0.21.0 (2021-02-23)
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ kn is the command line interface for managing Knative Serving and Eventing resou
* [kn broker](kn_broker.md) - Manage message brokers
* [kn channel](kn_channel.md) - Manage event channels
* [kn completion](kn_completion.md) - Output shell completion code
* [kn domain](kn_domain.md) - Manage domain mappings
* [kn options](kn_options.md) - Print the list of flags inherited by all commands
* [kn plugin](kn_plugin.md) - Manage kn plugins
* [kn revision](kn_revision.md) - Manage service revisions
Expand Down
33 changes: 33 additions & 0 deletions docs/cmd/kn_domain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## kn domain

Manage domain mappings

```
kn domain COMMAND
```

### Options

```
-h, --help help for domain
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn](kn.md) - kn manages Knative Serving and Eventing resources
* [kn domain create](kn_domain_create.md) - Create a domain mapping
* [kn domain delete](kn_domain_delete.md) - Delete a domain mapping
* [kn domain describe](kn_domain_describe.md) - Show details of a domain mapping
* [kn domain list](kn_domain_list.md) - List domain mappings
* [kn domain update](kn_domain_update.md) - Update a domain mapping

38 changes: 38 additions & 0 deletions docs/cmd/kn_domain_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## kn domain create

Create a domain mapping

```
kn domain create FQDN
```

### Examples

```
# Create a domain mappings 'hello.example.com' for Knative service 'hello'
kn domain create hello.example.com --ref hello
```

### Options

```
-h, --help help for create
-n, --namespace string Specify the namespace to operate in.
--ref string Addressable target reference for Domain Mapping. You can specify a Knative Service name.
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn domain](kn_domain.md) - Manage domain mappings

37 changes: 37 additions & 0 deletions docs/cmd/kn_domain_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## kn domain delete

Delete a domain mapping

```
kn domain delete FQDN
```

### Examples

```
# Delete domain mappings 'hello.example.com'
kn domain delete hello.example.com
```

### Options

```
-h, --help help for delete
-n, --namespace string Specify the namespace to operate in.
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn domain](kn_domain.md) - Manage domain mappings

33 changes: 33 additions & 0 deletions docs/cmd/kn_domain_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## kn domain describe

Show details of a domain mapping

```
kn domain describe FQDN
```

### Options

```
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for describe
-n, --namespace string Specify the namespace to operate in.
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file|url.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
-v, --verbose More output.
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn domain](kn_domain.md) - Manage domain mappings

45 changes: 45 additions & 0 deletions docs/cmd/kn_domain_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## kn domain list

List domain mappings

```
kn domain list
```

### Examples

```
# List all domain mappings
kn domain list
# List all domain mappings in JSON output format
kn revision list -o json
```

### Options

```
-A, --all-namespaces If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.
--allow-missing-template-keys If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. (default true)
-h, --help help for list
-n, --namespace string Specify the namespace to operate in.
--no-headers When using the default output format, don't print headers (default: print headers).
-o, --output string Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
--template string Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn domain](kn_domain.md) - Manage domain mappings

38 changes: 38 additions & 0 deletions docs/cmd/kn_domain_update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## kn domain update

Update a domain mapping

```
kn domain update FQDN
```

### Examples

```
# Update a domain mappings 'hello.example.com' for Knative service 'hello'
kn domain create hello.example.com --ref hello
```

### Options

```
-h, --help help for update
-n, --namespace string Specify the namespace to operate in.
--ref string Addressable target reference for Domain Mapping. You can specify a Knative Service name.
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
--config string kn configuration file (default: ~/.config/kn/config.yaml)
--context string name of the kubeconfig context to use
--kubeconfig string kubectl configuration file (default: ~/.kube/config)
--log-http log http traffic
```

### SEE ALSO

* [kn domain](kn_domain.md) - Manage domain mappings

79 changes: 79 additions & 0 deletions pkg/kn/commands/domain/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright © 2021 The Knative Authors
//
// 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 domain

import (
"errors"
"fmt"

"github.com/spf13/cobra"

knerrors "knative.dev/client/pkg/errors"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/flags"
clientv1alpha1 "knative.dev/client/pkg/serving/v1alpha1"
)

// NewDomainMappingCreateCommand to create event channels
func NewDomainMappingCreateCommand(p *commands.KnParams) *cobra.Command {
var refFlags flags.SinkFlags
cmd := &cobra.Command{
Use: "create FQDN",
Short: "Create a domain mapping",
Example: `
# Create a domain mappings 'hello.example.com' for Knative service 'hello'
kn domain create hello.example.com --ref hello`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'kn domain create' requires the domain name given as single argument")
}
name := args[0]
namespace, err := p.GetNamespace(cmd)
if err != nil {
return err
}

dynamicClient, err := p.NewDynamicClient(namespace)
if err != nil {
return err
}
destination, err := refFlags.ResolveSink(dynamicClient, namespace)
if err != nil {
return err
}

builder := clientv1alpha1.NewDomainMappingBuilder(name).
Namespace(namespace).
Reference(*destination.Ref)

client, err := p.NewServingV1alpha1Client(namespace)
if err != nil {
return err
}
err = client.CreateDomainMapping(builder.Build())
if err != nil {
return knerrors.GetError(err)
}

fmt.Fprintf(cmd.OutOrStdout(), "Domain mapping '%s' created in namespace '%s'.\n", name, namespace)
return nil
},
}
commands.AddNamespaceFlags(cmd.Flags(), false)
refFlags.AddWithFlagName(cmd, "ref", "")
cmd.Flag("ref").Usage = "Addressable target reference for Domain Mapping. You can specify a Knative Service name."
cmd.MarkFlagRequired("ref")
return cmd
}
60 changes: 60 additions & 0 deletions pkg/kn/commands/domain/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright © 2021 The Knative Authors
//
// 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 domain

import (
"testing"

"gotest.tools/v3/assert"

dynamicfake "knative.dev/client/pkg/dynamic/fake"
"knative.dev/client/pkg/serving/v1alpha1"
"knative.dev/client/pkg/util"
)

func TestDomainMappingCreate(t *testing.T) {
client := v1alpha1.NewMockKnServiceClient(t)
dynamicClient := dynamicfake.CreateFakeKnDynamicClient(client.Namespace(), createService("foo"))

servingRecorder := client.Recorder()
servingRecorder.CreateDomainMapping(createDomainMapping("foo.bar", createServiceRef("foo", "default")), nil)

out, err := executeDomainCommand(client, dynamicClient, "create", "foo.bar", "--ref", "foo")
assert.NilError(t, err, "Domain mapping should be created")
assert.Assert(t, util.ContainsAll(out, "Domain", "mapping", "foo.bar", "created", "namespace", "default"))

servingRecorder.Validate()
}
func TestDomainMappingCreateWithError(t *testing.T) {
client := v1alpha1.NewMockKnServiceClient(t)
dynamicClient := dynamicfake.CreateFakeKnDynamicClient(client.Namespace(), createService("foo"))

// No call should be recorded
servingRecorder := client.Recorder()

_, err := executeDomainCommand(client, dynamicClient, "create", "--ref", "foo")
assert.ErrorContains(t, err, "domain create")
assert.Assert(t, util.ContainsAll(err.Error(), "domain create", "requires", "name", "argument"))

_, err = executeDomainCommand(client, dynamicClient, "create", "bar")
assert.ErrorContains(t, err, "required flag")
assert.Assert(t, util.ContainsAll(err.Error(), "required", "flag", "not", "set"))

_, err = executeDomainCommand(client, dynamicClient, "create", "foo.bar", "--ref", "bar")
assert.ErrorContains(t, err, "not found")
assert.Assert(t, util.ContainsAll(err.Error(), "services", "\"bar\"", "not", "found"))

servingRecorder.Validate()
}
Loading

0 comments on commit ff5f028

Please sign in to comment.