Skip to content

Commit

Permalink
Fix: The hash generation is broken, because an omitempty was removed (
Browse files Browse the repository at this point in the history
#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`
  • Loading branch information
Robert-Stam authored Dec 9, 2024
1 parent 5880dfa commit 6b58f1c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/v1/qdrantcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func GetQdrantClusterCrdForHash(qc QdrantCluster) QdrantCluster {
cloned.Resources.Storage = ""
cloned.RestartAllPodsConcurrently = false
cloned.Service = nil
cloned.ServicePerNode = false
cloned.ServicePerNode = nil
cloned.Size = 1
if v := cloned.StatefulSet; v != nil {
v.Annotations = nil
Expand All @@ -86,7 +86,7 @@ type QdrantClusterSpec struct {
// ServicePerNode specifies whether the cluster should start a dedicated service for each node.
// +kubebuilder:default=true
// +optional
ServicePerNode bool `json:"servicePerNode"`
ServicePerNode *bool `json:"servicePerNode,omitempty"`
// ClusterManager specifies whether to use the cluster manager for this cluster.
// The Python-operator will deploy a dedicated cluster manager instance.
// The Go-operator will use a shared instance.
Expand Down
52 changes: 52 additions & 0 deletions api/v1/qdrantcluster_types_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package v1

import (
"crypto/sha256"
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -17,3 +21,51 @@ func TestValidate(t *testing.T) {
require.Error(t, err)
require.ErrorContains(t, err, "Spec.Resources.CPU error: quantities must match the regular expression '^([+-]?[0-9.]+)([eEinumkKMGTP]*[-+]?[0-9]*)$'")
}

func TestGetQdrantClusterCrdForHash(t *testing.T) {
qc := QdrantCluster{}
hash, err := getQdrantClusterCrdHash(qc)
require.NoError(t, err)
assert.Equal(t, "523a8cb", hash)

falseVal := false
qc.Spec.ServicePerNode = &falseVal
hash, err = getQdrantClusterCrdHash(qc)
require.NoError(t, err)
assert.Equal(t, "523a8cb", hash)

trueVal := true
qc.Spec.ServicePerNode = &trueVal
hash, err = getQdrantClusterCrdHash(qc)
require.NoError(t, err)
assert.Equal(t, "523a8cb", hash)
}

// getQdrantClusterCrdHash created a hash for the provided QdrantCluster,
// however a subset only, see GetQdrantClusterCrdForHash for details.
func getQdrantClusterCrdHash(qc QdrantCluster) (string, error) {
inspect := GetQdrantClusterCrdForHash(qc)
// Get the hash, so we can diff later
hash, err := getHash(inspect)
if err != nil {
return "", fmt.Errorf("failed to get hash for QdrantCluster: %w", err)
}
return hash, err
}

// Get hash of provided value.
// Returns the first 7 characters of the hash (like GitHub).
func getHash(v any) (string, error) {
json, err := json.Marshal(v)
if err != nil {
return "", fmt.Errorf("marshal failed: %w", err)
}
// Initialize hash
hash := sha256.New()
// add the serialized content
hash.Write(json)
// close hash
sum := hash.Sum(nil)
// Return first 7 characters
return fmt.Sprintf("%x", sum)[:7], nil
}
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b58f1c

Please sign in to comment.