zelda/backend/internal/controller/zservice_controller.go

188 lines
6.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Copyright 2023 ycyxuehan.
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"
"fmt"
"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"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
zeldaiov1alpha1 "github.com/ycyxuehan/zelda/api/v1alpha1"
watchers "github.com/ycyxuehan/zelda/watcher"
)
// ZServiceReconciler reconciles a ZService object
type ZServiceReconciler struct {
client.Client
Scheme *runtime.Scheme
}
//+kubebuilder:rbac:groups=zelda.io,resources=zservices,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=zelda.io,resources=zservices/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=zelda.io,resources=zservices/finalizers,verbs=update
// +kubebuilder:rbac:groups=*,resources=services,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=*,resources=services/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=*,resources=pods,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=*,resources=pods/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=batch,resources=jobs/status,verbs=get;update;patch
// 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 ZService 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.10.0/pkg/reconcile
func (r *ZServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
// TODO(user): your logic here
zservice := zeldaiov1alpha1.ZService{}
err := r.Get(ctx, req.NamespacedName, &zservice)
if err != nil {
log.Log.Error(err, "cannot get resource zservices.zelda.io/%s/%s", req.Namespace, req.Name)
return ctrl.Result{}, err
}
if zservice.DeletionTimestamp == nil || zservice.DeletionTimestamp.IsZero() {
err = r.Process(ctx, &zservice)
if err != nil {
log.Log.Error(err, "create resource zservices.zelda.io/%s/%s failed: %v", req.Namespace, req.Name, err)
}
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ZServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&zeldaiov1alpha1.ZService{}).
Watches(&corev1.Pod{}, &watchers.ZServiceWatcher{
Client: mgr.GetClient(),
}).
Watches(&appsv1.Deployment{},
&watchers.ZServiceWatcher{
Client: mgr.GetClient(),
}).
Watches(&batchv1.Job{},
&watchers.ZServiceWatcher{
Client: mgr.GetClient(),
}).
Complete(r)
}
//process zservice 负责运行服务。暂时兼容deploymentjobpod三种模式。zservice每次变动都会去检查其子资源是否存在是否一致
func (r *ZServiceReconciler) Process(ctx context.Context, zservice *zeldaiov1alpha1.ZService) error {
var managedResource client.Object
switch zservice.Spec.Kind {
case "Deployment":
managedResource = &appsv1.Deployment{}
case "Job":
managedResource = &batchv1.Job{}
case "Pod":
managedResource = &corev1.Pod{}
default:
return fmt.Errorf("unkown kind")
}
changed, err := zservice.IsSpecChanged()
if err != nil {
//日志打印错误
fmt.Println(err)
}
err = r.Get(context.Background(), zservice.NamespacedNameResource(), managedResource)
if err != nil || changed { //子对象不见了,需要创建, spec有变动需要更新这里先创建option
runTemplate := zeldaiov1alpha1.ZRunTemplate{}
err = r.Get(context.Background(), zservice.NamespacedNameRunTemplate(), &runTemplate)
if err != nil {
return err
}
project := zeldaiov1alpha1.ZProject{}
err = r.Get(context.Background(), zservice.NamespacedNameProject(), &project)
if err != nil {
return err
}
registry := zeldaiov1alpha1.ZRegistry{}
err = r.Get(context.Background(), project.NamespacedNameRegistry(), &registry)
if err != nil {
return err
}
customHost := zeldaiov1alpha1.ZCustomHost{}
err = r.Get(context.Background(), zservice.NamespacedNameCustomHost(), &customHost)
if err != nil {
return err
}
sa := corev1.ServiceAccount{}
err = r.Get(context.Background(), zservice.NamespacedNameUser(), &sa)
if err != nil {
return err
}
secrets := []corev1.LocalObjectReference{}
for _, secret := range sa.Secrets {
if secret.Namespace == zservice.GetNamespace() {
secrets = append(secrets, corev1.LocalObjectReference{Name: secret.Name})
}
}
option := zeldaiov1alpha1.NewClientObjectOption{
ZRunTemplateSpec: runTemplate.Spec.DeepCopy(),
Registry: registry.DeepCopy(),
HostAlias: customHost.Spec.HostAlias,
UserSecrets: secrets,
}
managedResource = zservice.ClientObject(&option)
}
if err != nil {
err = r.Create(context.Background(), managedResource, &client.CreateOptions{})
return err
}
if changed {
err = r.Update(context.Background(), managedResource, &client.UpdateOptions{})
return err
}
////////////////////////////////////////
//这里是处理状态更新
r.UpdateStatus(ctx, zservice, managedResource)
return nil
}
//处理状态更新
func (r *ZServiceReconciler) UpdateStatus(ctx context.Context, zservice *zeldaiov1alpha1.ZService, managedResource client.Object) error {
err := zservice.UpdateStatus(managedResource)
if err != nil {
return err
}
err = r.Status().Update(context.Background(), zservice)
return err
}