Skip to content

Commit

Permalink
issue-607, cassandra user creation and deletion logic extracted from …
Browse files Browse the repository at this point in the history
…cassandra controller predicates
  • Loading branch information
Bohdan Siryk authored and Bohdan Siryk committed Nov 9, 2023
1 parent 1a20736 commit 60a82b9
Show file tree
Hide file tree
Showing 9 changed files with 383 additions and 67 deletions.
5 changes: 3 additions & 2 deletions apis/clusters/v1beta1/cassandra_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ type CassandraSpec struct {
PasswordAndUserAuth bool `json:"passwordAndUserAuth,omitempty"`
Spark []*Spark `json:"spark,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs UserReferences `json:"userRefs,omitempty"`
//+kubebuilder:validate:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}

// CassandraStatus defines the observed state of Cassandra
type CassandraStatus struct {
ClusterStatus `json:",inline"`
ClusterStatus `json:",inline"`
AvailableUsers UserReferences `json:"availableUsers,omitempty"`
}

type CassandraDataCentre struct {
Expand Down
37 changes: 37 additions & 0 deletions apis/clusters/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,40 @@ type UserReference struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
}

type UserReferences []*UserReference

// Diff returns difference between two UserReferences.
// added stores elements which are presented in new UserReferences, but aren't presented in old.
// deleted stores elements which aren't presented in new UserReferences, but are presented in old.
func (old UserReferences) Diff(new UserReferences) (added, deleted UserReferences) {
// filtering deleted references
for _, oldRef := range old {
var exists bool
for _, newRef := range new {
if *oldRef == *newRef {
exists = true
}
}

if !exists {
deleted = append(deleted, oldRef)
}
}

// filtering added references
for _, newRef := range new {
var exists bool
for _, oldRef := range old {
if *newRef == *oldRef {
exists = true
}
}

if !exists {
added = append(added, newRef)
}
}

return added, deleted
}
187 changes: 187 additions & 0 deletions apis/clusters/v1beta1/structs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package v1beta1_test

import (
"encoding/json"
"reflect"
"testing"

"github.com/instaclustr/operator/apis/clusters/v1beta1"
)

func TestUserReferencesDiff(t *testing.T) {
t.Parallel()

cases := []struct {
name string
old v1beta1.UserReferences
new v1beta1.UserReferences
expectedAdded v1beta1.UserReferences
expectedDeleted v1beta1.UserReferences
}{
{
name: "nothing changed",
old: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
},
new: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
},
},
{
name: "added new reference",
old: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
},
new: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
},
expectedAdded: v1beta1.UserReferences{
{
Name: "name2",
Namespace: "namespace2",
},
},
},
{
name: "deleted old reference",
old: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
},
new: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
},
expectedDeleted: v1beta1.UserReferences{
{
Name: "name2",
Namespace: "namespace2",
},
},
},
{
name: "both slices are nil",
old: nil,
new: nil,
},
{
name: "deleting the first out of 3",
old: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
},
new: v1beta1.UserReferences{
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
},
expectedDeleted: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
},
},
{
name: "deleting the first and adding a new one",
old: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
},
new: v1beta1.UserReferences{
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
{
Name: "name4",
Namespace: "namespace4",
},
},
expectedDeleted: v1beta1.UserReferences{
{
Name: "name1",
Namespace: "namespace1",
},
},
expectedAdded: v1beta1.UserReferences{
{
Name: "name4",
Namespace: "namespace4",
},
},
},
}

for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
added, deleted := c.old.Diff(c.new)

if !reflect.DeepEqual(added, c.expectedAdded) || !reflect.DeepEqual(deleted, c.expectedDeleted) {
t.Errorf("expected added %s, got %s; expected deleted %s, got %s",
toJson(c.expectedAdded), toJson(added),
toJson(c.expectedDeleted), toJson(deleted),
)
}
})
}
}

func toJson(obj any) string {
b, _ := json.Marshal(obj)
return string(b)
}
38 changes: 37 additions & 1 deletion apis/clusters/v1beta1/zz_generated.deepcopy.go

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

12 changes: 12 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_cassandras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ spec:
status:
description: CassandraStatus defines the observed state of Cassandra
properties:
availableUsers:
items:
properties:
name:
type: string
namespace:
type: string
required:
- name
- namespace
type: object
type: array
cdcid:
type: string
currentClusterOperationStatus:
Expand Down
37 changes: 37 additions & 0 deletions config/samples/clusterresources_v1beta1_cassandrauser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,40 @@ spec:
secretRef:
name: "cassandra-user-secret"
namespace: "default"
---
apiVersion: v1
kind: Secret
metadata:
name: cassandra-user-secret2
data:
password: NDgxMzU5ODM1NzlmMDU0ZTlhY2I4ZjcxMTMzMzQ1MjM3ZQ==
username: b2xvbG8xCg==
---

apiVersion: clusterresources.instaclustr.com/v1beta1
kind: CassandraUser
metadata:
name: cassandrauser-sample2
spec:
secretRef:
name: "cassandra-user-secret2"
namespace: "default"
---
apiVersion: v1
kind: Secret
metadata:
name: cassandra-user-secret3
data:
password: NDgxMzU5ODM1NzlmMDU0ZTlhY2I4ZjcxMTMzMzQ1MjM3ZQ==
username: b2xvbG8yCg==
---

apiVersion: clusterresources.instaclustr.com/v1beta1
kind: CassandraUser
metadata:
name: cassandrauser-sample3
spec:
secretRef:
name: "cassandra-user-secret3"
namespace: "default"
---
4 changes: 3 additions & 1 deletion config/samples/clusters_v1beta1_cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ spec:
pciCompliance: false
luceneEnabled: false # can be enabled only on 3.11.13 version of Cassandra
passwordAndUserAuth: true
# userRefs:
userRefs:
# - namespace: default
# name: cassandrauser-sample
# - namespace: default
# name: cassandrauser-sample2
# - namespace: default
# name: cassandrauser-sample3
slaTier: "NON_PRODUCTION"
# resizeSettings:
# - notifySupportContacts: false
Expand Down
Loading

0 comments on commit 60a82b9

Please sign in to comment.