What I am trying to do is having the operator running and be responsible for my new CRD across all the namespaces. In the crd.go (as the pod terminator example) I have the following:
func (p *myCRD) GetListerWatcher() cache.ListerWatcher {
return &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return p.myCRDCli.V1alpha1().MyCRD("").List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return p.myCRDCli.V1alpha1().MyCRD("").Watch(options)
},
}
}
In the above way I am able to monitor the custom CRDs across all the namespaces.
The definition of the CRD is:
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// PodTerminator represents a pod terminator.
type MyCRD struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`
}
Notice that I omit the nonNamespaced option of the example.
In the handler this code:
func (h *handler) Add(_ context.Context, obj runtime.Object) error {
pt, ok := obj.(*v1alpha1.MyCRD)
if !ok {
return fmt.Errorf("%v is not a valid object", obj.GetObjectKind())
}
fmt.Println("namespace is :"+pt.Namespace)
it produces an empty namespace.
An example of the object I create is:
apiVersion: v1alpha1
kind: MyCRD
metadata:
name: test
namespace: development
Is this the expected behavior? Is it also possible the namespace to be filled out by the current kubectl context?