Skip to content

Commit

Permalink
draft impl for defaulting-webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
worryg0d committed Mar 11, 2024
1 parent 30260ea commit 63439da
Show file tree
Hide file tree
Showing 25 changed files with 253 additions and 43 deletions.
49 changes: 32 additions & 17 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@
{
"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 @@ -118,15 +114,6 @@
}
],
"results": {
".idea/workspace.xml": [
{
"type": "Base64 High Entropy String",
"filename": ".idea/workspace.xml",
"hashed_secret": "ceda554256d1b9fa42791b09d9798634f930ad04",
"is_verified": false,
"line_number": 42
}
],
"apis/clusterresources/v1beta1/cassandrauser_types.go": [
{
"type": "Secret Keyword",
Expand Down Expand Up @@ -217,7 +204,7 @@
"filename": "apis/clusters/v1beta1/cassandra_webhook.go",
"hashed_secret": "e0a46b27231f798fe22dc4d5d82b5feeb5dcf085",
"is_verified": false,
"line_number": 232
"line_number": 244
}
],
"apis/clusters/v1beta1/kafka_types.go": [
Expand Down Expand Up @@ -360,7 +347,7 @@
"filename": "apis/clusters/v1beta1/redis_webhook.go",
"hashed_secret": "bc1c5ae5fd4a238d86261f422e62c489de408c22",
"is_verified": false,
"line_number": 323
"line_number": 335
}
],
"apis/clusters/v1beta1/zz_generated.deepcopy.go": [
Expand Down Expand Up @@ -505,12 +492,40 @@
}
],
"controllers/clusters/cadence_controller.go": [
{
"type": "Secret Keyword",
"filename": "controllers/clusters/cadence_controller.go",
"hashed_secret": "2d8ca9a11a2e2ef6931d0f0ef52b1896114a4558",
"is_verified": false,
"line_number": 167
},
{
"type": "Secret Keyword",
"filename": "controllers/clusters/cadence_controller.go",
"hashed_secret": "8902e39ab80b0808afb4654751df63517d106008",
"is_verified": false,
"line_number": 171
},
{
"type": "Secret Keyword",
"filename": "controllers/clusters/cadence_controller.go",
"hashed_secret": "dd8e1e714663d659a0b6932bfad048e0f0ef1f82",
"is_verified": false,
"line_number": 221
},
{
"type": "Secret Keyword",
"filename": "controllers/clusters/cadence_controller.go",
"hashed_secret": "2ba76f2b678a4173c4989c88db3505b0a0265d6f",
"is_verified": false,
"line_number": 222
},
{
"type": "Secret Keyword",
"filename": "controllers/clusters/cadence_controller.go",
"hashed_secret": "5ffe533b830f08a0326348a9160afafc8ada44db",
"is_verified": false,
"line_number": 750
"line_number": 831
}
],
"controllers/clusters/datatest/kafka_v1beta1.yaml": [
Expand Down Expand Up @@ -1134,5 +1149,5 @@
}
]
},
"generated_at": "2024-03-06T14:06:22Z"
"generated_at": "2024-03-11T14:02:24Z"
}
12 changes: 12 additions & 0 deletions apis/clusters/v1beta1/cadence_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ var _ webhook.Defaulter = &Cadence{}
func (c *Cadence) Default() {
cadencelog.Info("default", "name", c.Name)

if c.Spec.Inherits() && c.Status.ID == "" && c.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
c.Spec = CadenceSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: c.Spec.InheritsFrom},
DataCentres: []*CadenceDataCentre{{}},
}
c.Spec.GenericClusterSpec.setDefaultValues()
}

if c.Spec.Name == "" {
c.Spec.Name = c.Name
}
Expand Down Expand Up @@ -85,6 +93,10 @@ func (cv *cadenceValidator) ValidateCreate(ctx context.Context, obj runtime.Obje

cadencelog.Info("validate create", "name", c.Name)

if c.Spec.Inherits() {
return nil
}

err := requiredfieldsvalidator.ValidateRequiredFields(c.Spec)
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions apis/clusters/v1beta1/cassandra_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ var _ webhook.Defaulter = &Cassandra{}
func (c *Cassandra) Default() {
cassandralog.Info("default", "name", c.Name)

if c.Spec.Inherits() && c.Status.ID == "" && c.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
c.Spec = CassandraSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: c.Spec.InheritsFrom},
DataCentres: []*CassandraDataCentre{{}},
}
c.Spec.GenericClusterSpec.setDefaultValues()
}

if c.Spec.Name == "" {
c.Spec.Name = c.Name
}
Expand All @@ -76,6 +84,10 @@ func (cv *cassandraValidator) ValidateCreate(ctx context.Context, obj runtime.Ob
return fmt.Errorf("cannot assert object %v to cassandra", obj.GetObjectKind())
}

if c.Spec.Inherits() {
return nil
}

cassandralog.Info("validate create", "name", c.Name)

err := requiredfieldsvalidator.ValidateRequiredFields(c.Spec)
Expand Down
5 changes: 5 additions & 0 deletions apis/clusters/v1beta1/generic_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,8 @@ func (s *GenericDataCentreSpec) cloudProviderSettingsFromInstAPI(instaModel *mod
func (s *GenericClusterSpec) Inherits() bool {
return s.InheritsFrom != ""
}

func (in *GenericClusterSpec) setDefaultValues() {
in.Version = "DEFAULT_VERSION"
in.SLATier = "NON_PRODUCTION"
}
12 changes: 12 additions & 0 deletions apis/clusters/v1beta1/kafka_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ var _ webhook.Defaulter = &Kafka{}
func (k *Kafka) Default() {
kafkalog.Info("default", "name", k.Name)

if k.Spec.Inherits() && k.Status.ID == "" && k.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
k.Spec = KafkaSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: k.Spec.InheritsFrom},
DataCentres: []*KafkaDataCentre{{}},
}
k.Spec.GenericClusterSpec.setDefaultValues()
}

if k.Spec.Name == "" {
k.Spec.Name = k.Name
}
Expand All @@ -79,6 +87,10 @@ func (kv *kafkaValidator) ValidateCreate(ctx context.Context, obj runtime.Object
return fmt.Errorf("cannot assert object %v to kafka", obj.GetObjectKind())
}

if k.Spec.Inherits() {
return nil
}

kafkalog.Info("validate create", "name", k.Name)

err := requiredfieldsvalidator.ValidateRequiredFields(k.Spec)
Expand Down
24 changes: 18 additions & 6 deletions apis/clusters/v1beta1/kafkaconnect_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,23 @@ func (r *KafkaConnect) SetupWebhookWithManager(mgr ctrl.Manager, api validation.
var _ webhook.Defaulter = &KafkaConnect{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *KafkaConnect) Default() {
kafkaconnectlog.Info("default", "name", r.Name)
func (k *KafkaConnect) Default() {
kafkaconnectlog.Info("default", "name", k.Name)

if r.Spec.Name == "" {
r.Spec.Name = r.Name
if k.Spec.Inherits() && k.Status.ID == "" && k.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
k.Spec = KafkaConnectSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: k.Spec.InheritsFrom},
DataCentres: []*KafkaConnectDataCentre{{}},
}
k.Spec.GenericClusterSpec.setDefaultValues()
}

if k.Spec.Name == "" {
k.Spec.Name = k.Name
}

if r.GetAnnotations() == nil {
r.SetAnnotations(map[string]string{
if k.GetAnnotations() == nil {
k.SetAnnotations(map[string]string{
models.ResourceStateAnnotation: "",
})
}
Expand All @@ -79,6 +87,10 @@ func (kcv *kafkaConnectValidator) ValidateCreate(ctx context.Context, obj runtim
return fmt.Errorf("cannot assert object %v to kafka connect", obj.GetObjectKind())
}

if kc.Spec.Inherits() {
return nil
}

kafkaconnectlog.Info("validate create", "name", kc.Name)

err := requiredfieldsvalidator.ValidateRequiredFields(kc.Spec)
Expand Down
13 changes: 13 additions & 0 deletions apis/clusters/v1beta1/opensearch_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ var _ webhook.CustomValidator = &openSearchValidator{}
var _ webhook.Defaulter = &OpenSearch{}

func (os *OpenSearch) Default() {
if os.Spec.Inherits() && os.Status.ID == "" && os.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
os.Spec = OpenSearchSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: os.Spec.InheritsFrom},
DataCentres: []*OpenSearchDataCentre{{}},
ClusterManagerNodes: []*ClusterManagerNodes{{}},
}
os.Spec.GenericClusterSpec.setDefaultValues()
}

for _, dataCentre := range os.Spec.DataCentres {
setDefaultValues(dataCentre)

Expand Down Expand Up @@ -86,6 +95,10 @@ func (osv *openSearchValidator) ValidateCreate(ctx context.Context, obj runtime.
return fmt.Errorf("cannot assert object %v to openSearch", obj.GetObjectKind())
}

if os.Spec.Inherits() {
return nil
}

opensearchlog.Info("validate create", "name", os.Name)

err := requiredfieldsvalidator.ValidateRequiredFields(os.Spec)
Expand Down
12 changes: 12 additions & 0 deletions apis/clusters/v1beta1/postgresql_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ var _ webhook.Defaulter = &PostgreSQL{}
func (pg *PostgreSQL) Default() {
postgresqllog.Info("default", "name", pg.Name)

if pg.Spec.Inherits() && pg.Status.ID == "" && pg.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
pg.Spec = PgSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: pg.Spec.InheritsFrom},
DataCentres: []*PgDataCentre{},
}
pg.Spec.GenericClusterSpec.setDefaultValues()
}

if pg.Spec.Name == "" {
pg.Spec.Name = pg.Name
}
Expand All @@ -78,6 +86,10 @@ func (pgv *pgValidator) ValidateCreate(ctx context.Context, obj runtime.Object)
return fmt.Errorf("cannot assert object %v to postgreSQL", obj.GetObjectKind())
}

if pg.Spec.Inherits() {
return nil
}

postgresqllog.Info("validate create", "name", pg.Name)

err := requiredfieldsvalidator.ValidateRequiredFields(pg.Spec)
Expand Down
12 changes: 12 additions & 0 deletions apis/clusters/v1beta1/redis_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ var _ webhook.Defaulter = &Redis{}
func (r *Redis) Default() {
redislog.Info("default", "name", r.Name)

if r.Spec.Inherits() && r.Status.ID == "" && r.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
r.Spec = RedisSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: r.Spec.InheritsFrom},
DataCentres: []*RedisDataCentre{{}},
}
r.Spec.GenericClusterSpec.setDefaultValues()
}

if r.Spec.Name == "" {
r.Spec.Name = r.Name
redislog.Info("default values are set", "name", r.Name)
Expand Down Expand Up @@ -89,6 +97,10 @@ func (rv *redisValidator) ValidateCreate(ctx context.Context, obj runtime.Object
return fmt.Errorf("cannot assert object %v to redis", obj.GetObjectKind())
}

if r.Spec.Inherits() {
return nil
}

redislog.Info("validate create", "name", r.Name)

err := requiredfieldsvalidator.ValidateRequiredFields(r.Spec)
Expand Down
12 changes: 12 additions & 0 deletions apis/clusters/v1beta1/zookeeper_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ var _ webhook.Defaulter = &Zookeeper{}
func (z *Zookeeper) Default() {
redislog.Info("default", "name", z.Name)

if z.Spec.Inherits() && z.Status.ID == "" && z.Annotations[models.ResourceStateAnnotation] != models.SyncingEvent {
z.Spec = ZookeeperSpec{
GenericClusterSpec: GenericClusterSpec{InheritsFrom: z.Spec.InheritsFrom},
DataCentres: []*ZookeeperDataCentre{{}},
}
z.Spec.GenericClusterSpec.setDefaultValues()
}

if z.Spec.Name == "" {
z.Spec.Name = z.Name
}
Expand All @@ -75,6 +83,10 @@ func (zv *zookeeperValidator) ValidateCreate(ctx context.Context, obj runtime.Ob
return fmt.Errorf("cannot assert object %v to zookeeper", obj.GetObjectKind())
}

if z.Spec.Inherits() {
return nil
}

zookeeperlog.Info("validate create", "name", z.Name)

err := requiredfieldsvalidator.ValidateRequiredFields(z.Spec)
Expand Down
14 changes: 10 additions & 4 deletions config/samples/clusters_v1beta1_cadence.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ kind: Cadence
metadata:
name: cadence-sample
spec:
name: "bohdan-cadence-test"
name: "example-cadence"
version: "1.2.2"
# standardProvisioning:
# - targetCassandra:
# dependencyCdcId: "9d43ac54-7317-4ce5-859a-e9d0443508a4"
# dependencyVpcType: "VPC_PEERED"
packagedProvisioning:
- useAdvancedVisibility: true
solutionSize: "Production-Small"
-
# useAdvancedVisibility: true
solutionSize: "Production-Small"
# twoFactorDelete:
# - email: "[email protected]"
privateNetwork: false
Expand All @@ -44,4 +45,9 @@ spec:
# dependencyVpcType: "SEPARATE_VPC"
resizeSettings:
- notifySupportContacts: false
concurrency: 1
concurrency: 1
# awsArchival:
# - archivalS3Uri: s3bucketURI
# archivalS3Region: US_EAST_1
# awsAccessKeySecretNamespace: default
# awsAccessKeySecretName: inst-test-aws-cred-secret
6 changes: 6 additions & 0 deletions config/samples/clusters_v1beta1_cadence_inherits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: clusters.instaclustr.com/v1beta1
kind: Cadence
metadata:
name: cadence-inherited
spec:
inheritsFrom: "c8c80bf3-4489-426f-8a84-450b9acfefc3"
2 changes: 1 addition & 1 deletion config/samples/clusters_v1beta1_cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Cassandra
metadata:
name: cassandra-cluster
spec:
name: "bohdan-cassandra" #(immutable)
name: "example-cassandra" #(immutable)
version: "4.1.3" #(immutable)
# inheritsFrom: "42a0fa34-a647-4a30-96e0-fde64aba0eae"
privateNetwork: false #(immutable)
Expand Down
6 changes: 6 additions & 0 deletions config/samples/clusters_v1beta1_cassandra_inherits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: clusters.instaclustr.com/v1beta1
kind: Cassandra
metadata:
name: cassandra-inherited
spec:
inheritsFrom: "c1a84b4f-9b67-43e0-ac33-038290aea71d"
6 changes: 6 additions & 0 deletions config/samples/clusters_v1beta1_kafka_inherits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: clusters.instaclustr.com/v1beta1
kind: Kafka
metadata:
name: kafka-inherited
spec:
inheritsFrom: "681faf22-b305-45bb-bfcf-c85ea15dab37"
6 changes: 6 additions & 0 deletions config/samples/clusters_v1beta1_kafkaconnect_inherits.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: clusters.instaclustr.com/v1beta1
kind: KafkaConnect
metadata:
name: kafkaconnect-inherited
spec:
inheritsFrom: "c1a84b4f-9b67-43e0-ac33-038290aea71d"
Loading

0 comments on commit 63439da

Please sign in to comment.