Skip to content

Commit

Permalink
zookeeper cr codebase was refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
worryg0d committed Feb 27, 2024
1 parent 13f37bd commit 1782400
Show file tree
Hide file tree
Showing 27 changed files with 440 additions and 309 deletions.
10 changes: 5 additions & 5 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,14 @@
"filename": "apis/clusters/v1beta1/zookeeper_types.go",
"hashed_secret": "5ffe533b830f08a0326348a9160afafc8ada44db",
"is_verified": false,
"line_number": 235
"line_number": 214
},
{
"type": "Secret Keyword",
"filename": "apis/clusters/v1beta1/zookeeper_types.go",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 240
"line_number": 219
}
],
"apis/clusters/v1beta1/zz_generated.deepcopy.go": [
Expand Down Expand Up @@ -574,7 +574,7 @@
"filename": "controllers/clusters/postgresql_controller.go",
"hashed_secret": "5ffe533b830f08a0326348a9160afafc8ada44db",
"is_verified": false,
"line_number": 1272
"line_number": 1265
}
],
"controllers/clusters/zookeeper_controller_test.go": [
Expand Down Expand Up @@ -739,7 +739,7 @@
"filename": "pkg/instaclustr/client.go",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 2072
"line_number": 2078
}
],
"pkg/instaclustr/mock/client.go": [
Expand Down Expand Up @@ -1146,5 +1146,5 @@
}
]
},
"generated_at": "2024-02-26T10:23:28Z"
"generated_at": "2024-02-27T14:03:16Z"
}
183 changes: 109 additions & 74 deletions apis/clusters/v1beta1/zookeeper_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,42 @@ limitations under the License.
package v1beta1

import (
"encoding/json"
"fmt"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/instaclustr/operator/pkg/models"
"github.com/instaclustr/operator/pkg/utils/slices"
)

type ZookeeperDataCentre struct {
DataCentre `json:",inline"`
GenericDataCentreSpec `json:",inline"`

NumberOfNodes int `json:"numberOfNodes"`
NodeSize string `json:"nodeSize"`
ClientToServerEncryption bool `json:"clientToServerEncryption"`
EnforceAuthSchemes []string `json:"enforceAuthSchemes,omitempty"`
EnforceAuthEnabled bool `json:"enforceAuthEnabled,omitempty"`
EnforceAuthSchemes []string `json:"enforceAuthSchemes,omitempty"`
}

// ZookeeperSpec defines the desired state of Zookeeper
type ZookeeperSpec struct {
Cluster `json:",inline"`
DataCentres []*ZookeeperDataCentre `json:"dataCentres"`
GenericClusterSpec `json:",inline"`
DataCentres []*ZookeeperDataCentre `json:"dataCentres"`
}

// ZookeeperStatus defines the observed state of Zookeeper
type ZookeeperStatus struct {
ClusterStatus `json:",inline"`
DefaultUserSecretRef *Reference `json:"defaultUserSecretRef,omitempty"`
GenericStatus `json:",inline"`
DataCentres []*ZookeeperDataCentreStatus `json:"dataCentres,omitempty"`
DefaultUserSecretRef *Reference `json:"defaultUserSecretRef,omitempty"`
}

type ZookeeperDataCentreStatus struct {
GenericDataCentreStatus `json:",inline"`
Nodes []*Node `json:"nodes"`
}

//+kubebuilder:object:root=true
Expand Down Expand Up @@ -85,19 +94,9 @@ func (z *Zookeeper) NewPatch() client.Patch {
return client.MergeFrom(old)
}

func (z *Zookeeper) FromInstAPI(iData []byte) (*Zookeeper, error) {
iZook := &models.ZookeeperCluster{}
err := json.Unmarshal(iData, iZook)
if err != nil {
return nil, err
}

return &Zookeeper{
TypeMeta: z.TypeMeta,
ObjectMeta: z.ObjectMeta,
Spec: z.Spec.FromInstAPI(iZook),
Status: z.Status.FromInstAPI(iZook),
}, nil
func (z *Zookeeper) FromInstAPI(instaModel *models.ZookeeperCluster) {
z.Spec.FromInstAPI(instaModel)
z.Status.FromInstAPI(instaModel)
}

func (z *Zookeeper) GetDataCentreID(cdcName string) string {
Expand All @@ -116,60 +115,41 @@ func (z *Zookeeper) GetClusterID() string {
return z.Status.ID
}

func (zs *ZookeeperSpec) FromInstAPI(iZook *models.ZookeeperCluster) ZookeeperSpec {
return ZookeeperSpec{
Cluster: Cluster{
Name: iZook.Name,
Version: iZook.ZookeeperVersion,
Description: iZook.Description,
PrivateNetworkCluster: iZook.PrivateNetworkCluster,
SLATier: iZook.SLATier,
TwoFactorDelete: zs.Cluster.TwoFactorDeleteFromInstAPI(iZook.TwoFactorDelete),
},
DataCentres: zs.DCsFromInstAPI(iZook.DataCentres),
}
func (zs *ZookeeperSpec) FromInstAPI(instaModel *models.ZookeeperCluster) {
zs.GenericClusterSpec.FromInstAPI(&instaModel.GenericClusterFields, instaModel.ZookeeperVersion)
zs.DCsFromInstAPI(instaModel.DataCentres)
}

func (zs *ZookeeperStatus) FromInstAPI(iZook *models.ZookeeperCluster) ZookeeperStatus {
return ZookeeperStatus{
ClusterStatus: ClusterStatus{
ID: iZook.ID,
State: iZook.Status,
DataCentres: zs.DCsFromInstAPI(iZook.DataCentres),
CurrentClusterOperationStatus: iZook.CurrentClusterOperationStatus,
MaintenanceEvents: zs.MaintenanceEvents,
},
}
func (zs *ZookeeperStatus) FromInstAPI(instaModel *models.ZookeeperCluster) {
zs.GenericStatus.FromInstAPI(&instaModel.GenericClusterFields)
zs.DCsFromInstAPI(instaModel.DataCentres)
}

func (zs *ZookeeperSpec) DCsFromInstAPI(iDCs []*models.ZookeeperDataCentre) (dcs []*ZookeeperDataCentre) {
for _, iDC := range iDCs {
dcs = append(dcs, &ZookeeperDataCentre{
DataCentre: zs.Cluster.DCFromInstAPI(iDC.DataCentre),
ClientToServerEncryption: iDC.ClientToServerEncryption,
EnforceAuthSchemes: iDC.EnforceAuthSchemes,
EnforceAuthEnabled: iDC.EnforceAuthEnabled,
})
func (zs *ZookeeperSpec) DCsFromInstAPI(instaModels []*models.ZookeeperDataCentre) {
dcs := make([]*ZookeeperDataCentre, len(instaModels))
for i, instaModel := range instaModels {
dc := &ZookeeperDataCentre{}
dc.FromInstAPI(instaModel)
dcs[i] = dc
}
return
zs.DataCentres = dcs
}

func (zs *ZookeeperStatus) DCsFromInstAPI(iDCs []*models.ZookeeperDataCentre) (dcs []*DataCentreStatus) {
for _, iDC := range iDCs {
dcs = append(dcs, zs.ClusterStatus.DCFromInstAPI(iDC.DataCentre))
func (zs *ZookeeperStatus) DCsFromInstAPI(instaModels []*models.ZookeeperDataCentre) {
dcs := make([]*ZookeeperDataCentreStatus, len(instaModels))
for i, instaModel := range instaModels {
dc := &ZookeeperDataCentreStatus{}
dc.FromInstAPI(instaModel)
dcs[i] = dc
}
return
zs.DataCentres = dcs
}

func (zs *ZookeeperSpec) ToInstAPI() *models.ZookeeperCluster {
return &models.ZookeeperCluster{
Name: zs.Name,
ZookeeperVersion: zs.Version,
PrivateNetworkCluster: zs.PrivateNetworkCluster,
SLATier: zs.SLATier,
TwoFactorDelete: zs.Cluster.TwoFactorDeletesToInstAPI(),
DataCentres: zs.DCsToInstAPI(),
Description: zs.Description,
GenericClusterFields: zs.GenericClusterSpec.ToInstAPI(),
ZookeeperVersion: zs.Version,
DataCentres: zs.DCsToInstAPI(),
}
}

Expand All @@ -182,10 +162,12 @@ func (zs *ZookeeperSpec) DCsToInstAPI() (dcs []*models.ZookeeperDataCentre) {

func (zdc *ZookeeperDataCentre) ToInstAPI() *models.ZookeeperDataCentre {
return &models.ZookeeperDataCentre{
DataCentre: zdc.DataCentre.ToInstAPI(),
GenericDataCentreFields: zdc.GenericDataCentreSpec.ToInstAPI(),
ClientToServerEncryption: zdc.ClientToServerEncryption,
EnforceAuthSchemes: zdc.EnforceAuthSchemes,
EnforceAuthEnabled: zdc.EnforceAuthEnabled,
NumberOfNodes: zdc.NumberOfNodes,
NodeSize: zdc.NodeSize,
}
}

Expand All @@ -194,23 +176,23 @@ func (z *Zookeeper) GetSpec() ZookeeperSpec { return z.Spec }
func (z *Zookeeper) IsSpecEqual(spec ZookeeperSpec) bool { return z.Spec.IsEqual(spec) }

func (a *ZookeeperSpec) IsEqual(b ZookeeperSpec) bool {
return a.Cluster.IsEqual(b.Cluster) &&
a.areDCsEqual(b.DataCentres)
return a.GenericClusterSpec.Equals(&b.GenericClusterSpec) &&
a.DCsEqual(b.DataCentres)
}

func (rs *ZookeeperSpec) areDCsEqual(b []*ZookeeperDataCentre) bool {
a := rs.DataCentres
if len(a) != len(b) {
func (rs *ZookeeperSpec) DCsEqual(o []*ZookeeperDataCentre) bool {
if len(rs.DataCentres) != len(o) {
return false
}

for i := range b {
if a[i].Name != b[i].Name {
continue
}
m := map[string]*ZookeeperDataCentre{}
for _, dc := range rs.DataCentres {
m[dc.Name] = dc
}

if !a[i].DataCentre.IsEqual(b[i].DataCentre) ||
a[i].ClientToServerEncryption != b[i].ClientToServerEncryption {
for _, iDC := range o {
dc, ok := m[iDC.Name]
if !ok || !dc.Equals(iDC) {
return false
}
}
Expand Down Expand Up @@ -238,3 +220,56 @@ func (a *Zookeeper) NewDefaultUserSecret(username, password string) *v1.Secret {
},
}
}

func (zdc *ZookeeperDataCentre) FromInstAPI(instaModel *models.ZookeeperDataCentre) {
zdc.GenericDataCentreSpec.FromInstAPI(&instaModel.GenericDataCentreFields)
zdc.NodeSize = instaModel.NodeSize
zdc.NumberOfNodes = instaModel.NumberOfNodes
zdc.ClientToServerEncryption = instaModel.ClientToServerEncryption
zdc.EnforceAuthEnabled = instaModel.EnforceAuthEnabled
zdc.EnforceAuthSchemes = instaModel.EnforceAuthSchemes
}

func (s *ZookeeperDataCentreStatus) FromInstAPI(instaModel *models.ZookeeperDataCentre) {
s.GenericDataCentreStatus.FromInstAPI(&instaModel.GenericDataCentreFields)
s.Nodes = nodesFromInstAPI(instaModel.Nodes)
}

func (zdc *ZookeeperDataCentre) Equals(o *ZookeeperDataCentre) bool {
return zdc.GenericDataCentreSpec.Equals(&o.GenericDataCentreSpec) &&
zdc.NumberOfNodes == o.NumberOfNodes &&
zdc.NodeSize == o.NodeSize &&
zdc.ClientToServerEncryption == o.ClientToServerEncryption &&
zdc.EnforceAuthEnabled == o.EnforceAuthEnabled &&
slices.Equals(zdc.EnforceAuthSchemes, o.EnforceAuthSchemes)
}

func (zs *ZookeeperStatus) Equals(o *ZookeeperStatus) bool {
return zs.GenericStatus.Equals(&o.GenericStatus) &&
zs.DCsEquals(o.DataCentres)
}

func (zs *ZookeeperStatus) DCsEquals(o []*ZookeeperDataCentreStatus) bool {
if len(zs.DataCentres) != len(o) {
return false
}

m := map[string]*ZookeeperDataCentreStatus{}
for _, dc := range zs.DataCentres {
m[dc.Name] = dc
}

for _, iDC := range o {
dc, ok := m[iDC.Name]
if !ok || !dc.Equals(iDC) {
return false
}
}

return true
}

func (s *ZookeeperDataCentreStatus) Equals(o *ZookeeperDataCentreStatus) bool {
return s.GenericDataCentreStatus.Equals(&o.GenericDataCentreStatus) &&
nodesEqual(s.Nodes, o.Nodes)
}
22 changes: 11 additions & 11 deletions apis/clusters/v1beta1/zookeeper_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ func (z *Zookeeper) Default() {
models.ResourceStateAnnotation: "",
})
}

for _, dataCentre := range z.Spec.DataCentres {
dataCentre.SetDefaultValues()
}
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
Expand All @@ -86,7 +82,7 @@ func (zv *zookeeperValidator) ValidateCreate(ctx context.Context, obj runtime.Ob
return err
}

err = z.Spec.Cluster.ValidateCreation()
err = z.Spec.GenericClusterSpec.ValidateCreation()
if err != nil {
return err
}
Expand All @@ -107,7 +103,7 @@ func (zv *zookeeperValidator) ValidateCreate(ctx context.Context, obj runtime.Ob
}

for _, dc := range z.Spec.DataCentres {
err = dc.DataCentre.ValidateCreation()
err = dc.GenericDataCentreSpec.validateCreation()
if err != nil {
return err
}
Expand All @@ -127,17 +123,21 @@ func (zv *zookeeperValidator) ValidateUpdate(ctx context.Context, old runtime.Ob
return fmt.Errorf("cannot assert object %v to zookeeper", new.GetObjectKind())
}

zookeeperlog.Info("validate update", "name", newZookeeper.Name)

if newZookeeper.Status.ID == "" {
return zv.ValidateCreate(ctx, newZookeeper)
if newZookeeper.Annotations[models.ResourceStateAnnotation] == models.SyncingEvent {
return nil
}

if newZookeeper.Annotations[models.ExternalChangesAnnotation] == models.True {
return nil
}

if newZookeeper.Generation != oldZookeeper.Generation && !oldZookeeper.Spec.ClusterSettingsNeedUpdate(newZookeeper.Spec.Cluster) {
if newZookeeper.Status.ID == "" {
return zv.ValidateCreate(ctx, newZookeeper)
}

zookeeperlog.Info("validate update", "name", newZookeeper.Name)

if newZookeeper.Generation != oldZookeeper.Generation && !oldZookeeper.Spec.ClusterSettingsNeedUpdate(&newZookeeper.Spec.GenericClusterSpec) {
return fmt.Errorf("update is not allowed")
}

Expand Down
Loading

0 comments on commit 1782400

Please sign in to comment.