-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathconsumer_group.go
104 lines (90 loc) · 3.09 KB
/
consumer_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package kong2kic
import (
"encoding/json"
"log"
"github.com/kong/go-database-reconciler/pkg/file"
"github.com/kong/go-kong/kong"
configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Helper function to populate consumer group plugins
func createConsumerGroupKongPlugin(
plugin *kong.ConsumerGroupPlugin, ownerName string,
) (*configurationv1.KongPlugin, error) {
if plugin.Name == nil {
log.Println("Plugin name is empty. Please provide a name for the plugin.")
return nil, nil
}
pluginName := *plugin.Name
kongPlugin := &configurationv1.KongPlugin{
TypeMeta: metav1.TypeMeta{
APIVersion: ConfigurationKongHQv1,
Kind: KongPluginKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: calculateSlug(ownerName + "-" + pluginName),
Annotations: map[string]string{IngressClass: ClassName},
},
PluginName: pluginName,
}
// Transform the plugin config
configJSON, err := json.Marshal(plugin.Config)
if err != nil {
return nil, err
}
kongPlugin.Config = apiextensionsv1.JSON{
Raw: configJSON,
}
return kongPlugin, nil
}
func populateKICConsumerGroups(content *file.Content, kicContent *KICContent) error {
for _, consumerGroup := range content.ConsumerGroups {
if consumerGroup.Name == nil {
log.Println("Consumer group name is empty. Please provide a name for the consumer group.")
continue
}
groupName := *consumerGroup.Name
kongConsumerGroup := configurationv1beta1.KongConsumerGroup{
TypeMeta: metav1.TypeMeta{
APIVersion: ConfigurationKongHQv1beta1,
Kind: KongConsumerGroupKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: calculateSlug(groupName),
Annotations: map[string]string{IngressClass: ClassName},
},
}
// Add tags to annotations
addTagsToAnnotations(consumerGroup.Tags, kongConsumerGroup.ObjectMeta.Annotations)
// Update the ConsumerGroups field of the KongConsumers
for _, consumer := range consumerGroup.Consumers {
if consumer.Username == nil {
log.Println("Consumer username is empty. Please provide a username for the consumer.")
continue
}
username := *consumer.Username
for idx := range kicContent.KongConsumers {
if kicContent.KongConsumers[idx].Username == username {
kicContent.KongConsumers[idx].ConsumerGroups = append(kicContent.KongConsumers[idx].ConsumerGroups, groupName)
}
}
}
// Handle plugins
for _, plugin := range consumerGroup.Plugins {
kongPlugin, err := createConsumerGroupKongPlugin(plugin, groupName)
if err != nil {
return err
}
if kongPlugin == nil {
continue
}
kicContent.KongPlugins = append(kicContent.KongPlugins, *kongPlugin)
// Add plugin to kongConsumerGroup annotations
addPluginToAnnotations(kongPlugin.ObjectMeta.Name, kongConsumerGroup.ObjectMeta.Annotations)
}
kicContent.KongConsumerGroups = append(kicContent.KongConsumerGroups, kongConsumerGroup)
}
return nil
}