-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathservice.go
179 lines (157 loc) · 5.41 KB
/
service.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package kong2kic
import (
"log"
"strconv"
"strings"
"github.com/kong/go-database-reconciler/pkg/file"
k8scorev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
// Main function to populate KIC services with annotations
func populateKICServicesWithAnnotations(content *file.Content, kicContent *KICContent) error {
for _, service := range content.Services {
if service.Name == nil {
log.Println("Service name is empty. Please provide a name for the service.")
continue
}
// Create Kubernetes service
k8sService := createK8sService(&service, content.Upstreams)
// Add annotations from the service object
addAnnotationsFromService(&service, k8sService.ObjectMeta.Annotations)
// Populate upstream policies based on KIC version
if targetKICVersionAPI == KICV3GATEWAY || targetKICVersionAPI == KICV3INGRESS {
populateKICUpstreamPolicy(content, &service, k8sService, kicContent)
} else {
populateKICUpstream(content, &service, k8sService, kicContent)
}
// Add plugins to the service
err := addPluginsToService(&service, k8sService, kicContent)
if err != nil {
return err
}
// Append the Kubernetes service to KIC content
kicContent.Services = append(kicContent.Services, *k8sService)
}
return nil
}
// Helper function to create a Kubernetes Service from a Kong service
func createK8sService(service *file.FService, upstreams []file.FUpstream) *k8scorev1.Service {
k8sService := &k8scorev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: ServiceAPIVersionv1,
Kind: ServiceKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: calculateSlug(*service.Name),
Annotations: make(map[string]string),
},
}
// Determine the protocol (default to TCP)
protocol := k8scorev1.ProtocolTCP
if service.Protocol != nil && strings.ToUpper(*service.Protocol) == string(k8scorev1.ProtocolUDP) {
protocol = k8scorev1.ProtocolUDP
}
// Set the service port
if service.Port != nil {
// check that the port is within the valid range
if *service.Port > 65535 || *service.Port < 0 {
log.Fatalf("Port %d is not within the valid range. Please provide a port between 0 and 65535.\n", *service.Port)
}
servicePort := k8scorev1.ServicePort{
Protocol: protocol,
//nolint: gosec
Port: int32(*service.Port),
TargetPort: intstr.FromInt(*service.Port),
}
k8sService.Spec.Ports = []k8scorev1.ServicePort{servicePort}
}
// Configure the service selector or external name
if service.Host == nil {
k8sService.Spec.Selector = map[string]string{"app": *service.Name}
} else {
k8sService.Spec.Type = k8scorev1.ServiceTypeExternalName
k8sService.Spec.ExternalName = *service.Host
// Check if the host matches any upstream
if isUpstreamReferenced(service.Host, upstreams) {
k8sService.Spec.Selector = map[string]string{"app": *service.Name}
k8sService.Spec.Type = ""
k8sService.Spec.ExternalName = ""
}
}
return k8sService
}
// Helper function to check if the service host matches any upstream name
func isUpstreamReferenced(host *string, upstreams []file.FUpstream) bool {
for _, upstream := range upstreams {
if upstream.Name != nil && strings.EqualFold(*upstream.Name, *host) {
return true
}
}
return false
}
// Helper function to add annotations from a service to a Kubernetes service
func addAnnotationsFromService(service *file.FService, annotations map[string]string) {
if service.Protocol != nil {
annotations[KongHQProtocol] = *service.Protocol
}
if service.Path != nil {
annotations[KongHQPath] = *service.Path
}
if service.ClientCertificate != nil && service.ClientCertificate.ID != nil {
annotations[KongHQClientCert] = *service.ClientCertificate.ID
}
if service.ReadTimeout != nil {
annotations[KongHQReadTimeout] = strconv.Itoa(*service.ReadTimeout)
}
if service.WriteTimeout != nil {
annotations[KongHQWriteTimeout] = strconv.Itoa(*service.WriteTimeout)
}
if service.ConnectTimeout != nil {
annotations[KongHQConnectTimeout] = strconv.Itoa(*service.ConnectTimeout)
}
if service.Retries != nil {
annotations[KongHQRetries] = strconv.Itoa(*service.Retries)
}
addTagsToAnnotations(service.Tags, annotations)
}
// processPlugin is a helper function that processes a single plugin for a service
func processPlugin(
plugin *file.FPlugin,
ownerName string,
annotations map[string]string,
kicContent *KICContent,
) error {
if plugin.Name == nil {
log.Println("Plugin name is empty. Please provide a name for the plugin.")
return nil
}
// Create a KongPlugin
kongPlugin, err := createKongPlugin(plugin, ownerName)
if err != nil {
return err
}
if kongPlugin == nil {
return nil
}
// Add the plugin name to the service annotations
addPluginToAnnotations(kongPlugin.ObjectMeta.Name, annotations)
// Append the KongPlugin to KIC content
kicContent.KongPlugins = append(kicContent.KongPlugins, *kongPlugin)
return nil
}
// addPluginsToService adds plugins from both service-level and top-level plugin configurations
func addPluginsToService(service *file.FService, k8sService *k8scorev1.Service, kicContent *KICContent) error {
if service.Name == nil {
log.Println("Service name is empty. Please provide a name for the service.")
return nil
}
ownerName := *service.Name
// Process service-level plugins
for _, plugin := range service.Plugins {
if err := processPlugin(plugin, ownerName, k8sService.ObjectMeta.Annotations, kicContent); err != nil {
return err
}
}
return nil
}