Last active
September 13, 2021 07:46
-
-
Save leonsteinhaeuser/6dd934f1d30ed0d270351bdc016442fd to your computer and use it in GitHub Desktop.
Watch k8s (kubernetes) pod restarts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | |
"k8s.io/apimachinery/pkg/runtime" | |
"k8s.io/apimachinery/pkg/watch" | |
"k8s.io/client-go/dynamic" | |
ctrl "sigs.k8s.io/controller-runtime" | |
appsv1 "k8s.io/api/apps/v1" | |
) | |
func panicIfError(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func main() { | |
config, err := ctrl.GetConfig() | |
panicIfError(err) | |
dynamicInterface, err := dynamic.NewForConfig(config) | |
panicIfError(err) | |
// defines a group version resource (gvr) that is used to identify objects within kubernetes. | |
// If the gvr could not be found within kubernetes, the string inside the WithResource function is probably false. | |
groupVersionResource := appsv1.SchemeGroupVersion.WithResource("deployments") | |
// watches the specified type | |
watchInterface, err := dynamicInterface.Resource(groupVersionResource).Watch(context.Background(), metav1.SingleObject(metav1.ObjectMeta{ | |
Name: "execserver-execserver-sample", | |
})) | |
panicIfError(err) | |
// receive the channel containing the data consumed by the watcher | |
wch := watchInterface.ResultChan() | |
//repCount := 0 | |
setAvailableSeen := false | |
for { | |
dt := <-wch | |
// convert the runtime.Object to *unstructured.Unstructured | |
// it is not possible to convert this directly to v1.Instance because kubernetes returns an object of type unstructured.Unstructured | |
typeUnstructured := dt.Object.(*unstructured.Unstructured) | |
inst := &appsv1.Deployment{} | |
// finally we can convert the object from the unstructured.Unstructured type to the concrete type | |
err := runtime.DefaultUnstructuredConverter.FromUnstructured(typeUnstructured.UnstructuredContent(), inst) | |
panicIfError(err) | |
if dt.Type != watch.Modified { | |
continue | |
} | |
countCond := len(inst.Status.Conditions) | |
fmt.Println("======================================================") | |
fmt.Println("Type:", dt.Type) | |
fmt.Println("repcount-1:", inst.Status.Conditions[countCond-1].Reason) | |
if inst.Status.Conditions[countCond-1].Reason != "NewReplicaSetAvailable" { | |
continue | |
} | |
if inst.Status.Conditions[countCond-1].Reason == "NewReplicaSetAvailable" && !setAvailableSeen { | |
setAvailableSeen = !setAvailableSeen | |
continue | |
} | |
if inst.Status.Conditions[countCond-1].Reason == "NewReplicaSetAvailable" && setAvailableSeen { | |
fmt.Println("Restarted") | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not work during the scaling process. When the pod size is changed, this code breaks out and we never get the actual restart change thingy.