Table of Contents
Problem content
Workaround
Home Backend Development Golang golang error 'object has been modified' on k8s operator

golang error 'object has been modified' on k8s operator

Feb 13, 2024 pm 03:42 PM
golang development

golang 在 k8s 运算符上出现错误“对象已被修改”

php editor Strawberry encountered a common error when exploring the use of golang to develop kubernetes operators: "The object has been modified". This error usually occurs when multiple goroutines modify the same object at the same time, resulting in data inconsistency or race conditions. In this article, we will explore the causes and solutions of this error to help developers better understand and deal with similar problems.

Problem content

The "object has been modified" error appears on the k8s operator

1

import "sigs.k8s.io/controller-runtime"

Copy after login

There is a lot of discussion about this error. The main answer is "This problem occurs because I have an old version of the object when I try to update." But I also have some questions. In my operator, for some scenarios, I need to update a pod's annotation 2 times during a single "coordinate" call. Of course I often get the "object has been modified" error.

Question: I want to know where 'r.Get()' and 'r.Update()' get/update objects? From local cache, or API server?

1: I think 'r.Get()' is getting the object from 'cache' and 'r.Update()' is updating the object to cache, right? If so, why am I getting this error? If the pod object changes due to reasons other than the operator, will I never be able to update my pod object during the current "reconciliation"? (Since the cached object is local, it is already out of sync with the API server.) Why "retry" sometimes to get the latest object?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import "sigs.k8s.io/controller-runtime"

 

var pod corev1.Pod

if err := r.Get(ctx, req.NamespacedName, &pod); err != nil {

    if !apierrors.IsNotFound(err) {

        log.Error(err, "unable to get pod")

        return ctrl.Result{}, err

    }

}

 

if err := r.Update(ctx, &pod); err != nil {

    log.Error(err, "unable to update chaosctl status")

    return ctrl.Result{}, err

}

Copy after login

2: If 'r.Get()' is to get the object from the API server, and 'r.Update()' also updates the API server. Why do I need to retry updating the object?

Workaround

When you use r.Get() and r.Update() in a Kubernetes controller, the interaction with the API server involves the local cache and the API server itself.

r.Get():

The r.Get() function retrieves the object from the local cache if it exists; otherwise, it gets it from the API server. If the object exists in the local cache, it is returned immediately. If not, request the API server to obtain the object, and store the obtained object in the local cache for subsequent use. r.Update():

r.Update() The function updates objects in the local cache and API server. Update operations to the API server may fail with a "conflict" error if the object has been modified in the local cache since it was originally retrieved. This occurs when the version of an object in the cache does not match the version on the API server, indicating that someone else modified the object in the meantime.

Strategies can be adopted to deal with it -

  1. Optimistic Concurrency Control (OCC): - Update to match previous version.
  2. retry -

1

2

3

4

5

6

retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {

        return r.Update(ctx, pod)

    })

    if retryErr != nil {

        return retryErr

    }

Copy after login

The above is the detailed content of golang error 'object has been modified' on k8s operator. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Develop powerful desktop applications with Golang Develop powerful desktop applications with Golang Mar 19, 2024 pm 05:45 PM

Use Golang to develop powerful desktop applications. With the continuous development of the Internet, people have become inseparable from various types of desktop applications. For developers, it is crucial to use efficient programming languages ​​to develop powerful desktop applications. This article will introduce how to use Golang (Go language) to develop powerful desktop applications and provide some specific code examples. Golang is an open source programming language developed by Google. It has the characteristics of simplicity, efficiency, strong concurrency, etc., and is very suitable for

Security challenges in Golang development: How to avoid being exploited for virus creation? Security challenges in Golang development: How to avoid being exploited for virus creation? Mar 19, 2024 pm 12:39 PM

Security challenges in Golang development: How to avoid being exploited for virus creation? With the wide application of Golang in the field of programming, more and more developers choose to use Golang to develop various types of applications. However, like other programming languages, there are security challenges in Golang development. In particular, Golang's power and flexibility also make it a potential virus creation tool. This article will delve into security issues in Golang development and provide some methods to avoid G

Steps and tips for programming with Golang on Mac Steps and tips for programming with Golang on Mac Mar 03, 2024 am 08:30 AM

Title: Steps and techniques for using Golang programming on Mac In the current field of software development, Golang (also known as Go), as an efficient, concise, and highly concurrency programming language, has attracted the attention of more and more developers. use. When programming Golang on the Mac platform, you can use some tools and techniques to improve development efficiency. This article will introduce the steps and techniques of using Golang programming on Mac, and provide specific code examples to help readers better understand and apply. Step 1: Install Gol

Research on the impact and role of Golang on the development of blockchain Research on the impact and role of Golang on the development of blockchain Feb 26, 2024 pm 04:24 PM

Golang (Go language for short) as a programming language has gradually emerged in the blockchain field in recent years. Its efficient concurrent processing capabilities and concise syntax features make it a favored choice in blockchain development. This article will explore how Golang helps the development of blockchain and demonstrate its superiority in blockchain applications through specific code examples. 1. Golang’s advantages in the blockchain field: Efficient concurrent processing capabilities: Nodes in the blockchain system need to process a large amount of transactions and data at the same time, and Gola

What programming languages ​​are commonly used by Golang developers? What programming languages ​​are commonly used by Golang developers? Mar 18, 2024 pm 09:06 PM

Golang is an open source programming language developed by Google and is widely used in back-end service development, cloud computing, network programming and other fields. As a statically typed language, Golang has an efficient concurrency model and a powerful standard library, so it is favored by developers. However, in actual development, Golang developers usually need to combine other programming languages ​​for project development to meet the needs of different scenarios. PythonPython is an object-oriented programming language that is concise, clear, and easy to learn.

Go kit framework helps improve Golang API performance Go kit framework helps improve Golang API performance May 07, 2024 pm 03:24 PM

Gokit is a Golang microservice framework that improves API performance through optimized, scalable, maintainable and test-friendly features. It provides a range of tools and patterns that enable users to quickly build performant and maintainable APIs. In actual production, it is widely used in API construction of large platforms such as Netflix, Spotify and Uber, handling massive requests.

Advantages and Disadvantages of Using Golang to Develop Mobile Games Advantages and Disadvantages of Using Golang to Develop Mobile Games Mar 05, 2024 pm 03:51 PM

Advantages and Disadvantages of Using Golang to Develop Mobile Games With the popularity of mobile devices and the continuous improvement of their performance, the mobile game market is becoming more and more popular, attracting more and more developers to join it. When choosing a development language, Golang, as a fast, efficient and easy-to-learn language, attracts the attention of many developers. This article will discuss the advantages and disadvantages of using Golang to develop mobile games, and illustrate it through specific code examples. Advantages: Strong cross-platform: Golang can be compiled into binaries for different platforms

Golang's approach to implementing cross-platform desktop applications Golang's approach to implementing cross-platform desktop applications Mar 20, 2024 am 11:39 AM

Golang (also known as Go language) is an open source programming language developed by Google. It is increasingly popular among developers through its simplicity, efficiency and powerful features. When implementing cross-platform desktop applications, Golang also provides some powerful tools and libraries to make development easier and more efficient. This article will introduce how to use Golang to implement cross-platform desktop applications and provide specific code examples for reference. 1. Use fyne library to create cross-platform GUI applications in Golang, one

See all articles