Skip to content

Commit 4312cc8

Browse files
Add ingress
1 parent 48640ea commit 4312cc8

File tree

9 files changed

+244
-2
lines changed

9 files changed

+244
-2
lines changed

api/v1/immudb_types.go

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1
1818

1919
import (
2020
corev1 "k8s.io/api/core/v1"
21+
knetworkingv1 "k8s.io/api/networking/v1"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
)
2324

@@ -46,6 +47,9 @@ type ImmudbSpec struct {
4647

4748
// +kubebuilder:validation:Required
4849
Volume ImmudbVolumeSpec `json:"volume"`
50+
51+
// +kubebuilder:validation:Required
52+
Ingress ImmudbIngressSpec `json:"ingress"`
4953
}
5054

5155
type ImmudbVolumeSpec struct {
@@ -60,6 +64,21 @@ type ImmudbVolumeSpec struct {
6064
Size string `json:"size"`
6165
}
6266

67+
type ImmudbIngressSpec struct {
68+
// +kubebuilder:validation:Required
69+
Enabled bool `json:"enabled"`
70+
71+
// +kubebuilder:validation:Optional
72+
// +kubebuilder:default=nginx
73+
IngressClassName *string `json:"ingressClassName"`
74+
75+
// +kubebuilder:validation:Optional
76+
TLS []knetworkingv1.IngressTLS `json:"tls"`
77+
78+
// +kubebuilder:validation:Optional
79+
Host string `json:"host"`
80+
}
81+
6382
// ImmudbStatus defines the observed state of Immudb
6483
type ImmudbStatus struct {
6584
// Important: Run "make" to regenerate code after modifying this file

api/v1/zz_generated.deepcopy.go

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/operator/Chart.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ apiVersion: v2
22
name: immudb-operator
33
description: Helm chart to deploy [unagex-immudb-operator](https://github.com/unagex/immudb-operator)
44
type: application
5-
version: 0.0.5
6-
appVersion: 0.0.5
5+
version: 0.0.6
6+
appVersion: 0.0.6
77
home: https://github.com/unagex/immudb-operator

charts/operator/templates/crds/unagex.com_immudbs.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,44 @@ spec:
4747
- Never
4848
- IfNotPresent
4949
type: string
50+
ingress:
51+
properties:
52+
enabled:
53+
type: boolean
54+
host:
55+
type: string
56+
ingressClassName:
57+
default: nginx
58+
type: string
59+
tls:
60+
items:
61+
description: IngressTLS describes the transport layer security
62+
associated with an ingress.
63+
properties:
64+
hosts:
65+
description: hosts is a list of hosts included in the TLS
66+
certificate. The values in this list must match the name/s
67+
used in the tlsSecret. Defaults to the wildcard host setting
68+
for the loadbalancer controller fulfilling this Ingress,
69+
if left unspecified.
70+
items:
71+
type: string
72+
type: array
73+
x-kubernetes-list-type: atomic
74+
secretName:
75+
description: secretName is the name of the secret used to
76+
terminate TLS traffic on port 443. Field is left optional
77+
to allow TLS routing based on SNI hostname alone. If the
78+
SNI host in a listener conflicts with the "Host" header
79+
field used by an IngressRule, the SNI host is used for
80+
termination and value of the "Host" header is used for
81+
routing.
82+
type: string
83+
type: object
84+
type: array
85+
required:
86+
- enabled
87+
type: object
5088
replicas:
5189
default: 1
5290
description: Number of desired immudb pods. At the moment, you can
@@ -68,6 +106,7 @@ spec:
68106
- size
69107
type: object
70108
required:
109+
- ingress
71110
- volume
72111
type: object
73112
status:
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: networking.k8s.io/v1
2+
kind: Ingress
3+
metadata:
4+
name: oula-immudb-http
5+
namespace: default
6+
spec:
7+
rules:
8+
- host: immudb-example.localhost
9+
http:
10+
paths:
11+
- backend:
12+
service:
13+
name: oula-immudb-http
14+
port:
15+
number: 8080
16+
path: /
17+
pathType: Prefix
18+
status:
19+
loadBalancer: {}

config/crd/bases/unagex.com_immudbs.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,44 @@ spec:
4747
- Never
4848
- IfNotPresent
4949
type: string
50+
ingress:
51+
properties:
52+
enabled:
53+
type: boolean
54+
host:
55+
type: string
56+
ingressClassName:
57+
default: nginx
58+
type: string
59+
tls:
60+
items:
61+
description: IngressTLS describes the transport layer security
62+
associated with an ingress.
63+
properties:
64+
hosts:
65+
description: hosts is a list of hosts included in the TLS
66+
certificate. The values in this list must match the name/s
67+
used in the tlsSecret. Defaults to the wildcard host setting
68+
for the loadbalancer controller fulfilling this Ingress,
69+
if left unspecified.
70+
items:
71+
type: string
72+
type: array
73+
x-kubernetes-list-type: atomic
74+
secretName:
75+
description: secretName is the name of the secret used to
76+
terminate TLS traffic on port 443. Field is left optional
77+
to allow TLS routing based on SNI hostname alone. If the
78+
SNI host in a listener conflicts with the "Host" header
79+
field used by an IngressRule, the SNI host is used for
80+
termination and value of the "Host" header is used for
81+
routing.
82+
type: string
83+
type: object
84+
type: array
85+
required:
86+
- enabled
87+
type: object
5088
replicas:
5189
default: 1
5290
description: Number of desired immudb pods. At the moment, you can
@@ -68,6 +106,7 @@ spec:
68106
- size
69107
type: object
70108
required:
109+
- ingress
71110
- volume
72111
type: object
73112
status:

config/samples/v1_immudb.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ spec:
99
volume:
1010
# storageClassName: "standard"
1111
size: 1Gi
12+
ingress:
13+
enabled: false
14+
# ingressClassName: nginx
15+
# host: example.com
16+
# tls:
17+
# - hosts:
18+
# - example.com
19+
# secretName : immudb-sample-secret

internal/controller/controller.go

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func (r *ImmudbReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
6666
return ctrl.Result{}, err
6767
}
6868

69+
err = r.ManageIngress(ctx, immudb)
70+
if err != nil {
71+
return ctrl.Result{}, err
72+
}
73+
6974
return ctrl.Result{}, nil
7075
}
7176

internal/controller/ingress.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
unagexcomv1 "github.com/unagex/immudb-operator/api/v1"
8+
"github.com/unagex/immudb-operator/internal/controller/common"
9+
knetworkingv1 "k8s.io/api/networking/v1"
10+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/types"
13+
"k8s.io/utils/ptr"
14+
)
15+
16+
func (r *ImmudbReconciler) ManageIngress(ctx context.Context, immudb *unagexcomv1.Immudb) error {
17+
if !immudb.Spec.Ingress.Enabled {
18+
return nil
19+
}
20+
21+
sts := &knetworkingv1.Ingress{}
22+
err := r.Get(ctx, types.NamespacedName{
23+
Namespace: immudb.Namespace,
24+
Name: immudb.Name,
25+
}, sts)
26+
27+
// create if ingress does not exist
28+
if k8serrors.IsNotFound(err) {
29+
sts := r.GetIngress(immudb)
30+
err := r.Create(ctx, sts)
31+
if err != nil && !k8serrors.IsAlreadyExists(err) {
32+
return fmt.Errorf("error creating ingress: %w", err)
33+
}
34+
if err == nil {
35+
r.Log.Info("ingress created")
36+
}
37+
return nil
38+
}
39+
40+
if err != nil {
41+
return fmt.Errorf("error getting ingress: %w", err)
42+
}
43+
44+
return nil
45+
}
46+
47+
func (r *ImmudbReconciler) GetIngress(immudb *unagexcomv1.Immudb) *knetworkingv1.Ingress {
48+
ls := common.GetLabels(immudb.Name)
49+
return &knetworkingv1.Ingress{
50+
ObjectMeta: metav1.ObjectMeta{
51+
Name: immudb.Name,
52+
Namespace: immudb.Namespace,
53+
OwnerReferences: common.GetOwnerReferences(immudb),
54+
Labels: ls,
55+
},
56+
Spec: knetworkingv1.IngressSpec{
57+
IngressClassName: immudb.Spec.Ingress.IngressClassName,
58+
TLS: immudb.Spec.Ingress.TLS,
59+
Rules: []knetworkingv1.IngressRule{
60+
{
61+
Host: immudb.Spec.Ingress.Host,
62+
IngressRuleValue: knetworkingv1.IngressRuleValue{
63+
HTTP: &knetworkingv1.HTTPIngressRuleValue{
64+
Paths: []knetworkingv1.HTTPIngressPath{
65+
{
66+
Path: "/",
67+
PathType: ptr.To(knetworkingv1.PathTypePrefix),
68+
Backend: knetworkingv1.IngressBackend{
69+
Service: &knetworkingv1.IngressServiceBackend{
70+
Name: immudb.Name + "-http",
71+
Port: knetworkingv1.ServiceBackendPort{
72+
Name: "http",
73+
},
74+
},
75+
},
76+
},
77+
},
78+
},
79+
},
80+
},
81+
},
82+
},
83+
}
84+
}

0 commit comments

Comments
 (0)