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

Closed
wants to merge 1 commit into from
Closed

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?

Copy link

github-actions bot commented Jan 8, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 24 hours if no further activity occurs.
If this change is desirable, please accelerate completing it. If it is not, please close the PR. If you're blocked on something, please ensure there's a reference to this PR in a story on your team's board so the team will follow up, and consider closing the PR for now.
Please do not artificially extend the deadline with a dummy comment. If necessary, provide a status update, such as "this change is being actively tested".
Thank you for your contributions and your collaboration in reducing WIP and cycle time.

@github-actions github-actions bot added the Stale label Jan 8, 2025
@github-actions github-actions bot closed this Jan 9, 2025
@github-actions github-actions bot deleted the mvleandro-patch-1 branch January 9, 2025 02:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants