Skip to content

Commit

Permalink
implemented everything expect cadence
Browse files Browse the repository at this point in the history
  • Loading branch information
worryg0d committed Mar 6, 2024
1 parent ef8d9ea commit 98c80a4
Show file tree
Hide file tree
Showing 22 changed files with 215 additions and 97 deletions.
8 changes: 6 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
{
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
},
{
"path": "detect_secrets.filters.common.is_baseline_file",
"filename": ".secrets.baseline"
},
{
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
"min_level": 2
Expand Down Expand Up @@ -558,7 +562,7 @@
"filename": "controllers/clusters/postgresql_controller.go",
"hashed_secret": "5ffe533b830f08a0326348a9160afafc8ada44db",
"is_verified": false,
"line_number": 1192
"line_number": 1199
}
],
"controllers/clusters/zookeeper_controller_test.go": [
Expand Down Expand Up @@ -1130,5 +1134,5 @@
}
]
},
"generated_at": "2024-03-05T16:46:11Z"
"generated_at": "2024-03-06T14:06:22Z"
}
6 changes: 6 additions & 0 deletions apis/clusters/v1beta1/generic_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type GenericClusterSpec struct {

Description string `json:"description,omitempty"`

InheritsFrom string `json:"inheritsFrom,omitempty"`

TwoFactorDelete []*TwoFactorDelete `json:"twoFactorDelete,omitempty"`
}

Expand Down Expand Up @@ -227,3 +229,7 @@ func (s *GenericDataCentreSpec) cloudProviderSettingsFromInstAPI(instaModel *mod
}}
}
}

func (s *GenericClusterSpec) Inherits() bool {
return s.InheritsFrom != ""
}
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_cadences.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ spec:
type: array
description:
type: string
inheritsFrom:
type: string
name:
description: Name [ 3 .. 32 ] characters.
type: string
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_cassandras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ spec:
type: array
description:
type: string
inheritsFrom:
type: string
luceneEnabled:
type: boolean
name:
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_kafkaconnects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ spec:
type: array
description:
type: string
inheritsFrom:
type: string
name:
description: Name [ 3 .. 32 ] characters.
type: string
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_kafkas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ spec:
type: array
description:
type: string
inheritsFrom:
type: string
karapaceRestProxy:
items:
properties:
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_opensearches.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ spec:
- nodeSize
type: object
type: array
inheritsFrom:
type: string
knnPlugin:
type: boolean
loadBalancer:
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_postgresqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ spec:
- name
type: object
type: array
inheritsFrom:
type: string
name:
description: Name [ 3 .. 32 ] characters.
type: string
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ spec:
type: array
description:
type: string
inheritsFrom:
type: string
name:
description: Name [ 3 .. 32 ] characters.
type: string
Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_zookeepers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ spec:
type: array
description:
type: string
inheritsFrom:
type: string
name:
description: Name [ 3 .. 32 ] characters.
type: string
Expand Down
3 changes: 2 additions & 1 deletion config/samples/clusters_v1beta1_cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ kind: Cassandra
metadata:
name: cassandra-cluster
spec:
name: "username-cassandra" #(immutable)
name: "bohdan-cassandra" #(immutable)
version: "4.1.3" #(immutable)
# inheritsFrom: "42a0fa34-a647-4a30-96e0-fde64aba0eae"
privateNetwork: false #(immutable)
dataCentres:
- name: "AWS_cassandra" #(mutable)
Expand Down
13 changes: 7 additions & 6 deletions config/samples/clusters_v1beta1_kafka.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ kind: Kafka
metadata:
name: kafka
spec:
name: "example-kafka"
name: "bohdan-kafka"
# inheritsFrom: 42a0fa34-a647-4a30-96e0-fde64aba0eae
version: "3.5.1"
pciCompliance: false
replicationFactor: 3
Expand All @@ -21,13 +22,13 @@ spec:
# twoFactorDelete:
# - email: "asdfadfsdsf"
# phone: "ddsafasdf"
karapaceSchemaRegistry:
- version: "3.6.2"
# karapaceSchemaRegistry:
# - version: "3.6.2"
# schemaRegistry:
# - version: "3.0.0"
karapaceRestProxy:
- integrateRestProxyWithSchemaRegistry: true
version: "3.6.2"
# karapaceRestProxy:
# - integrateRestProxyWithSchemaRegistry: true
# version: "3.6.2"
# kraft:
# - controllerNodeCount: 3
# restProxy:
Expand Down
5 changes: 3 additions & 2 deletions config/samples/clusters_v1beta1_opensearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ metadata:
app.kubernetes.io/part-of: operator
app.kuberentes.io/managed-by: kustomize
app.kubernetes.io/created-by: operator
name: opensearch-sample
name: opensearch-sample2
annotations:
test.annotation/first: testAnnotation
spec:
name: opensearch-test
name: bohdan-test2
inheritsFrom: ed8a0dc3-0a41-4e94-a508-3d5cf4b1a28b
alertingPlugin: false
anomalyDetectionPlugin: false
asynchronousSearchPlugin: false
Expand Down
4 changes: 3 additions & 1 deletion config/samples/clusters_v1beta1_redis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ metadata:
app.kuberentes.io/managed-by: kustomize
app.kubernetes.io/created-by: operator
name: redis-sample
namespace: nm1
spec:
name: "example-redis"
name: "bohdan-redis1"
version: "7.0.14"
slaTier: "NON_PRODUCTION"
clientEncryption: false
passwordAndUserAuth: true
privateNetwork: false
# inheritsFrom: "83bb77aa-16fb-4f3e-b103-93b1c0352c1b"
# userRefs:
# - name: redisuser-sample-1
# namespace: default
Expand Down
3 changes: 2 additions & 1 deletion config/samples/clusters_v1beta1_zookeeper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ kind: Zookeeper
metadata:
name: zookeeper-sample
spec:
name: "example-zookeeper"
name: "bohdan-zookeeper2"
# inheritsFrom: "3afa5885-116e-4414-b3ac-678d7d195baa"
# description: "some description"
dataCentres:
- clientToServerEncryption: false
Expand Down
23 changes: 14 additions & 9 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,25 @@ func (r *CassandraReconciler) createCassandra(c *v1beta1.Cassandra, l logr.Logge
}

func (r *CassandraReconciler) createCluster(ctx context.Context, c *v1beta1.Cassandra, l logr.Logger) error {
var instModel *models.CassandraCluster
var err error
if !c.Spec.Inherits() {
id, err := getClusterIDByName(r.API, models.CassandraAppType, c.Spec.Name)
if err != nil {
return err
}

id, err := getClusterIDByName(r.API, models.CassandraAppType, c.Spec.Name)
if err != nil {
return err
if id != "" && c.Spec.Inherits() {
l.Info("Cluster with provided name already exists", "name", c.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", c.Spec.Name)
}
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", c.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", c.Spec.Name)
}
var instModel *models.CassandraCluster
var err error

switch {
case c.Spec.Inherits():
l.Info("Inheriting from the cluster", "clusterID", c.Spec.InheritsFrom)
instModel, err = r.API.GetCassandra(c.Spec.InheritsFrom)
case c.Spec.HasRestore():
instModel, err = r.createCassandraFromRestore(c, l)
default:
Expand Down
54 changes: 37 additions & 17 deletions controllers/clusters/kafka_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,20 @@ func (r *KafkaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return models.ExitReconcile, nil
}

func (r *KafkaReconciler) createCluster(ctx context.Context, k *v1beta1.Kafka, l logr.Logger) error {
id, err := getClusterIDByName(r.API, models.KafkaAppType, k.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", k.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", k.Spec.Name)
}

func (r *KafkaReconciler) createKafka(k *v1beta1.Kafka, l logr.Logger) (*models.KafkaCluster, error) {
l.Info("Creating cluster",
"cluster name", k.Spec.Name,
"data centres", k.Spec.DataCentres)

b, err := r.API.CreateClusterRaw(instaclustr.KafkaEndpoint, k.Spec.ToInstAPI())
if err != nil {
return fmt.Errorf("failed to create kafka cluster, err: %w", err)
return nil, fmt.Errorf("failed to create kafka cluster, err: %w", err)
}

instaModel := models.KafkaCluster{}
err = json.Unmarshal(b, &instaModel)
instaModel := &models.KafkaCluster{}
err = json.Unmarshal(b, instaModel)
if err != nil {
return fmt.Errorf("failed to unmarshal json to kafka model, err: %w", err)
return nil, fmt.Errorf("failed to unmarshal json to kafka model, err: %w", err)
}

r.EventRecorder.Eventf(
Expand All @@ -138,14 +128,44 @@ func (r *KafkaReconciler) createCluster(ctx context.Context, k *v1beta1.Kafka, l
instaModel.ID,
)

k.Spec.FromInstAPI(&instaModel)
return instaModel, nil
}

func (r *KafkaReconciler) createCluster(ctx context.Context, k *v1beta1.Kafka, l logr.Logger) error {
if !k.Spec.Inherits() {
id, err := getClusterIDByName(r.API, models.KafkaAppType, k.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", k.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", k.Spec.Name)
}
}

var instaModel *models.KafkaCluster
var err error

switch {
case k.Spec.Inherits():
l.Info("Inheriting from the cluster", "clusterID", k.Spec.InheritsFrom)
instaModel, err = r.API.GetKafka(k.Spec.InheritsFrom)
default:
instaModel, err = r.createKafka(k, l)
}
if err != nil {
return err
}

k.Spec.FromInstAPI(instaModel)
k.Annotations[models.ResourceStateAnnotation] = models.SyncingEvent
err = r.Update(ctx, k)
if err != nil {
return fmt.Errorf("failed to update kafka spec, err: %w", err)
}

k.Status.FromInstAPI(&instaModel)
k.Status.FromInstAPI(instaModel)
err = r.Status().Update(ctx, k)
if err != nil {
return fmt.Errorf("failed to update kafka status, err: %w", err)
Expand Down
52 changes: 36 additions & 16 deletions controllers/clusters/kafkaconnect_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,61 @@ func (r *KafkaConnectReconciler) mergeManagedClusterFromRef(ctx context.Context,
return nil
}

func (r *KafkaConnectReconciler) createCluster(ctx context.Context, kc *v1beta1.KafkaConnect, l logr.Logger) error {
id, err := getClusterIDByName(r.API, models.KafkaConnectAppType, kc.Spec.Name)
func (r *KafkaConnectReconciler) createKafkaConnect(ctx context.Context, kc *v1beta1.KafkaConnect) (*models.KafkaConnectCluster, error) {
err := r.mergeManagedClusterFromRef(ctx, kc)
if err != nil {
return err
return nil, err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", kc.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", kc.Spec.Name)
b, err := r.API.CreateClusterRaw(instaclustr.KafkaConnectEndpoint, kc.Spec.ToInstAPI())
if err != nil {
return nil, fmt.Errorf("failed to create KafkaConnect cluster, err: %w", err)
}

err = r.mergeManagedClusterFromRef(ctx, kc)
var instaModel models.KafkaConnectCluster
err = json.Unmarshal(b, &instaModel)
if err != nil {
return err
return nil, fmt.Errorf("failed to unmarshal body to KafkaConnect model, err: %w", err)
}

b, err := r.API.CreateClusterRaw(instaclustr.KafkaConnectEndpoint, kc.Spec.ToInstAPI())
if err != nil {
return fmt.Errorf("failed to create KafkaConnect cluster, err: %w", err)
return &instaModel, nil
}

func (r *KafkaConnectReconciler) createCluster(ctx context.Context, kc *v1beta1.KafkaConnect, l logr.Logger) error {
if !kc.Spec.Inherits() {
id, err := getClusterIDByName(r.API, models.KafkaConnectAppType, kc.Spec.Name)
if err != nil {
return err
}

if id != "" {
l.Info("Cluster with provided name already exists", "name", kc.Spec.Name, "clusterID", id)
return fmt.Errorf("cluster %s already exists, please change name property", kc.Spec.Name)
}
}

var instaModel models.KafkaConnectCluster
err = json.Unmarshal(b, &instaModel)
var instaModel *models.KafkaConnectCluster
var err error

switch {
case kc.Spec.Inherits():
l.Info("Inheriting from the cluster", "clusterID", kc.Spec.InheritsFrom)
instaModel, err = r.API.GetKafkaConnect(kc.Spec.InheritsFrom)
default:
instaModel, err = r.createKafkaConnect(ctx, kc)
}
if err != nil {
return fmt.Errorf("failed to unmarshal body to KafkaConnect model, err: %w", err)
return err
}

kc.Spec.FromInstAPI(&instaModel)
kc.Spec.FromInstAPI(instaModel)
kc.Annotations[models.ResourceStateAnnotation] = models.SyncingEvent
err = r.Update(ctx, kc)
if err != nil {
return fmt.Errorf("failed to update resource spec, err: %w", err)
}

kc.Status.FromInstAPI(&instaModel)
kc.Status.FromInstAPI(instaModel)
err = r.Status().Update(ctx, kc)
if err != nil {
return fmt.Errorf("failed to update resource status, err: %w", err)
Expand Down
Loading

0 comments on commit 98c80a4

Please sign in to comment.