mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Refactor diff/prune
This PR does some refactors for diff/prune; - GetRESTMappings takes value array instead reference - Move getObjectName into diff instead prune - License, etc. changes
This commit is contained in:
parent
34f0148414
commit
6c449dd272
@ -21,9 +21,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/util/prune"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
@ -44,6 +43,7 @@ import (
|
|||||||
"k8s.io/kubectl/pkg/util"
|
"k8s.io/kubectl/pkg/util"
|
||||||
"k8s.io/kubectl/pkg/util/i18n"
|
"k8s.io/kubectl/pkg/util/i18n"
|
||||||
"k8s.io/kubectl/pkg/util/openapi"
|
"k8s.io/kubectl/pkg/util/openapi"
|
||||||
|
"k8s.io/kubectl/pkg/util/prune"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
"k8s.io/kubectl/pkg/validation"
|
"k8s.io/kubectl/pkg/validation"
|
||||||
)
|
)
|
||||||
|
@ -21,8 +21,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/util/prune"
|
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@ -30,6 +28,7 @@ import (
|
|||||||
"k8s.io/cli-runtime/pkg/printers"
|
"k8s.io/cli-runtime/pkg/printers"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
|
"k8s.io/kubectl/pkg/util/prune"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pruner struct {
|
type pruner struct {
|
||||||
@ -71,7 +70,7 @@ func newPruner(o *ApplyOptions) pruner {
|
|||||||
|
|
||||||
func (p *pruner) pruneAll(o *ApplyOptions) error {
|
func (p *pruner) pruneAll(o *ApplyOptions) error {
|
||||||
|
|
||||||
namespacedRESTMappings, nonNamespacedRESTMappings, err := prune.GetRESTMappings(o.Mapper, &(o.PruneResources))
|
namespacedRESTMappings, nonNamespacedRESTMappings, err := prune.GetRESTMappings(o.Mapper, o.PruneResources)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error retrieving RESTMappings to prune: %v", err)
|
return fmt.Errorf("error retrieving RESTMappings to prune: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,6 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/util/prune"
|
|
||||||
|
|
||||||
"github.com/jonboulle/clockwork"
|
"github.com/jonboulle/clockwork"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
@ -46,6 +44,7 @@ import (
|
|||||||
"k8s.io/kubectl/pkg/util"
|
"k8s.io/kubectl/pkg/util"
|
||||||
"k8s.io/kubectl/pkg/util/i18n"
|
"k8s.io/kubectl/pkg/util/i18n"
|
||||||
"k8s.io/kubectl/pkg/util/openapi"
|
"k8s.io/kubectl/pkg/util/openapi"
|
||||||
|
"k8s.io/kubectl/pkg/util/prune"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
"k8s.io/utils/exec"
|
"k8s.io/utils/exec"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
@ -173,7 +172,7 @@ func NewCmdDiff(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.C
|
|||||||
|
|
||||||
usage := "contains the configuration to diff"
|
usage := "contains the configuration to diff"
|
||||||
cmd.Flags().StringVarP(&options.Selector, "selector", "l", options.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
|
cmd.Flags().StringVarP(&options.Selector, "selector", "l", options.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
|
||||||
cmd.Flags().StringArray("prunewhitelist", []string{}, "Overwrite the default whitelist with <group/version/kind> for --prune")
|
cmd.Flags().StringArray("prune-allowlist", []string{}, "Overwrite the default whitelist with <group/version/kind> for --prune")
|
||||||
cmd.Flags().Bool("prune", false, "Include resources that would be deleted by pruning. Can be used with -l and default shows all resources would be pruned")
|
cmd.Flags().Bool("prune", false, "Include resources that would be deleted by pruning. Can be used with -l and default shows all resources would be pruned")
|
||||||
cmdutil.AddFilenameOptionFlags(cmd, &options.FilenameOptions, usage)
|
cmdutil.AddFilenameOptionFlags(cmd, &options.FilenameOptions, usage)
|
||||||
cmdutil.AddServerSideApplyFlags(cmd)
|
cmdutil.AddServerSideApplyFlags(cmd)
|
||||||
@ -653,7 +652,7 @@ func (o *DiffOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
resources, err := prune.ParseResources(mapper, cmdutil.GetFlagStringArray(cmd, "prunewhitelist"))
|
resources, err := prune.ParseResources(mapper, cmdutil.GetFlagStringArray(cmd, "prune-allowlist"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -749,7 +748,7 @@ func (o *DiffOptions) Run() error {
|
|||||||
// Print pruned objects into old file and thus, diff
|
// Print pruned objects into old file and thus, diff
|
||||||
// command will show them as pruned.
|
// command will show them as pruned.
|
||||||
for _, p := range prunedObjs {
|
for _, p := range prunedObjs {
|
||||||
name, err := o.pruner.GetObjectName(p)
|
name, err := getObjectName(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Warningf("pruning failed and object name could not be retrieved: %v", err)
|
klog.Warningf("pruning failed and object name could not be retrieved: %v", err)
|
||||||
continue
|
continue
|
||||||
@ -766,3 +765,25 @@ func (o *DiffOptions) Run() error {
|
|||||||
|
|
||||||
return differ.Run(o.Diff)
|
return differ.Run(o.Diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getObjectName(obj runtime.Object) (string, error) {
|
||||||
|
gvk := obj.GetObjectKind().GroupVersionKind()
|
||||||
|
metadata, err := meta.Accessor(obj)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
name := metadata.GetName()
|
||||||
|
ns := metadata.GetNamespace()
|
||||||
|
|
||||||
|
group := ""
|
||||||
|
if gvk.Group != "" {
|
||||||
|
group = fmt.Sprintf("%v.", gvk.Group)
|
||||||
|
}
|
||||||
|
return group + fmt.Sprintf(
|
||||||
|
"%v.%v.%v.%v",
|
||||||
|
gvk.Version,
|
||||||
|
gvk.Kind,
|
||||||
|
ns,
|
||||||
|
name,
|
||||||
|
), nil
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2019 The Kubernetes Authors.
|
Copyright 2021 The Kubernetes Authors.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
@ -20,17 +20,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"k8s.io/cli-runtime/pkg/resource"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/util/prune"
|
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
|
"k8s.io/kubectl/pkg/util/prune"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pruner struct {
|
type pruner struct {
|
||||||
@ -55,7 +52,7 @@ func newPruner(dc dynamic.Interface, m meta.RESTMapper, r []prune.Resource) *pru
|
|||||||
|
|
||||||
func (p *pruner) pruneAll() ([]runtime.Object, error) {
|
func (p *pruner) pruneAll() ([]runtime.Object, error) {
|
||||||
var allPruned []runtime.Object
|
var allPruned []runtime.Object
|
||||||
namespacedRESTMappings, nonNamespacedRESTMappings, err := prune.GetRESTMappings(p.mapper, &(p.resources))
|
namespacedRESTMappings, nonNamespacedRESTMappings, err := prune.GetRESTMappings(p.mapper, p.resources)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return allPruned, fmt.Errorf("error retrieving RESTMappings to prune: %v", err)
|
return allPruned, fmt.Errorf("error retrieving RESTMappings to prune: %v", err)
|
||||||
}
|
}
|
||||||
@ -115,28 +112,6 @@ func (p *pruner) prune(namespace string, mapping *meta.RESTMapping) ([]runtime.O
|
|||||||
return pobjs, nil
|
return pobjs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pruner) GetObjectName(obj runtime.Object) (string, error) {
|
|
||||||
gvk := obj.GetObjectKind().GroupVersionKind()
|
|
||||||
metadata, err := meta.Accessor(obj)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
name := metadata.GetName()
|
|
||||||
ns := metadata.GetNamespace()
|
|
||||||
|
|
||||||
group := ""
|
|
||||||
if gvk.Group != "" {
|
|
||||||
group = fmt.Sprintf("%v.", gvk.Group)
|
|
||||||
}
|
|
||||||
return group + fmt.Sprintf(
|
|
||||||
"%v.%v.%v.%v",
|
|
||||||
gvk.Version,
|
|
||||||
gvk.Kind,
|
|
||||||
ns,
|
|
||||||
name,
|
|
||||||
), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarkVisited marks visited namespaces and uids
|
// MarkVisited marks visited namespaces and uids
|
||||||
func (p *pruner) MarkVisited(info *resource.Info) {
|
func (p *pruner) MarkVisited(info *resource.Info) {
|
||||||
if info.Namespaced() {
|
if info.Namespaced() {
|
||||||
|
@ -35,10 +35,10 @@ func (pr Resource) String() string {
|
|||||||
return fmt.Sprintf("%v/%v, Kind=%v, Namespaced=%v", pr.group, pr.version, pr.kind, pr.namespaced)
|
return fmt.Sprintf("%v/%v, Kind=%v, Namespaced=%v", pr.group, pr.version, pr.kind, pr.namespaced)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRESTMappings(mapper meta.RESTMapper, pruneResources *[]Resource) (namespaced, nonNamespaced []*meta.RESTMapping, err error) {
|
func GetRESTMappings(mapper meta.RESTMapper, pruneResources []Resource) (namespaced, nonNamespaced []*meta.RESTMapping, err error) {
|
||||||
if len(*pruneResources) == 0 {
|
if len(pruneResources) == 0 {
|
||||||
// default allowlist
|
// default allowlist
|
||||||
*pruneResources = []Resource{
|
pruneResources = []Resource{
|
||||||
{"", "v1", "ConfigMap", true},
|
{"", "v1", "ConfigMap", true},
|
||||||
{"", "v1", "Endpoints", true},
|
{"", "v1", "Endpoints", true},
|
||||||
{"", "v1", "Namespace", false},
|
{"", "v1", "Namespace", false},
|
||||||
@ -58,7 +58,7 @@ func GetRESTMappings(mapper meta.RESTMapper, pruneResources *[]Resource) (namesp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, resource := range *pruneResources {
|
for _, resource := range pruneResources {
|
||||||
addedMapping, err := mapper.RESTMapping(schema.GroupKind{Group: resource.group, Kind: resource.kind}, resource.version)
|
addedMapping, err := mapper.RESTMapping(schema.GroupKind{Group: resource.group, Kind: resource.kind}, resource.version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("invalid resource %v: %v", resource, err)
|
return nil, nil, fmt.Errorf("invalid resource %v: %v", resource, err)
|
||||||
|
@ -49,14 +49,14 @@ func (m *testRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*
|
|||||||
func TestGetRESTMappings(t *testing.T) {
|
func TestGetRESTMappings(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
mapper *testRESTMapper
|
mapper *testRESTMapper
|
||||||
pr *[]Resource
|
pr []Resource
|
||||||
expectedns int
|
expectedns int
|
||||||
expectednns int
|
expectednns int
|
||||||
expectederr error
|
expectederr error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
mapper: &testRESTMapper{},
|
mapper: &testRESTMapper{},
|
||||||
pr: &[]Resource{},
|
pr: []Resource{},
|
||||||
expectedns: 14,
|
expectedns: 14,
|
||||||
expectednns: 2,
|
expectednns: 2,
|
||||||
expectederr: nil,
|
expectederr: nil,
|
||||||
|
Loading…
Reference in New Issue
Block a user