Skip to content

Commit

Permalink
feat(cmd/run): modeline resource support
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Jun 16, 2021
1 parent df1c0a1 commit 4dca135
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cli/modeline.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The following is a partial list of useful options:
|Add a build time property or properties file (syntax: _[my-key=my-value\|file:/path/to/my-conf.properties]_

|config
|Add runtime configuration from a Configmap, a Secret or a file (syntax: _[configmap\|secret\|file]:name)_
|Add a runtime configuration from a Configmap, a Secret or a file (syntax: _[configmap\|secret\|file]:name)_

|dependency
|An external library that should be included, e.g. for Maven dependencies `dependency=mvn:org.my:app:1.0`
Expand All @@ -94,7 +94,7 @@ The following is a partial list of useful options:
|Add a runtime property or properties file (syntax: _[my-key=my-value\|file:/path/to/my-conf.properties]_)

|resource
|Add a resource
|Add a runtime resource from a Configmap, a Secret or a file (syntax: _[configmap\|secret\|file]:name)_

|trait
|Configure a trait, e.g. `trait=service.enabled=false`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// To run this integrations use:
//
// kubectl create secret generic my-sec --from-literal=my-secret-key="very top secret"
// kamel run secret-route.groovy --dev
// kamel run modeline-config-secret-route.groovy --dev
//

// camel-k: config=secret:my-sec
Expand Down
27 changes: 27 additions & 0 deletions examples/modeline/modeline-resource-file-route.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// camel-k: language=groovy
/*
* 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.
*/

//
// To run this integrations use:
// kamel run modeline-resource-file-route.groovy --dev
//

// camel-k: resource=file:resources-data.txt

from('file:/etc/camel/data/resources/?fileName=resources-data.txt&noop=true&idempotent=false')
.log('resource file content is: ${body}')
2 changes: 1 addition & 1 deletion pkg/cmd/modeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ var (

// file options must be considered relative to the source files they belong to
fileOptions = map[string]bool{
"resource": true,
"kube-config": true,
"open-api": true,
"property-file": true,
}

// file format options are those options that admit multiple values, not only files (ie, key=value|configmap|secret|file syntax)
fileFormatOptions = map[string]bool{
"resource": true,
"config": true,
"property": true,
"build-property": true,
Expand Down
73 changes: 73 additions & 0 deletions pkg/cmd/modeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,79 @@ func TestModelineRunConfigFile(t *testing.T) {
assert.Equal(t, []string{"run", fileName, fmt.Sprintf("--config=file:%s", propFileName)}, flags)
}

func TestModelineRunResourceConfigmap(t *testing.T) {
dir, err := ioutil.TempDir("", "camel-k-test-")
assert.NoError(t, err)
defer os.RemoveAll(dir)

subDir := path.Join(dir, "sub")
err = os.Mkdir(subDir, 0777)
assert.NoError(t, err)

file := `
// camel-k: resource=configmap:my-cm
`
fileName := path.Join(subDir, "simple.groovy")
err = ioutil.WriteFile(fileName, []byte(file), 0777)
assert.NoError(t, err)

cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), []string{"kamel", "run", fileName})
assert.NoError(t, err)
assert.NotNil(t, cmd)
assert.Equal(t, []string{"run", fileName, "--resource=configmap:my-cm"}, flags)
}

func TestModelineRunResourceSecret(t *testing.T) {
dir, err := ioutil.TempDir("", "camel-k-test-")
assert.NoError(t, err)
defer os.RemoveAll(dir)

subDir := path.Join(dir, "sub")
err = os.Mkdir(subDir, 0777)
assert.NoError(t, err)

file := `
// camel-k: resource=secret:my-secret
`
fileName := path.Join(subDir, "simple.groovy")
err = ioutil.WriteFile(fileName, []byte(file), 0777)
assert.NoError(t, err)

cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), []string{"kamel", "run", fileName})
assert.NoError(t, err)
assert.NotNil(t, cmd)
assert.Equal(t, []string{"run", fileName, "--resource=secret:my-secret"}, flags)
}

func TestModelineRunResourceFile(t *testing.T) {
dir, err := ioutil.TempDir("", "camel-k-test-")
assert.NoError(t, err)
defer os.RemoveAll(dir)

subDir := path.Join(dir, "sub")
err = os.Mkdir(subDir, 0777)
assert.NoError(t, err)

file := `
// camel-k: resource=file:application.properties
`
fileName := path.Join(subDir, "simple.groovy")
err = ioutil.WriteFile(fileName, []byte(file), 0777)
assert.NoError(t, err)

propFile := `
a=b
`
propFileName := path.Join(subDir, "application.properties")
err = ioutil.WriteFile(propFileName, []byte(propFile), 0777)
assert.NoError(t, err)

cmd, flags, err := NewKamelWithModelineCommand(context.TODO(), []string{"kamel", "run", fileName})
assert.NoError(t, err)
assert.NotNil(t, cmd)
assert.Equal(t, []string{"run", fileName, fmt.Sprintf("--resource=file:%s", propFileName)}, flags)
}

func TestModelineInspectSimple(t *testing.T) {
dir, err := ioutil.TempDir("", "camel-k-test-")
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions)
cmd.Flags().StringP("kit", "k", "", "The kit used to run the integration")
cmd.Flags().StringArrayP("property", "p", nil, "Add a runtime property or properties file (syntax: [my-key=my-value|file:/path/to/my-conf.properties])")
cmd.Flags().StringArray("build-property", nil, "Add a build time property or properties file (syntax: [my-key=my-value|file:/path/to/my-conf.properties])")
cmd.Flags().StringArray("config", nil, "Add runtime configuration from a Configmap, a Secret or a file (syntax: [configmap|secret|file]:name)")
cmd.Flags().StringArray("resource", nil, "Add runtime resource from a Configmap, a Secret or a file (syntax: [configmap|secret|file]:name)")
cmd.Flags().StringArray("config", nil, "Add a runtime configuration from a Configmap, a Secret or a file (syntax: [configmap|secret|file]:name)")
cmd.Flags().StringArray("resource", nil, "Add a runtime resource from a Configmap, a Secret or a file (syntax: [configmap|secret|file]:name)")
cmd.Flags().StringArray("configmap", nil, "[Deprecated] Add a ConfigMap")
cmd.Flags().StringArray("secret", nil, "[Deprecated] Add a Secret")
cmd.Flags().StringArray("maven-repository", nil, "Add a maven repository")
Expand Down

0 comments on commit 4dca135

Please sign in to comment.