kubernetes-sigs / mcs-api

This repository hosts the Multi-Cluster Service APIs. Providers can import packages in this repo to ensure their multi-cluster service controller implementations will be compatible with MCS data planes.
Apache License 2.0
210 stars 43 forks source link

ServiceExport Client should support operations across multiple namespaces #2

Closed josephpeacock closed 3 years ago

josephpeacock commented 3 years ago

Current Behavior: The serviceexport client has ns as a private field in the struct (https://github.com/kubernetes-sigs/mcs-api/blob/master/pkg/client/clientset/versioned/typed/apis/v1alpha1/serviceexport.go#L55), and uses that as a hardcoded namespace value in all operations. This makes it difficult to use the client for an entire cluster, to create ServiceExport resources for Services in arbitrary namespaces.

Expected Behavior: I can create a single client, and provide the namespace at call-time to specify CRUD operations to occur in/for a specific namespace

As a workaround, I have been directly using the RESTClient, for example:

func (sc *ServiceExportController) createServiceExport(service *v1.Service) (v1alpha1.ServiceExport, error) {
    serviceExport := v1alpha1.ServiceExport{}
    serviceExport.Namespace = service.Namespace
    serviceExport.Name = service.Name

    result := &v1alpha1.ServiceExport{}
    err := sc.client.Post().
        Namespace(service.Namespace).
        Resource("serviceexports").
        VersionedParams(&metav1.CreateOptions{}, scheme.ParameterCodec).
        Body(serviceExport).
        Do(context.TODO()).
        Into(result)

    return err
}

But that is unwieldy in the code, and makes unit testing more difficult.

josephpeacock commented 3 years ago

This is false. I was using the wrong syntax - for anyone in the future having similar issues to me, here's how to do it right:

kubeClient.MCSApis().MulticlusterV1alpha1().ServiceExports("nsName").Get(context.TODO(), "serviceExportName", metav1.GetOptions{})