-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pierangelo Di Pilato <pdipilat@redhat.com>
- Loading branch information
Showing
12 changed files
with
734 additions
and
3 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
control-plane/pkg/apis/internals/kafka/eventing/v1alpha1/consumer_defaults.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2021 The Knative Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
|
||
"knative.dev/pkg/apis" | ||
) | ||
|
||
var ( | ||
_ apis.Defaultable = &Consumer{} | ||
) | ||
|
||
// SetDefaults implements apis.Defaultable. | ||
func (c *Consumer) SetDefaults(ctx context.Context) { | ||
ctx = apis.WithinParent(ctx, c.ObjectMeta) | ||
c.Spec.SetDefaults(ctx) | ||
} | ||
|
||
func (c *ConsumerSpec) SetDefaults(ctx context.Context) { | ||
c.Delivery.SetDefaults(ctx) | ||
c.Subscriber.SetDefaults(ctx) | ||
} | ||
|
||
func (d *DeliverySpec) SetDefaults(ctx context.Context) { | ||
if d == nil { | ||
return | ||
} | ||
d.DeliverySpec.SetDefaults(ctx) | ||
} |
52 changes: 52 additions & 0 deletions
52
control-plane/pkg/apis/internals/kafka/eventing/v1alpha1/consumer_defaults_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2021 The Knative Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestConsumerSetDefaults(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx context.Context | ||
given *Consumer | ||
want *Consumer | ||
}{ | ||
{ | ||
name: "with delivery", | ||
ctx: context.Background(), | ||
given: &Consumer{ | ||
Spec: ConsumerSpec{Delivery: &DeliverySpec{}}, | ||
}, | ||
want: &Consumer{ | ||
Spec: ConsumerSpec{Delivery: &DeliverySpec{}}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.given.SetDefaults(tt.ctx) | ||
if diff := cmp.Diff(tt.want, tt.given); diff != "" { | ||
t.Error("(-want, +got)", diff) | ||
} | ||
}) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
control-plane/pkg/apis/internals/kafka/eventing/v1alpha1/consumer_group_defaults.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2021 The Knative Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/utils/pointer" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
var ( | ||
_ apis.Defaultable = &ConsumerGroup{} | ||
) | ||
|
||
// SetDefaults implements apis.Defaultable. | ||
func (cg *ConsumerGroup) SetDefaults(ctx context.Context) { | ||
ctx = apis.WithinParent(ctx, cg.ObjectMeta) | ||
|
||
// Replicas is the number of Consumers for this ConsumerGroup. | ||
// When unset, set it to 1. | ||
if cg.Spec.Replicas == nil { | ||
cg.Spec.Replicas = pointer.Int32Ptr(1) | ||
} | ||
// Selector is a label query over consumers that should match the Replicas count. | ||
// If Selector is empty, it is defaulted to the labels present on the template. | ||
if cg.Spec.Selector == nil || len(cg.Spec.Selector) == 0 { | ||
cg.Spec.Selector = cg.Spec.Template.Labels | ||
} | ||
|
||
// Force template namespace to be set to ConsumerGroup's namespace. | ||
cg.Spec.Template.Namespace = cg.Namespace | ||
cg.Spec.Template.Spec.SetDefaults(ctx) | ||
} |
138 changes: 138 additions & 0 deletions
138
control-plane/pkg/apis/internals/kafka/eventing/v1alpha1/consumer_group_defaults_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2021 The Knative Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
func TestConsumerGroupSetDefaults(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ctx context.Context | ||
given *ConsumerGroup | ||
want *ConsumerGroup | ||
}{ | ||
{ | ||
name: "default replicas", | ||
ctx: context.Background(), | ||
given: &ConsumerGroup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
}, | ||
Spec: ConsumerGroupSpec{ | ||
Template: ConsumerTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &ConsumerGroup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
}, | ||
Spec: ConsumerGroupSpec{ | ||
Template: ConsumerTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
}, | ||
}, | ||
Replicas: pointer.Int32Ptr(1), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "default selector", | ||
ctx: context.Background(), | ||
given: &ConsumerGroup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
}, | ||
Spec: ConsumerGroupSpec{ | ||
Template: ConsumerTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Labels: map[string]string{"app": "app"}, | ||
}, | ||
}, | ||
Replicas: pointer.Int32Ptr(1), | ||
}, | ||
}, | ||
want: &ConsumerGroup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
}, | ||
Spec: ConsumerGroupSpec{ | ||
Template: ConsumerTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Labels: map[string]string{"app": "app"}, | ||
}, | ||
}, | ||
Replicas: pointer.Int32Ptr(1), | ||
Selector: map[string]string{"app": "app"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "default namespace", | ||
ctx: context.Background(), | ||
given: &ConsumerGroup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
}, | ||
Spec: ConsumerGroupSpec{ | ||
Replicas: pointer.Int32Ptr(1), | ||
}, | ||
}, | ||
want: &ConsumerGroup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
Name: "name", | ||
}, | ||
Spec: ConsumerGroupSpec{ | ||
Template: ConsumerTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "ns", | ||
}, | ||
}, | ||
Replicas: pointer.Int32Ptr(1), | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.given.SetDefaults(tt.ctx) | ||
|
||
if diff := cmp.Diff(tt.want, tt.given); diff != "" { | ||
t.Error("(-want, +got)", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.