Skip to content

Instantly share code, notes, and snippets.

yuri@Yuris-MBP k8s-test-deployment % kubectl rollout history deployment/golang-demo-deployment
>>
>> deployment.apps/golang-demo-deployment
>> REVISION CHANGE-CAUSE
>> 1 <none>
>> 2 <none>
>> 3 <none>
>>
yuri@Yuris-MBP k8s-test-deployment % kubectl get deployment golang-demo-deployment -o yaml | yq eval '.spec.template.metadata.labels, .spec.template.spec.containers[].env, .spec.template.spec.containers[].resources.limits' -
>> app: golang-demo
yuri@Yuris-MBP k8s-test-deployment % kubectl rollout history deployment/golang-demo-deployment
>>
>> deployment.apps/golang-demo-deployment
>> REVISION CHANGE-CAUSE
>> 1 <none>
>> 2 <none>
>>
yuri@Yuris-MBP k8s-test-deployment % kubectl get deployment golang-demo-deployment -o yaml | yq eval '.spec.template.metadata.labels, .spec.template.spec.containers[].env, .spec.template.spec.containers[].resources.limits' -
>> app: golang-demo
>> - name: GOGC
yuri@Yuris-MBP k8s-test-deployment % ./apply.sh
>> deployment.apps/golang-demo-deployment created
>> service/golang-demo-service created
yuri@Yuris-MBP k8s-test-deployment % kubectl rollout history deployment/golang-demo-deployment
>>
>> deployment.apps/golang-demo-deployment
>> REVISION CHANGE-CAUSE
>> 1 <none>
>>
yuri@Yuris-MBP k8s-test-deployment % kubectl get deployment golang-demo-deployment -o yaml | yq eval '.spec.template.metadata.labels, .spec.template.spec.containers[].env, .spec.template.spec.containers[].resources.limits' -
func (r *MemoryAdjusterReconciler) adjustContainerMemory(ctx context.Context, deployment *appsv1.Deployment, containerIndex int, container *corev1.Container, adjuster demov1.MemoryAdjuster) error {
log := log.FromContext(ctx)
memoryIncrement, err := resource.ParseQuantity(adjuster.Spec.MemoryIncrement)
if err != nil {
return err
}
memoryLimit := container.Resources.Limits[corev1.ResourceMemory]
newMemoryLimit := memoryLimit.DeepCopy()
newMemoryLimit.Add(memoryIncrement)
func (r *MemoryAdjusterReconciler) adjustPodResources(ctx context.Context, pod *corev1.Pod, adjuster demov1.MemoryAdjuster) error {
log := log.FromContext(ctx)
log.Info("Adjusting resources for Pod", "Pod", pod.Name)
deployment := &appsv1.Deployment{}
if err := r.findDeployment(ctx, pod, deployment); err != nil {
return err
}
for i, container := range deployment.Spec.Template.Spec.Containers {
func (r *MemoryAdjusterReconciler) handleOOMPods(ctx context.Context, adjuster demov1.MemoryAdjuster) error {
log := log.FromContext(ctx)
log.Info("Handling OOM pods")
// List all pods with the given label in the namespace
podList := &corev1.PodList{}
labelSelector := client.MatchingLabels{"app": adjuster.Spec.TargetPodLabel}
if err := r.List(ctx, podList, client.InNamespace(adjuster.Namespace), labelSelector); err != nil {
return err
}
func (r *MemoryAdjusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
...
if err := r.handleOOMPods(ctx, adjuster); err != nil {
log.Error(err, "Failed to handle OOM pods")
}
return ctrl.Result{RequeueAfter: time.Minute}, nil
}
...
func (r *MemoryAdjusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
log.Info("Reconciling MemoryAdjuster...", "name", req.NamespacedName)
// Fetch the OOMAdjuster instance
var adjuster demov1.MemoryAdjuster
if err := r.Get(ctx, req.NamespacedName, &adjuster); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
...
func (r *MemoryAdjusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
// TODO(user): your logic here
return ctrl.Result{}, nil
}
...
yuri@Yuris-MBP kubebuilder-project % kubectl apply -f config/crd/bases/demo.operator.k8s.yfenyuk.io_memoryadjusters.yaml
>> customresourcedefinition.apiextensions.k8s.io/memoryadjusters.demo.operator.k8s.yfenyuk.io created
yuri@Yuris-MBP kubebuilder-project % kubectl apply -f config/samples/demo_v1_memoryadjuster.yaml
>> memoryadjuster.demo.operator.k8s.yfenyuk.io/memoryadjuster-sample created
yuri@Yuris-MBP kubebuilder-project % kubectl get crds | grep memoryadjusters
>> memoryadjusters.demo.operator.k8s.yfenyuk.io 2024-08-04T12:41:06Z
yuri@Yuris-MBP kubebuilder-project % kubectl get MemoryAdjuster memoryadjuster-sample
>> NAME AGE
>> memoryadjuster-sample 51s