Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9f180e6

Browse files
sandhosesgotti
authored andcommitted
k8s store: patch pod annotations
Use k8s client patch method to update pod annotations (in some versionsreplacing the whole pod object will return an error).
1 parentdccf63f commit9f180e6

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

‎go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/docker/libkvv0.2.1
1717
github.com/emicklei/go-restfulv2.5.0+incompatible// indirect
1818
github.com/emicklei/go-restful-swagger12v0.0.0-20170208215640-dcef7f557305// indirect
19+
github.com/evanphx/json-patchv4.5.0+incompatible
1920
github.com/go-openapi/jsonpointerv0.0.0-20170102174223-779f45308c19// indirect
2021
github.com/go-openapi/jsonreferencev0.0.0-20161105162150-36d33bfe519e// indirect
2122
github.com/go-openapi/specv0.0.0-20180131233152-f3499b5df538// indirect

‎go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ github.com/docker/leadership v0.1.0/go.mod h1:6yL2hg00l43fYEJagcF7eIS4PootU7TAO1
2525
github.com/docker/libkvv0.2.1/go.mod h1:r5hEwHwW8dr0TFBYGCarMNbrQOiwL1xoqDYZ/JqoTK0=
2626
github.com/emicklei/go-restfulv2.5.0+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
2727
github.com/emicklei/go-restful-swagger12v0.0.0-20170208215640-dcef7f557305/go.mod h1:qr0VowGBT4CS4Q8vFF8BSeKz34PuqKGxs/L0IAQA9DQ=
28+
github.com/evanphx/json-patchv4.5.0+incompatible h1:ouOWdg56aJriqS0huScTkVXPC5IcNrDCXZ6OoTAWu7M=
29+
github.com/evanphx/json-patchv4.5.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
2830
github.com/fatih/colorv1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
2931
github.com/ghodss/yamlv1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
3032
github.com/go-kit/kitv0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=

‎internal/store/k8s.go

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import (
2323
"github.com/sorintlab/stolon/internal/cluster"
2424
"github.com/sorintlab/stolon/internal/util"
2525

26+
jsonpatch"github.com/evanphx/json-patch"
2627
v1"k8s.io/api/core/v1"
2728
apierrors"k8s.io/apimachinery/pkg/api/errors"
2829
metav1"k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/labels"
31+
"k8s.io/apimachinery/pkg/types"
3032
"k8s.io/client-go/kubernetes"
3133
"k8s.io/client-go/kubernetes/scheme"
3234
v1core"k8s.io/client-go/kubernetes/typed/core/v1"
@@ -72,6 +74,43 @@ func (s *KubeStore) labelSelector(componentLabel ComponentLabelValue) labels.Sel
7274
returnlabels.SelectorFromSet(selector)
7375
}
7476

77+
func (s*KubeStore)patchKubeStatusAnnotation(annotationData []byte)error {
78+
podsClient:=s.client.CoreV1().Pods(s.namespace)
79+
retryErr:=retry.RetryOnConflict(retry.DefaultRetry,func()error {
80+
pod,err:=podsClient.Get(s.podName, metav1.GetOptions{})
81+
iferr!=nil {
82+
returnfmt.Errorf("failed to get latest version of pod: %v",err)
83+
}
84+
85+
oldPodJSON,err:=json.Marshal(pod)
86+
iferr!=nil {
87+
returnfmt.Errorf("failed to marshal pod: %v",err)
88+
}
89+
90+
ifpod.Annotations==nil {
91+
pod.Annotations=map[string]string{}
92+
}
93+
pod.Annotations[util.KubeStatusAnnnotation]=string(annotationData)
94+
95+
newPodJSON,err:=json.Marshal(pod)
96+
iferr!=nil {
97+
returnfmt.Errorf("failed to marshal pod: %v",err)
98+
}
99+
100+
patchBytes,err:=jsonpatch.CreateMergePatch(oldPodJSON,newPodJSON)
101+
iferr!=nil {
102+
returnfmt.Errorf("failed to create pod merge patch: %v",err)
103+
}
104+
105+
_,err=podsClient.Patch(s.podName,types.MergePatchType,patchBytes)
106+
returnerr
107+
})
108+
ifretryErr!=nil {
109+
returnfmt.Errorf("update failed: %v",retryErr)
110+
}
111+
returnnil
112+
}
113+
75114
func (s*KubeStore)AtomicPutClusterData(ctx context.Context,cd*cluster.ClusterData,previous*KVPair) (*KVPair,error) {
76115
cdj,err:=json.Marshal(cd)
77116
iferr!=nil {
@@ -210,23 +249,7 @@ func (s *KubeStore) SetKeeperInfo(ctx context.Context, id string, ms *cluster.Ke
210249
iferr!=nil {
211250
returnerr
212251
}
213-
podsClient:=s.client.CoreV1().Pods(s.namespace)
214-
retryErr:=retry.RetryOnConflict(retry.DefaultRetry,func()error {
215-
result,err:=podsClient.Get(s.podName, metav1.GetOptions{})
216-
iferr!=nil {
217-
returnfmt.Errorf("failed to get latest version of pod: %v",err)
218-
}
219-
ifresult.Annotations==nil {
220-
result.Annotations=map[string]string{}
221-
}
222-
result.Annotations[util.KubeStatusAnnnotation]=string(msj)
223-
_,err=podsClient.Update(result)
224-
returnerr
225-
})
226-
ifretryErr!=nil {
227-
returnfmt.Errorf("update failed: %v",retryErr)
228-
}
229-
returnnil
252+
returns.patchKubeStatusAnnotation(msj)
230253
}
231254

232255
func (s*KubeStore)GetKeepersInfo(ctx context.Context) (cluster.KeepersInfo,error) {
@@ -261,23 +284,7 @@ func (s *KubeStore) SetSentinelInfo(ctx context.Context, si *cluster.SentinelInf
261284
iferr!=nil {
262285
returnerr
263286
}
264-
podsClient:=s.client.CoreV1().Pods(s.namespace)
265-
retryErr:=retry.RetryOnConflict(retry.DefaultRetry,func()error {
266-
result,err:=podsClient.Get(s.podName, metav1.GetOptions{})
267-
iferr!=nil {
268-
returnfmt.Errorf("failed to get latest version of pod: %v",err)
269-
}
270-
ifresult.Annotations==nil {
271-
result.Annotations=map[string]string{}
272-
}
273-
result.Annotations[util.KubeStatusAnnnotation]=string(sij)
274-
_,err=podsClient.Update(result)
275-
returnerr
276-
})
277-
ifretryErr!=nil {
278-
returnfmt.Errorf("update failed: %v",retryErr)
279-
}
280-
returnnil
287+
returns.patchKubeStatusAnnotation(sij)
281288
}
282289

283290
func (s*KubeStore)GetSentinelsInfo(ctx context.Context) (cluster.SentinelsInfo,error) {
@@ -312,23 +319,7 @@ func (s *KubeStore) SetProxyInfo(ctx context.Context, pi *cluster.ProxyInfo, ttl
312319
iferr!=nil {
313320
returnerr
314321
}
315-
podsClient:=s.client.CoreV1().Pods(s.namespace)
316-
retryErr:=retry.RetryOnConflict(retry.DefaultRetry,func()error {
317-
result,err:=podsClient.Get(s.podName, metav1.GetOptions{})
318-
iferr!=nil {
319-
returnfmt.Errorf("failed to get latest version of pod: %v",err)
320-
}
321-
ifresult.Annotations==nil {
322-
result.Annotations=map[string]string{}
323-
}
324-
result.Annotations[util.KubeStatusAnnnotation]=string(pij)
325-
_,err=podsClient.Update(result)
326-
returnerr
327-
})
328-
ifretryErr!=nil {
329-
returnfmt.Errorf("update failed: %v",retryErr)
330-
}
331-
returnnil
322+
returns.patchKubeStatusAnnotation(pij)
332323
}
333324

334325
func (s*KubeStore)GetProxiesInfo(ctx context.Context) (cluster.ProxiesInfo,error) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp