mirror of https://github.com/fluxcd/cli-utils.git
				
				
				
			Drop k8s.io/apiextensions-apiserver dependency
This commit is contained in:
		
							parent
							
								
									a1b40b59d5
								
							
						
					
					
						commit
						7333873c91
					
				
							
								
								
									
										1
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										1
									
								
								go.mod
								
								
								
								
							| 
						 | 
				
			
			@ -13,7 +13,6 @@ require (
 | 
			
		|||
	golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
 | 
			
		||||
	k8s.io/api v0.21.1
 | 
			
		||||
	k8s.io/apiextensions-apiserver v0.21.1
 | 
			
		||||
	k8s.io/apimachinery v0.21.1
 | 
			
		||||
	k8s.io/cli-runtime v0.21.1
 | 
			
		||||
	k8s.io/client-go v0.21.1
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,10 +7,15 @@ import (
 | 
			
		|||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/util/sets"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/object"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	namespaceGK = schema.GroupKind{Group: "", Kind: "Namespace"}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// LocalNamespacesFilter encapsulates the set of namespaces
 | 
			
		||||
// that are currently in use. Used to ensure we do not delete
 | 
			
		||||
// namespaces with currently applied objects in them.
 | 
			
		||||
| 
						 | 
				
			
			@ -29,7 +34,7 @@ func (lnf LocalNamespacesFilter) Name() string {
 | 
			
		|||
// for "destroying", since every object is being delete. Never returns an error.
 | 
			
		||||
func (lnf LocalNamespacesFilter) Filter(obj *unstructured.Unstructured) (bool, string, error) {
 | 
			
		||||
	id := object.UnstructuredToObjMetaOrDie(obj)
 | 
			
		||||
	if id.GroupKind == object.CoreV1Namespace.GroupKind() &&
 | 
			
		||||
	if id.GroupKind == namespaceGK &&
 | 
			
		||||
		lnf.LocalNamespaces.Has(id.Name) {
 | 
			
		||||
		reason := fmt.Sprintf("namespace still in use (namespace: %q)", id.Name)
 | 
			
		||||
		return true, reason, nil
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,6 +5,7 @@ package task
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
	"k8s.io/klog/v2"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/apply/event"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/apply/taskrunner"
 | 
			
		||||
| 
						 | 
				
			
			@ -13,6 +14,10 @@ import (
 | 
			
		|||
	"sigs.k8s.io/cli-utils/pkg/object"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	namespaceGVKv1 = schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Namespace"}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// InvAddTask encapsulates structures necessary to add/merge inventory
 | 
			
		||||
// into the cluster. The InvAddTask should add/merge inventory references
 | 
			
		||||
// before the actual object is applied.
 | 
			
		||||
| 
						 | 
				
			
			@ -74,7 +79,7 @@ func inventoryNamespaceInSet(inv inventory.InventoryInfo, objs object.Unstructur
 | 
			
		|||
 | 
			
		||||
	for _, obj := range objs {
 | 
			
		||||
		gvk := obj.GetObjectKind().GroupVersionKind()
 | 
			
		||||
		if gvk == object.CoreV1Namespace && obj.GetName() == invNamespace {
 | 
			
		||||
		if gvk == namespaceGVKv1 && obj.GetName() == invNamespace {
 | 
			
		||||
			inventory.AddInventoryIDAnnotation(obj, inv)
 | 
			
		||||
			return obj
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,15 +8,18 @@ import (
 | 
			
		|||
	"reflect"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 | 
			
		||||
	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/meta"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
	"k8s.io/client-go/restmapper"
 | 
			
		||||
	"k8s.io/klog/v2"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/apply/event"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/object"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	crdGK = schema.GroupKind{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Task is the interface that must be implemented by
 | 
			
		||||
// all tasks that will be executed by the taskrunner.
 | 
			
		||||
type Task interface {
 | 
			
		||||
| 
						 | 
				
			
			@ -163,9 +166,7 @@ func (w *WaitTask) startAndComplete(taskContext *TaskContext) {
 | 
			
		|||
func (w *WaitTask) complete(taskContext *TaskContext) {
 | 
			
		||||
	var err error
 | 
			
		||||
	for _, obj := range w.Ids {
 | 
			
		||||
		if (obj.GroupKind.Group == v1.SchemeGroupVersion.Group ||
 | 
			
		||||
			obj.GroupKind.Group == v1beta1.SchemeGroupVersion.Group) &&
 | 
			
		||||
			obj.GroupKind.Kind == "CustomResourceDefinition" {
 | 
			
		||||
		if obj.GroupKind == crdGK {
 | 
			
		||||
			ddRESTMapper, err := extractDeferredDiscoveryRESTMapper(w.mapper)
 | 
			
		||||
			if err == nil {
 | 
			
		||||
				ddRESTMapper.Reset()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,7 +13,6 @@ import (
 | 
			
		|||
	"github.com/stretchr/testify/require"
 | 
			
		||||
	appsv1 "k8s.io/api/apps/v1"
 | 
			
		||||
	v1 "k8s.io/api/core/v1"
 | 
			
		||||
	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/errors"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/meta"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
| 
						 | 
				
			
			@ -26,6 +25,7 @@ var (
 | 
			
		|||
	deploymentGVK = appsv1.SchemeGroupVersion.WithKind("Deployment")
 | 
			
		||||
	rsGVK         = appsv1.SchemeGroupVersion.WithKind("ReplicaSet")
 | 
			
		||||
	podGVK        = v1.SchemeGroupVersion.WithKind("Pod")
 | 
			
		||||
	crdGVK        = schema.GroupVersionKind{Group: "apiextensions.k8s.io", Version: "v1", Kind: "CustomResourceDefinition"}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestSync(t *testing.T) {
 | 
			
		||||
| 
						 | 
				
			
			@ -79,8 +79,8 @@ func TestSync(t *testing.T) {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	fakeMapper := testutil.NewFakeRESTMapper(
 | 
			
		||||
		appsv1.SchemeGroupVersion.WithKind("Deployment"),
 | 
			
		||||
		appsv1.SchemeGroupVersion.WithKind("ReplicaSet"),
 | 
			
		||||
		deploymentGVK,
 | 
			
		||||
		rsGVK,
 | 
			
		||||
		v1.SchemeGroupVersion.WithKind("Pod"),
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -115,7 +115,7 @@ func TestSync_Errors(t *testing.T) {
 | 
			
		|||
	}{
 | 
			
		||||
		"mapping and reader are successful": {
 | 
			
		||||
			mapper: testutil.NewFakeRESTMapper(
 | 
			
		||||
				apiextv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"),
 | 
			
		||||
				crdGVK,
 | 
			
		||||
			),
 | 
			
		||||
			readerError:     nil,
 | 
			
		||||
			expectSyncError: false,
 | 
			
		||||
| 
						 | 
				
			
			@ -123,7 +123,7 @@ func TestSync_Errors(t *testing.T) {
 | 
			
		|||
		},
 | 
			
		||||
		"reader returns NotFound error": {
 | 
			
		||||
			mapper: testutil.NewFakeRESTMapper(
 | 
			
		||||
				apiextv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"),
 | 
			
		||||
				crdGVK,
 | 
			
		||||
			),
 | 
			
		||||
			readerError: errors.NewNotFound(schema.GroupResource{
 | 
			
		||||
				Group:    "apiextensions.k8s.io",
 | 
			
		||||
| 
						 | 
				
			
			@ -135,7 +135,7 @@ func TestSync_Errors(t *testing.T) {
 | 
			
		|||
		},
 | 
			
		||||
		"reader returns other error": {
 | 
			
		||||
			mapper: testutil.NewFakeRESTMapper(
 | 
			
		||||
				apiextv1.SchemeGroupVersion.WithKind("CustomResourceDefinition"),
 | 
			
		||||
				crdGVK,
 | 
			
		||||
			),
 | 
			
		||||
			readerError:     errors.NewInternalError(fmt.Errorf("testing")),
 | 
			
		||||
			expectSyncError: false,
 | 
			
		||||
| 
						 | 
				
			
			@ -178,7 +178,7 @@ func TestSync_Errors(t *testing.T) {
 | 
			
		|||
			require.NoError(t, err)
 | 
			
		||||
 | 
			
		||||
			cacheEntry, found := clusterReader.cache[gkNamespace{
 | 
			
		||||
				GroupKind: apiextv1.SchemeGroupVersion.WithKind("CustomResourceDefinition").GroupKind(),
 | 
			
		||||
				GroupKind: crdGVK.GroupKind(),
 | 
			
		||||
			}]
 | 
			
		||||
			require.True(t, found)
 | 
			
		||||
			if tc.cacheError {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,11 +9,10 @@ import (
 | 
			
		|||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"github.com/stretchr/testify/require"
 | 
			
		||||
	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/meta"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
 | 
			
		||||
	"k8s.io/kubectl/pkg/scheme"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/object"
 | 
			
		||||
	"sigs.k8s.io/kustomize/kyaml/kio/filters"
 | 
			
		||||
	"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
 | 
			
		||||
| 
						 | 
				
			
			@ -21,10 +20,6 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
func TestSetNamespaces(t *testing.T) {
 | 
			
		||||
	// We need the RESTMapper in the testFactory to contain the CRD
 | 
			
		||||
	// types, so add them to the scheme here.
 | 
			
		||||
	_ = apiextv1.AddToScheme(scheme.Scheme)
 | 
			
		||||
 | 
			
		||||
	testCases := map[string]struct {
 | 
			
		||||
		objs             []*unstructured.Unstructured
 | 
			
		||||
		defaultNamspace  string
 | 
			
		||||
| 
						 | 
				
			
			@ -166,9 +161,13 @@ func TestSetNamespaces(t *testing.T) {
 | 
			
		|||
			defer tf.Cleanup()
 | 
			
		||||
 | 
			
		||||
			mapper, err := tf.ToRESTMapper()
 | 
			
		||||
			if !assert.NoError(t, err) {
 | 
			
		||||
				t.FailNow()
 | 
			
		||||
			}
 | 
			
		||||
			require.NoError(t, err)
 | 
			
		||||
			crdGV := schema.GroupVersion{Group: "apiextensions.k8s.io", Version: "v1"}
 | 
			
		||||
			crdMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{crdGV})
 | 
			
		||||
			crdMapper.AddSpecific(crdGV.WithKind("CustomResourceDefinition"),
 | 
			
		||||
				crdGV.WithResource("customresourcedefinitions"),
 | 
			
		||||
				crdGV.WithResource("customresourcedefinition"), meta.RESTScopeRoot)
 | 
			
		||||
			mapper = meta.MultiRESTMapper([]meta.RESTMapper{mapper, crdMapper})
 | 
			
		||||
 | 
			
		||||
			err = SetNamespaces(mapper, tc.objs, tc.defaultNamspace, tc.enforceNamespace)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,18 +7,14 @@ package object
 | 
			
		|||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	corev1 "k8s.io/api/core/v1"
 | 
			
		||||
	extensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/meta"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	CoreNamespace   = CoreV1Namespace.GroupKind()
 | 
			
		||||
	CoreV1Namespace = corev1.SchemeGroupVersion.WithKind("Namespace")
 | 
			
		||||
	ExtensionsCRD   = ExtensionsV1CRD.GroupKind()
 | 
			
		||||
	ExtensionsV1CRD = extensionsv1.SchemeGroupVersion.WithKind("CustomResourceDefinition")
 | 
			
		||||
	namespaceGK = schema.GroupKind{Group: "", Kind: "Namespace"}
 | 
			
		||||
	crdGK       = schema.GroupKind{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// UnstructuredsToObjMetas converts a slice of unstructureds to a slice of
 | 
			
		||||
| 
						 | 
				
			
			@ -73,7 +69,7 @@ func IsKindNamespace(u *unstructured.Unstructured) bool {
 | 
			
		|||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	gvk := u.GroupVersionKind()
 | 
			
		||||
	return CoreNamespace == gvk.GroupKind()
 | 
			
		||||
	return namespaceGK == gvk.GroupKind()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsNamespaced returns true if the passed Unstructured object
 | 
			
		||||
| 
						 | 
				
			
			@ -92,7 +88,7 @@ func IsCRD(u *unstructured.Unstructured) bool {
 | 
			
		|||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	gvk := u.GroupVersionKind()
 | 
			
		||||
	return ExtensionsCRD == gvk.GroupKind()
 | 
			
		||||
	return crdGK == gvk.GroupKind()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetCRDGroupKind returns the GroupKind stored in the passed
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,12 +8,10 @@ import (
 | 
			
		|||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"github.com/stretchr/testify/require"
 | 
			
		||||
	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/meta"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
 | 
			
		||||
	"k8s.io/kubectl/pkg/scheme"
 | 
			
		||||
	. "sigs.k8s.io/cli-utils/pkg/object"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/testutil"
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -258,7 +256,6 @@ func TestGetCRDGroupKind(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestLookupResourceScope(t *testing.T) {
 | 
			
		||||
	_ = apiextv1.AddToScheme(scheme.Scheme)
 | 
			
		||||
	testCases := map[string]struct {
 | 
			
		||||
		resource      *unstructured.Unstructured
 | 
			
		||||
		crds          []*unstructured.Unstructured
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,18 +8,16 @@ import (
 | 
			
		|||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
	"github.com/stretchr/testify/require"
 | 
			
		||||
	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/api/meta"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
			
		||||
	"k8s.io/apimachinery/pkg/util/validation/field"
 | 
			
		||||
	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
 | 
			
		||||
	"k8s.io/kubectl/pkg/scheme"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/object"
 | 
			
		||||
	"sigs.k8s.io/cli-utils/pkg/testutil"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestValidate(t *testing.T) {
 | 
			
		||||
	_ = apiextv1.AddToScheme(scheme.Scheme)
 | 
			
		||||
	testCases := map[string]struct {
 | 
			
		||||
		resources     []*unstructured.Unstructured
 | 
			
		||||
		expectedError error
 | 
			
		||||
| 
						 | 
				
			
			@ -234,6 +232,12 @@ metadata:
 | 
			
		|||
 | 
			
		||||
			mapper, err := tf.ToRESTMapper()
 | 
			
		||||
			require.NoError(t, err)
 | 
			
		||||
			crdGV := schema.GroupVersion{Group: "apiextensions.k8s.io", Version: "v1"}
 | 
			
		||||
			crdMapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{crdGV})
 | 
			
		||||
			crdMapper.AddSpecific(crdGV.WithKind("CustomResourceDefinition"),
 | 
			
		||||
				crdGV.WithResource("customresourcedefinitions"),
 | 
			
		||||
				crdGV.WithResource("customresourcedefinition"), meta.RESTScopeRoot)
 | 
			
		||||
			mapper = meta.MultiRESTMapper([]meta.RESTMapper{mapper, crdMapper})
 | 
			
		||||
 | 
			
		||||
			err = (&object.Validator{
 | 
			
		||||
				Mapper: mapper,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue