Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add a config command to manage the default settings #3599

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 79 additions & 9 deletions docs/modules/ROOT/pages/cli/file-based-config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,93 @@

File-based configuration is used to set command flags. Flag values do not need to be entered on a regular basis. The file is read on Kamel startup and the flags are set accordingly.

The file's default name is `kamel-config.yaml` . Which should be placed in either of this directory structure:
The file's default name is `kamel-config.yaml`, it can be changed by setting the environment variable `KAMEL_CONFIG_NAME`. Kamel tries to read the file from the following directories in the given order:

- `~/.kamel/`
- `./.kamel/`
- `.`
- `./.kamel/`
- `~/.kamel/`

it can be overridden by setting an env value `KAMEL_CONFIG_NAME` to file path.
It can be overridden by setting the environment variable `KAMEL_CONFIG_PATH` to file path.


To configure this flag, create a file named kamel-config.yaml on the same directory as your integration. The file must contain a yaml structure as shown below:
To configure this flag, create a file named `kamel-config.yaml` on the same directory as your integration. The file must contain a yaml structure as shown below:

.Kamel-config.yaml
.kamel-config.yaml

```yaml
kamel:
install:
namespace: kamel
healthPort: 8081
monitoringPort: 8082
health-port: 8081
monitoring-port: 8082
```

As there are several supported locations, it can be handy to list a configuration file in one specific location, in this particular case the `config` command can be used.

To list the configuration file used in practice by Kamel:

[source,console]
----
$ kamel config --list
The configuration file is read from /some/path/kamel-config.yaml
kamel:
config:
default-namespace: some-name
----

Alternatively, the same result can be retrieved using the `--folder` flag with `used` as value.

[source,console]
----
$ kamel config --list --folder used
----

The flag `--folder` accepts 4 other possible values, one per possible location.

To list the configuration file in the working directory (`.`):

[source,console]
----
$ kamel config --list --folder working
----

To list the configuration file in the folder `.kamel` located in the working directory (`./.kamel/`):

[source,console]
----
$ kamel config --list --folder sub
----

To list the configuration file in the home directory (`~/.kamel/`):

[source,console]
----
$ kamel config --list --folder home
----

To list the configuration file located in the folder whose path is set in the environment variable `KAMEL_CONFIG_PATH`:

[source,console]
----
$ kamel config --list --folder env
----

The `config` command can also set the default namespace for all Kamel commands thanks to the flag `--default-namespace` as next:

[source,console]
----
$ kamel config --default-namespace some-name
----

Note that the flag `--default-namespace` can be associated with `--list` to see directly the resulting content:

[source,console]
----
$ kamel config --list --default-namespace some-name
The configuration file is read from /some/path/kamel-config.yaml
kamel:
config:
default-namespace: some-name
install:
health-port: 8081
monitoring-port: 8082
----
71 changes: 71 additions & 0 deletions e2e/namespace/install/cli/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build integration
// +build integration

// To enable compilation of this file in Goland, go to "Settings -> Go -> Vendoring & Build Tags -> Custom Tags" and add "integration"

/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 common

import (
"os"
"strings"
"testing"

corev1 "k8s.io/api/core/v1"

. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"

. "github.com/apache/camel-k/e2e/support"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/cmd"
)

func TestKamelCLIConfig(t *testing.T) {
WithNewTestNamespace(t, func(ns string) {
operatorID := "camel-k-cli-config"
Expect(KamelInstallWithID(operatorID, ns).Execute()).To(Succeed())

t.Run("check default namespace", func(t *testing.T) {
_, err := os.Stat(cmd.DefaultConfigLocation)
assert.True(t, os.IsNotExist(err), "No file at "+cmd.DefaultConfigLocation+" was expected")
t.Cleanup(func() { os.Remove(cmd.DefaultConfigLocation) })
Expect(Kamel("config", "--default-namespace", ns).Execute()).To(Succeed())
_, err = os.Stat(cmd.DefaultConfigLocation)
assert.Nil(t, err, "A file at "+cmd.DefaultConfigLocation+" was expected")
Expect(Kamel("run", "--operator-id", operatorID, "files/yaml.yaml").Execute()).To(Succeed())

Eventually(IntegrationPodPhase(ns, "yaml"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
essobedo marked this conversation as resolved.
Show resolved Hide resolved
Eventually(IntegrationConditionStatus(ns, "yaml", v1.IntegrationConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, "yaml"), TestTimeoutShort).Should(ContainSubstring("Magicstring!"))

// first line of the integration logs
logs := strings.Split(IntegrationLogs(ns, "yaml")(), "\n")[0]
podName := IntegrationPod(ns, "yaml")().Name

logsCLI := GetOutputStringAsync(Kamel("log", "yaml"))
Eventually(logsCLI).Should(ContainSubstring("Monitoring pod " + podName))
Eventually(logsCLI).Should(ContainSubstring(logs))
squakez marked this conversation as resolved.
Show resolved Hide resolved

// Clean up
Expect(Kamel("delete", "--all").Execute()).To(Succeed())
})
})
}
164 changes: 164 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 cmd

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

// ConfigFolder defines the different types of folder containing the configuration file.
type ConfigFolder string

const (
// The path of the folder containing the configuration file is retrieved from the environment
// variable KAMEL_CONFIG_PATH.
ConfigFolderEnvVar ConfigFolder = "env"
// The path of the folder containing the configuration file is $HOME/.kamel.
ConfigFolderHome ConfigFolder = "home"
// The folder containing the configuration file is .kamel located in the working directory.
ConfigFolderSubDirectory ConfigFolder = "sub"
// The folder containing the configuration file is the working directory.
ConfigFolderWorking ConfigFolder = "working"
// The folder containing the configuration file is the directory currently used by Kamel.
ConfigFolderUsed ConfigFolder = "used"
)

// nolint: unparam
func newCmdConfig(rootCmdOptions *RootCmdOptions) (*cobra.Command, *configCmdOptions) {
options := configCmdOptions{}
cmd := cobra.Command{
Use: "config",
Short: "Configure the default settings",
PreRunE: decode(&options),
Args: options.validateArgs,
RunE: options.run,
}

cmd.Flags().String("folder", "used", "The type of folder containing the configuration file to read/write. The supported values are 'env', 'home', 'sub', 'working' and 'used' for respectively $KAMEL_CONFIG_PATH, $HOME/.kamel, .kamel, . and the folder used by kamel")
cmd.Flags().String("default-namespace", "", "The name of the namespace to use by default")
cmd.Flags().BoolP("list", "l", false, "List all existing settings")
return &cmd, &options
}

type configCmdOptions struct {
DefaultNamespace string `mapstructure:"default-namespace"`
}

func (o *configCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.New("no arguments are expected")
}
return nil
}

func (o *configCmdOptions) run(cmd *cobra.Command, args []string) error {
path, err := getConfigLocation(cmd)
if err != nil {
return err
}
if cmd.Flags().Lookup("default-namespace").Changed {
err = o.saveConfiguration(cmd, path)
if err != nil {
return err
}
}
if cmd.Flags().Lookup("list").Changed {
err = printConfiguration(cmd, path)
if err != nil {
return err
}
}
return nil
}

// Save the configuration at the given location.
func (o *configCmdOptions) saveConfiguration(cmd *cobra.Command, path string) error {
cfg, err := LoadConfigurationFrom(path)
if err != nil {
return err
}

cfg.Update(cmd, pathToRoot(cmd), o, true)

err = cfg.Save()
if err != nil {
return err
}
return nil
}

// Gives the location of the configuration file.
func getConfigLocation(cmd *cobra.Command) (string, error) {
var folder ConfigFolder
if s, err := cmd.Flags().GetString("folder"); err == nil {
folder = ConfigFolder(s)
} else {
return "", err
}
var path string
switch folder {
case ConfigFolderUsed:
path = viper.ConfigFileUsed()
if path != "" {
return path, nil
}
case ConfigFolderEnvVar:
path = os.Getenv("KAMEL_CONFIG_PATH")
case ConfigFolderHome:
home, err := os.UserHomeDir()
cobra.CheckErr(err)
path = filepath.Join(home, ".kamel")
case ConfigFolderSubDirectory:
path = ".kamel"
case ConfigFolderWorking:
path = "."
default:
return "", fmt.Errorf("unsupported type of folder: %s", folder)
}
configName := os.Getenv("KAMEL_CONFIG_NAME")
if configName == "" {
configName = DefaultConfigName
}
return filepath.Join(path, fmt.Sprintf("%s.yaml", configName)), nil
}

// Print the content of the configuration file located at the given path.
func printConfiguration(cmd *cobra.Command, path string) error {
cfg, err := LoadConfigurationFrom(path)
if err != nil {
return err
}
if len(cfg.content) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "No settings could be found in %s\n", cfg.location)
} else {
bs, err := yaml.Marshal(cfg.content)
if err != nil {
return fmt.Errorf("unable to marshal config to YAML: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "The configuration file is read from %s\n", cfg.location)
fmt.Fprintln(cmd.OutOrStdout(), string(bs))
}
return nil
}
Loading