feat: add ingress
This commit is contained in:
parent
c20a316026
commit
4983f8b4b1
@ -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 {
|
||||
|
@ -143,6 +143,8 @@ spec:
|
||||
- name
|
||||
type: object
|
||||
type: array
|
||||
hostname:
|
||||
type: string
|
||||
image:
|
||||
type: string
|
||||
ports:
|
||||
|
@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user