go17/skills/operator/internal/controller/accessbinding_controller.go

102 lines
3.4 KiB
Go
Raw Normal View History

2025-03-30 10:08:43 +08:00
/*
Copyright 2025 will.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"context"
2025-03-30 10:59:33 +08:00
apierrors "k8s.io/apimachinery/pkg/api/errors"
2025-03-30 10:08:43 +08:00
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
2025-03-30 10:59:33 +08:00
"gitlab.com/go-course-project/go17/skills/operator/api/v1beta1"
2025-03-30 10:08:43 +08:00
devopsv1beta1 "gitlab.com/go-course-project/go17/skills/operator/api/v1beta1"
)
// AccessBindingReconciler reconciles a AccessBinding object
type AccessBindingReconciler struct {
client.Client
Scheme *runtime.Scheme
}
// +kubebuilder:rbac:groups=devops.go17,resources=accessbindings,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=devops.go17,resources=accessbindings/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=devops.go17,resources=accessbindings/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the AccessBinding object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - 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) {
2025-03-30 10:59:33 +08:00
l := log.FromContext(ctx, "namesapce", req.Namespace)
2025-03-30 10:08:43 +08:00
// TODO(user): your logic here
2025-03-30 10:59:33 +08:00
// 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)
}
2025-03-30 10:08:43 +08:00
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *AccessBindingReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&devopsv1beta1.AccessBinding{}).
Named("accessbinding").
Complete(r)
}