From 4983f8b4b1789d5d398c897f97c229d69c863950 Mon Sep 17 00:00:00 2001 From: cdryzun Date: Tue, 5 Sep 2023 20:14:59 +0800 Subject: [PATCH] feat: add ingress --- api/v1alpha1/appservice_types.go | 1 + ...p.treesir.pub.treesir.pub_appservices.yaml | 2 + controllers/appservice_controller.go | 92 ++++++++++++++++++- test.yaml | 1 + 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/api/v1alpha1/appservice_types.go b/api/v1alpha1/appservice_types.go index 1ed5765..559a0f1 100644 --- a/api/v1alpha1/appservice_types.go +++ b/api/v1alpha1/appservice_types.go @@ -35,6 +35,7 @@ type AppServiceSpec struct { Resources *Resources `json:"resources,omitempty"` Envs []corev1.EnvVar `json:"envs,omitempty"` Ports []corev1.ServicePort `json:"ports,omitempty"` + Hostname string `json:"hostname,omitempty"` } type Resources struct { diff --git a/config/crd/bases/app.treesir.pub.treesir.pub_appservices.yaml b/config/crd/bases/app.treesir.pub.treesir.pub_appservices.yaml index 2a83e13..519fdf8 100644 --- a/config/crd/bases/app.treesir.pub.treesir.pub_appservices.yaml +++ b/config/crd/bases/app.treesir.pub.treesir.pub_appservices.yaml @@ -143,6 +143,8 @@ spec: - name type: object type: array + hostname: + type: string image: type: string ports: diff --git a/controllers/appservice_controller.go b/controllers/appservice_controller.go index dd25214..20c17af 100644 --- a/controllers/appservice_controller.go +++ b/controllers/appservice_controller.go @@ -22,6 +22,8 @@ import ( "fmt" "reflect" + v1 "k8s.io/api/networking/v1" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -96,6 +98,15 @@ func (r *AppServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) if err := r.Create(context.TODO(), service); err != nil { return ctrl.Result{}, err } + + // 2.1 创建 Ingress + if instance.Spec.Hostname != "" { + ingress := NewIngress(instance) + if err := r.Client.Create(context.TODO(), ingress); err != nil { + return ctrl.Result{}, err + } + } + // 3. 关联 Annotations data, _ := json.Marshal(instance.Spec) if instance.Annotations != nil { @@ -103,10 +114,10 @@ func (r *AppServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) } else { instance.Annotations = map[string]string{"spec": string(data)} } - if err := r.Update(context.TODO(), instance); err != nil { return ctrl.Result{}, nil } + return ctrl.Result{}, nil } @@ -137,6 +148,31 @@ func (r *AppServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{}, err } + if instance.Spec.Hostname != "" { + newIngress := NewIngress(instance) + r.Client.Create(context.TODO(), newIngress) + + oldIngress := &v1.Ingress{} + if err := r.Get(context.TODO(), req.NamespacedName, oldIngress); err != nil { + return ctrl.Result{}, err + } + oldIngress.Spec = newIngress.Spec + if err := r.Update(context.TODO(), oldIngress); err != nil { + return ctrl.Result{}, err + } + } else { + // 删除 Ingress + oldIngress := &v1.Ingress{ + ObjectMeta: ctrl.ObjectMeta{ + Name: instance.Name, + Namespace: instance.Namespace, + }, + } + if err := r.Delete(context.Background(), oldIngress); err != nil { + return ctrl.Result{}, err + } + } + return ctrl.Result{}, nil } @@ -246,3 +282,57 @@ func newContainers(instance *apptreesirpubv1alpha1.AppService) []corev1.Containe container, } } + +// 实现 NewIngress + +func NewIngress(instance *apptreesirpubv1alpha1.AppService) *v1.Ingress { + return &v1.Ingress{ + ObjectMeta: ctrl.ObjectMeta{ + Name: instance.Name, + Namespace: instance.Namespace, + Annotations: map[string]string{ + "nginx.ingress.kubernetes.io/rewrite-target": "/", + }, + Labels: map[string]string{ + "app": instance.Name, + }, + OwnerReferences: []metav1.OwnerReference{ + *metav1.NewControllerRef( + instance, + schema.GroupVersionKind{ + Group: apptreesirpubv1alpha1.SchemeBuilder.GroupVersion.Group, + Version: apptreesirpubv1alpha1.SchemeBuilder.GroupVersion.Version, + Kind: "AppService", + }, + )}, + }, + Spec: v1.IngressSpec{ + Rules: []v1.IngressRule{ + { + Host: instance.Spec.Hostname, + IngressRuleValue: v1.IngressRuleValue{ + HTTP: &v1.HTTPIngressRuleValue{ + Paths: []v1.HTTPIngressPath{ + { + Path: "/", + PathType: func() *v1.PathType { + pathType := v1.PathTypePrefix + return &pathType + }(), + Backend: v1.IngressBackend{ + Service: &v1.IngressServiceBackend{ + Name: instance.Name, + Port: v1.ServiceBackendPort{ + Number: instance.Spec.Ports[0].TargetPort.IntVal, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + } +} diff --git a/test.yaml b/test.yaml index 73dcd8d..bd49b9a 100644 --- a/test.yaml +++ b/test.yaml @@ -11,6 +11,7 @@ metadata: spec: size: 1 image: nginx:1.7.9 + hostname: "test.treesir.pub" ports: - port: 80 targetPort: 80