func (w *watch) Query() (interface{}, error) {
	if w.name == "" {
		return nil, nil
	}
	return w.queryfn(w.ns, w.name)
}
Query queries the object being watched.
package agent
import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"time"
	core_v1 "k8s.io/api/core/v1"
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
	k8swatch "k8s.io/apimachinery/pkg/watch"
	k8sclient "k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	resmgr "github.com/intel/cri-resource-manager/pkg/apis/resmgr/generated/clientset/versioned/typed/resmgr/v1alpha1"
	agent_v1 "github.com/intel/cri-resource-manager/pkg/agent/api/v1"
)
type namespace string
var nodeName string
func (a *agent) getK8sClient(kubeconfig string) (*k8sclient.Clientset, *resmgr.CriresmgrV1alpha1Client, error) {
	var config *rest.Config
	var err error
	if kubeconfig == "" {
		a.Info("using in-cluster kubeconfig")
		config, err = rest.InClusterConfig()
	} else {
		config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
	}
	if err != nil {
		return nil, nil, err
	}
	genCli, err := k8sclient.NewForConfig(config)
	if err != nil {
		return nil, nil, err
	}
	resmgr, err := resmgr.NewForConfig(config)
	if err != nil {
		return nil, nil, err
	}
	return genCli, resmgr, nil
}
func getNodeObject(cli *k8sclient.Clientset) (*core_v1.Node, error) {
	node, err := cli.CoreV1().Nodes().Get(context.TODO(), nodeName, meta_v1.GetOptions{})
	if err != nil {
		return nil, agentError("failed to get node object for node %q: %v", nodeName, err)
	}
	return node, nil
}
func patchNode(cli *k8sclient.Clientset, patchList []*agent_v1.JsonPatch) error {
	data, err := json.Marshal(patchList)
	if err != nil {
		return agentError("failed to marshal Node patches: %v", err)
	}
	pt := types.JSONPatchType
	_, err = cli.CoreV1().Nodes().Patch(context.TODO(), nodeName, pt, data, meta_v1.PatchOptions{})
	if err != nil {
		return err
	}
	return nil
}
func patchNodeStatus(cli *k8sclient.Clientset, fields map[string]string) error {
	patch, sep := fmt.Sprintf(`{"status": {`), ""
	for f, v := range fields {
		patch += sep + fmt.Sprintf(`"%s": %s`, f, v)
		sep = ","
	}
	patch += "}}"
	_, err := cli.CoreV1().Nodes().PatchStatus(context.TODO(), nodeName, []byte(patch))
	return err
}
func patchAdjustmentStatus(cli *resmgr.CriresmgrV1alpha1Client, status *resmgrStatus, names ...string) error {
	return nil
}
type watch struct {
	parent  *watcher
	kind    string
	ns      namespace
	name    string
	openfn  func(namespace, string) (k8swatch.Interface, error)
	queryfn func(namespace, string) (interface{}, error)
	stop    chan struct{}
	events  chan k8swatch.Event
}
type openFn func(ns namespace, name string) (k8swatch.Interface, error)
type queryFn func(ns namespace, name string) (interface{}, error)
const (
	SyntheticMissing = k8swatch.EventType("SyntheticMissing")
)
func newWatch(parent *watcher, kind string, ns namespace, open openFn, query queryFn) *watch {
	return &watch{
		parent:  parent,
		kind:    kind,
		ns:      ns,
		stop:    make(chan struct{}),
		events:  make(chan k8swatch.Event),
		openfn:  open,
		queryfn: query,
	}
}
func newNodeWatch(parent *watcher) *watch {
	w := newWatch(parent, "Node", namespace(""),
		func(ns namespace, name string) (k8swatch.Interface, error) {
			selector := meta_v1.ListOptions{FieldSelector: "metadata.name=" + name}
			k8w, err := parent.k8sCli.CoreV1().Nodes().Watch(context.TODO(), selector)
			if err != nil {
				return nil, err
			}
			return k8w, nil
		},
		func(ns namespace, name string) (interface{}, error) {
			noopts := meta_v1.GetOptions{}
			node, err := parent.k8sCli.CoreV1().Nodes().Get(context.TODO(), name, noopts)
			if err != nil {
				return nil, err
			}
			return node, nil
		})
	w.Start(nodeName)
	return w
}
func newConfigMapWatch(parent *watcher, name string, ns namespace) *watch {
	w := newWatch(parent, "ConfigMap", ns,
		func(ns namespace, name string) (k8swatch.Interface, error) {
			selector := meta_v1.ListOptions{FieldSelector: "metadata.name=" + name}
			k8w, err := parent.k8sCli.CoreV1().ConfigMaps(string(ns)).Watch(context.TODO(), selector)
			if err != nil {
				return nil, err
			}
			return k8w, nil
		},
		func(ns namespace, name string) (interface{}, error) {
			noopts := meta_v1.GetOptions{}
			cm, err := parent.k8sCli.CoreV1().ConfigMaps(string(ns)).Get(context.TODO(), name, noopts)
			if err != nil {
				return nil, err
			}
			return cm, nil
		})
	w.Start(name)
	return w
}
func newAdjustmentCRDWatch(parent *watcher, ns namespace) *watch {
	w := newWatch(parent, "AdjustmentCRD", ns,
		func(ns namespace, name string) (k8swatch.Interface, error) {
			k8w, err := parent.resmgrCli.Adjustments(string(ns)).Watch(meta_v1.ListOptions{})
			if err != nil {
				return nil, err
			}
			return k8w, nil
		},
		func(ns namespace, name string) (interface{}, error) {
			crds, err := parent.resmgrCli.Adjustments(string(ns)).List(meta_v1.ListOptions{})
			if err != nil {
				return nil, err
			}
			if crds == nil || len(crds.Items) == 0 {
				crds = nil
			}
			return crds, nil
		})
	w.Start("AdjustmentCRD")
	return w
}
func (w *watch) Name() string {
	ns, name := w.ns, w.name
	if ns != "" {
		ns += "/"
	}
	if name == "" {
		name = "<none>"
	}
	return w.kind + ":" + string(ns) + name
}