Code Monkey home page Code Monkey logo

Comments (6)

ericchiang avatar ericchiang commented on May 16, 2024 1

@F21 do you know what the retry behavior for the upstream client is? I've generally avoided adding higher level APIs because:

  • It's hard to inject loggers into higher level interfaces (client-go just assumes glog everwhere).
  • It's hard to get it right, and hard to produce a typed interface without generating a lot more code.
  • client-go already implements these interfaces.

I think I'd rather have this client provide raw API capabilities and provide examples like the one above for users who want the added features.

from k8s.

ericchiang avatar ericchiang commented on May 16, 2024

watcher.Next is actually just de-serializing from the HTTP long poll. So when Next fails, it indicate the watcher is dead and shouldn't be called again. Maybe I should provide better examples?

It sounds like you want to actually watch forever, handling each event as it comes in, then attempting to re-watch when the connection fails. Maybe something like this? (I haven't actually tested this)

type podEvent struct {
	event *versioned.Event
	pod   *v1.Pod
}

// watchPods watches pods in the client's namespace, calling handler on each
// event received from the watch. It watches until context is canceled.
func watchPods(ctx context.Context, client *k8s.Client, handler func(e podEvent)) {
	// Last observed resource version.
	resourceVersion := ""

	// Channels used for each instance of the watch.
	eventCh := make(chan podEvent, 1)
	errCh := make(chan error, 1)
	for {
		opt := k8s.ResourceVersion(resourceVersion)

		watcher, err := client.CoreV1().WatchPods(ctx, client.Namespace, opt)
		if err != nil {
			log.Printf("watch pods: %v", err)
			// Implement backoff behavior here.
			continue
		}

		for {
			go func() {
				event, pod, err := watcher.Next()
				if err != nil {
					errCh <- err
					return
				}
				eventCh <- podEvent{event, pod}
			}()

			select {
			case e := <-eventCh:
				// Update last observed resource version.
				resourceVersion = *e.pod.Metadata.ResourceVersion
				handler(e)
			case err := <-errCh:
				log.Printf("watch err: %v", err)
				break
			case <-ctx.Done():
				watcher.Close()
				return
			}
		}
	}
}

from k8s.

ericchiang avatar ericchiang commented on May 16, 2024

I think you bring up a good point though. Watches are a little different than client-go and that should be called out in the docs.

from k8s.

F21 avatar F21 commented on May 16, 2024

Hey, thanks! Your example makes a lot of sense.

It would be great if a higher level api is implemented, smilar to the one in the official k8s.io client: https://godoc.org/k8s.io/apimachinery/pkg/watch#Interface

It would then be pretty easy to use the watcher in a for select loop and read events over a channel.

from k8s.

F21 avatar F21 commented on May 16, 2024

@ericchiang Unfortunately, no. However, your reasoning makes a lot of sense!

from k8s.

antoniomo avatar antoniomo commented on May 16, 2024

watcher.Next is actually just de-serializing from the HTTP long poll. So when Next fails, it indicate the watcher is dead and shouldn't be called again.

In the above snippet, the code:

			case err := <-errCh:
				log.Printf("watch err: %v", err)
				break

Would just break from the select statement, causing us to restart the inner for-loop and re-using the existing watcher in a new goroutine with watcher.Next(), without reconnecting. Am I missing something?

from k8s.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.