/
gateway.go
233 lines (187 loc) · 8.36 KB
/
gateway.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
package reconcile
import (
"context"
"fmt"
"strconv"
"time"
securityv1 "github.com/caapim/layer7-operator/api/v1"
"github.com/caapim/layer7-operator/pkg/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetGatewayPods returns the pods in a Gateway Deployment
func getGatewayPods(ctx context.Context, params Params) (*corev1.PodList, error) {
podList := &corev1.PodList{}
listOpts := []client.ListOption{
client.InNamespace(params.Instance.Namespace),
client.MatchingLabels(util.DefaultLabels(params.Instance.Name, map[string]string{})),
}
if err := params.Client.List(ctx, podList, listOpts...); err != nil {
return podList, err
}
return podList, nil
}
func getGatewayDeployment(ctx context.Context, params Params) (appsv1.Deployment, error) {
gatewayDeployment := &appsv1.Deployment{}
err := params.Client.Get(ctx, types.NamespacedName{Name: params.Instance.Name, Namespace: params.Instance.Namespace}, gatewayDeployment)
if err != nil {
return *gatewayDeployment, err
}
return *gatewayDeployment, nil
}
func getGraphmanEncryptionPassphrase(ctx context.Context, params Params, existingSecretName string, existingSecretKey string) (string, error) {
graphmanEncryptionSecret, err := getGatewaySecret(ctx, params, existingSecretName)
if err != nil {
return "", err
}
return string(graphmanEncryptionSecret.Data[existingSecretKey]), nil
}
func getGatewaySecret(ctx context.Context, params Params, name string) (*corev1.Secret, error) {
gwSecret := &corev1.Secret{}
err := params.Client.Get(ctx, types.NamespacedName{Name: name, Namespace: params.Instance.Namespace}, gwSecret)
if err != nil {
if k8serrors.IsNotFound(err) {
if err != nil {
return gwSecret, err
}
}
}
return gwSecret, nil
}
func GatewayLicense(ctx context.Context, params Params) error {
gatewayLicense := &corev1.Secret{}
err := params.Client.Get(ctx, types.NamespacedName{Name: params.Instance.Spec.License.SecretName, Namespace: params.Instance.Namespace}, gatewayLicense)
if k8serrors.IsNotFound(err) {
params.Log.Error(err, "license not found", "name", params.Instance.Name, "namespace", params.Instance.Namespace)
if err != nil {
return err
}
return nil
}
return err
}
func ManagementPod(ctx context.Context, params Params) error {
podList, err := getGatewayPods(ctx, params)
if err != nil {
return err
}
for p := range podList.Items {
if podList.Items[p].Labels["management-access"] == "leader" {
if podList.Items[p].DeletionTimestamp == nil {
return nil
}
}
}
tagged := false
for p := range podList.Items {
if podList.Items[p].Status.Phase == "Running" && podList.Items[p].DeletionTimestamp == nil && !tagged {
patch := []byte(`{"metadata":{"labels":{"management-access": "leader"}}}`)
if err := params.Client.Patch(context.Background(), &podList.Items[p],
client.RawPatch(types.StrategicMergePatchType, patch)); err != nil {
params.Log.Error(err, "failed to update pod label", "namespace", params.Instance.Namespace, "name", params.Instance.Name)
return err
}
params.Log.V(2).Info("new leader elected", "name", params.Instance.Name, "pod", podList.Items[p].Name, "namespace", params.Instance.Namespace)
tagged = true
}
}
return nil
}
func ReconcileEphemeralGateway(ctx context.Context, params Params, kind string, podList corev1.PodList, gateway *securityv1.Gateway, gwSecret *corev1.Secret, graphmanEncryptionPassphrase string, annotation string, sha1Sum string, otkCerts bool, bundle []byte) error {
graphmanPort := 9443
if gateway.Spec.App.Management.Graphman.DynamicSyncPort != 0 {
graphmanPort = gateway.Spec.App.Management.Graphman.DynamicSyncPort
}
for i, pod := range podList.Items {
currentSha1Sum := pod.ObjectMeta.Annotations[annotation]
update := false
ready := false
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.Name == "gateway" {
ready = containerStatus.Ready
}
}
if otkCerts {
if pod.ObjectMeta.Annotations["security.brcmlabs.com/"+gateway.Name+"-"+string(gateway.Spec.App.Otk.Type)+"-policies"] == "" {
ready = false
}
}
patch := fmt.Sprintf("{\"metadata\": {\"annotations\": {\"%s\": \"%s\"}}}", annotation, sha1Sum)
if currentSha1Sum != sha1Sum || currentSha1Sum == "" {
update = true
}
if update && ready {
endpoint := pod.Status.PodIP + ":" + strconv.Itoa(graphmanPort) + "/graphman"
requestCacheEntry := pod.Name + "-" + gateway.Name + "-" + sha1Sum
syncRequest, err := syncCache.Read(requestCacheEntry)
tryRequest := true
if err != nil {
params.Log.V(2).Info("request has not been attempted or cache was flushed", "action", "sync "+kind, "pod", pod.Name, "name", gateway.Name, "namespace", gateway.Namespace)
}
if syncRequest.Attempts > 0 {
params.Log.V(2).Info("request has been attempted in the last 30 seconds, backing off", "hash", sha1Sum, "pod", pod.Name, "name", gateway.Name, "namespace", gateway.Namespace)
tryRequest = false
}
if tryRequest {
syncCache.Update(util.SyncRequest{RequestName: requestCacheEntry, Attempts: 1}, time.Now().Add(30*time.Second).Unix())
params.Log.V(2).Info("applying "+kind, "hash", sha1Sum, "pod", pod.Name, "name", gateway.Name, "namespace", gateway.Namespace)
err = util.ApplyGraphmanBundle(string(gwSecret.Data["SSG_ADMIN_USERNAME"]), string(gwSecret.Data["SSG_ADMIN_PASSWORD"]), endpoint, graphmanEncryptionPassphrase, bundle)
if err != nil {
return err
}
params.Log.Info("applied "+kind, "hash", sha1Sum, "pod", pod.Name, "name", gateway.Name, "namespace", gateway.Namespace)
if err := params.Client.Patch(context.Background(), &podList.Items[i],
client.RawPatch(types.StrategicMergePatchType, []byte(patch))); err != nil {
params.Log.Error(err, "failed to update pod label", "Name", gateway.Name, "namespace", gateway.Namespace)
return err
}
}
}
}
return nil
}
func ReconcileDBGateway(ctx context.Context, params Params, kind string, gatewayDeployment appsv1.Deployment, gateway *securityv1.Gateway, gwSecret *corev1.Secret, graphmanEncryptionPassphrase string, annotation string, sha1Sum string, otkCerts bool, bundle []byte) error {
graphmanPort := 9443
if gateway.Spec.App.Management.Graphman.DynamicSyncPort != 0 {
graphmanPort = gateway.Spec.App.Management.Graphman.DynamicSyncPort
}
patch := fmt.Sprintf("{\"metadata\": {\"annotations\": {\"%s\": \"%s\"}}}", annotation, sha1Sum)
ready := false
if gatewayDeployment.ObjectMeta.Annotations[annotation] == sha1Sum {
return nil
}
if gatewayDeployment.Status.ReadyReplicas == gatewayDeployment.Status.Replicas {
ready = true
}
if ready {
requestCacheEntry := gatewayDeployment.Name + "-" + sha1Sum
syncRequest, err := syncCache.Read(requestCacheEntry)
if err != nil {
params.Log.V(2).Info("request has not been attempted or cache was flushed", "action", "sync "+kind, "Name", gateway.Name, "Namespace", gateway.Namespace)
}
if syncRequest.Attempts > 0 {
params.Log.V(2).Info("request has been attempted in the last 30 seconds, backing off", "hash", sha1Sum, "Name", gateway.Name, "Namespace", gateway.Namespace)
return nil
}
syncCache.Update(util.SyncRequest{RequestName: requestCacheEntry, Attempts: 1}, time.Now().Add(30*time.Second).Unix())
endpoint := gateway.Name + "." + gateway.Namespace + ".svc.cluster.local:" + strconv.Itoa(graphmanPort) + "/graphman"
if gateway.Spec.App.Management.Service.Enabled {
endpoint = gateway.Name + "-management-service." + gateway.Namespace + ".svc.cluster.local:" + strconv.Itoa(graphmanPort) + "/graphman"
}
params.Log.V(2).Info("applying latest "+kind, "sha1Sum", sha1Sum, "name", gateway.Name, "namespace", gateway.Namespace)
err = util.ApplyGraphmanBundle(string(gwSecret.Data["SSG_ADMIN_USERNAME"]), string(gwSecret.Data["SSG_ADMIN_PASSWORD"]), endpoint, graphmanEncryptionPassphrase, bundle)
if err != nil {
return err
}
params.Log.Info("applied latest "+kind, "sha1Sum", sha1Sum, "name", gateway.Name, "namespace", gateway.Namespace)
if err := params.Client.Patch(context.Background(), &gatewayDeployment,
client.RawPatch(types.StrategicMergePatchType, []byte(patch))); err != nil {
params.Log.Error(err, "Failed to update deployment annotations", "Namespace", params.Instance.Namespace, "Name", params.Instance.Name)
return err
}
}
return nil
}