Use code-generator to generate clientset/informer/lister (#1492)
To reduce maintenance effort and avoid writing duplicate informer code, use code-generator to generate clientset/informer/lister code.
This commit is contained in:
@@ -106,7 +106,7 @@ func (kw *KubeWatcher) svc() {
|
||||
case SYNC:
|
||||
newWatchUids := make(map[types.UID]bool)
|
||||
for _, w := range req.watches {
|
||||
newWatchUids[w.Metadata.UID] = true
|
||||
newWatchUids[w.ObjectMeta.UID] = true
|
||||
}
|
||||
// Remove old watches
|
||||
for uid, ws := range kw.watches {
|
||||
@@ -116,7 +116,7 @@ func (kw *KubeWatcher) svc() {
|
||||
}
|
||||
// Add new watches
|
||||
for _, w := range req.watches {
|
||||
if _, ok := kw.watches[w.Metadata.UID]; !ok {
|
||||
if _, ok := kw.watches[w.ObjectMeta.UID]; !ok {
|
||||
kw.addWatch(&w)
|
||||
}
|
||||
}
|
||||
@@ -176,23 +176,23 @@ func createKubernetesWatch(kubeClient *kubernetes.Clientset, w *fv1.KubernetesWa
|
||||
}
|
||||
|
||||
func (kw *KubeWatcher) addWatch(w *fv1.KubernetesWatchTrigger) error {
|
||||
kw.logger.Info("adding watch", zap.String("name", w.Metadata.Name), zap.Any("function", w.Spec.FunctionReference))
|
||||
kw.logger.Info("adding watch", zap.String("name", w.ObjectMeta.Name), zap.Any("function", w.Spec.FunctionReference))
|
||||
ws, err := MakeWatchSubscription(kw.logger.Named("watchsubscription"), w, kw.kubernetesClient, kw.publisher)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kw.watches[w.Metadata.UID] = *ws
|
||||
kw.watches[w.ObjectMeta.UID] = *ws
|
||||
return nil
|
||||
}
|
||||
|
||||
func (kw *KubeWatcher) removeWatch(w *fv1.KubernetesWatchTrigger) error {
|
||||
kw.logger.Info("removing watch", zap.String("name", w.Metadata.Name), zap.Any("function", w.Spec.FunctionReference))
|
||||
ws, ok := kw.watches[w.Metadata.UID]
|
||||
kw.logger.Info("removing watch", zap.String("name", w.ObjectMeta.Name), zap.Any("function", w.Spec.FunctionReference))
|
||||
ws, ok := kw.watches[w.ObjectMeta.UID]
|
||||
if !ok {
|
||||
return ferror.MakeError(ferror.ErrorNotFound,
|
||||
fmt.Sprintf("watch doesn't exist: %v", w.Metadata))
|
||||
fmt.Sprintf("watch doesn't exist: %v", w.ObjectMeta))
|
||||
}
|
||||
delete(kw.watches, w.Metadata.UID)
|
||||
delete(kw.watches, w.ObjectMeta.UID)
|
||||
ws.stop()
|
||||
return nil
|
||||
}
|
||||
@@ -222,7 +222,7 @@ func (ws *watchSubscription) restartWatch() error {
|
||||
retries := 60
|
||||
for {
|
||||
ws.logger.Info("(re)starting watch",
|
||||
zap.Any("watch", ws.watch.Metadata),
|
||||
zap.Any("watch", ws.watch.ObjectMeta),
|
||||
zap.String("namespace", ws.watch.Spec.Namespace),
|
||||
zap.String("type", ws.watch.Spec.Type),
|
||||
zap.String("last_resource_version", ws.lastResourceVersion))
|
||||
@@ -250,7 +250,7 @@ func getResourceVersion(obj runtime.Object) (string, error) {
|
||||
}
|
||||
|
||||
func (ws *watchSubscription) eventDispatchLoop() {
|
||||
ws.logger.Info("listening to watch", zap.String("name", ws.watch.Metadata.Name))
|
||||
ws.logger.Info("listening to watch", zap.String("name", ws.watch.ObjectMeta.Name))
|
||||
for {
|
||||
// check watchSubscription is stopped or not before waiting for event
|
||||
// comes from the kubeWatch.ResultChan(). This fix the edge case that
|
||||
@@ -263,14 +263,14 @@ func (ws *watchSubscription) eventDispatchLoop() {
|
||||
if !more {
|
||||
if ws.isStopped() {
|
||||
// watch is removed by user.
|
||||
ws.logger.Warn("watch stopped", zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
ws.logger.Warn("watch stopped", zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
return
|
||||
} else {
|
||||
// watch closed due to timeout, restart it.
|
||||
ws.logger.Warn("watch timed out - restarting", zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
ws.logger.Warn("watch timed out - restarting", zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
err := ws.restartWatch()
|
||||
if err != nil {
|
||||
ws.logger.Panic("failed to restart watch", zap.Error(err), zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
ws.logger.Panic("failed to restart watch", zap.Error(err), zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -278,19 +278,19 @@ func (ws *watchSubscription) eventDispatchLoop() {
|
||||
|
||||
if ev.Type == watch.Error {
|
||||
e := errors.FromObject(ev.Object)
|
||||
ws.logger.Warn("watch error - retrying after one second", zap.Error(e), zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
ws.logger.Warn("watch error - retrying after one second", zap.Error(e), zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
// Start from the beginning to get around "too old resource version"
|
||||
ws.lastResourceVersion = ""
|
||||
time.Sleep(time.Second)
|
||||
err := ws.restartWatch()
|
||||
if err != nil {
|
||||
ws.logger.Panic("failed to restart watch", zap.Error(err), zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
ws.logger.Panic("failed to restart watch", zap.Error(err), zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
}
|
||||
continue
|
||||
}
|
||||
rv, err := getResourceVersion(ev.Object)
|
||||
if err != nil {
|
||||
ws.logger.Error("error getting resourceVersion from object", zap.Error(err), zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
ws.logger.Error("error getting resourceVersion from object", zap.Error(err), zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
} else {
|
||||
ws.lastResourceVersion = rv
|
||||
}
|
||||
@@ -299,7 +299,7 @@ func (ws *watchSubscription) eventDispatchLoop() {
|
||||
var buf bytes.Buffer
|
||||
err = printKubernetesObject(ev.Object, &buf)
|
||||
if err != nil {
|
||||
ws.logger.Error("failed to serialize object", zap.Error(err), zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
ws.logger.Error("failed to serialize object", zap.Error(err), zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
// TODO send a POST request indicating error
|
||||
}
|
||||
|
||||
@@ -314,14 +314,14 @@ func (ws *watchSubscription) eventDispatchLoop() {
|
||||
if ws.watch.Spec.FunctionReference.Type != fv1.FunctionReferenceTypeFunctionName {
|
||||
ws.logger.Error("unsupported function ref type - cannot publish event",
|
||||
zap.Any("type", ws.watch.Spec.FunctionReference.Type),
|
||||
zap.String("watch_name", ws.watch.Metadata.Name))
|
||||
zap.String("watch_name", ws.watch.ObjectMeta.Name))
|
||||
continue
|
||||
}
|
||||
|
||||
// with the addition of multi-tenancy, the users can create functions in any namespace. however,
|
||||
// the triggers can only be created in the same namespace as the function.
|
||||
// so essentially, function namespace = trigger namespace.
|
||||
url := utils.UrlForFunction(ws.watch.Spec.FunctionReference.Name, ws.watch.Metadata.Namespace)
|
||||
url := utils.UrlForFunction(ws.watch.Spec.FunctionReference.Name, ws.watch.ObjectMeta.Namespace)
|
||||
ws.publisher.Publish(buf.String(), headers, url)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ func MakeWatchSync(logger *zap.Logger, client *crd.FissionClient, kubeWatcher *K
|
||||
func (ws *WatchSync) syncSvc() {
|
||||
// TODO watch instead of polling
|
||||
for {
|
||||
watches, err := ws.client.KubernetesWatchTriggers(metav1.NamespaceAll).List(metav1.ListOptions{})
|
||||
watches, err := ws.client.V1().KubernetesWatchTriggers(metav1.NamespaceAll).List(metav1.ListOptions{})
|
||||
if err != nil {
|
||||
ws.logger.Fatal("failed to get Kubernetes watch trigger list", zap.Error(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user