Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
go-version: 1.16

- uses: actions/checkout@v2

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.42.1
version: v1.45.2
args: -c=".github/linters/.golangci.yml"
build:
# Name the Job
Expand Down
11 changes: 0 additions & 11 deletions .golangci.yml

This file was deleted.

12 changes: 11 additions & 1 deletion internal/controllers/sync-volume/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (r *Reconciler) sync(ctx context.Context) {
// save receives a volume to persists it.
func (r *Reconciler) save(ctx context.Context, obj storageos.Object) {
log := r.log.WithValues("name", obj.GetName())
log.V(5).Info("storageos volume", "dump", obj.GetInner())

hash, err := hash(obj)
if err != nil {
Expand Down Expand Up @@ -226,7 +227,7 @@ func (r *Reconciler) save(ctx context.Context, obj storageos.Object) {

vol := getVolumeStub(obj.GetName())

result, err := controllerutil.CreateOrPatch(ctx, r.Client, vol, getConverter(vol, obj, log))
result, err := controllerutil.CreateOrPatch(ctx, r.Client, vol, getVolumeConverter(vol, obj, log))
if err != nil {
r.recorder.Event(vol, "Warning", "Failed", fmt.Sprintf("Failed to persist volume %s: %s", vol.Name, err.Error()))
observeErr(err, "unable to persist volume")
Expand All @@ -239,6 +240,15 @@ func (r *Reconciler) save(ctx context.Context, obj storageos.Object) {
log.Info("volume persisted")
}

err = getVolumeStatusConverter(vol, obj, log)()
if err != nil {
r.recorder.Event(vol, "Warning", "Failed", fmt.Sprintf("Failed to convert status %s: %s", vol.Name, err.Error()))
observeErr(err, "unable to convert status")
return
}

log.V(5).Info("K8s volume", "dump", vol)

err = r.Client.Status().Update(ctx, vol)
if err != nil {
r.recorder.Event(vol, "Warning", "Failed", fmt.Sprintf("Failed to persist volume status %s: %s", vol.Name, err.Error()))
Expand Down
39 changes: 35 additions & 4 deletions internal/controllers/sync-volume/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package syncvolume
import (
"encoding/json"
"fmt"
"hash/fnv"
"math"
"strconv"
"strings"
Expand All @@ -41,7 +42,27 @@ import (
)

func hash(v storageos.Object) ([]byte, error) {
return json.Marshal(v)
stosVol, ok := v.GetInner().(stosapi.Volume)
if !ok {
return nil, errors.New("type cast error")
}

raw, err := json.Marshal(stosVol)
if err != nil {
return nil, err
}

hash := []byte{}
for _, s := range [][]byte{[]byte(stosVol.Id), []byte(stosVol.CreatedAt.String()), raw} {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the benefit of hashing the id, the createdAt and the entire raw json string?

Doesn't the entire json string contain the id and createdAt anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is we convert undefined number of any characters to ~19 digits. Because the output is slimmer than the input, we have some chance for collision. So this should power the output space and drastically decreases chance of collision.

h := fnv.New64a()
_, err = h.Write(s)
if err != nil {
return nil, err
}
hash = append(hash, []byte(strconv.Itoa(int(h.Sum64())))...)
}

return hash, nil
}

func getVolumeStub(name string) *stosv1.Volume {
Expand All @@ -56,7 +77,7 @@ func getVolumeStub(name string) *stosv1.Volume {
}
}

func getConverter(vol *stosv1.Volume, obj storageos.Object, log logr.Logger) func() error {
func getVolumeConverter(vol *stosv1.Volume, obj storageos.Object, log logr.Logger) func() error {
return func() error {
stosVol, ok := obj.GetInner().(stosapi.Volume)
if !ok {
Expand Down Expand Up @@ -146,6 +167,17 @@ func getConverter(vol *stosv1.Volume, obj storageos.Object, log logr.Logger) fun
}
}

return nil
}
}

func getVolumeStatusConverter(vol *stosv1.Volume, obj storageos.Object, log logr.Logger) func() error {
return func() error {
stosVol, ok := obj.GetInner().(stosapi.Volume)
if !ok {
return errors.New("type cast error")
}

vol.Status = stosv1.VolumeStatus{
VolumeID: obj.GetID(),
AttachedOn: stosVol.AttachedOn,
Expand All @@ -160,9 +192,8 @@ func getConverter(vol *stosv1.Volume, obj storageos.Object, log logr.Logger) fun
AttachedOnHost: stosVol.AttachedOnHost,
}

vol.Status.Replicas = nil
vol.Status.Replicas = []stosv1.ReplicaDeploymentInfo{}
if stosVol.Replicas != nil {
vol.Status.Replicas = make([]stosv1.ReplicaDeploymentInfo, 0, len(*stosVol.Replicas))
for _, stosReplica := range *stosVol.Replicas {
replica := stosv1.ReplicaDeploymentInfo{
ID: stosReplica.Id,
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/storageos/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type MockObject struct {
Namespace string
Labels map[string]string
Healthy bool
Inner interface{}
}

func (m MockObject) GetInner() interface{} {
return nil
return m.Inner
}

// GetID returns the object ID.
Expand Down