func (tc *TestClient) DeleteResourceMatchingFile(
	ctx context.Context, src InputSource) error {
	objs, err := getUnstructuredObjects(src)
	if err != nil {
		return err
	}
	for _, u := range objs {
		err := tc.dynDelete(ctx, src.GetNamespace(), u)
		if kerrors.IsNotFound(err) {
			continue
		}
		if err != nil {
			return err
		}
	}
	return nil
}
DeleteResourceMatchingFile deletes a resource given a (yaml) file input.
The resource with the matching group-version-kind and name will be
removed if it exists.
package kube
import (
	"context"
	"errors"
	"io"
	"os"
	kerrors "k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/api/meta"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/apimachinery/pkg/util/yaml"
	"k8s.io/client-go/discovery"
	"k8s.io/client-go/discovery/cached/memory"
	"k8s.io/client-go/dynamic"
	"k8s.io/client-go/restmapper"
)
type InputSource interface {
	Open() (io.ReadCloser, error)
	GetNamespace() string
}
type FileSource struct {
	Path      string
	Namespace string
}
func (f FileSource) Open() (io.ReadCloser, error) {
	file, err := os.Open(f.Path)
	if err != nil {
		return nil, err
	}
	return file, nil
}
func (f FileSource) GetNamespace() string {
	return f.Namespace
}
type DirectSource struct {
	Source    io.ReadCloser
	Namespace string
}
func (d DirectSource) Open() (io.ReadCloser, error) {
	return d.Source, nil
}
func (d DirectSource) GetNamespace() string {
	return d.Namespace
}
func (tc *TestClient) CreateFromFile(
	ctx context.Context, src InputSource) ([]types.NamespacedName, error) {
	n := []types.NamespacedName{}
	objs, err := getUnstructuredObjects(src)
	if err != nil {
		return n, err
	}
	for _, u := range objs {
		newu, err := tc.dynCreate(ctx, src.GetNamespace(), u)
		if err != nil {
			return n, err
		}
		n = append(n, types.NamespacedName{
			Namespace: newu.GetNamespace(),
			Name:      newu.GetName(),
		})
	}
	return n, nil
}
func (tc *TestClient) CreateFromFileIfMissing(
	ctx context.Context, src InputSource) ([]types.NamespacedName, error) {
	n := []types.NamespacedName{}
	objs, err := getUnstructuredObjects(src)
	if err != nil {
		return n, err
	}
	for _, u := range objs {
		newu, err := tc.dynCreate(ctx, src.GetNamespace(), u)
		if kerrors.IsAlreadyExists(err) {
			continue
		}
		if err != nil {
			return n, err
		}
		n = append(n, types.NamespacedName{
			Namespace: newu.GetNamespace(),
			Name:      newu.GetName(),
		})
	}
	return n, nil
}