-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingress.go
144 lines (118 loc) · 4 KB
/
ingress.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
package vshnkeycloak
import (
"context"
"encoding/json"
"fmt"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
xhelmv1 "github.com/vshn/appcat/v4/apis/helm/release/v1beta1"
vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// AddIngress adds an inrgess to the Keycloak instance.
func AddIngress(_ context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {
comp := &vshnv1.VSHNKeycloak{}
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot get composite: %w", err))
}
if comp.Spec.Parameters.Service.FQDN == "" {
return nil
}
values, err := common.GetDesiredReleaseValues(svc, comp.GetName()+"-release")
if err != nil {
return runtime.NewWarningResult(fmt.Sprintf("cannot get desired release values: %s", err))
}
svc.Log.Info("Enable ingress for release")
enableIngresValues(svc, comp, values)
release := &xhelmv1.Release{}
err = svc.GetDesiredComposedResourceByName(release, comp.GetName()+"-release")
if err != nil {
return runtime.NewWarningResult(fmt.Sprintf("cannot get desired release: %s", err))
}
vb, err := json.Marshal(values)
if err != nil {
return runtime.NewWarningResult(fmt.Sprintf("cannot marhal values: %s", err))
}
release.Spec.ForProvider.Values.Raw = vb
err = svc.SetDesiredComposedResourceWithName(release, comp.GetName()+"-release")
if err != nil {
return runtime.NewWarningResult(fmt.Sprintf("cannot set desired release: %s", err))
}
return nil
}
func enableIngresValues(svc *runtime.ServiceRuntime, comp *vshnv1.VSHNKeycloak, values map[string]any) {
fqdn := comp.Spec.Parameters.Service.FQDN
relPath := `'{{ tpl .Values.http.relativePath $ | trimSuffix " / " }}/'`
if comp.Spec.Parameters.Service.RelativePath == "/" {
relPath = "/"
}
values["ingress"] = map[string]any{
"enabled": true,
"servicePort": "https",
"rules": []map[string]any{
{
"host": fqdn,
"paths": []map[string]any{
{
"path": relPath,
"pathType": "Prefix",
},
},
},
},
"tls": []map[string]any{
{
"hosts": []string{
fqdn,
},
"secretName": "keycloak-ingress-cert",
},
},
}
if svc.Config.Data["ingress_annotations"] != "" {
annotations := map[string]any{}
err := yaml.Unmarshal([]byte(svc.Config.Data["ingress_annotations"]), annotations)
if err != nil {
svc.Log.Error(err, "cannot unmarshal ingress annotations from input")
svc.AddResult(runtime.NewWarningResult(fmt.Sprintf("cannot unmarshal ingress annotations from input: %s", err)))
}
unstructured.SetNestedMap(values, annotations, "ingress", "annotations")
}
if svc.GetBoolFromCompositionConfig("isOpenshift") {
err := addOpenShiftCa(svc, comp)
if err != nil {
svc.Log.Error(err, "cannot add openshift ca secret")
svc.AddResult(runtime.NewWarningResult(fmt.Sprintf("cannot add openshift ca secret: %s", err)))
}
}
}
// addOpenShiftCa creates a separate secret just with the ca in it.
// This is required so that the ca on the route is properly set.
// For some reason openshift doesn't take the CA certificate from the ca.crt
// field of a tls secret... So we create a separate one which contains the CA
// cert in each mandatory field.
func addOpenShiftCa(svc *runtime.ServiceRuntime, comp *vshnv1.VSHNKeycloak) error {
cd, err := svc.GetObservedComposedResourceConnectionDetails(comp.GetName() + cdCertsSuffix)
if err != nil {
return err
}
keyName := "ca.crt"
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "route-ca",
Namespace: comp.GetInstanceNamespace(),
},
Type: corev1.SecretTypeTLS,
Data: map[string][]byte{
keyName: cd[keyName],
"tls.crt": cd[keyName],
"tls.key": cd[keyName],
},
}
return svc.SetDesiredKubeObject(secret, comp.GetName()+"-route-ca")
}