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

Add support for multi-containers #1382

Merged
merged 8 commits into from
Aug 9, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
| Adding base64 data handling to ping update command
| https://github.com/knative/client/pull/1392[#1392], https://github.com/knative/client/pull/1388[#1388]

| 🎁
| Add support for multiple containers in Service spec
| https://github.com/knative/client/pull/1382[#1382]

| ✨
| make --cmd flag as an array instead of string
| https://github.com/knative/client/pull/1380[#1380]
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 container](kn_container.md) - Manage service's containers (experimental)
* [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
Expand Down
29 changes: 29 additions & 0 deletions docs/cmd/kn_container.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## kn container

Manage service's containers (experimental)

```
kn container COMMAND
```

### Options

```
-h, --help help for container
```

### 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 container add](kn_container_add.md) - Add a container

61 changes: 61 additions & 0 deletions docs/cmd/kn_container_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## kn container add

Add a container

```
kn container add NAME
```

### Examples

```

The command is experimental and may change in the future releases.

The 'container add' represents utility command that prints YAML container spec to standard output. It's useful for
dsimansk marked this conversation as resolved.
Show resolved Hide resolved
multi-container use cases to create definition with help of standard 'kn' option flags. It accepts all container related
flag available for 'service create'. The command can be chained through Unix pipes to create multiple containers at once.

# Add a container 'sidecar' from image 'docker.io/example/sidecar' and print it to standard output
kn container add sidecar --image docker.io/example/sidecar

# Add command can be chained by standard Unix pipe symbol '|' and passed to 'service add|update|apply' commands
kn container add sidecar --image docker.io/example/sidecar:first | \
kn container add second --image docker.io/example/sidecar:second | \
kn service create myksvc --image docker.io/example/my-app:latest --extra-containers -
```
rhuss marked this conversation as resolved.
Show resolved Hide resolved

### Options

```
--arg stringArray Add argument to the container command. Example: --arg myArg1 --arg --myArg2 --arg myArg3=3. You can use this flag multiple times.
--cmd stringArray Specify command to be used as entrypoint instead of default one. Example: --cmd /app/start or --cmd sh --cmd /app/start.sh or --cmd /app/start --arg myArg to pass additional arguments.
-e, --env stringArray Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
--env-from stringArray Add environment variables from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret:). Example: --env-from cm:myconfigmap or --env-from secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --env-from cm:myconfigmap-.
--env-value-from stringArray Add environment variable from a value of key in ConfigMap (prefix cm: or config-map:) or a Secret (prefix sc: or secret:). Example: --env-value-from NAME=cm:myconfigmap:key or --env-value-from NAME=secret:mysecret:key. You can use this flag multiple times. To unset a value from a ConfigMap/Secret key reference, append "-" to the key, e.g. --env-value-from ENV-.
--extra-containers string Specify path to file including definition for additional containers, alternatively use '-' to read from stdin. Example: --extra-containers ./containers.yaml or --extra-containers -.
-h, --help help for add
--image string Image to run.
--limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'.
--mount stringArray Mount a ConfigMap (prefix cm: or config-map:), a Secret (prefix secret: or sc:), or an existing Volume (without any prefix) on the specified directory. Example: --mount /mydir=cm:myconfigmap, --mount /mydir=secret:mysecret, or --mount /mydir=myvolume. When a configmap or a secret is specified, a corresponding volume is automatically generated. You can use this flag multiple times. For unmounting a directory, append "-", e.g. --mount /mydir-, which also removes any auto-generated volume.
-p, --port string The port where application listens on, in the format 'NAME:PORT', where 'NAME' is optional. Examples: '--port h2c:8080' , '--port 8080'.
--pull-secret string Image pull secret to set. An empty argument ("") clears the pull secret. The referenced secret must exist in the service's namespace.
--request strings The resource requirement requests for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource request, append "-" to the resource name, e.g. '--request cpu-'.
--service-account string Service account name to set. An empty argument ("") clears the service account. The referenced service account must exist in the service's namespace.
--user int The user ID to run the container (e.g., 1001).
```

### Options inherited from parent commands

```
--cluster string name of the kubeconfig cluster to use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably out of scope for this PR, but cluster/config configuration doesn't make any sense for this offline-online command (and might even confuse users)

--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 container](kn_container.md) - Manage service's containers (experimental)

1 change: 1 addition & 0 deletions docs/cmd/kn_service_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ kn service apply s0 --filename my-svc.yml
-e, --env stringArray Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
--env-from stringArray Add environment variables from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret:). Example: --env-from cm:myconfigmap or --env-from secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --env-from cm:myconfigmap-.
--env-value-from stringArray Add environment variable from a value of key in ConfigMap (prefix cm: or config-map:) or a Secret (prefix sc: or secret:). Example: --env-value-from NAME=cm:myconfigmap:key or --env-value-from NAME=secret:mysecret:key. You can use this flag multiple times. To unset a value from a ConfigMap/Secret key reference, append "-" to the key, e.g. --env-value-from ENV-.
--extra-containers string Specify path to file including definition for additional containers, alternatively use '-' to read from stdin. Example: --extra-containers ./containers.yaml or --extra-containers -.
-f, --filename string Create a service from file. The created service can be further modified by combining with other options. For example, -f /path/to/file --env NAME=value adds also an environment variable.
--force Create service forcefully, replaces existing service if any.
-h, --help help for apply
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ kn service create NAME --image IMAGE
-e, --env stringArray Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
--env-from stringArray Add environment variables from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret:). Example: --env-from cm:myconfigmap or --env-from secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --env-from cm:myconfigmap-.
--env-value-from stringArray Add environment variable from a value of key in ConfigMap (prefix cm: or config-map:) or a Secret (prefix sc: or secret:). Example: --env-value-from NAME=cm:myconfigmap:key or --env-value-from NAME=secret:mysecret:key. You can use this flag multiple times. To unset a value from a ConfigMap/Secret key reference, append "-" to the key, e.g. --env-value-from ENV-.
--extra-containers string Specify path to file including definition for additional containers, alternatively use '-' to read from stdin. Example: --extra-containers ./containers.yaml or --extra-containers -.
-f, --filename string Create a service from file. The created service can be further modified by combining with other options. For example, -f /path/to/file --env NAME=value adds also an environment variable.
--force Create service forcefully, replaces existing service if any.
-h, --help help for create
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ kn service update NAME
-e, --env stringArray Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
--env-from stringArray Add environment variables from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret:). Example: --env-from cm:myconfigmap or --env-from secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --env-from cm:myconfigmap-.
--env-value-from stringArray Add environment variable from a value of key in ConfigMap (prefix cm: or config-map:) or a Secret (prefix sc: or secret:). Example: --env-value-from NAME=cm:myconfigmap:key or --env-value-from NAME=secret:mysecret:key. You can use this flag multiple times. To unset a value from a ConfigMap/Secret key reference, append "-" to the key, e.g. --env-value-from ENV-.
--extra-containers string Specify path to file including definition for additional containers, alternatively use '-' to read from stdin. Example: --extra-containers ./containers.yaml or --extra-containers -.
-h, --help help for update
--image string Image to run.
-l, --label stringArray Labels to set for both Service and Revision. name=value; you may provide this flag any number of times to set multiple labels. To unset, specify the label name followed by a "-" (e.g., name-).
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_source_container_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ kn source container create NAME --image IMAGE --sink SINK
-e, --env stringArray Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
--env-from stringArray Add environment variables from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret:). Example: --env-from cm:myconfigmap or --env-from secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --env-from cm:myconfigmap-.
--env-value-from stringArray Add environment variable from a value of key in ConfigMap (prefix cm: or config-map:) or a Secret (prefix sc: or secret:). Example: --env-value-from NAME=cm:myconfigmap:key or --env-value-from NAME=secret:mysecret:key. You can use this flag multiple times. To unset a value from a ConfigMap/Secret key reference, append "-" to the key, e.g. --env-value-from ENV-.
--extra-containers string Specify path to file including definition for additional containers, alternatively use '-' to read from stdin. Example: --extra-containers ./containers.yaml or --extra-containers -.
-h, --help help for create
--image string Image to run.
--limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'.
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_source_container_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ kn source container update NAME --image IMAGE
-e, --env stringArray Environment variable to set. NAME=value; you may provide this flag any number of times to set multiple environment variables. To unset, specify the environment variable name followed by a "-" (e.g., NAME-).
--env-from stringArray Add environment variables from a ConfigMap (prefix cm: or config-map:) or a Secret (prefix secret:). Example: --env-from cm:myconfigmap or --env-from secret:mysecret. You can use this flag multiple times. To unset a ConfigMap/Secret reference, append "-" to the name, e.g. --env-from cm:myconfigmap-.
--env-value-from stringArray Add environment variable from a value of key in ConfigMap (prefix cm: or config-map:) or a Secret (prefix sc: or secret:). Example: --env-value-from NAME=cm:myconfigmap:key or --env-value-from NAME=secret:mysecret:key. You can use this flag multiple times. To unset a value from a ConfigMap/Secret key reference, append "-" to the key, e.g. --env-value-from ENV-.
--extra-containers string Specify path to file including definition for additional containers, alternatively use '-' to read from stdin. Example: --extra-containers ./containers.yaml or --extra-containers -.
-h, --help help for update
--image string Image to run.
--limit strings The resource requirement limits for this Service. For example, 'cpu=100m,memory=256Mi'. You can use this flag multiple times. To unset a resource limit, append "-" to the resource name, e.g. '--limit memory-'.
Expand Down
2 changes: 2 additions & 0 deletions hack/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ import (
_ "k8s.io/code-generator"
_ "k8s.io/code-generator/cmd/deepcopy-gen"
_ "knative.dev/serving/test/test_images/grpc-ping"
_ "knative.dev/serving/test/test_images/multicontainer/servingcontainer"
_ "knative.dev/serving/test/test_images/multicontainer/sidecarcontainer"
rhuss marked this conversation as resolved.
Show resolved Hide resolved
)
91 changes: 91 additions & 0 deletions pkg/kn/commands/container/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 container

import (
"errors"
"os"

"github.com/spf13/cobra"

corev1 "k8s.io/api/core/v1"
"knative.dev/client/pkg/kn/commands"
knflags "knative.dev/client/pkg/kn/flags"
sigyaml "sigs.k8s.io/yaml"
)

// NewContainerAddCommand to create event channels
func NewContainerAddCommand(p *commands.KnParams) *cobra.Command {
var podSpecFlags knflags.PodSpecFlags
cmd := &cobra.Command{
Use: "add NAME",
Short: "Add a container",
Example: `
The command is experimental and may change in the future releases.

The 'container add' represents utility command that prints YAML container spec to standard output. It's useful for
multi-container use cases to create definition with help of standard 'kn' option flags. It accepts all container related
flag available for 'service create'. The command can be chained through Unix pipes to create multiple containers at once.

# Add a container 'sidecar' from image 'docker.io/example/sidecar' and print it to standard output
kn container add sidecar --image docker.io/example/sidecar

# Add command can be chained by standard Unix pipe symbol '|' and passed to 'service add|update|apply' commands
kn container add sidecar --image docker.io/example/sidecar:first | \
kn container add second --image docker.io/example/sidecar:second | \
kn service create myksvc --image docker.io/example/my-app:latest --extra-containers -`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("'container add' requires the container name given as single argument")
}
name := args[0]
if podSpecFlags.Image == "" {
return errors.New("'container add' requires the image name to run provided with the --image option")
}

// Detect pipe input from previous container command
if IsPipeInput(os.Stdin) {
podSpecFlags.ExtraContainers = "-"
}
podSpec := &corev1.PodSpec{}
if err = podSpecFlags.ResolvePodSpec(podSpec, cmd.Flags(), os.Args); err != nil {
return err
}
// Add container's name to current one
podSpec.Containers[0].Name = name

b, err := sigyaml.Marshal(podSpec)
if err != nil {
return err
}
_, err = cmd.OutOrStdout().Write(b)
return err
},
}
podSpecFlags.AddFlags(cmd.Flags())
// Volume is not part of ContainerSpec
cmd.Flag("volume").Hidden = true

return cmd
}

// IsPipeInput determines the input is created using unix '|' pipe
func IsPipeInput(f *os.File) bool {
fi, err := f.Stat()
if err != nil {
return false
}
return fi.Mode()&os.ModeCharDevice == 0
rhuss marked this conversation as resolved.
Show resolved Hide resolved
}
Loading