|
|
|
@ -22,6 +22,8 @@ import (
|
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
|
networkv1 "k8s.io/api/networking/v1"
|
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
"reflect"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
@ -81,7 +83,7 @@ func (r *MsbDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
|
|
|
|
|
} else {
|
|
|
|
|
// 2.2 存在对象
|
|
|
|
|
// 2.2.1 更新 deployment
|
|
|
|
|
if err := r.updateDeployment(ctx, mdCopy); err != nil {
|
|
|
|
|
if err := r.updateDeployment(ctx, mdCopy, deploy); err != nil {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -115,13 +117,13 @@ func (r *MsbDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
|
|
|
|
|
if strings.ToLower(mdCopy.Spec.Expose.Mode) == myAppsv1.ModeIngress {
|
|
|
|
|
// 3.2.1 mode 为 ingress
|
|
|
|
|
// 3.2.1.1 更新普通的 service
|
|
|
|
|
if err := r.updateService(ctx, mdCopy); err != nil {
|
|
|
|
|
if err := r.updateService(ctx, mdCopy, svc); err != nil {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
} else if strings.ToLower(mdCopy.Spec.Expose.Mode) == myAppsv1.ModeNodePort {
|
|
|
|
|
// 3.2.2 mode 为 nodeport
|
|
|
|
|
// 3.2.2.1 更新nodeport模式的service
|
|
|
|
|
if err := r.updateNPService(ctx, mdCopy); err != nil {
|
|
|
|
|
if err := r.updateNPService(ctx, mdCopy, svc); err != nil {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
@ -154,7 +156,7 @@ func (r *MsbDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
|
|
|
|
|
if strings.ToLower(mdCopy.Spec.Expose.Mode) == myAppsv1.ModeIngress {
|
|
|
|
|
// 4.2.1 mode 为 ingress
|
|
|
|
|
// 4.2.1.1更新 ingress
|
|
|
|
|
if err := r.updateIngress(ctx, mdCopy); err != nil {
|
|
|
|
|
if err := r.updateIngress(ctx, mdCopy, ig); err != nil {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
} else if strings.ToLower(mdCopy.Spec.Expose.Mode) == myAppsv1.ModeNodePort {
|
|
|
|
@ -174,6 +176,9 @@ func (r *MsbDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Reques
|
|
|
|
|
func (r *MsbDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
|
|
|
return ctrl.NewControllerManagedBy(mgr).
|
|
|
|
|
For(&myAppsv1.MsbDeployment{}).
|
|
|
|
|
Owns(&appsv1.Deployment{}). // 监控 deployment 类型,变更就触发 reconciler
|
|
|
|
|
Owns(&corev1.Service{}). // 监控 service 类型,变更就触发 reconciler
|
|
|
|
|
Owns(&networkv1.Ingress{}). // 监控 ingress 类型,变更就触发 reconciler
|
|
|
|
|
Complete(r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -182,14 +187,36 @@ func (r *MsbDeploymentReconciler) createDeployment(ctx context.Context, md *myAp
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 deployment 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, deploy, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Create(ctx, deploy)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateDeployment(ctx context.Context, md *myAppsv1.MsbDeployment) error {
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateDeployment(ctx context.Context, md *myAppsv1.MsbDeployment, dp *appsv1.Deployment) error {
|
|
|
|
|
deploy, err := NewDeployment(md)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 deployment 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, deploy, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 预更新deployment,得到更新后的数据
|
|
|
|
|
if err := r.Update(ctx, deploy, client.DryRunAll); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 和之前的数据进行比较,如果相同,说明更新不需要。
|
|
|
|
|
if reflect.DeepEqual(dp.Spec, deploy.Spec) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Update(ctx, deploy)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -198,6 +225,12 @@ func (r *MsbDeploymentReconciler) createService(ctx context.Context, md *myAppsv
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 service 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, svc, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Create(ctx, svc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -206,22 +239,60 @@ func (r *MsbDeploymentReconciler) createNPService(ctx context.Context, md *myApp
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 service 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, svc, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Create(ctx, svc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateService(ctx context.Context, md *myAppsv1.MsbDeployment) error {
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateService(ctx context.Context, md *myAppsv1.MsbDeployment, service *corev1.Service) error {
|
|
|
|
|
svc, err := NewService(md)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 service 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, svc, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 预更新service,得到更新后的数据
|
|
|
|
|
if err := r.Update(ctx, svc, client.DryRunAll); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 和之前的数据进行比较,如果相同,说明更新不需要。
|
|
|
|
|
if reflect.DeepEqual(service.Spec, svc.Spec) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Update(ctx, svc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateNPService(ctx context.Context, md *myAppsv1.MsbDeployment) error {
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateNPService(ctx context.Context, md *myAppsv1.MsbDeployment, service *corev1.Service) error {
|
|
|
|
|
svc, err := NewServiceNP(md)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 service 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, svc, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 预更新service,得到更新后的数据
|
|
|
|
|
if err := r.Update(ctx, svc, client.DryRunAll); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 和之前的数据进行比较,如果相同,说明更新不需要。
|
|
|
|
|
if reflect.DeepEqual(service.Spec, svc.Spec) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Update(ctx, svc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -230,14 +301,36 @@ func (r *MsbDeploymentReconciler) createIngress(ctx context.Context, md *myAppsv
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 ingress 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, ig, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Create(ctx, ig)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateIngress(ctx context.Context, md *myAppsv1.MsbDeployment) error {
|
|
|
|
|
func (r *MsbDeploymentReconciler) updateIngress(ctx context.Context, md *myAppsv1.MsbDeployment, ingress *networkv1.Ingress) error {
|
|
|
|
|
ig, err := NewIngress(md)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 ingress 所属于 md
|
|
|
|
|
if err := controllerutil.SetControllerReference(md, ig, r.Scheme); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 预更新ingress,得到更新后的数据
|
|
|
|
|
if err := r.Update(ctx, ingress, client.DryRunAll); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 和之前的数据进行比较,如果相同,说明更新不需要。
|
|
|
|
|
if reflect.DeepEqual(ingress.Spec, ig.Spec) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r.Client.Update(ctx, ig)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|