Added --privileged flag to kubectl run

This commit is contained in:
Brian Pursley 2020-04-28 16:30:59 -04:00
parent 0f2cccc98c
commit cd005c1da4
3 changed files with 54 additions and 6 deletions

View File

@ -120,6 +120,7 @@ type RunOptions struct {
Interactive bool Interactive bool
LeaveStdinOpen bool LeaveStdinOpen bool
Port string Port string
Privileged bool
Quiet bool Quiet bool
Schedule string Schedule string
TTY bool TTY bool
@ -199,6 +200,7 @@ func addRunFlags(cmd *cobra.Command, opt *RunOptions) {
cmd.Flags().BoolVar(&opt.Quiet, "quiet", opt.Quiet, "If true, suppress prompt messages.") cmd.Flags().BoolVar(&opt.Quiet, "quiet", opt.Quiet, "If true, suppress prompt messages.")
cmd.Flags().StringVar(&opt.Schedule, "schedule", opt.Schedule, i18n.T("A schedule in the Cron format the job should be run with.")) cmd.Flags().StringVar(&opt.Schedule, "schedule", opt.Schedule, i18n.T("A schedule in the Cron format the job should be run with."))
cmd.Flags().MarkDeprecated("schedule", "has no effect and will be removed in the future.") cmd.Flags().MarkDeprecated("schedule", "has no effect and will be removed in the future.")
cmd.Flags().BoolVar(&opt.Privileged, "privileged", opt.Privileged, i18n.T("If true, run the container in privileged mode."))
cmdutil.AddFieldManagerFlagVar(cmd, &opt.fieldManager, "kubectl-run") cmdutil.AddFieldManagerFlagVar(cmd, &opt.fieldManager, "kubectl-run")
} }

View File

@ -229,6 +229,7 @@ func (BasicPod) ParamNames() []generate.GeneratorParam {
{Name: "requests", Required: false}, {Name: "requests", Required: false},
{Name: "limits", Required: false}, {Name: "limits", Required: false},
{Name: "serviceaccount", Required: false}, {Name: "serviceaccount", Required: false},
{Name: "privileged", Required: false},
} }
} }
@ -281,6 +282,18 @@ func (BasicPod) Generate(genericParams map[string]interface{}) (runtime.Object,
if len(restartPolicy) == 0 { if len(restartPolicy) == 0 {
restartPolicy = v1.RestartPolicyAlways restartPolicy = v1.RestartPolicyAlways
} }
privileged, err := generate.GetBool(params, "privileged", false)
if err != nil {
return nil, err
}
var securityContext *v1.SecurityContext
if privileged {
securityContext = &v1.SecurityContext{
Privileged: &privileged,
}
}
pod := v1.Pod{ pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
@ -296,6 +309,7 @@ func (BasicPod) Generate(genericParams map[string]interface{}) (runtime.Object,
StdinOnce: !leaveStdinOpen && stdin, StdinOnce: !leaveStdinOpen && stdin,
TTY: tty, TTY: tty,
Resources: resourceRequirements, Resources: resourceRequirements,
SecurityContext: securityContext,
}, },
}, },
DNSPolicy: v1.DNSClusterFirst, DNSPolicy: v1.DNSClusterFirst,

View File

@ -254,6 +254,32 @@ func TestGeneratePod(t *testing.T) {
}, },
}, },
}, },
{
name: "test10: privileged mode",
params: map[string]interface{}{
"name": "foo",
"image": "someimage",
"replicas": "1",
"privileged": "true",
},
expected: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{"run": "foo"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "foo",
Image: "someimage",
SecurityContext: securityContextWithPrivilege(true),
},
},
DNSPolicy: v1.DNSClusterFirst,
RestartPolicy: v1.RestartPolicyAlways,
},
},
},
} }
generator := BasicPod{} generator := BasicPod{}
for _, tt := range tests { for _, tt := range tests {
@ -358,3 +384,9 @@ func TestParseEnv(t *testing.T) {
}) })
} }
} }
func securityContextWithPrivilege(privileged bool) *v1.SecurityContext {
return &v1.SecurityContext{
Privileged: &privileged,
}
}