Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Update README.md #4

wants to merge 1 commit into from

Conversation

mvleandro
Copy link
Contributor

Removing the know flaws section because when the watcher starts it picks up all the changes from the beginning, that is, if something happens between the end of the bootstrap and the start of the watcher, it will pick it up. There is no flaw.

Removing the know flaws section because when the watcher starts it picks up all the changes from the beginning, that is, if something happens between the end of the bootstrap and the start of the watcher, it will pick it up. There is no flaw.
@mvleandro mvleandro requested a review from a team as a code owner July 31, 2023 18:28
@benlangfeld
Copy link
Member

How do you define "the beginning"? Do you have a documentation reference?

@mvleandro
Copy link
Contributor Author

How do you define "the beginning"? Do you have a documentation reference?

Yes, I have not only documentation, but I have tested it. This behavior comes from the Kubernetes API itself. Reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#watch-configmap-v1-core

You can run the code bellow on your machine and try It by yourself.

package main

import (
	"context"
	"flag"
	"fmt"
	"path/filepath"

	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
)

func main() {
	var kubeconfig *string
	if home := homedir.HomeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()

	var betaPXClient *kubernetes.Clientset
	//var betaHQClient *kubernetes.Clientset
	var config *rest.Config
	var err error

	// using `contextName` context in kubeConfig
	contextName := "app-beta-hq"
	config, err = buildConfigWithContextFromFlags(contextName, *kubeconfig)
	if err != nil {
		panic(err)
	}

	// // create the clientset
	// betaHQClient, err = kubernetes.NewForConfig(config)
	// if err != nil {
	// 	panic(err.Error())
	// }

	// using `contextName` context in kubeConfig
	contextName = "app-beta-px"
	config, err = buildConfigWithContextFromFlags(contextName, *kubeconfig)
	if err != nil {
		panic(err)
	}

	// create the clientset
	betaPXClient, err = kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}

	pods, err := betaPXClient.CoreV1().Pods("monitoring").Watch(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}

	for event := range pods.ResultChan() {
		pod := event.Object.(*corev1.Pod)

		fmt.Printf("Pod %s was %s on namespace %s.\n", pod.Name, event.Type, pod.Namespace)
	}
}

func buildConfigWithContextFromFlags(context string, kubeconfigPath string) (*rest.Config, error) {
	return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
		&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
		&clientcmd.ConfigOverrides{
			CurrentContext: context,
		}).ClientConfig()
}

@benlangfeld
Copy link
Member

How do you define "the beginning"? Do you have a documentation reference?

Yes, I have not only documentation, but I have tested it. This behavior comes from the Kubernetes API itself. Reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.27/#watch-configmap-v1-core

You can run the code bellow on your machine and try It by yourself.

package main

import (
	"context"
	"flag"
	"fmt"
	"path/filepath"

	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
)

func main() {
	var kubeconfig *string
	if home := homedir.HomeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()

	var betaPXClient *kubernetes.Clientset
	//var betaHQClient *kubernetes.Clientset
	var config *rest.Config
	var err error

	// using `contextName` context in kubeConfig
	contextName := "app-beta-hq"
	config, err = buildConfigWithContextFromFlags(contextName, *kubeconfig)
	if err != nil {
		panic(err)
	}

	// // create the clientset
	// betaHQClient, err = kubernetes.NewForConfig(config)
	// if err != nil {
	// 	panic(err.Error())
	// }

	// using `contextName` context in kubeConfig
	contextName = "app-beta-px"
	config, err = buildConfigWithContextFromFlags(contextName, *kubeconfig)
	if err != nil {
		panic(err)
	}

	// create the clientset
	betaPXClient, err = kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}

	pods, err := betaPXClient.CoreV1().Pods("monitoring").Watch(context.TODO(), metav1.ListOptions{})
	if err != nil {
		panic(err.Error())
	}

	for event := range pods.ResultChan() {
		pod := event.Object.(*corev1.Pod)

		fmt.Printf("Pod %s was %s on namespace %s.\n", pod.Name, event.Type, pod.Namespace)
	}
}

func buildConfigWithContextFromFlags(context string, kubeconfigPath string) (*rest.Config, error) {
	return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
		&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
		&clientcmd.ConfigOverrides{
			CurrentContext: context,
		}).ClientConfig()
}

Does this work on the basis of the sendInitialEvents parameter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants