Skip to content

Commit

Permalink
consumer: allow specifying different name when writing to k8s (#705)
Browse files Browse the repository at this point in the history
This make it possible to contain special info in the name of k8s
custom resource, which is different from the name used in the request
matching. This feature is useful with server side filter.
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored Sep 5, 2024
1 parent 4cdefbe commit 15734b8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
16 changes: 14 additions & 2 deletions controller/internal/controller/consumer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,21 @@ func (r *ConsumerReconciler) consumersToState(ctx context.Context,
if namespaceToConsumers[namespace] == nil {
namespaceToConsumers[namespace] = make(map[string]*mosniov1.Consumer)
}
namespaceToConsumers[namespace][consumer.Name] = consumer

consumer.SetAccepted(mosniov1.ReasonAccepted)
name := consumer.Name
if consumer.Spec.Name != "" {
name = consumer.Spec.Name
}

if namespaceToConsumers[namespace][name] != nil {
log.Errorf("duplicate Consumer %s/%s, k8s name %s takes effect, k8s name %s ignored", namespace, name,
namespaceToConsumers[namespace][name].Name, consumer.Name)
consumer.SetAccepted(mosniov1.ReasonInvalid,
fmt.Sprintf("duplicate with another consumer %s/%s, k8s name %s", namespace, name, consumer.Name))
} else {
namespaceToConsumers[namespace][name] = consumer
consumer.SetAccepted(mosniov1.ReasonAccepted)
}
}

state := &consumerReconcileState{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"path/filepath"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -277,5 +278,42 @@ var _ = Describe("Consumer controller", func() {
Expect(filter["demo"]).ToNot(BeNil())
})

It("deal with name conflict", func() {
ctx := context.Background()
input := []map[string]interface{}{}
mustReadConsumer("consumer_name_conflict", &input)
for _, in := range input {
obj := pkg.MapToObj(in)
Expect(k8sClient.Create(ctx, obj)).Should(Succeed())
}

var consumers mosniov1.ConsumerList
Eventually(func() bool {
if err := k8sClient.List(ctx, &consumers); err != nil {
return false
}
handled := len(consumers.Items) == 2
for _, item := range consumers.Items {
conds := item.Status.Conditions
if len(conds) != 1 {
handled = false
break
}
}

return handled
}, timeout, interval).Should(BeTrue())

duplicatedFound := false
for _, item := range consumers.Items {
cs := item.Status.Conditions
if cs[0].Reason != string(mosniov1.ReasonAccepted) {
duplicatedFound = true
Expect(strings.Contains(cs[0].Message, "duplicate")).To(BeTrue())
break
}
}
Expect(duplicatedFound).To(BeTrue())
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
- apiVersion: htnn.mosn.io/v1
kind: Consumer
metadata:
name: spacewander
namespace: default
spec:
auth:
keyAuth:
config:
key: xx
- apiVersion: htnn.mosn.io/v1
kind: Consumer
metadata:
name: spacewander2
namespace: default
spec:
name: spacewander
auth:
keyAuth:
config:
key: xx
2 changes: 1 addition & 1 deletion e2e/tests/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func init() {

c := suite.K8sClient()
ctx := context.Background()
nsName := types.NamespacedName{Name: "morty", Namespace: k8s.DefaultNamespace}
nsName := types.NamespacedName{Name: "summer", Namespace: k8s.DefaultNamespace}
var consumer mosniov1.Consumer
err = c.Get(ctx, nsName, &consumer)
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion e2e/tests/consumer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ spec:
apiVersion: htnn.mosn.io/v1
kind: Consumer
metadata:
name: morty
name: summer
spec:
name: morty # the name specific in the spec is prior to the metadata name
auth:
keyAuth:
config:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ spec:
type: object
description: Filters is a map of filter names to filter configurations.
type: object
name:
description: |-
Name is the name of consumer, which is used in the data plane matching.
If this field is not set, the name of the consumer CustomResource will be used.
type: string
required:
- auth
type: object
Expand Down
6 changes: 6 additions & 0 deletions types/apis/v1/consumer_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type ConsumerSpec struct {
//
// +optional
Filters map[string]Plugin `json:"filters,omitempty"`

// Name is the name of consumer, which is used in the data plane matching.
// If this field is not set, the name of the consumer CustomResource will be used.
//
// +optional
Name string `json:"name,omitempty"`
}

// ConsumerStatus defines the observed state of Consumer
Expand Down

0 comments on commit 15734b8

Please sign in to comment.