-
Notifications
You must be signed in to change notification settings - Fork 0
/
j8a.go
266 lines (240 loc) · 7.33 KB
/
j8a.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package server
import (
"context"
"fmt"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"os"
"strings"
)
func int32Ptr(i int32) *int32 { return &i }
func (s *Server) createOrDetectJ8aNamespace() *Server {
nsName := &apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: s.J8a.Namespace,
},
}
ns, e := s.Kube.Client.CoreV1().Namespaces().
Create(context.Background(), nsName, metav1.CreateOptions{})
if e == nil {
s.J8a.Namespace = ns.ObjectMeta.Name
s.Log.Infof("created namespace '%v'", ns.ObjectMeta.Name)
} else {
ns, e := s.Kube.Client.CoreV1().Namespaces().
Get(context.Background(), s.J8a.Namespace, metav1.GetOptions{})
if ns != nil {
s.J8a.Namespace = ns.ObjectMeta.Name
s.Log.Infof("detected namespace '%v'", ns.ObjectMeta.Name)
}
if e != nil {
s.panic(fmt.Errorf("unable to create or namespace %v, cause: %v", s.J8a.Namespace, e))
}
}
return s
}
func (s *Server) createOrDetectJ8aServiceTypeLoadBalancer() *Server {
servicesClient := s.Kube.Client.CoreV1().Services(s.J8a.Namespace)
// Define the service
service := &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: s.J8a.Service,
Namespace: s.J8a.Namespace,
Annotations: map[string]string{
"service.beta.kubernetes.io/aws-load-balancer-type": "nlb",
//"service.beta.kubernetes.io/aws-load-balancer-internal": "false",
},
},
Spec: apiv1.ServiceSpec{
Selector: s.J8a.Pod.Label,
Type: apiv1.ServiceTypeLoadBalancer,
Ports: []apiv1.ServicePort{
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
Port: 80,
TargetPort: intstr.FromInt(80), // Assuming the pods expose port 80
},
{
Name: "https",
Protocol: apiv1.ProtocolTCP,
Port: 443,
TargetPort: intstr.FromInt(443), // Assuming the pods expose port 443
},
},
},
}
result, err := servicesClient.Create(context.TODO(), service, metav1.CreateOptions{})
if err != nil {
result, err := servicesClient.Get(context.TODO(), s.J8a.Service, metav1.GetOptions{})
if err != nil {
s.Log.Fatalf("unable to create or detect service '%v', cause: %v", s.J8a.Service, err)
} else {
s.Log.Infof("detected service '%v'", result.ObjectMeta.Name)
}
} else {
s.Log.Infof("created service '%v'", result.GetObjectMeta().GetName())
}
return s
}
func (s *Server) createOrDetectJ8aDeployment() *Server {
var v string
if strings.HasPrefix(s.J8a.Version, "v") {
v = s.J8a.Version[1:]
} else {
v = s.J8a.Version
}
deploymentsClient := s.Kube.Client.AppsV1().Deployments(s.J8a.Namespace)
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: s.J8a.Deployment.Name,
Namespace: s.J8a.Namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: int32Ptr(int32(s.J8a.Deployment.Replicas)),
Selector: &metav1.LabelSelector{
MatchLabels: s.J8a.Pod.Label,
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: s.J8a.Pod.Label,
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: s.J8a.Pod.Name,
Image: s.J8a.Image + ":" + v,
Ports: []apiv1.ContainerPort{
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
ContainerPort: 80,
},
{
Name: "https",
Protocol: apiv1.ProtocolTCP,
ContainerPort: 443,
},
},
Env: []apiv1.EnvVar{{
Name: "J8ACFG_YML",
Value: getInitialJ8aConfig(),
}},
},
},
},
},
},
}
result, err := deploymentsClient.Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil {
result, err := deploymentsClient.Get(context.TODO(), s.J8a.Deployment.Name, metav1.GetOptions{})
if err != nil {
s.Log.Fatalf("unable to create or detect deployment '%v', cause: %v", s.J8a.Deployment.Name, err)
} else {
s.Log.Infof("detected deployment '%v'", result.ObjectMeta.Name)
r := int(*result.Spec.Replicas)
if r != s.J8a.Deployment.Replicas {
//remember the current deployment scale of j8a
s.J8a.Deployment.Replicas = r
s.Log.Infof("j8a replicas configuration set to %v based on current value of deployment '%v'", r, result.ObjectMeta.Name)
}
}
} else {
s.Log.Infof("created deployment '%v'", result.GetObjectMeta().GetName())
}
return s
}
func (s *Server) createOrDetectJ8aIngressClass() *Server {
ingressClassClient := s.Kube.Client.NetworkingV1().IngressClasses()
// Create the IngressClass resource
ingressClass := &netv1.IngressClass{
ObjectMeta: metav1.ObjectMeta{
Name: "ingress-j8a",
Annotations: map[string]string{
"ingressclass.kubernetes.io/is-default-class": "true",
},
},
Spec: netv1.IngressClassSpec{
//TODO: what does this point to? the docker image name?
Controller: "github.com/simonmittag/ingress-j8a",
},
}
// Create the IngressClass using the client
ic, err := ingressClassClient.Create(context.TODO(), ingressClass, metav1.CreateOptions{})
if err != nil {
result, err := ingressClassClient.Get(context.TODO(), "ingress-j8a", metav1.GetOptions{})
if err != nil {
s.Log.Fatalf("unable to create or detect ingress class '%v', cause %v", s.J8a.Deployment.Name, err)
} else {
s.Log.Infof("detected ingressClass '%v'", result.ObjectMeta.Name)
}
} else {
s.Log.Infof("created ingressClass '%v'", ic.ObjectMeta.Name)
}
return s
}
// TODO: this may not work in the future but for initial config.
// server cannot process listener callbacks while this is running (but it could queue them)
func (s *Server) updateJ8aDeploymentWithFullClusterConfig() {
il, _ := s.fetchIngress()
s.updateCacheFromIngressList(il)
// get services
// get secrets
}
func (s *Server) updateCacheFromIngressList(il *netv1.IngressList) {
for _, igrs := range il.Items {
//we only process ingress where class is specified and points to J8a. Older kube versions without
//ingressclass are not supported.
if igrs.Spec.IngressClassName != nil && *igrs.Spec.IngressClassName == s.J8a.IngressClass {
if db := igrs.Spec.DefaultBackend; db != nil {
s1 := s.fetchServiceDNSName(*db)
_ = s1
}
for _, r := range igrs.Spec.Rules {
if r.HTTP != nil {
for _, b := range r.HTTP.Paths {
//TODO: these routes need hashed, then cached as resources.
s1 := s.fetchServiceDNSName(b.Backend)
//TODO: find a named service port if it is available, then look up the name and port.
//TODO: if this fails, skip the faulty ingress resource, don't include in config
po, e := s.FetchBackendServicePort(b.Backend)
_ = po
_ = e
jr := *NewRouteFrom(b.Path,
r.Host,
b.PathType,
s1)
s.Cache.update(jr)
}
}
}
}
}
}
func getTemplateJ8aConfig() string {
f := "resources/j8a/configtemplate.yml"
t, e := os.ReadFile(f)
if e != nil {
t, _ = os.ReadFile("../" + f)
}
return string(t)
}
func getInitialJ8aConfig() string {
return `---
connection:
downstream:
http:
port: 80
routes:
- path: "/"
resource: placeholder
resources:
placeholder:
- url:
scheme: http
host: localhost
port: 59999`
}