Skip to content

Commit

Permalink
issue-669, added immutability for redis dc name
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Siryk authored and ribaraka committed Jan 11, 2024
1 parent 2380abc commit f7a26a7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
3 changes: 1 addition & 2 deletions apis/clusters/v1beta1/cadence_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"

k8scorev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -288,7 +287,7 @@ func (cs *CadenceSpec) ArchivalToInstAPI(ctx context.Context, k8sClient client.C

func getSecret(ctx context.Context, k8sClient client.Client, aws *AWSArchival) (string, string, error) {
var err error
awsCredsSecret := &v1.Secret{}
awsCredsSecret := &k8scorev1.Secret{}
awsSecretNamespacedName := types.NamespacedName{
Name: aws.AccessKeySecretName,
Namespace: aws.AccessKeySecretNamespace,
Expand Down
5 changes: 2 additions & 3 deletions apis/clusters/v1beta1/kafkaconnect_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"

k8scorev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -584,8 +583,8 @@ func (tc *TargetCluster) ManagedClustersToInstAPI() (iClusters []*models.Managed
return
}

func (k *KafkaConnect) NewDefaultUserSecret(username, password string) *v1.Secret {
return &v1.Secret{
func (k *KafkaConnect) NewDefaultUserSecret(username, password string) *k8scorev1.Secret {
return &k8scorev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: models.SecretKind,
APIVersion: models.K8sAPIVersionV1,
Expand Down
11 changes: 5 additions & 6 deletions apis/clusters/v1beta1/postgresql_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"strconv"

k8sCore "k8s.io/api/core/v1"
k8scorev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -342,7 +341,7 @@ func (pg *PostgreSQL) GetUserSecretName(ctx context.Context, k8sClient client.Cl
return "", err
}

userSecretList := &k8sCore.SecretList{}
userSecretList := &k8scorev1.SecretList{}
err = k8sClient.List(ctx, userSecretList, &client.ListOptions{LabelSelector: selector})
if err != nil {
return "", err
Expand All @@ -355,8 +354,8 @@ func (pg *PostgreSQL) GetUserSecretName(ctx context.Context, k8sClient client.Cl
return userSecretList.Items[0].Name, nil
}

func (pg *PostgreSQL) NewUserSecret(defaultUserPassword string) *k8sCore.Secret {
return &k8sCore.Secret{
func (pg *PostgreSQL) NewUserSecret(defaultUserPassword string) *k8scorev1.Secret {
return &k8scorev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: models.SecretKind,
APIVersion: models.K8sAPIVersionV1,
Expand Down Expand Up @@ -490,8 +489,8 @@ func GetDefaultPgUserSecret(
name string,
ns string,
k8sClient client.Client,
) (*k8sCore.Secret, error) {
userSecret := &k8sCore.Secret{}
) (*k8scorev1.Secret, error) {
userSecret := &k8scorev1.Secret{}
userSecretNamespacedName := types.NamespacedName{
Name: fmt.Sprintf(models.DefaultUserSecretNameTemplate, models.DefaultUserSecretPrefix, name),
Namespace: ns,
Expand Down
71 changes: 40 additions & 31 deletions apis/clusters/v1beta1/redis_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,46 +255,55 @@ func (rs *RedisSpec) ValidateUpdate(oldSpec RedisSpec) error {
}

func (rs *RedisSpec) validateDCsUpdate(oldSpec RedisSpec) error {
if len(rs.DataCentres) < len(oldSpec.DataCentres) {
return models.ErrDecreasedDataCentresNumber
// contains old DCs which should be validated
toValidate := map[string]*RedisDataCentre{}
for _, dc := range oldSpec.DataCentres {
toValidate[dc.Name] = dc
}

for _, newDC := range rs.DataCentres {
for _, oldDC := range oldSpec.DataCentres {
if newDC.Name == oldDC.Name {
newDCImmutableFields := newDC.newImmutableFields()
oldDCImmutableFields := oldDC.newImmutableFields()

if *newDCImmutableFields != *oldDCImmutableFields {
return fmt.Errorf("cannot update immutable data centre fields: new spec: %v: old spec: %v", newDCImmutableFields, oldDCImmutableFields)
}

err := newDC.validateImmutableCloudProviderSettingsUpdate(oldDC.CloudProviderSettings)
if err != nil {
return err
}

if newDC.MasterNodes < oldDC.MasterNodes {
return fmt.Errorf("deleting nodes is not supported. Master nodes number must be greater than: %v", oldDC.MasterNodes)
}

if newDC.NodesNumber < oldDC.NodesNumber {
return fmt.Errorf("deleting nodes is not supported. Number of nodes must be greater than: %v", oldDC.NodesNumber)
}

err = validatePrivateLinkUpdate(newDC.PrivateLink, oldDC.PrivateLink)
if err != nil {
return err
}
oldDC, ok := toValidate[newDC.Name]
if !ok {
// validating creation of the new dc
if err := newDC.ValidateCreate(); err != nil {
return err
}
continue
}

// validating updating of the DC
newDCImmutableFields := newDC.newImmutableFields()
oldDCImmutableFields := oldDC.newImmutableFields()

if *newDCImmutableFields != *oldDCImmutableFields {
return fmt.Errorf("cannot update immutable data centre fields: new spec: %v: old spec: %v", newDCImmutableFields, oldDCImmutableFields)
}

err := newDC.validateImmutableCloudProviderSettingsUpdate(oldDC.CloudProviderSettings)
if err != nil {
return err
}

if newDC.MasterNodes < oldDC.MasterNodes {
return fmt.Errorf("deleting nodes is not supported. Master nodes number must be greater than: %v", oldDC.MasterNodes)
}

if newDC.NodesNumber < oldDC.NodesNumber {
return fmt.Errorf("deleting nodes is not supported. Number of nodes must be greater than: %v", oldDC.NodesNumber)
}
}

for i := len(oldSpec.DataCentres); i < len(rs.DataCentres); i++ {
err := rs.DataCentres[i].ValidateCreate()
err = validatePrivateLinkUpdate(newDC.PrivateLink, oldDC.PrivateLink)
if err != nil {
return err
}

// deleting validated DCs from the map to ensure if it is validated
delete(toValidate, oldDC.Name)
}

// ensuring if all old DCs were validated
if len(toValidate) > 0 {
return models.ErrUnsupportedDeletingDC
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/models/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var (
ErrDebeziumImmutable = errors.New("debezium array is immutable")
ErrEmptyNamespace = errors.New("namespace field is empty")
ErrEmptyName = errors.New("name field is empty")
ErrCreateClusterWithMultiDC = errors.New("Multiple data center is still not supported. Please create a cluster with one data centre and add a second one when the cluster is in the running state")
ErrCreateClusterWithMultiDC = errors.New("multiple data center is still not supported. Please create a cluster with one data centre and add a second one when the cluster is in the running state")
ErrOnPremicesWithMultiDC = errors.New("on-premises cluster can be provisioned with only one data centre")
ErrUnsupportedDeletingDC = errors.New("deleting data centre is not supported")
)

0 comments on commit f7a26a7

Please sign in to comment.