Skip to content

Commit

Permalink
Switched templates to not be pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Cali0707 committed Jun 28, 2023
1 parent a9d4ee9 commit d7206cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
34 changes: 10 additions & 24 deletions control-plane/pkg/apis/config/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ type features struct {
DispatcherRateLimiter feature.Flag
DispatcherOrderedExecutorMetrics feature.Flag
ControllerAutoscaler feature.Flag
TriggersConsumerGroupTemplate *template.Template
BrokersTopicTemplate *template.Template
ChannelsTopicTemplate *template.Template
TriggersConsumerGroupTemplate template.Template
BrokersTopicTemplate template.Template
ChannelsTopicTemplate template.Template
}

type KafkaFeatureFlags struct {
Expand All @@ -63,28 +63,14 @@ func init() {
}

func DefaultFeaturesConfig() *KafkaFeatureFlags {
// we need to clone the default values when creating a new features config
// otherwise, when calling NewFeaturesConfigFromMap, we will overwrite the default variables
triggersGroupTemplate, err := defaultTriggersConsumerGroupTemplate.Clone()
if err != nil {
panic("failed to clone default triggers group template")
}
brokersTopicTemplate, err := defaultBrokersTopicTemplate.Clone()
if err != nil {
panic("failed to clone default brokers topic template")
}
channelsTopicTemplate, err := defaultChannelsTopicTemplate.Clone()
if err != nil {
panic("failed to clone default channels topic template")
}
return &KafkaFeatureFlags{
features: features{
DispatcherRateLimiter: feature.Disabled,
DispatcherOrderedExecutorMetrics: feature.Disabled,
ControllerAutoscaler: feature.Disabled,
TriggersConsumerGroupTemplate: triggersGroupTemplate,
BrokersTopicTemplate: brokersTopicTemplate,
ChannelsTopicTemplate: channelsTopicTemplate,
TriggersConsumerGroupTemplate: *defaultTriggersConsumerGroupTemplate,
BrokersTopicTemplate: *defaultBrokersTopicTemplate,
ChannelsTopicTemplate: *defaultChannelsTopicTemplate,
},
}
}
Expand All @@ -96,9 +82,9 @@ func NewFeaturesConfigFromMap(cm *corev1.ConfigMap) (*KafkaFeatureFlags, error)
asFlag("dispatcher.rate-limiter", &nc.features.DispatcherRateLimiter),
asFlag("dispatcher.ordered-executor-metrics", &nc.features.DispatcherOrderedExecutorMetrics),
asFlag("controller.autoscaler", &nc.features.ControllerAutoscaler),
asTemplate("triggers.consumergroup.template", nc.features.TriggersConsumerGroupTemplate),
asTemplate("brokers.topic.template", nc.features.BrokersTopicTemplate),
asTemplate("channels.topic.template", nc.features.ChannelsTopicTemplate),
asTemplate("triggers.consumergroup.template", &nc.features.TriggersConsumerGroupTemplate),
asTemplate("brokers.topic.template", &nc.features.BrokersTopicTemplate),
asTemplate("channels.topic.template", &nc.features.ChannelsTopicTemplate),
)
return nc, err
}
Expand Down Expand Up @@ -216,7 +202,7 @@ func asTemplate(key string, target *template.Template) configmap.ParseFunc {
}
}

func executeTemplateToString(template *template.Template, metadata v1.ObjectMeta, errorMessage string) (string, error) {
func executeTemplateToString(template template.Template, metadata v1.ObjectMeta, errorMessage string) (string, error) {
var result bytes.Buffer
err := template.Execute(&result, metadata)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions control-plane/pkg/apis/config/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func TestExecuteTriggersConsumerGroupTemplateDefault(t *testing.T) {

func TestExecuteTriggersConsumerGroupTemplateOverride(t *testing.T) {
nc := DefaultFeaturesConfig()
nc.features.TriggersConsumerGroupTemplate, _ = template.New("custom-template").Parse("knative-trigger-{{ .Namespace }}-{{ .Name }}-{{ .UID }}")
customTemplate, _ := template.New("custom-template").Parse("knative-trigger-{{ .Namespace }}-{{ .Name }}-{{ .UID }}")
nc.features.TriggersConsumerGroupTemplate = *customTemplate

result, err := nc.ExecuteTriggersConsumerGroupTemplate(v1.ObjectMeta{
Name: "trigger",
Expand Down Expand Up @@ -141,7 +142,8 @@ func TestExecuteBrokersTopicTemplateDefault(t *testing.T) {

func TestExecuteBrokersTopicTemplateOverride(t *testing.T) {
nc := DefaultFeaturesConfig()
nc.features.BrokersTopicTemplate, _ = template.New("custom-template").Parse("knative-broker-{{ .Namespace }}-{{ .Name }}-{{ .UID }}")
customTemplate, _ := template.New("custom-template").Parse("knative-broker-{{ .Namespace }}-{{ .Name }}-{{ .UID }}")
nc.features.BrokersTopicTemplate = *customTemplate

result, err := nc.ExecuteBrokersTopicTemplate(v1.ObjectMeta{
Name: "topic",
Expand Down Expand Up @@ -171,7 +173,8 @@ func TestExecuteChannelsTopicTemplateDefault(t *testing.T) {

func TestExecuteChannelsTopicTemplateOverride(t *testing.T) {
nc := DefaultFeaturesConfig()
nc.features.ChannelsTopicTemplate, _ = template.New("custom-template").Parse("knative-channel-{{ .Namespace }}-{{ .Name }}-{{ .UID }}")
customTemplate, _ := template.New("custom-template").Parse("knative-channel-{{ .Namespace }}-{{ .Name }}-{{ .UID }}")
nc.features.ChannelsTopicTemplate = *customTemplate

result, err := nc.ExecuteChannelsTopicTemplate(v1.ObjectMeta{
Name: "topic",
Expand Down

0 comments on commit d7206cd

Please sign in to comment.