Skip to content

Commit daa3a38

Browse files
author
玖宇
committed
fix comments
1 parent 8059abd commit daa3a38

File tree

4 files changed

+89
-41
lines changed

4 files changed

+89
-41
lines changed

pkg/application/inject/fuse/mount_point_script.go

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/fluid-cloudnative/fluid/pkg/utils"
2929
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
3030
corev1 "k8s.io/api/core/v1"
31+
"k8s.io/client-go/util/retry"
3132
)
3233

3334
func (s *Injector) injectCheckMountReadyScript(podSpecs *mutator.MutatingPodSpecs, runtimeInfos map[string]base.RuntimeInfoInterface) error {
@@ -154,24 +155,33 @@ func (s *Injector) ensureScriptConfigMapExists(namespace string) (*poststart.Scr
154155
return appScriptGen, nil
155156
}
156157

157-
// ConfigMap exists, check if the script SHA256 annotation matches
158+
// ConfigMap exists, check if the script SHA256 annotation matches; update with retry on conflict.
158159
currentSHA256 := appScriptGen.GetScriptSHA256()
159-
if existingCM.Annotations != nil {
160-
if annotationSHA256, ok := existingCM.Annotations[common.AnnotationCheckMountScriptSHA256]; ok && annotationSHA256 == currentSHA256 {
161-
s.log.V(1).Info("configmap script is up-to-date, skip update", "configMap", cmKey)
162-
return appScriptGen, nil
160+
if err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
161+
latest, getErr := kubeclient.GetConfigmapByName(s.client, cm.Name, cm.Namespace)
162+
if getErr != nil {
163+
return getErr
163164
}
164-
}
165-
166-
// SHA256 mismatch or annotation missing: update the ConfigMap with latest script and SHA256
167-
s.log.Info("configmap script SHA256 mismatch or annotation missing, updating", "configMap", cmKey, "expectedSHA256", currentSHA256)
168-
existingCM.Data = cm.Data
169-
if existingCM.Annotations == nil {
170-
existingCM.Annotations = map[string]string{}
171-
}
172-
existingCM.Annotations[common.AnnotationCheckMountScriptSHA256] = currentSHA256
173-
if err = s.client.Update(context.TODO(), existingCM); err != nil {
174-
s.log.Error(err, "error when updating configMap", "cm.Name", cm.Name, "cm.Namespace", cm.Namespace)
165+
if latest == nil {
166+
// Deleted between Get calls; recreate
167+
return s.client.Create(context.TODO(), cm)
168+
}
169+
if latest.Annotations != nil {
170+
if annotationSHA256, ok := latest.Annotations[common.AnnotationCheckMountScriptSHA256]; ok && annotationSHA256 == currentSHA256 {
171+
s.log.V(1).Info("configmap script is up-to-date, skip update", "configMap", cmKey)
172+
return nil
173+
}
174+
}
175+
// SHA256 mismatch or annotation missing: update the ConfigMap with latest script and SHA256
176+
s.log.Info("configmap script SHA256 mismatch or annotation missing, updating", "configMap", cmKey, "expectedSHA256", currentSHA256)
177+
latest.Data = cm.Data
178+
if latest.Annotations == nil {
179+
latest.Annotations = map[string]string{}
180+
}
181+
latest.Annotations[common.AnnotationCheckMountScriptSHA256] = currentSHA256
182+
return s.client.Update(context.TODO(), latest)
183+
}); err != nil {
184+
s.log.Error(err, "error when ensuring configMap is up-to-date", "cm.Name", cm.Name, "cm.Namespace", cm.Namespace)
175185
return nil, err
176186
}
177187

pkg/application/inject/fuse/mutator/mutator_default.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ import (
2222
"encoding/hex"
2323
"fmt"
2424
"path/filepath"
25+
"reflect"
2526
"strings"
2627
"time"
2728

2829
"github.com/go-logr/logr"
2930
"github.com/pkg/errors"
3031
corev1 "k8s.io/api/core/v1"
3132
"k8s.io/apimachinery/pkg/types"
33+
"k8s.io/client-go/util/retry"
3234
"sigs.k8s.io/controller-runtime/pkg/client"
3335

3436
"github.com/fluid-cloudnative/fluid/pkg/application/inject/fuse/poststart"
@@ -338,30 +340,31 @@ func prepareFuseContainerPostStartScript(helper *helperData) error {
338340
}
339341
}
340342
} else {
341-
// ConfigMap exists, check if the script SHA256 annotation matches
343+
// ConfigMap exists; update with retry on conflict to handle concurrent webhook mutations.
342344
currentSHA256 := gen.GetScriptSHA256()
343-
needUpdate := true
344-
if existingCM.Annotations != nil {
345-
if annotationSHA256, ok := existingCM.Annotations[common.AnnotationCheckMountScriptSHA256]; ok && annotationSHA256 == currentSHA256 {
346-
needUpdate = false
345+
if err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
346+
latest, getErr := kubeclient.GetConfigmapByName(helper.client, cmKey.Name, cmKey.Namespace)
347+
if getErr != nil {
348+
return getErr
347349
}
348-
}
349-
350-
if needUpdate {
351-
// SHA256 mismatch or annotation missing: update the ConfigMap with latest script and SHA256
352-
newCM := gen.BuildConfigMap(dataset, cmKey)
353-
existingCM.Data = newCM.Data
354-
if existingCM.Annotations == nil {
355-
existingCM.Annotations = map[string]string{}
350+
if latest == nil {
351+
// Deleted between Get calls; recreate
352+
return helper.client.Create(context.TODO(), gen.BuildConfigMap(dataset, cmKey))
356353
}
357-
existingCM.Annotations[common.AnnotationCheckMountScriptSHA256] = currentSHA256
358-
// Preserve the dataset-id label if already set
359-
if _, ok := existingCM.Labels[common.LabelAnnotationDatasetId]; !ok {
360-
existingCM.Labels[common.LabelAnnotationDatasetId] = newCM.Labels[common.LabelAnnotationDatasetId]
354+
// Fast path: SHA256 annotation already matches, no update needed.
355+
if latest.Annotations != nil {
356+
if sha, ok := latest.Annotations[common.AnnotationCheckMountScriptSHA256]; ok && sha == currentSHA256 {
357+
return nil
358+
}
361359
}
362-
if err = helper.client.Update(context.TODO(), existingCM); err != nil {
363-
return err
360+
// Refresh Data/Labels/Annotations to match the desired state.
361+
updated := gen.RefreshConfigMapContents(dataset, cmKey, latest.DeepCopy())
362+
if reflect.DeepEqual(latest, updated) {
363+
return nil
364364
}
365+
return helper.client.Update(context.TODO(), updated)
366+
}); err != nil {
367+
return err
365368
}
366369
}
367370

pkg/application/inject/fuse/poststart/check_fuse_default.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,27 @@ log "succeed in checking mount point $ConditionPathIsMountPoint after $count att
102102
`
103103
)
104104

105+
// defaultPrivilegedSidecarScriptSHA256 stores the SHA256 (first 63 chars) of the privileged
106+
// sidecar script content, computed once at package initialization.
107+
var defaultPrivilegedSidecarScriptSHA256 string
108+
109+
func init() {
110+
defaultPrivilegedSidecarScriptSHA256 = computeScriptSHA256(replacer.Replace(contentPrivilegedSidecar))
111+
}
112+
105113
// DefaultMountCheckScriptGenerator is a generator to render resources and specs related to post start mount-check script for the DefaultMutator
106114
type defaultPostStartScriptGenerator struct {
107115
scriptGeneratorHelper
108116
}
109117

110118
func NewDefaultPostStartScriptGenerator() *defaultPostStartScriptGenerator {
111-
content := replacer.Replace(contentPrivilegedSidecar)
112119
return &defaultPostStartScriptGenerator{
113120
scriptGeneratorHelper: scriptGeneratorHelper{
114121
configMapName: "default-check-mount",
115122
scriptFileName: "check-mount.sh",
116123
scriptMountPath: "/check-mount.sh",
117-
scriptContent: content,
118-
scriptSHA256: computeScriptSHA256(content),
124+
scriptContent: replacer.Replace(contentPrivilegedSidecar),
125+
scriptSHA256: defaultPrivilegedSidecarScriptSHA256,
119126
},
120127
}
121128
}

pkg/application/inject/fuse/poststart/script_gen_helper.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type scriptGeneratorHelper struct {
3636
scriptContent string
3737
scriptFileName string
3838
scriptMountPath string
39-
scriptSHA256 string // SHA256 of scriptContent (first 63 chars), computed at construction
39+
scriptSHA256 string // first 63 chars of SHA256 of the scriptContent
4040
}
4141

4242
// computeScriptSHA256 computes the SHA256 of content and returns the first 63 hex chars
@@ -65,9 +65,37 @@ func (helper *scriptGeneratorHelper) BuildConfigMap(dataset *datav1alpha1.Datase
6565
}
6666
}
6767

68-
// GetScriptSHA256 returns the SHA256 of the helper's script content computed at construction.
68+
// GetScriptSHA256 returns the SHA256 of the helper's script content.
69+
// If the SHA was not set at construction time, it is computed lazily from scriptContent.
6970
func (helper *scriptGeneratorHelper) GetScriptSHA256() string {
70-
return helper.scriptSHA256
71+
if helper.scriptSHA256 != "" {
72+
return helper.scriptSHA256
73+
}
74+
if helper.scriptContent == "" {
75+
return ""
76+
}
77+
return computeScriptSHA256(helper.scriptContent)
78+
}
79+
80+
// RefreshConfigMapContents updates existingCM's Data, Labels, and Annotations in-place to
81+
// match what BuildConfigMap would produce for the given dataset and configMapKey, then returns
82+
// the updated object. The caller is responsible for persisting the change.
83+
func (helper *scriptGeneratorHelper) RefreshConfigMapContents(dataset *datav1alpha1.Dataset, configMapKey types.NamespacedName, existingCM *corev1.ConfigMap) *corev1.ConfigMap {
84+
newCM := helper.BuildConfigMap(dataset, configMapKey)
85+
existingCM.Data = newCM.Data
86+
if existingCM.Labels == nil {
87+
existingCM.Labels = map[string]string{}
88+
}
89+
for k, v := range newCM.Labels {
90+
existingCM.Labels[k] = v
91+
}
92+
if existingCM.Annotations == nil {
93+
existingCM.Annotations = map[string]string{}
94+
}
95+
for k, v := range newCM.Annotations {
96+
existingCM.Annotations[k] = v
97+
}
98+
return existingCM
7199
}
72100

73101
func (helper *scriptGeneratorHelper) GetNamespacedConfigMapKey(datasetKey types.NamespacedName, runtimeType string) types.NamespacedName {

0 commit comments

Comments
 (0)