Skip to content

Commit

Permalink
Set namespace when using kubectl create service
Browse files Browse the repository at this point in the history
  • Loading branch information
zxh326 authored and eddiezane committed May 6, 2021
1 parent fe7d806 commit 0f2adad
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions staging/src/k8s.io/kubectl/pkg/cmd/create/create_service.go
Expand Up @@ -72,6 +72,7 @@ type ServiceOptions struct {
FieldManager string
CreateAnnotation bool
Namespace string
EnforceNamespace bool

Client corev1client.CoreV1Interface
DryRunStrategy cmdutil.DryRunStrategy
Expand Down Expand Up @@ -105,7 +106,7 @@ func (o *ServiceOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
return err
}

o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}
Expand Down Expand Up @@ -177,13 +178,19 @@ func (o *ServiceOptions) createService() (*corev1.Service, error) {
selector := map[string]string{}
selector["app"] = o.Name

namespace := ""
if o.EnforceNamespace {
namespace = o.Namespace
}

service := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: o.Name,
Labels: labels,
Name: o.Name,
Labels: labels,
Namespace: namespace,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceType(o.Type),
Type: o.Type,
Selector: selector,
Ports: ports,
ExternalName: o.ExternalName,
Expand Down

0 comments on commit 0f2adad

Please sign in to comment.