Skip to content

Commit

Permalink
generic solution for handling changes in user resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Siryk authored and Bohdan Siryk committed Nov 14, 2023
1 parent 60a82b9 commit 8191a3a
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 346 deletions.
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
}
28 changes: 26 additions & 2 deletions apis/clusters/v1beta1/cassandra_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ type CassandraSpec struct {
PasswordAndUserAuth bool `json:"passwordAndUserAuth,omitempty"`
Spark []*Spark `json:"spark,omitempty"`
BundledUseOnly bool `json:"bundledUseOnly,omitempty"`
UserRefs UserReferences `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"`
AvailableUsers UserReferences `json:"availableUsers,omitempty"`
AvailableUsers References `json:"availableUsers,omitempty"`
}

type CassandraDataCentre struct {
Expand Down Expand Up @@ -495,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
23 changes: 16 additions & 7 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,17 +713,24 @@ 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 UserReferences []*UserReference
type References []*Reference

// 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) {
// 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
Expand Down
69 changes: 50 additions & 19 deletions apis/clusters/v1beta1/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ func TestUserReferencesDiff(t *testing.T) {

cases := []struct {
name string
old v1beta1.UserReferences
new v1beta1.UserReferences
expectedAdded v1beta1.UserReferences
expectedDeleted v1beta1.UserReferences
old v1beta1.References
new v1beta1.References
expectedAdded v1beta1.References
expectedDeleted v1beta1.References
}{
{
name: "nothing changed",
old: v1beta1.UserReferences{
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
new: v1beta1.UserReferences{
new: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
Expand All @@ -35,13 +35,13 @@ func TestUserReferencesDiff(t *testing.T) {
},
{
name: "added new reference",
old: v1beta1.UserReferences{
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
new: v1beta1.UserReferences{
new: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
Expand All @@ -51,7 +51,7 @@ func TestUserReferencesDiff(t *testing.T) {
Namespace: "namespace2",
},
},
expectedAdded: v1beta1.UserReferences{
expectedAdded: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
Expand All @@ -60,7 +60,7 @@ func TestUserReferencesDiff(t *testing.T) {
},
{
name: "deleted old reference",
old: v1beta1.UserReferences{
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
Expand All @@ -70,13 +70,13 @@ func TestUserReferencesDiff(t *testing.T) {
Namespace: "namespace2",
},
},
new: v1beta1.UserReferences{
new: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
expectedDeleted: v1beta1.UserReferences{
expectedDeleted: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
Expand All @@ -90,7 +90,7 @@ func TestUserReferencesDiff(t *testing.T) {
},
{
name: "deleting the first out of 3",
old: v1beta1.UserReferences{
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
Expand All @@ -104,7 +104,7 @@ func TestUserReferencesDiff(t *testing.T) {
Namespace: "namespace3",
},
},
new: v1beta1.UserReferences{
new: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
Expand All @@ -114,7 +114,7 @@ func TestUserReferencesDiff(t *testing.T) {
Namespace: "namespace3",
},
},
expectedDeleted: v1beta1.UserReferences{
expectedDeleted: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
Expand All @@ -123,7 +123,7 @@ func TestUserReferencesDiff(t *testing.T) {
},
{
name: "deleting the first and adding a new one",
old: v1beta1.UserReferences{
old: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
Expand All @@ -137,7 +137,7 @@ func TestUserReferencesDiff(t *testing.T) {
Namespace: "namespace3",
},
},
new: v1beta1.UserReferences{
new: v1beta1.References{
{
Name: "name2",
Namespace: "namespace2",
Expand All @@ -151,13 +151,44 @@ func TestUserReferencesDiff(t *testing.T) {
Namespace: "namespace4",
},
},
expectedDeleted: v1beta1.UserReferences{
expectedDeleted: v1beta1.References{
{
Name: "name1",
Namespace: "namespace1",
},
},
expectedAdded: v1beta1.UserReferences{
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",
Expand Down
Loading

0 comments on commit 8191a3a

Please sign in to comment.