Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extraction of creation and deletion logic of Cassandra User from controller predicates #616

Merged
merged 1 commit into from
Nov 14, 2023
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
8 changes: 8 additions & 0 deletions apis/clusterresources/v1beta1/cassandrauser_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@ func (r *CassandraUser) ToInstAPI(username, password string) *models.InstaUser {
func (r *CassandraUser) GetDeletionFinalizer() string {
return models.DeletionFinalizer + "_" + r.Namespace + "_" + r.Name
}

func (r *CassandraUser) GetClusterEvents() map[string]string {
return r.Status.ClustersEvents
}

func (r *CassandraUser) SetClusterEvents(events map[string]string) {
r.Status.ClustersEvents = events
}
29 changes: 27 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 References `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 References `json:"availableUsers,omitempty"`
}

type CassandraDataCentre struct {
Expand Down Expand Up @@ -494,6 +495,30 @@ func (c *CassandraSpec) validateResizeSettings(nodeNumber int) error {
return nil
}

func (c *Cassandra) GetAvailableUsers() References {
return c.Status.AvailableUsers
}

func (c *Cassandra) SetAvailableUsers(users References) {
c.Status.AvailableUsers = users
}

func (c *Cassandra) GetUserRefs() References {
return c.Spec.UserRefs
}

func (c *Cassandra) SetUserRefs(refs References) {
c.Spec.UserRefs = refs
}

func (c *Cassandra) GetClusterID() string {
return c.Status.ID
}

func (c *Cassandra) SetClusterID(id string) {
c.Status.ID = id
}

func init() {
SchemeBuilder.Register(&Cassandra{}, &CassandraList{})
}
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/kafka_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type KafkaSpec struct {
KarapaceRestProxy []*KarapaceRestProxy `json:"karapaceRestProxy,omitempty"`
KarapaceSchemaRegistry []*KarapaceSchemaRegistry `json:"karapaceSchemaRegistry,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*Reference `json:"userRefs,omitempty"`
Kraft []*Kraft `json:"kraft,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/opensearch_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type OpenSearchSpec struct {
IndexManagementPlugin bool `json:"indexManagementPlugin,omitempty"`
AlertingPlugin bool `json:"alertingPlugin,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*Reference `json:"userRefs,omitempty"`
//+kubuilder:validation:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
//+kubuilder:validation:MaxItems:=1
Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/postgresql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type PgSpec struct {
DataCentres []*PgDataCentre `json:"dataCentres,omitempty"`
ClusterConfigurations map[string]string `json:"clusterConfigurations,omitempty"`
SynchronousModeStrict bool `json:"synchronousModeStrict,omitempty"`
UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*Reference `json:"userRefs,omitempty"`
//+kubebuilder:validate:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion apis/clusters/v1beta1/redis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type RedisSpec struct {
//+kubebuilder:validation:MaxItems:=2
DataCentres []*RedisDataCentre `json:"dataCentres,omitempty"`

UserRefs []*UserReference `json:"userRefs,omitempty"`
UserRefs []*Reference `json:"userRefs,omitempty"`
//+kubebuilder:validation:MaxItems:=1
ResizeSettings []*ResizeSettings `json:"resizeSettings,omitempty"`
}
Expand Down
50 changes: 48 additions & 2 deletions apis/clusters/v1beta1/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"encoding/json"
"net"

"k8s.io/apimachinery/pkg/types"

clusterresource "github.com/instaclustr/operator/apis/clusterresources/v1beta1"
"github.com/instaclustr/operator/pkg/models"
)
Expand Down Expand Up @@ -711,7 +713,51 @@ func (cs *ClusterStatus) PrivateLinkStatusesEqual(iStatus *ClusterStatus) bool {
return true
}

type UserReference struct {
Namespace string `json:"namespace"`
type Reference struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}

func (r *Reference) AsNamespacedName() types.NamespacedName {
return types.NamespacedName{
Name: r.Name,
Namespace: r.Namespace,
}
}

type References []*Reference

// Diff returns difference between two References.
// Added stores elements which are presented in new References, but aren't presented in old.
// Deleted stores elements which aren't presented in new References, but are presented in old.
func (old References) Diff(new References) (added, deleted References) {
// 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
}
218 changes: 218 additions & 0 deletions apis/clusters/v1beta1/structs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
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.References
new v1beta1.References
expectedAdded v1beta1.References
expectedDeleted v1beta1.References
}{
{
name: "nothing changed",
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
new: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
},
{
name: "added new reference",
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
new: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
},
expectedAdded: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
},
},
},
{
name: "deleted old reference",
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
},
new: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
expectedDeleted: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
},
},
},
{
name: "both slices are nil",
old: nil,
new: nil,
},
{
name: "deleting the first out of 3",
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
},
new: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
},
expectedDeleted: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
},
{
name: "deleting the first and adding a new one",
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
},
new: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
{
Name: "name4",
Namespace: "namespace4",
},
},
expectedDeleted: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
expectedAdded: v1beta1.References{
{
Name: "name4",
Namespace: "namespace4",
},
},
},
{
name: "deleting the whole references",
old: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
{
Name: "name4",
Namespace: "namespace4",
},
},
expectedDeleted: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
},
{
Name: "name3",
Namespace: "namespace3",
},
{
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)
}
Loading