I can run the following code when providing a kubeconfig. Doing so I was able to get the value of gr:
func (o *ApplyOptions) RestMapper() (meta.RESTMapper, error) {
gr, err := restmapper.GetAPIGroupResources(o.discoveryClient)
if err != nil {
return nil, err
}
mapper := restmapper.NewDiscoveryRESTMapper(gr)
return mapper, nil
}
Then I have initiated the following code:
var kubeconfig *string
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
panic(err.Error())
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
panic(err.Error())
}
applyOptions := apply.NewApplyOptions(dynamicClient, discoveryClient)
if err := applyOptions.Apply(context.Background(), []byte(applyStr)); err != nil {
log.Fatalf("apply error: %v", err)
}
Now I use it for unit test and gr is empty (from restmapper.GetAPIGroupResources(o.discoveryClient)) above when debug it,
, how can I add some fake to make it works also from the test ?
var _ = DescribeTable(“test”, func(applyOptions *ApplyOptions, filename string, isExpectedErr bool, expectedErrMsg string) {
applyOptions = ApplyOptions{
discoveryClient: clientset.Discovery() ,
//discoveryClient: &k8sfake.Clientset,
dynamicClient: dynamicfake.NewSimpleDynamicClient(runtime.NewScheme()),
}
// Here I need to initiate some fake data
restmapper, err := applyOptions.RestMapper()
if err != nil {
Fail(err.Error())
}
The error which I get is "k8s.io/apimachinery/pkg/api/**meta.NoKindMatchError**"
Update
To make it clear:
I am trying to create a unit test for this function with kind: deployment and I got the above error.