mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Add add a utility for merging JSON fragments, and use it in run-container.
This commit is contained in:
parent
224ffa4567
commit
4209bd4888
@ -885,7 +885,7 @@ Examples:
|
|||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
```
|
```
|
||||||
kubectl run-container <name> --image=<image> [--replicas=replicas] [--dry-run=<bool>] [flags]
|
kubectl run-container <name> --image=<image> [--replicas=replicas] [--dry-run=<bool>] [--overrides=<inline-json>] [flags]
|
||||||
|
|
||||||
Available Flags:
|
Available Flags:
|
||||||
--alsologtostderr=false: log to standard error as well as files
|
--alsologtostderr=false: log to standard error as well as files
|
||||||
@ -913,6 +913,7 @@ Usage:
|
|||||||
--ns-path="/home/username/.kubernetes_ns": Path to the namespace info file that holds the namespace context to use for CLI requests.
|
--ns-path="/home/username/.kubernetes_ns": Path to the namespace info file that holds the namespace context to use for CLI requests.
|
||||||
-o, --output="": Output format: json|yaml|template|templatefile
|
-o, --output="": Output format: json|yaml|template|templatefile
|
||||||
--output-version="": Output the formatted object with the given version (default api-version)
|
--output-version="": Output the formatted object with the given version (default api-version)
|
||||||
|
--overrides="": An inline JSON override for the generated object. If this is non-empty, it is parsed used to override the generated object
|
||||||
-r, --replicas=1: Number of replicas to create for this container. Default 1
|
-r, --replicas=1: Number of replicas to create for this container. Default 1
|
||||||
-s, --server="": The address of the Kubernetes API server
|
-s, --server="": The address of the Kubernetes API server
|
||||||
--stderrthreshold=2: logs at or above this threshold go to stderr
|
--stderrthreshold=2: logs at or above this threshold go to stderr
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -26,7 +27,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
"github.com/imdario/mergo"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -171,3 +175,37 @@ func ReadConfigDataFromLocation(location string) ([]byte, error) {
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Merge(dst runtime.Object, fragment, kind string) error {
|
||||||
|
// Ok, this is a little hairy, we'd rather not force the user to specify a kind for their JSON
|
||||||
|
// So we pull it into a map, add the Kind field, and then reserialize.
|
||||||
|
// We also pull the apiVersion for proper parsing
|
||||||
|
var intermediate interface{}
|
||||||
|
if err := json.Unmarshal([]byte(fragment), &intermediate); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dataMap, ok := intermediate.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Expected a map, found something else: %s", fragment)
|
||||||
|
}
|
||||||
|
version, found := dataMap["apiVersion"]
|
||||||
|
if !found {
|
||||||
|
return fmt.Errorf("Inline JSON requires an apiVersion field")
|
||||||
|
}
|
||||||
|
versionString, ok := version.(string)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("apiVersion must be a string")
|
||||||
|
}
|
||||||
|
codec := runtime.CodecFor(api.Scheme, versionString)
|
||||||
|
|
||||||
|
intermediate.(map[string]interface{})["kind"] = kind
|
||||||
|
data, err := json.Marshal(intermediate)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
src, err := codec.Decode(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return mergo.Merge(dst, src)
|
||||||
|
}
|
||||||
|
108
pkg/kubectl/cmd/helpers_test.go
Normal file
108
pkg/kubectl/cmd/helpers_test.go
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
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 cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMerge(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
obj runtime.Object
|
||||||
|
fragment string
|
||||||
|
expected runtime.Object
|
||||||
|
expectErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
obj: &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fragment: "{ \"apiVersion\": \"v1beta1\" }",
|
||||||
|
expected: &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fragment: "{ \"apiVersion\": \"v1beta1\", \"id\": \"baz\", \"desiredState\": { \"host\": \"bar\" } }",
|
||||||
|
expected: &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "baz",
|
||||||
|
},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Host: "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fragment: "{ \"apiVersion\": \"v1beta3\", \"spec\": { \"volumes\": [ {\"name\": \"v1\"}, {\"name\": \"v2\"} ] } }",
|
||||||
|
expected: &api.Pod{
|
||||||
|
ObjectMeta: api.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
},
|
||||||
|
Spec: api.PodSpec{
|
||||||
|
Volumes: []api.Volume{
|
||||||
|
{
|
||||||
|
Name: "v1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "v2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
obj: &api.Pod{},
|
||||||
|
fragment: "invalid json",
|
||||||
|
expected: &api.Pod{},
|
||||||
|
expectErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
err := Merge(test.obj, test.fragment, "Pod")
|
||||||
|
if !test.expectErr {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
} else if !reflect.DeepEqual(test.obj, test.expected) {
|
||||||
|
t.Errorf("\nexpected:\n%v\nsaw:\n%v", test.expected, test.obj)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if test.expectErr && err == nil {
|
||||||
|
t.Errorf("unexpected non-error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -27,7 +27,7 @@ import (
|
|||||||
|
|
||||||
func (f *Factory) NewCmdRunContainer(out io.Writer) *cobra.Command {
|
func (f *Factory) NewCmdRunContainer(out io.Writer) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "run-container <name> --image=<image> [--replicas=replicas] [--dry-run=<bool>]",
|
Use: "run-container <name> --image=<image> [--replicas=replicas] [--dry-run=<bool>] [--overrides=<inline-json>]",
|
||||||
Short: "Run a particular image on the cluster.",
|
Short: "Run a particular image on the cluster.",
|
||||||
Long: `Create and run a particular image, possibly replicated.
|
Long: `Create and run a particular image, possibly replicated.
|
||||||
Creates a replication controller to manage the created container(s)
|
Creates a replication controller to manage the created container(s)
|
||||||
@ -65,6 +65,11 @@ Examples:
|
|||||||
controller, err := generator.Generate(params)
|
controller, err := generator.Generate(params)
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
|
|
||||||
|
inline := GetFlagString(cmd, "overrides")
|
||||||
|
if len(inline) > 0 {
|
||||||
|
Merge(controller, inline, "ReplicationController")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: extract this flag to a central location, when such a location exists.
|
// TODO: extract this flag to a central location, when such a location exists.
|
||||||
if !GetFlagBool(cmd, "dry-run") {
|
if !GetFlagBool(cmd, "dry-run") {
|
||||||
controller, err = client.ReplicationControllers(namespace).Create(controller.(*api.ReplicationController))
|
controller, err = client.ReplicationControllers(namespace).Create(controller.(*api.ReplicationController))
|
||||||
@ -79,5 +84,6 @@ Examples:
|
|||||||
cmd.Flags().IntP("replicas", "r", 1, "Number of replicas to create for this container. Default 1")
|
cmd.Flags().IntP("replicas", "r", 1, "Number of replicas to create for this container. Default 1")
|
||||||
cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, don't actually do anything")
|
cmd.Flags().Bool("dry-run", false, "If true, only print the object that would be sent, don't actually do anything")
|
||||||
cmd.Flags().StringP("labels", "l", "", "Labels to apply to the pod(s) created by this call to run.")
|
cmd.Flags().StringP("labels", "l", "", "Labels to apply to the pod(s) created by this call to run.")
|
||||||
|
cmd.Flags().String("overrides", "", "An inline JSON override for the generated object. If this is non-empty, it is parsed used to override the generated object")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user