add namespace param for fn and env (#2556)

This PR moves fission CLI as closer as possible to kubectl command behaviour.  We have improved namespace handling behaviour across CLI.

* add namespace param for fn and env
* use common fn for ns check
* update validation
* default namespace for httpTrigger, env and package, config and triggers
* use default ns
* add namespace filter to spec
* add forceNamespace flag
* set current namespace
* add default namespace in config
* add namespace specific destroy
* add all namespace in the list of resources
* add namespace as global tag
* use %s instead of %v
* add test cases for namespace
* use ns in get all functions
This commit is contained in:
neha_gupta
2022-10-11 13:23:47 +05:30
committed by GitHub
parent 0739aca920
commit b9513868ed
74 changed files with 1289 additions and 304 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ func Commands() *cobra.Command {
RunE: wrapper.Wrapper(List),
}
wrapper.SetFlags(listCmd, flag.FlagSet{
Optional: []flag.Flag{flag.NamespaceEnvironment},
Optional: []flag.Flag{flag.NamespaceEnvironment, flag.AllNamespaces},
})
listPodsCmd := &cobra.Command{
+26 -8
View File
@@ -63,7 +63,7 @@ func (opts *CreateSubCommand) complete(input cli.Input) error {
// run write the resource to a spec file or create a fission CRD with remote fission server.
// It also prints warning/error if necessary.
func (opts *CreateSubCommand) run(input cli.Input) error {
func (opts *CreateSubCommand) run(input cli.Input) (err error) {
m := opts.env.ObjectMeta
envList, err := opts.Client().V1().Environment().List(m.Namespace)
@@ -75,13 +75,30 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
len(envList), m.Namespace)
}
userDefinedNS, currentNS, err := util.GetResourceNamespace(input, flagkey.NamespaceEnvironment)
if err != nil {
return fv1.AggregateValidationErrors("Environment", err)
}
// we use user provided NS in spec. While creating actual record we use the current context's NS.
opts.env.ObjectMeta.Namespace = userDefinedNS
// if we're writing a spec, don't call the API
// save to spec file or display the spec to console
if input.Bool(flagkey.SpecDry) {
err = opts.env.Validate()
if err != nil {
return fv1.AggregateValidationErrors("Environment", err)
}
return spec.SpecDry(*opts.env)
}
if input.Bool(flagkey.SpecSave) {
err = opts.env.Validate()
if err != nil {
return fv1.AggregateValidationErrors("Environment", err)
}
specFile := fmt.Sprintf("env-%v.yaml", m.Name)
err = spec.SpecSave(*opts.env, specFile)
if err != nil {
@@ -90,6 +107,12 @@ func (opts *CreateSubCommand) run(input cli.Input) error {
return nil
}
opts.env.ObjectMeta.Namespace = currentNS
err = opts.env.Validate()
if err != nil {
return fv1.AggregateValidationErrors("Environment", err)
}
_, err = opts.Client().V1().Environment().Create(opts.env)
if err != nil {
return errors.Wrap(err, "error creating environment")
@@ -105,7 +128,7 @@ func createEnvironmentFromCmd(input cli.Input) (*fv1.Environment, error) {
envName := input.String(flagkey.EnvName)
envImg := input.String(flagkey.EnvImage)
envNamespace := input.String(flagkey.NamespaceEnvironment)
envBuildCmd := input.String(flagkey.EnvBuildcommand)
envExternalNetwork := input.Bool(flagkey.EnvExternalNetwork)
keepArchive := input.Bool(flagkey.EnvKeeparchive)
@@ -165,8 +188,7 @@ func createEnvironmentFromCmd(input cli.Input) (*fv1.Environment, error) {
APIVersion: fv1.CRD_VERSION,
},
ObjectMeta: metav1.ObjectMeta{
Name: envName,
Namespace: envNamespace,
Name: envName,
},
Spec: fv1.EnvironmentSpec{
Version: envVersion,
@@ -196,10 +218,6 @@ func createEnvironmentFromCmd(input cli.Input) (*fv1.Environment, error) {
if err != nil {
return nil, err
}
err = env.Validate()
if err != nil {
return nil, fv1.AggregateValidationErrors("Environment", err)
}
return env, nil
}
+11 -3
View File
@@ -24,6 +24,7 @@ import (
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
@@ -36,10 +37,17 @@ func Delete(input cli.Input) error {
return (&DeleteSubCommand{}).do(input)
}
func (opts *DeleteSubCommand) do(input cli.Input) error {
func (opts *DeleteSubCommand) do(input cli.Input) (err error) {
_, currentContextNS, err := util.GetResourceNamespace(input, flagkey.NamespaceEnvironment)
if err != nil {
return errors.Wrap(err, "error creating environment")
}
console.Verbose(2, "Namespace used to delete resource: %s ", currentContextNS)
m := &metav1.ObjectMeta{
Name: input.String(flagkey.EnvName),
Namespace: input.String(flagkey.NamespaceEnvironment),
Namespace: currentContextNS,
}
if !input.Bool(flagkey.EnvForce) {
@@ -56,7 +64,7 @@ func (opts *DeleteSubCommand) do(input cli.Input) error {
}
}
err := opts.Client().V1().Environment().Delete(m)
err = opts.Client().V1().Environment().Delete(m)
if err != nil {
if input.Bool(flagkey.IgnoreNotFound) && util.IsNotFound(err) {
return nil
+9 -2
View File
@@ -27,6 +27,7 @@ import (
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type GetSubCommand struct {
@@ -37,10 +38,16 @@ func Get(input cli.Input) error {
return (&GetSubCommand{}).do(input)
}
func (opts *GetSubCommand) do(input cli.Input) error {
func (opts *GetSubCommand) do(input cli.Input) (err error) {
_, currentNS, err := util.GetResourceNamespace(input, flagkey.NamespaceEnvironment)
if err != nil {
return errors.Wrap(err, "error creating environment")
}
m := &metav1.ObjectMeta{
Name: input.String(flagkey.EnvName),
Namespace: input.String(flagkey.NamespaceEnvironment),
Namespace: currentNS,
}
env, err := opts.Client().V1().Environment().Get(m)
+20 -5
View File
@@ -23,9 +23,11 @@ import (
"github.com/pkg/errors"
v1 "github.com/fission/fission/pkg/apis/core/v1"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type ListSubCommand struct {
@@ -36,20 +38,33 @@ func List(input cli.Input) error {
return (&ListSubCommand{}).do(input)
}
func (opts *ListSubCommand) do(input cli.Input) error {
envs, err := opts.Client().V1().Environment().List(input.String(flagkey.NamespaceEnvironment))
func (opts *ListSubCommand) do(input cli.Input) (err error) {
_, currentNS, err := util.GetResourceNamespace(input, flagkey.NamespaceEnvironment)
if err != nil {
return errors.Wrap(err, "error creating environment")
}
var envs []v1.Environment
if input.Bool(flagkey.AllNamespaces) {
envs, err = opts.Client().V1().Environment().List("")
} else {
envs, err = opts.Client().V1().Environment().List(currentNS)
}
if err != nil {
return errors.Wrap(err, "error listing environments")
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", "NAME", "IMAGE", "BUILDER_IMAGE", "POOLSIZE", "MINCPU", "MAXCPU", "MINMEMORY", "MAXMEMORY", "EXTNET", "GRACETIME")
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", "NAME", "IMAGE", "BUILDER_IMAGE", "POOLSIZE", "MINCPU", "MAXCPU", "MINMEMORY", "MAXMEMORY", "EXTNET", "GRACETIME", "NAMESPACE")
for _, env := range envs {
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n",
fmt.Fprintf(w, "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n",
env.ObjectMeta.Name, env.Spec.Runtime.Image, env.Spec.Builder.Image, env.Spec.Poolsize,
env.Spec.Resources.Requests.Cpu(), env.Spec.Resources.Limits.Cpu(),
env.Spec.Resources.Requests.Memory(), env.Spec.Resources.Limits.Memory(),
env.Spec.AllowAccessToExternalNetwork, env.Spec.TerminationGracePeriod)
env.Spec.AllowAccessToExternalNetwork, env.Spec.TerminationGracePeriod, env.Namespace,
)
}
w.Flush()
+10 -4
View File
@@ -28,6 +28,7 @@ import (
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
"github.com/fission/fission/pkg/utils"
)
@@ -39,13 +40,18 @@ func ListPods(input cli.Input) error {
return (&ListPodsSubCommand{}).do(input)
}
func (opts *ListPodsSubCommand) do(input cli.Input) error {
func (opts *ListPodsSubCommand) do(input cli.Input) (err error) {
_, currentNS, err := util.GetResourceNamespace(input, flagkey.NamespaceEnvironment)
if err != nil {
return errors.Wrap(err, "error creating environment")
}
// validate environment
_, err := opts.Client().V1().Environment().Get(
_, err = opts.Client().V1().Environment().Get(
&metav1.ObjectMeta{
Name: input.String(flagkey.EnvName),
Namespace: input.String(flagkey.NamespaceEnvironment),
Namespace: currentNS,
})
if err != nil {
return errors.Wrap(err, "error getting environment")
@@ -54,7 +60,7 @@ func (opts *ListPodsSubCommand) do(input cli.Input) error {
m := &metav1.ObjectMeta{
Name: input.String(flagkey.EnvName),
Labels: map[string]string{
v1.ENVIRONMENT_NAMESPACE: input.String(flagkey.NamespaceEnvironment),
v1.ENVIRONMENT_NAMESPACE: currentNS,
v1.EXECUTOR_TYPE: input.String(flagkey.EnvExecutorType),
},
}
+10 -2
View File
@@ -52,10 +52,15 @@ func (opts *UpdateSubCommand) do(input cli.Input) error {
return opts.run(input)
}
func (opts *UpdateSubCommand) complete(input cli.Input) error {
func (opts *UpdateSubCommand) complete(input cli.Input) (err error) {
_, currentContextNS, err := util.GetResourceNamespace(input, flagkey.NamespaceEnvironment)
if err != nil {
return errors.Wrap(err, "error creating environment")
}
env, err := opts.Client().V1().Environment().Get(&metav1.ObjectMeta{
Name: input.String(flagkey.EnvName),
Namespace: input.String(flagkey.NamespaceEnvironment),
Namespace: currentContextNS,
})
if err != nil {
return errors.Wrap(err, "error finding environment")
@@ -127,6 +132,9 @@ func updateExistingEnvironmentWithCmd(env *fv1.Environment, input cli.Input) (*f
env.Spec.ImagePullSecret = input.String(flagkey.EnvImagePullSecret)
}
env.Spec.Resources.Requests = make(v1.ResourceList)
env.Spec.Resources.Limits = make(v1.ResourceList)
if input.IsSet(flagkey.RuntimeMincpu) {
mincpu := input.Int(flagkey.RuntimeMincpu)
cpuRequest, err := resource.ParseQuantity(strconv.Itoa(mincpu) + "m")