Skip to content

Commit 2c63514

Browse files
committed
Fix deepcopy hash error
- ACP-209 Signed-off-by: Hang Yan <[email protected]>
1 parent 09b3418 commit 2c63514

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

pkg/controller/controller.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,20 @@ func (c *Controller) updateHelmRequestStatus(helmRequest *alpha1.HelmRequest) er
321321
// NEVER modify objects from the store. It's a read-only, local cache.
322322
// You can use DeepCopy() to make a deep copy of original object and modify this copy
323323
// Or create a copy manually for better performance
324+
h := hash.GenHashStr(helmRequest.Spec)
325+
// Note: we have to generate the hash before the deepcopy, because somehow the deepcopy
326+
// can create a spec that have different hash value.
324327
request := helmRequest.DeepCopy()
325-
request.Status.LastSpecHash = hash.GenHashStr(request.Spec)
328+
request.Status.LastSpecHash = h
326329
return c.updateHelmRequestPhase(request, alpha1.HelmRequestSynced)
327330
}
328331

329332
// setPartialSyncedStatus set spec hash and partial-synced status for helm-request
330333
//TODO: merge with updateHelmRequestStatus
331334
func (c *Controller) setPartialSyncedStatus(helmRequest *alpha1.HelmRequest) error {
335+
h := hash.GenHashStr(helmRequest.Spec)
332336
request := helmRequest.DeepCopy()
333-
request.Status.LastSpecHash = hash.GenHashStr(request.Spec)
337+
request.Status.LastSpecHash = h
334338
return c.updateHelmRequestPhase(request, alpha1.HelmRequestPartialSynced)
335339
}
336340

@@ -351,14 +355,18 @@ func (c *Controller) updateHelmRequestPhase(helmRequest *alpha1.HelmRequest, pha
351355
// which is ideal for ensuring nothing other than resource status has been updated.
352356
_, err := c.hrClientSet.AppV1alpha1().HelmRequests(helmRequest.Namespace).UpdateStatus(request)
353357
if err != nil {
354-
klog.Warning("update helm request status conflict, retry...")
355358
if apierrors.IsConflict(err) {
359+
klog.Warning("update helm request status conflict, retry...")
356360
origin, err := c.hrClientSet.AppV1alpha1().HelmRequests(helmRequest.Namespace).Get(helmRequest.Name, metav1.GetOptions{})
357361
if err != nil {
358362
return err
359363
}
364+
klog.Warningf("origin status: %+v, current: %+v", origin.Status, request.Status)
360365
origin.Status = *request.Status.DeepCopy()
361366
_, err = c.hrClientSet.AppV1alpha1().HelmRequests(helmRequest.Namespace).UpdateStatus(origin)
367+
if err != nil {
368+
klog.Error("retrying update helmrequest status error:", err)
369+
}
362370
return err
363371
}
364372
klog.Errorf("update status for helmrequest %s error: %s", helmRequest.Name, err.Error())

pkg/controller/sync.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ func (c *Controller) syncToAllClusters(key string, helmRequest *v1alpha1.HelmReq
5555
helmRequest.Status.SyncedClusters = synced
5656
klog.Infof("synced %s to clusters: %+v", key, synced)
5757

58+
err = errors.NewAggregate(errs)
59+
5860
if len(synced) >= len(clusters) {
5961
// all synced
6062
return c.updateHelmRequestStatus(helmRequest)
6163
} else if len(synced) > 0 {
6264
// partial synced
63-
if err := c.setPartialSyncedStatus(helmRequest); err != nil {
64-
return err
65-
}
65+
c.sendFailedSyncEvent(helmRequest, err)
66+
return c.setPartialSyncedStatus(helmRequest)
6667
}
67-
return errors.NewAggregate(errs)
68+
return err
6869
}
6970

7071
// sync install/update chart to one cluster

pkg/controller/sync_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package controller
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gsamokovarov/assert"
7+
8+
"alauda.io/captain/pkg/apis/app/v1alpha1"
9+
"github.com/alauda/component-base/hash"
10+
"github.com/ghodss/yaml"
11+
"helm.sh/helm/pkg/chartutil"
12+
)
13+
14+
func TestHelmRequestDeepCopyHash(t *testing.T) {
15+
values := `
16+
global:
17+
registry:
18+
address: 10.0.128.234:60080
19+
replicas: 1
20+
resources:
21+
requests:
22+
cpu: 10m
23+
memory: 10m`
24+
var v chartutil.Values
25+
yaml.Unmarshal([]byte(values), &v)
26+
hr := &v1alpha1.HelmRequest{
27+
Spec: v1alpha1.HelmRequestSpec{
28+
Chart: "stable/captain-test-demo",
29+
InstallToAllClusters: true,
30+
Namespace: "default",
31+
ReleaseName: "cpatain-test-demo",
32+
HelmValues: v1alpha1.HelmValues{v},
33+
Version: "1.2.1",
34+
},
35+
}
36+
assert.Equal(t, hash.GenHashStr(hr.Spec), hash.GenHashStr(hr.DeepCopy().Spec))
37+
}

0 commit comments

Comments
 (0)