2024-09-10 08:25:32 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2024.
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
2024-09-25 01:51:11 +00:00
|
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
|
|
2024-09-10 08:25:32 +00: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"
|
|
|
|
|
|
2024-09-25 01:51:11 +00:00
|
|
|
|
devcontainer_v1 "devstar.cn/DevcontainerApp/api/v1"
|
|
|
|
|
devcontainer_controller_utils "devstar.cn/DevcontainerApp/internal/controller/utils"
|
|
|
|
|
apps_v1 "k8s.io/api/apps/v1"
|
|
|
|
|
core_v1 "k8s.io/api/core/v1"
|
|
|
|
|
k8s_sigs_controller_runtime_utils "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
2024-09-10 08:25:32 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DevcontainerAppReconciler reconciles a DevcontainerApp object
|
|
|
|
|
type DevcontainerAppReconciler struct {
|
|
|
|
|
client.Client
|
|
|
|
|
Scheme *runtime.Scheme
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// +kubebuilder:rbac:groups=devcontainer.devstar.cn,resources=devcontainerapps,verbs=get;list;watch;create;update;patch;delete
|
|
|
|
|
// +kubebuilder:rbac:groups=devcontainer.devstar.cn,resources=devcontainerapps/status,verbs=get;update;patch
|
|
|
|
|
// +kubebuilder:rbac:groups=devcontainer.devstar.cn,resources=devcontainerapps/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.
|
2024-09-25 01:51:11 +00:00
|
|
|
|
// Modify the Reconcile function to compare the state specified by
|
2024-09-10 08:25:32 +00:00
|
|
|
|
// the DevcontainerApp 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.19.0/pkg/reconcile
|
|
|
|
|
func (r *DevcontainerAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
2024-09-25 01:51:11 +00:00
|
|
|
|
logger := log.FromContext(ctx)
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
// 1. 读取缓存中的 DevcontainerApp
|
|
|
|
|
app := &devcontainer_v1.DevcontainerApp{}
|
|
|
|
|
err = r.Get(ctx, req.NamespacedName, app)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// 当 CRD 资源 “DevcontainerApp” 被删除后,直接返回空结果,跳过剩下步骤
|
|
|
|
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. 根据 DevcontainerApp 配置信息进行处理
|
|
|
|
|
// 2.1 StatefulSet 处理
|
|
|
|
|
statefulSet := devcontainer_controller_utils.NewStatefulSet(app)
|
|
|
|
|
err = k8s_sigs_controller_runtime_utils.SetControllerReference(app, statefulSet, r.Scheme)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2.2 查找 集群中同名称的 StatefulSet
|
|
|
|
|
statefulSetInNamespace := &apps_v1.StatefulSet{}
|
|
|
|
|
err = r.Get(ctx, req.NamespacedName, statefulSetInNamespace)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
err = r.Create(ctx, statefulSet)
|
|
|
|
|
if err != nil && !errors.IsAlreadyExists(err) {
|
|
|
|
|
logger.Error(err, "Failed to create StatefulSet")
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-09-30 06:38:22 +00:00
|
|
|
|
// 若 StatefulSet.Status.readyReplicas 变化,则更新 DevcontainerApp.Status.Ready 域(mark/un-mark)
|
|
|
|
|
if statefulSetInNamespace.Status.ReadyReplicas > 0 {
|
|
|
|
|
app.Status.Ready = true
|
|
|
|
|
if err := r.Status().Update(ctx, app); err != nil {
|
|
|
|
|
logger.Error(err, "Failed to update DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready)
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
logger.Info("DevContainer is now ready", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas)
|
|
|
|
|
} else {
|
|
|
|
|
app.Status.Ready = false
|
|
|
|
|
if err := r.Status().Update(ctx, app); err != nil {
|
|
|
|
|
logger.Error(err, "Failed to un-mark DevcontainerApp.Status.Ready", "DevcontainerApp.Status.Ready", app.Status.Ready)
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
logger.Info("DevContainer is NOT ready", "ReadyReplicas", statefulSetInNamespace.Status.ReadyReplicas)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 01:51:11 +00:00
|
|
|
|
// 这里会反复触发更新
|
|
|
|
|
// 原因:在 SetupWithManager方法中,监听了 StatefulSet ,所以只要更新 StatefulSet 就会触发
|
|
|
|
|
// 此处更新和 controllerManager 更新 StatefulSet 都会触发更新事件,导致循环触发
|
|
|
|
|
//修复方法:加上判断条件,仅在 app.Spec.StatefulSet.Image != statefulSet.Spec.Template.Spec.Containers[0].Image 时才更新 StatefulSet
|
|
|
|
|
if app.Spec.StatefulSet.Image != statefulSet.Spec.Template.Spec.Containers[0].Image {
|
|
|
|
|
if err := r.Update(ctx, statefulSet); err != nil {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-10 08:25:32 +00:00
|
|
|
|
|
2024-09-25 01:51:11 +00:00
|
|
|
|
// 2.2 Service 处理
|
|
|
|
|
service := devcontainer_controller_utils.NewService(app)
|
|
|
|
|
if err := k8s_sigs_controller_runtime_utils.SetControllerReference(app, service, r.Scheme); err != nil {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
serviceInCluster := &core_v1.Service{}
|
|
|
|
|
err = r.Get(ctx, types.NamespacedName{Name: app.Name, Namespace: app.Namespace}, serviceInCluster)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !errors.IsNotFound(err) {
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
err = r.Create(ctx, service)
|
|
|
|
|
if err == nil {
|
|
|
|
|
// 创建 NodePort Service 成功只执行一次 ==> 将NodePort 端口分配信息更新到 app.Status
|
|
|
|
|
logger.Info("[DevStar][DevContainer] NodePort Assigned", "nodePortAssigned", service.Spec.Ports[0].NodePort)
|
2024-09-10 08:25:32 +00:00
|
|
|
|
|
2024-09-25 01:51:11 +00:00
|
|
|
|
app.Status.NodePortAssigned = uint16(service.Spec.Ports[0].NodePort)
|
|
|
|
|
if err := r.Status().Update(ctx, app); err != nil {
|
|
|
|
|
logger.Error(err, "Failed to update NodePort of DevcontainerApp", "nodePortAssigned", service.Spec.Ports[0].NodePort)
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
} else if !errors.IsAlreadyExists(err) {
|
|
|
|
|
logger.Error(err, "Failed to create DevcontainerApp NodePort Service", "nodePortServiceName", service.Name)
|
|
|
|
|
return ctrl.Result{}, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-10 08:25:32 +00:00
|
|
|
|
return ctrl.Result{}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetupWithManager sets up the controller with the Manager.
|
|
|
|
|
func (r *DevcontainerAppReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
|
|
|
|
return ctrl.NewControllerManagedBy(mgr).
|
2024-09-25 01:51:11 +00:00
|
|
|
|
For(&devcontainer_v1.DevcontainerApp{}).
|
|
|
|
|
Owns(&apps_v1.StatefulSet{}).
|
|
|
|
|
Owns(&core_v1.Service{}).
|
2024-09-10 08:25:32 +00:00
|
|
|
|
Complete(r)
|
|
|
|
|
}
|