mirror of https://github.com/k3s-io/k3s
move resource builder flags to genericclioptions
parent
b9e46f5422
commit
d463bbddb1
|
@ -24,6 +24,7 @@
|
||||||
# TODO this one should be tightened. We depend on it for testing, but we should instead create our own scheme
|
# TODO this one should be tightened. We depend on it for testing, but we should instead create our own scheme
|
||||||
- k8s.io/api/core/v1
|
- k8s.io/api/core/v1
|
||||||
- k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers
|
- k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers
|
||||||
|
- k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource
|
||||||
|
|
||||||
- baseImportPath: "./vendor/k8s.io/apimachinery/"
|
- baseImportPath: "./vendor/k8s.io/apimachinery/"
|
||||||
allowedImports:
|
allowedImports:
|
||||||
|
|
|
@ -275,7 +275,7 @@ func (o *DeleteOptions) DeleteResult(r *resource.Result) error {
|
||||||
effectiveTimeout = 168 * time.Hour
|
effectiveTimeout = 168 * time.Hour
|
||||||
}
|
}
|
||||||
waitOptions := kubectlwait.WaitOptions{
|
waitOptions := kubectlwait.WaitOptions{
|
||||||
ResourceFinder: kubectlwait.ResourceFinderForResult(o.Result),
|
ResourceFinder: genericclioptions.ResourceFinderForResult(o.Result),
|
||||||
DynamicClient: o.DynamicClient,
|
DynamicClient: o.DynamicClient,
|
||||||
Timeout: effectiveTimeout,
|
Timeout: effectiveTimeout,
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||||
|
|
||||||
go_library(
|
go_library(
|
||||||
name = "go_default_library",
|
name = "go_default_library",
|
||||||
srcs = [
|
srcs = ["wait.go"],
|
||||||
"fakeresourcefinder.go",
|
|
||||||
"flags.go",
|
|
||||||
"wait.go",
|
|
||||||
],
|
|
||||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/wait",
|
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/wait",
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
|
@ -15,7 +11,6 @@ go_library(
|
||||||
"//pkg/kubectl/genericclioptions/printers:go_default_library",
|
"//pkg/kubectl/genericclioptions/printers:go_default_library",
|
||||||
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
||||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
|
||||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||||
|
|
|
@ -42,7 +42,7 @@ import (
|
||||||
type WaitFlags struct {
|
type WaitFlags struct {
|
||||||
RESTClientGetter genericclioptions.RESTClientGetter
|
RESTClientGetter genericclioptions.RESTClientGetter
|
||||||
PrintFlags *genericclioptions.PrintFlags
|
PrintFlags *genericclioptions.PrintFlags
|
||||||
ResourceBuilderFlags *ResourceBuilderFlags
|
ResourceBuilderFlags *genericclioptions.ResourceBuilderFlags
|
||||||
|
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
ForCondition string
|
ForCondition string
|
||||||
|
@ -55,7 +55,7 @@ func NewWaitFlags(restClientGetter genericclioptions.RESTClientGetter, streams g
|
||||||
return &WaitFlags{
|
return &WaitFlags{
|
||||||
RESTClientGetter: restClientGetter,
|
RESTClientGetter: restClientGetter,
|
||||||
PrintFlags: genericclioptions.NewPrintFlags("condition met"),
|
PrintFlags: genericclioptions.NewPrintFlags("condition met"),
|
||||||
ResourceBuilderFlags: NewResourceBuilderFlags(),
|
ResourceBuilderFlags: genericclioptions.NewResourceBuilderFlags(),
|
||||||
|
|
||||||
Timeout: 30 * time.Second,
|
Timeout: 30 * time.Second,
|
||||||
|
|
||||||
|
@ -151,7 +151,7 @@ func conditionFuncFor(condition string) (ConditionFunc, error) {
|
||||||
// WaitOptions is a set of options that allows you to wait. This is the object reflects the runtime needs of a wait
|
// WaitOptions is a set of options that allows you to wait. This is the object reflects the runtime needs of a wait
|
||||||
// command, making the logic itself easy to unit test with our existing mocks.
|
// command, making the logic itself easy to unit test with our existing mocks.
|
||||||
type WaitOptions struct {
|
type WaitOptions struct {
|
||||||
ResourceFinder ResourceFinder
|
ResourceFinder genericclioptions.ResourceFinder
|
||||||
DynamicClient dynamic.Interface
|
DynamicClient dynamic.Interface
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
|
|
||||||
|
|
|
@ -219,7 +219,7 @@ func TestWaitForDeletion(t *testing.T) {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
fakeClient := test.fakeClient()
|
fakeClient := test.fakeClient()
|
||||||
o := &WaitOptions{
|
o := &WaitOptions{
|
||||||
ResourceFinder: NewSimpleResourceFinder(test.info),
|
ResourceFinder: genericclioptions.NewSimpleResourceFinder(test.info),
|
||||||
DynamicClient: fakeClient,
|
DynamicClient: fakeClient,
|
||||||
Timeout: test.timeout,
|
Timeout: test.timeout,
|
||||||
|
|
||||||
|
@ -451,7 +451,7 @@ func TestWaitForCondition(t *testing.T) {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
fakeClient := test.fakeClient()
|
fakeClient := test.fakeClient()
|
||||||
o := &WaitOptions{
|
o := &WaitOptions{
|
||||||
ResourceFinder: NewSimpleResourceFinder(test.info),
|
ResourceFinder: genericclioptions.NewSimpleResourceFinder(test.info),
|
||||||
DynamicClient: fakeClient,
|
DynamicClient: fakeClient,
|
||||||
Timeout: test.timeout,
|
Timeout: test.timeout,
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||||
go_library(
|
go_library(
|
||||||
name = "go_default_library",
|
name = "go_default_library",
|
||||||
srcs = [
|
srcs = [
|
||||||
|
"builder_flags.go",
|
||||||
|
"builder_flags_fake.go",
|
||||||
"config_flags.go",
|
"config_flags.go",
|
||||||
"config_flags_fake.go",
|
"config_flags_fake.go",
|
||||||
"doc.go",
|
"doc.go",
|
||||||
|
@ -16,6 +18,7 @@ go_library(
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/kubectl/genericclioptions/printers:go_default_library",
|
"//pkg/kubectl/genericclioptions/printers:go_default_library",
|
||||||
|
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
||||||
"//vendor/github.com/evanphx/json-patch:go_default_library",
|
"//vendor/github.com/evanphx/json-patch:go_default_library",
|
||||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||||
|
|
|
@ -14,14 +14,13 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package wait
|
package genericclioptions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
|
||||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,15 +28,15 @@ import (
|
||||||
type ResourceBuilderFlags struct {
|
type ResourceBuilderFlags struct {
|
||||||
FilenameOptions resource.FilenameOptions
|
FilenameOptions resource.FilenameOptions
|
||||||
|
|
||||||
LabelSelector string
|
|
||||||
FieldSelector string
|
|
||||||
AllNamespaces bool
|
|
||||||
Namespace string
|
Namespace string
|
||||||
ExplicitNamespace bool
|
ExplicitNamespace bool
|
||||||
|
|
||||||
// TODO add conditional support. These are false for now.
|
LabelSelector *string
|
||||||
All bool
|
FieldSelector *string
|
||||||
Local bool
|
AllNamespaces *bool
|
||||||
|
|
||||||
|
All *bool
|
||||||
|
Local *bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResourceBuilderFlags returns a default ResourceBuilderFlags
|
// NewResourceBuilderFlags returns a default ResourceBuilderFlags
|
||||||
|
@ -46,6 +45,13 @@ func NewResourceBuilderFlags() *ResourceBuilderFlags {
|
||||||
FilenameOptions: resource.FilenameOptions{
|
FilenameOptions: resource.FilenameOptions{
|
||||||
Recursive: true,
|
Recursive: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
LabelSelector: str_ptr(""),
|
||||||
|
FieldSelector: str_ptr(""),
|
||||||
|
AllNamespaces: bool_ptr(false),
|
||||||
|
|
||||||
|
All: bool_ptr(false),
|
||||||
|
Local: bool_ptr(false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,23 +65,44 @@ func (o *ResourceBuilderFlags) AddFlags(flagset *pflag.FlagSet) {
|
||||||
flagset.SetAnnotation("filename", cobra.BashCompFilenameExt, annotations)
|
flagset.SetAnnotation("filename", cobra.BashCompFilenameExt, annotations)
|
||||||
flagset.BoolVar(&o.FilenameOptions.Recursive, "recursive", o.FilenameOptions.Recursive, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
|
flagset.BoolVar(&o.FilenameOptions.Recursive, "recursive", o.FilenameOptions.Recursive, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
|
||||||
|
|
||||||
flagset.StringVarP(&o.LabelSelector, "selector", "l", o.LabelSelector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
|
if o.LabelSelector != nil {
|
||||||
flagset.StringVar(&o.FieldSelector, "field-selector", o.FieldSelector, "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.")
|
flagset.StringVarP(o.LabelSelector, "selector", "l", *o.LabelSelector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
|
||||||
flagset.BoolVar(&o.AllNamespaces, "all-namespaces", o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
|
}
|
||||||
|
if o.FieldSelector != nil {
|
||||||
|
flagset.StringVar(o.FieldSelector, "field-selector", *o.FieldSelector, "Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector key1=value1,key2=value2). The server only supports a limited number of field queries per type.")
|
||||||
|
}
|
||||||
|
if o.AllNamespaces != nil {
|
||||||
|
flagset.BoolVar(o.AllNamespaces, "all-namespaces", *o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToBuilder gives you back a resource finder to visit resources that are located
|
// ToBuilder gives you back a resource finder to visit resources that are located
|
||||||
func (o *ResourceBuilderFlags) ToBuilder(restClientGetter genericclioptions.RESTClientGetter, resources []string) ResourceFinder {
|
func (o *ResourceBuilderFlags) ToBuilder(restClientGetter RESTClientGetter, resources []string) ResourceFinder {
|
||||||
namespace, enforceNamespace, namespaceErr := restClientGetter.ToRawKubeConfigLoader().Namespace()
|
namespace, enforceNamespace, namespaceErr := restClientGetter.ToRawKubeConfigLoader().Namespace()
|
||||||
|
|
||||||
|
labelSelector := ""
|
||||||
|
if o.LabelSelector != nil {
|
||||||
|
labelSelector = *o.LabelSelector
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldSelector := ""
|
||||||
|
if o.FieldSelector != nil {
|
||||||
|
fieldSelector = *o.FieldSelector
|
||||||
|
}
|
||||||
|
|
||||||
|
allResources := false
|
||||||
|
if o.All != nil {
|
||||||
|
allResources = *o.All
|
||||||
|
}
|
||||||
|
|
||||||
return &ResourceFindBuilderWrapper{
|
return &ResourceFindBuilderWrapper{
|
||||||
builder: resource.NewBuilder(restClientGetter).
|
builder: resource.NewBuilder(restClientGetter).
|
||||||
Unstructured().
|
Unstructured().
|
||||||
NamespaceParam(namespace).DefaultNamespace().
|
NamespaceParam(namespace).DefaultNamespace().
|
||||||
FilenameParam(enforceNamespace, &o.FilenameOptions).
|
FilenameParam(enforceNamespace, &o.FilenameOptions).
|
||||||
LabelSelectorParam(o.LabelSelector).
|
LabelSelectorParam(labelSelector).
|
||||||
FieldSelectorParam(o.FieldSelector).
|
FieldSelectorParam(fieldSelector).
|
||||||
ResourceTypeOrNameArgs(o.All, resources...).
|
ResourceTypeOrNameArgs(allResources, resources...).
|
||||||
Latest().
|
Latest().
|
||||||
Flatten().
|
Flatten().
|
||||||
AddError(namespaceErr),
|
AddError(namespaceErr),
|
||||||
|
@ -112,3 +139,11 @@ func ResourceFinderForResult(result resource.Visitor) ResourceFinder {
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func str_ptr(val string) *string {
|
||||||
|
return &val
|
||||||
|
}
|
||||||
|
|
||||||
|
func bool_ptr(val bool) *bool {
|
||||||
|
return &val
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package wait
|
package genericclioptions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
Loading…
Reference in New Issue