补充控制器核心逻辑

This commit is contained in:
yumaojun03 2025-03-30 10:59:33 +08:00
parent cb762c5701
commit da9b3b65df
4 changed files with 77 additions and 16 deletions

View File

@ -36,7 +36,7 @@ type AccessBindingSpec struct {
// AccessBindingStatus defines the observed state of AccessBinding. // AccessBindingStatus defines the observed state of AccessBinding.
type AccessBindingStatus struct { type AccessBindingStatus struct {
Status STATUS `json:"status"` Stage STAGE `json:"stage"`
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file // Important: Run "make" to regenerate code after modifying this file
// 服务的地址 Pod IP:Port, -->Traefic (4层) // 服务的地址 Pod IP:Port, -->Traefic (4层)
@ -47,12 +47,12 @@ type AccessBindingStatus struct {
Message string `json:"message,omitempty"` Message string `json:"message,omitempty"`
} }
type STATUS string type STAGE string
const ( const (
STATUS_PENDDING = "" STATUS_PENDDING STAGE = ""
STATUS_FAILED = "failed" STATUS_FAILED STAGE = "failed"
STAUTS_SUCCESS = "success" STAUTS_SUCCESS STAGE = "success"
) )
// +kubebuilder:object:root=true // +kubebuilder:object:root=true

View File

@ -62,10 +62,10 @@ spec:
Important: Run "make" to regenerate code after modifying this file Important: Run "make" to regenerate code after modifying this file
服务的地址 Pod IP:Port, -->Traefic (4层) 服务的地址 Pod IP:Port, -->Traefic (4层)
type: string type: string
status: stage:
type: string type: string
required: required:
- status - stage
type: object type: object
type: object type: object
served: true served: true

View File

@ -1,32 +1,55 @@
<mxfile host="65bd71144e"> <mxfile host="65bd71144e">
<diagram id="cPANeSLtDYNDRIsk2Du3" name="第 1 页"> <diagram id="cPANeSLtDYNDRIsk2Du3" name="第 1 页">
<mxGraphModel dx="852" dy="417" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0"> <mxGraphModel dx="852" dy="1640" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root> <root>
<mxCell id="0"/> <mxCell id="0"/>
<mxCell id="1" parent="0"/> <mxCell id="1" parent="0"/>
<mxCell id="2" value="AcessBinding&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;deploy: enabled&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;pod:ip ---&amp;gt; traefic&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;domain --&amp;gt; traefic(host)&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="2" value="AcessBinding&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;deploy: enabled&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;pod:ip ---&amp;gt; traefic&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;domain --&amp;gt; traefic(host)&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="140" y="90" width="250" height="130" as="geometry"/> <mxGeometry x="140" y="90" width="250" height="130" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="3" value="deployment&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="3" value="deployment&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="10" y="310" width="250" height="60" as="geometry"/> <mxGeometry x="10" y="310" width="250" height="60" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="4" value="Traefic" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="4" value="Traefic" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="414" y="310" width="250" height="60" as="geometry"/> <mxGeometry x="414" y="310" width="250" height="60" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="5" value="domain" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1"> <mxCell id="5" value="domain" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="480" y="120" width="250" height="60" as="geometry"/> <mxGeometry x="480" y="120" width="250" height="60" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="7" style="edgeStyle=none;html=1;exitX=1;exitY=1;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0.25;entryY=0;entryDx=0;entryDy=0;" edge="1" parent="1" source="6" target="3"> <mxCell id="7" style="edgeStyle=none;html=1;exitX=1;exitY=1;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0.25;entryY=0;entryDx=0;entryDy=0;" parent="1" source="6" target="3" edge="1">
<mxGeometry relative="1" as="geometry"/> <mxGeometry relative="1" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="8" value="servica.xxxxx" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="7"> <mxCell id="8" value="servica.xxxxx" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="7" vertex="1" connectable="0">
<mxGeometry x="0.0335" relative="1" as="geometry"> <mxGeometry x="0.0335" relative="1" as="geometry">
<mxPoint as="offset"/> <mxPoint as="offset"/>
</mxGeometry> </mxGeometry>
</mxCell> </mxCell>
<mxCell id="6" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1"> <mxCell id="11" style="edgeStyle=orthogonalEdgeStyle;html=1;exitX=1;exitY=0.3333333333333333;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0.5;entryY=1;entryDx=0;entryDy=0;" edge="1" parent="1" source="6" target="10">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="12" value="kubectl apply:&amp;nbsp;&lt;span style=&quot;color: rgb(0, 0, 0); font-size: 12px; text-wrap-mode: wrap; background-color: rgb(251, 251, 251);&quot;&gt;AcessBinding&lt;/span&gt;&amp;nbsp;&amp;nbsp;" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="11">
<mxGeometry x="-0.1367" y="2" relative="1" as="geometry">
<mxPoint as="offset"/>
</mxGeometry>
</mxCell>
<mxCell id="6" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" parent="1" vertex="1">
<mxGeometry x="20" y="20" width="30" height="60" as="geometry"/> <mxGeometry x="20" y="20" width="30" height="60" as="geometry"/>
</mxCell> </mxCell>
<mxCell id="9" value="&lt;div&gt;operator&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="140" y="-90" width="250" height="60" as="geometry"/>
</mxCell>
<mxCell id="10" value="k8s api server" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="450" y="-90" width="250" height="60" as="geometry"/>
</mxCell>
<mxCell id="14" value="&lt;div&gt;cmdb&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="140" y="-230" width="250" height="60" as="geometry"/>
</mxCell>
<mxCell id="15" value="traefic" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="10" y="-130" width="90" height="60" as="geometry"/>
</mxCell>
<mxCell id="16" value="dns" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="10" y="-50" width="90" height="60" as="geometry"/>
</mxCell>
</root> </root>
</mxGraphModel> </mxGraphModel>
</diagram> </diagram>

View File

@ -19,11 +19,13 @@ package controller
import ( import (
"context" "context"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log"
"gitlab.com/go-course-project/go17/skills/operator/api/v1beta1"
devopsv1beta1 "gitlab.com/go-course-project/go17/skills/operator/api/v1beta1" devopsv1beta1 "gitlab.com/go-course-project/go17/skills/operator/api/v1beta1"
) )
@ -47,9 +49,45 @@ type AccessBindingReconciler struct {
// For more details, check Reconcile and its Result here: // For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.2/pkg/reconcile // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.2/pkg/reconcile
func (r *AccessBindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { func (r *AccessBindingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx) l := log.FromContext(ctx, "namesapce", req.Namespace)
// TODO(user): your logic here // TODO(user): your logic here
// 1. 获取对象, 可能有,也可能没有
accessBinding := &v1beta1.AccessBinding{}
if err := r.Get(ctx, req.NamespacedName, accessBinding); err != nil {
if apierrors.IsNotFound(err) {
// 处理删除的逻辑
// 1. 删除DNS记录
// 2. 删除Traefik后端代理配置
l.Info("处理删除的逻辑 ...")
return ctrl.Result{}, nil
}
l.Error(err, "get access binding error")
return ctrl.Result{}, err
}
// 2. 如果对象已经找到, 进行业务变更
l.Info("access binding found", "name", accessBinding.Name)
switch accessBinding.Status.Stage {
case v1beta1.STATUS_PENDDING:
// 新增逻辑
// 1. 添加DNS记录
// 2. 添加后端代理
l.Info("add accessbinding", "name", accessBinding.Name)
// 变更完成后修改状态
accessBinding.Status.Stage = v1beta1.STAUTS_SUCCESS
default:
// 变更逻辑 (namespace, deployment)
// 当前没有变更逻辑
return ctrl.Result{}, nil
}
// 3. 通过变更后的状态
if err := r.Status().Update(ctx, accessBinding); err != nil {
l.Info("update status error", "name", accessBinding.Name)
}
return ctrl.Result{}, nil return ctrl.Result{}, nil
} }