Merge pull request #93388 from SaiHarshaK/feature/run-annotate

allow adding annotations to pod when using kubectl run

Kubernetes-commit: d20c43157f3f0f377bd25d2dabc0371b3db0aa6d
This commit is contained in:
Kubernetes Publisher 2020-09-22 12:22:21 -07:00
commit 2d2bc3f80d
3 changed files with 62 additions and 4 deletions

View File

@ -173,6 +173,7 @@ func NewCmdRun(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Co
func addRunFlags(cmd *cobra.Command, opt *RunOptions) {
cmdutil.AddDryRunFlag(cmd)
cmd.Flags().StringArray("annotations", []string{}, i18n.T("Annotations to apply to the pod."))
cmd.Flags().StringVar(&opt.Generator, "generator", opt.Generator, i18n.T("The name of the API generator to use, see http://kubernetes.io/docs/user-guide/kubectl-conventions/#generators for a list."))
cmd.Flags().MarkDeprecated("generator", "has no effect and will be removed in the future.")
cmd.Flags().StringVar(&opt.Image, "image", opt.Image, i18n.T("The image for the container to run."))
@ -319,6 +320,7 @@ func (o *RunOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
params["args"] = args[1:]
}
params["annotations"] = cmdutil.GetFlagStringArray(cmd, "annotations")
params["env"] = cmdutil.GetFlagStringArray(cmd, "env")
var createdObjects = []*RunObject{}

View File

@ -21,11 +21,12 @@ import (
"strconv"
"strings"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/generate"
)
@ -87,6 +88,27 @@ func getArgs(genericParams map[string]interface{}) ([]string, error) {
return args, nil
}
// getAnnotations returns map of annotations.
func getAnnotations(genericParams map[string]interface{}) (map[string]string, error) {
annotationStrings, ok := genericParams["annotations"]
if !ok {
return nil, nil
}
annotationStringArray, ok := annotationStrings.([]string)
if !ok {
return nil, fmt.Errorf("expected []string, found: %v", annotationStrings)
}
annotations, _, err := cmdutil.ParsePairs(annotationStringArray, "annotations", false)
if err != nil {
return nil, err
}
delete(genericParams, "annotations")
return annotations, nil
}
// getEnvs returns environment variables.
func getEnvs(genericParams map[string]interface{}) ([]v1.EnvVar, error) {
var envs []v1.EnvVar
@ -213,6 +235,7 @@ type BasicPod struct{}
func (BasicPod) ParamNames() []generate.GeneratorParam {
return []generate.GeneratorParam{
{Name: "labels", Required: false},
{Name: "annotations", Required: false},
{Name: "default-name", Required: false},
{Name: "name", Required: true},
{Name: "image", Required: true},
@ -244,6 +267,11 @@ func (BasicPod) Generate(genericParams map[string]interface{}) (runtime.Object,
return nil, err
}
annotations, err := getAnnotations(genericParams)
if err != nil {
return nil, err
}
params, err := getParams(genericParams)
if err != nil {
return nil, err
@ -296,8 +324,9 @@ func (BasicPod) Generate(genericParams map[string]interface{}) (runtime.Object,
pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: labels,
Name: name,
Labels: labels,
Annotations: annotations,
},
Spec: v1.PodSpec{
ServiceAccountName: params["serviceaccount"],

View File

@ -20,7 +20,7 @@ import (
"reflect"
"testing"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
@ -280,6 +280,33 @@ func TestGeneratePod(t *testing.T) {
},
},
},
{
name: "test11: check annotations",
params: map[string]interface{}{
"name": "foo",
"image": "someimage",
"replicas": "1",
"labels": "foo=bar,baz=blah",
"annotations": []string{"foo=bar1", "baz=blah1"},
},
expected: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{"foo": "bar", "baz": "blah"},
Annotations: map[string]string{"foo": "bar1", "baz": "blah1"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "foo",
Image: "someimage",
},
},
DNSPolicy: v1.DNSClusterFirst,
RestartPolicy: v1.RestartPolicyAlways,
},
},
},
}
generator := BasicPod{}
for _, tt := range tests {