Skip to content

Commit 6b58f1c

Browse files
authored
Fix: The hash generation is broken, because an omitempty was removed (#52)
* Fix: The hash genrration is brokewn, because a `omitempty` was removed This means rthat in production ALL pods are restarting, which is unwanted. For now a breaking API change has been applied to explicit set the value. We already has a ticket to harmonize the complete CRD API. * Ran `make generate`
1 parent 5880dfa commit 6b58f1c

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

api/v1/qdrantcluster_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func GetQdrantClusterCrdForHash(qc QdrantCluster) QdrantCluster {
5959
cloned.Resources.Storage = ""
6060
cloned.RestartAllPodsConcurrently = false
6161
cloned.Service = nil
62-
cloned.ServicePerNode = false
62+
cloned.ServicePerNode = nil
6363
cloned.Size = 1
6464
if v := cloned.StatefulSet; v != nil {
6565
v.Annotations = nil
@@ -86,7 +86,7 @@ type QdrantClusterSpec struct {
8686
// ServicePerNode specifies whether the cluster should start a dedicated service for each node.
8787
// +kubebuilder:default=true
8888
// +optional
89-
ServicePerNode bool `json:"servicePerNode"`
89+
ServicePerNode *bool `json:"servicePerNode,omitempty"`
9090
// ClusterManager specifies whether to use the cluster manager for this cluster.
9191
// The Python-operator will deploy a dedicated cluster manager instance.
9292
// The Go-operator will use a shared instance.

api/v1/qdrantcluster_types_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package v1
22

33
import (
4+
"crypto/sha256"
5+
"encoding/json"
6+
"fmt"
47
"testing"
58

9+
"github.com/stretchr/testify/assert"
610
"github.com/stretchr/testify/require"
711
)
812

@@ -17,3 +21,51 @@ func TestValidate(t *testing.T) {
1721
require.Error(t, err)
1822
require.ErrorContains(t, err, "Spec.Resources.CPU error: quantities must match the regular expression '^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$'")
1923
}
24+
25+
func TestGetQdrantClusterCrdForHash(t *testing.T) {
26+
qc := QdrantCluster{}
27+
hash, err := getQdrantClusterCrdHash(qc)
28+
require.NoError(t, err)
29+
assert.Equal(t, "523a8cb", hash)
30+
31+
falseVal := false
32+
qc.Spec.ServicePerNode = &falseVal
33+
hash, err = getQdrantClusterCrdHash(qc)
34+
require.NoError(t, err)
35+
assert.Equal(t, "523a8cb", hash)
36+
37+
trueVal := true
38+
qc.Spec.ServicePerNode = &trueVal
39+
hash, err = getQdrantClusterCrdHash(qc)
40+
require.NoError(t, err)
41+
assert.Equal(t, "523a8cb", hash)
42+
}
43+
44+
// getQdrantClusterCrdHash created a hash for the provided QdrantCluster,
45+
// however a subset only, see GetQdrantClusterCrdForHash for details.
46+
func getQdrantClusterCrdHash(qc QdrantCluster) (string, error) {
47+
inspect := GetQdrantClusterCrdForHash(qc)
48+
// Get the hash, so we can diff later
49+
hash, err := getHash(inspect)
50+
if err != nil {
51+
return "", fmt.Errorf("failed to get hash for QdrantCluster: %w", err)
52+
}
53+
return hash, err
54+
}
55+
56+
// Get hash of provided value.
57+
// Returns the first 7 characters of the hash (like GitHub).
58+
func getHash(v any) (string, error) {
59+
json, err := json.Marshal(v)
60+
if err != nil {
61+
return "", fmt.Errorf("marshal failed: %w", err)
62+
}
63+
// Initialize hash
64+
hash := sha256.New()
65+
// add the serialized content
66+
hash.Write(json)
67+
// close hash
68+
sum := hash.Sum(nil)
69+
// Return first 7 characters
70+
return fmt.Sprintf("%x", sum)[:7], nil
71+
}

api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)