Skip to content

Commit

Permalink
Allow configuration sink prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Feb 19, 2020
1 parent c2dcb6b commit dc301ff
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
17 changes: 14 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,37 @@ You'll need a `kubectl`-style config file to connect to your cluster.

## Kn Config

There are a set of configuration parameters you can setup to better customize `kn`. In particular, you can specify where your `kn` plugins are located and how they are found. The `kn` configuration file is meant to capture these configuration options. Let's explore this file's location, and the options you are able to change with it.
There are a set of configuration parameters you can setup to better customize `kn`. For example, you can specify where your `kn` plugins are located and how they are found, and you can specify the prefix for your addressable `sink` objects. The `kn` configuration file is meant to capture these configuration options. Let's explore this file's location, and the options you are able to change with it.

### Location

The default location `kn` looks for config is under the home directory of the user at `$HOME/.kn/config.yaml`. It is not created for you as part of the `kn` installation. You can create this file elsewhere and use the `--config` flag to specify its path.

### Options

There are two options you can specify in the `kn` config file and they are related to how `kn` locates plugins.
Below are the options you can specify in the `kn` config file.

1. `pluginsDir` which is the same as the persistent flag `--plugins-dir` and specifies the kn plugins directory. It defaults to: `~/.kn/plugins`. By using the persistent flag (when you issue a command) or by specifying the value in the `kn` config, a user can select which directory to find `kn` plugins. It can be any directory that is visible to the user.

2. `lookupPluginsInPath` which is the same as the persistent flag `--lookup-plugins-in-path` and specficies if `kn` should look for plugins anywhere in the specified `PATH` environment variable. This is a boolean configuration option and the default value is `false`.

For example, the following `kn` config will look for `kn` plugins in the user's `PATH` and also execute plugin in `~/.kn/plugins`.
3. `sink` defines your prefix to refer to Kubernetes resources which are described by APIGroup, Version and KIND. Then a `sink` object could be described as `<prefix>:<object name>` in `kn` command lines.
1. `prefix` is the prefix you want to describe your sink objects. `svc`, `service`, and `broker` are predefined prefixes in `kn`.
2. `group` is the APIGroup of Kubernetes resources.
3. `version` is the version of Kubernetes resources.
4. `resource` is the plural name of Kubernetes resources.

For example, the following `kn` config will look for `kn` plugins in the user's `PATH` and also execute plugin in `~/.kn/plugins`. It also defines a sink prefix `myprefix` which refer to `brokers` in `eventing.knative.dev/v1alpha1`. With this configuration, you could use `myprefix:default` to describe Broker `default` in `kn` command lines.

```bash
cat ~/.kn/config.yaml
lookupPluginsInPath: true
pluginsdir: ~/.kn/plugins
sink:
- prefix: myprefix
group: eventing.knative.dev
version: v1alpha1
resource: brokers
```
----------------------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions pkg/kn/commands/flags/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
duckv1 "knative.dev/pkg/apis/duck/v1"

clientdynamic "knative.dev/client/pkg/dynamic"
"knative.dev/client/pkg/kn/commands"
)

type SinkFlags struct {
Expand Down Expand Up @@ -55,6 +56,17 @@ var SinkPrefixes = map[string]schema.GroupVersionResource{
},
}

func ConfigSinkPrefixes(prefixes []commands.SinkPrefixConfig) {
for _, p := range prefixes {
//user configration might override the default configuration
SinkPrefixes[p.Prefix] = schema.GroupVersionResource{
Resource: p.Resource,
Group: p.Group,
Version: p.Version,
}
}
}

// ResolveSink returns the Destination referred to by the flags in the acceptor.
// It validates that any object the user is referring to exists.
func (i *SinkFlags) ResolveSink(knclient clientdynamic.KnDynamicClient, namespace string) (*duckv1.Destination, error) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/kn/commands/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ var Cfg Config = Config{
type Config struct {
PluginsDir string
LookupPlugins *bool
SinkPrefixes []SinkPrefixConfig
}

// SinkPrefixConfig is the struct of sink prefix config in kn config
type SinkPrefixConfig struct {
Prefix string
Resource string
Group string
Version string
}

// KnParams for creating commands. Useful for inserting mocks for testing.
Expand Down
10 changes: 10 additions & 0 deletions pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/kn/commands/completion"
cmdflags "knative.dev/client/pkg/kn/commands/flags"
"knative.dev/client/pkg/kn/commands/plugin"
"knative.dev/client/pkg/kn/commands/revision"
"knative.dev/client/pkg/kn/commands/route"
Expand Down Expand Up @@ -234,6 +235,15 @@ func initConfigFlags() {
var aBool bool
aBool = viper.GetBool("lookup-plugins")
commands.Cfg.LookupPlugins = &aBool

// set the Cfg.SinkPrefixes from viper if sink is configured
if viper.IsSet("sink") {
err := viper.UnmarshalKey("sink", &commands.Cfg.SinkPrefixes)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to parse sink prefixes configuration: %v .\n", err)
}
cmdflags.ConfigSinkPrefixes(commands.Cfg.SinkPrefixes)
}
}

func extractKnPluginFlags(args []string) (string, bool, error) {
Expand Down

0 comments on commit dc301ff

Please sign in to comment.