update
This commit is contained in:
parent
7c0ea16e35
commit
d644296a54
@ -31,7 +31,7 @@ type AppServiceSpec struct {
|
||||
// Important: Run "make" to regenerate code after modifying this file
|
||||
|
||||
// Foo is an example field of AppService. Edit appservice_types.go to remove/update
|
||||
Size int32 `json:"size"`
|
||||
Size *int32 `json:"size"`
|
||||
Image string `json:"image"`
|
||||
Resource *Resources `json:"resource,omitempty"`
|
||||
Envs []corev1.EnvVar `json:"envs,omitempty"`
|
||||
|
@ -88,6 +88,11 @@ func (in *AppServiceList) DeepCopyObject() runtime.Object {
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *AppServiceSpec) DeepCopyInto(out *AppServiceSpec) {
|
||||
*out = *in
|
||||
if in.Size != nil {
|
||||
in, out := &in.Size, &out.Size
|
||||
*out = new(int32)
|
||||
**out = **in
|
||||
}
|
||||
if in.Resource != nil {
|
||||
in, out := &in.Resource, &out.Resource
|
||||
*out = new(Resources)
|
||||
|
@ -18,13 +18,20 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
appv1alpha1 "git.treesir.pub/cdryzun/operator-demo/api/v1alpha1"
|
||||
"reflect"
|
||||
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
|
||||
apptreesirpubv1alpha1 "git.treesir.pub/cdryzun/operator-demo/api/v1alpha1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
)
|
||||
|
||||
// AppServiceReconciler reconciles a AppService object
|
||||
@ -47,11 +54,90 @@ type AppServiceReconciler struct {
|
||||
// For more details, check Reconcile and its Result here:
|
||||
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.1/pkg/reconcile
|
||||
func (r *AppServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
_ = log.FromContext(ctx)
|
||||
reqLogger := log.Log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name)
|
||||
reqLogger.Info("Reconciling AppService")
|
||||
|
||||
// TODO(user): your logic here
|
||||
// Fetch the AppService instance
|
||||
instance := &appv1alpha1.AppService{}
|
||||
err := r.Get(context.TODO(), req.NamespacedName, instance)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
// Request object not found, could have been deleted after reconcile request.
|
||||
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
|
||||
// Return and don't requeue
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
// Error reading the object - requeue the request.
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
if instance.DeletionTimestamp != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
// 如果不存在,则创建关联资源
|
||||
// 如果存在,判断是否需要更新
|
||||
// 如果需要更新,则直接更新
|
||||
// 如果不需要更新,则正常返回
|
||||
|
||||
deploy := &appsv1.Deployment{}
|
||||
if err := r.Get(context.TODO(), req.NamespacedName, deploy); err != nil && errors.IsNotFound(err) {
|
||||
// 创建关联资源
|
||||
// 1. 创建 Deploy
|
||||
deploy := NewDeploy(instance)
|
||||
if err := r.Create(context.TODO(), deploy); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
// 2. 创建 Service
|
||||
service := NewService(instance)
|
||||
if err := r.Create(context.TODO(), service); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
// 3. 关联 Annotations
|
||||
data, _ := json.Marshal(instance.Spec)
|
||||
if instance.Annotations != nil {
|
||||
instance.Annotations["spec"] = string(data)
|
||||
} else {
|
||||
instance.Annotations = map[string]string{"spec": string(data)}
|
||||
}
|
||||
|
||||
if err := r.Update(context.TODO(), instance); err != nil {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
oldspec := appv1alpha1.AppServiceSpec{}
|
||||
if err := json.Unmarshal([]byte(instance.Annotations["spec"]), &oldspec); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(instance.Spec, oldspec) {
|
||||
// 更新关联资源
|
||||
newDeploy := NewDeploy(instance)
|
||||
oldDeploy := &appsv1.Deployment{}
|
||||
if err := r.Get(context.TODO(), req.NamespacedName, oldDeploy); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
oldDeploy.Spec = newDeploy.Spec
|
||||
if err := r.Update(context.TODO(), oldDeploy); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
newService := NewService(instance)
|
||||
oldService := &corev1.Service{}
|
||||
if err := r.Get(context.TODO(), req.NamespacedName, oldService); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
oldService.Spec = newService.Spec
|
||||
if err := r.Update(context.TODO(), oldService); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
return reconcile.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
@ -60,3 +146,87 @@ func (r *AppServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
For(&apptreesirpubv1alpha1.AppService{}).
|
||||
Complete(r)
|
||||
}
|
||||
|
||||
func NewDeploy(app *appv1alpha1.AppService) *appsv1.Deployment {
|
||||
labels := map[string]string{"app": app.Name}
|
||||
selector := &metav1.LabelSelector{MatchLabels: labels}
|
||||
return &appsv1.Deployment{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "apps/v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: app.Name,
|
||||
Namespace: app.Namespace,
|
||||
|
||||
OwnerReferences: []metav1.OwnerReference{
|
||||
*metav1.NewControllerRef(app, schema.GroupVersionKind{
|
||||
Group: appsv1.SchemeGroupVersion.Group,
|
||||
Version: appsv1.SchemeGroupVersion.Version,
|
||||
Kind: "AppService",
|
||||
}),
|
||||
},
|
||||
},
|
||||
Spec: appsv1.DeploymentSpec{
|
||||
Replicas: app.Spec.Size,
|
||||
Template: corev1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: labels,
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: newContainers(app),
|
||||
},
|
||||
},
|
||||
Selector: selector,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newContainers(app *appv1alpha1.AppService) []corev1.Container {
|
||||
var containerPorts []corev1.ContainerPort
|
||||
for _, svcPort := range app.Spec.Ports {
|
||||
cport := corev1.ContainerPort{}
|
||||
cport.ContainerPort = svcPort.TargetPort.IntVal
|
||||
containerPorts = append(containerPorts, cport)
|
||||
}
|
||||
return []corev1.Container{
|
||||
{
|
||||
Name: app.Name,
|
||||
Image: app.Spec.Image,
|
||||
Resources: corev1.ResourceRequirements{
|
||||
Requests: app.Spec.Resources.Requests,
|
||||
Limits: app.Spec.Resources.Limits,
|
||||
},
|
||||
Ports: containerPorts,
|
||||
ImagePullPolicy: corev1.PullIfNotPresent,
|
||||
Env: app.Spec.Envs,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewService(app *appv1alpha1.AppService) *corev1.Service {
|
||||
return &corev1.Service{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "Service",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: app.Name,
|
||||
Namespace: app.Namespace,
|
||||
OwnerReferences: []metav1.OwnerReference{
|
||||
*metav1.NewControllerRef(app, schema.GroupVersionKind{
|
||||
Group: appsv1.SchemeGroupVersion.Group,
|
||||
Version: appsv1.SchemeGroupVersion.Version,
|
||||
Kind: "AppService",
|
||||
}),
|
||||
},
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Type: corev1.ServiceTypeNodePort,
|
||||
Ports: app.Spec.Ports,
|
||||
Selector: map[string]string{
|
||||
"app": app.Name,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -5,6 +5,7 @@ go 1.19
|
||||
require (
|
||||
github.com/onsi/ginkgo/v2 v2.6.0
|
||||
github.com/onsi/gomega v1.24.1
|
||||
k8s.io/api v0.26.0
|
||||
k8s.io/apimachinery v0.26.0
|
||||
k8s.io/client-go v0.26.0
|
||||
sigs.k8s.io/controller-runtime v0.14.1
|
||||
@ -58,7 +59,6 @@ require (
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
k8s.io/api v0.26.0 // indirect
|
||||
k8s.io/apiextensions-apiserver v0.26.0 // indirect
|
||||
k8s.io/component-base v0.26.0 // indirect
|
||||
k8s.io/klog/v2 v2.80.1 // indirect
|
||||
|
Loading…
x
Reference in New Issue
Block a user