update
This commit is contained in:
@ -19,7 +19,7 @@ package controllers
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
appv1alpha1 "git.treesir.pub/cdryzun/operator-demo/api/v1alpha1"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
@ -27,11 +27,12 @@ import (
|
||||
"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"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
apptreesirpubv1alpha1 "git.treesir.pub/cdryzun/operator-demo/api/v1alpha1"
|
||||
)
|
||||
|
||||
// AppServiceReconciler reconciles a AppService object
|
||||
@ -58,7 +59,7 @@ func (r *AppServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request)
|
||||
reqLogger.Info("Reconciling AppService")
|
||||
|
||||
// Fetch the AppService instance
|
||||
instance := &appv1alpha1.AppService{}
|
||||
instance := &apptreesirpubv1alpha1.AppService{}
|
||||
err := r.Get(context.TODO(), req.NamespacedName, instance)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
@ -81,6 +82,7 @@ func (r *AppServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request)
|
||||
// 如果不需要更新,则正常返回
|
||||
|
||||
deploy := &appsv1.Deployment{}
|
||||
fmt.Println(deploy)
|
||||
if err := r.Get(context.TODO(), req.NamespacedName, deploy); err != nil && errors.IsNotFound(err) {
|
||||
// 创建关联资源
|
||||
// 1. 创建 Deploy
|
||||
@ -107,7 +109,7 @@ func (r *AppServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request)
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
oldspec := appv1alpha1.AppServiceSpec{}
|
||||
oldspec := apptreesirpubv1alpha1.AppServiceSpec{}
|
||||
if err := json.Unmarshal([]byte(instance.Annotations["spec"]), &oldspec); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
@ -147,86 +149,71 @@ func (r *AppServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
Complete(r)
|
||||
}
|
||||
|
||||
func NewDeploy(app *appv1alpha1.AppService) *appsv1.Deployment {
|
||||
labels := map[string]string{"app": app.Name}
|
||||
selector := &metav1.LabelSelector{MatchLabels: labels}
|
||||
func NewDeploy(instance *apptreesirpubv1alpha1.AppService) *appsv1.Deployment {
|
||||
labels := map[string]string{
|
||||
"app": instance.Name,
|
||||
}
|
||||
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",
|
||||
}),
|
||||
},
|
||||
ObjectMeta: ctrl.ObjectMeta{
|
||||
Name: instance.Name,
|
||||
Namespace: instance.Namespace,
|
||||
Labels: labels,
|
||||
},
|
||||
Spec: appsv1.DeploymentSpec{
|
||||
Replicas: app.Spec.Size,
|
||||
Replicas: instance.Spec.Size,
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchLabels: labels,
|
||||
},
|
||||
Template: corev1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
ObjectMeta: ctrl.ObjectMeta{
|
||||
Labels: labels,
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: newContainers(app),
|
||||
Containers: newContainers(instance),
|
||||
},
|
||||
},
|
||||
Selector: selector,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newContainers(app *appv1alpha1.AppService) []corev1.Container {
|
||||
func NewService(instance *apptreesirpubv1alpha1.AppService) *corev1.Service {
|
||||
labels := map[string]string{
|
||||
"app": instance.Name,
|
||||
}
|
||||
return &corev1.Service{
|
||||
ObjectMeta: ctrl.ObjectMeta{
|
||||
Name: instance.Name,
|
||||
Namespace: instance.Namespace,
|
||||
Labels: labels,
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Type: corev1.ServiceTypeNodePort,
|
||||
Ports: instance.Spec.Ports,
|
||||
Selector: map[string]string{
|
||||
"app": instance.Name,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func newContainers(instance *apptreesirpubv1alpha1.AppService) []corev1.Container {
|
||||
var containerPorts []corev1.ContainerPort
|
||||
for _, svcPort := range app.Spec.Ports {
|
||||
for _, svcPort := range instance.Spec.Ports {
|
||||
cport := corev1.ContainerPort{}
|
||||
cport.ContainerPort = svcPort.TargetPort.IntVal
|
||||
containerPorts = append(containerPorts, cport)
|
||||
}
|
||||
return []corev1.Container{
|
||||
{
|
||||
Name: app.Name,
|
||||
Image: app.Spec.Image,
|
||||
Name: instance.Name,
|
||||
Image: instance.Spec.Image,
|
||||
Resources: corev1.ResourceRequirements{
|
||||
Requests: app.Spec.Resources.Requests,
|
||||
Limits: app.Spec.Resources.Limits,
|
||||
Requests: instance.Spec.Resources.Requests,
|
||||
Limits: instance.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,
|
||||
},
|
||||
Env: instance.Spec.Envs,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user