1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| package eru
import ( "context" "encoding/json" "eru-operator/resource/eru_res" mtsrev1 "eru-operator/pkg/apis/mtsre/v1" erulog "eru-operator/tools/log"
appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/util/retry"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/handler" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/reconcile" "sigs.k8s.io/controller-runtime/pkg/source"
)
var log = logf.Log.WithName("controller_eru")
func Add(mgr manager.Manager) error { return add(mgr, newReconciler(mgr)) }
func newReconciler(mgr manager.Manager) reconcile.Reconciler { return &ReconcileEru{client: mgr.GetClient(), scheme: mgr.GetScheme()} }
func add(mgr manager.Manager, r reconcile.Reconciler) error { c, err := controller.New("eru-controller", mgr, controller.Options{Reconciler: r}) if err != nil { return err }
err = c.Watch(&source.Kind{Type: &mtsrev1.Eru{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err }
err = c.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForObject{}) if err != nil { return err }
return nil }
var _ reconcile.Reconciler = &ReconcileEru{}
type ReconcileEru struct { client client.Client scheme *runtime.Scheme }
func (r *ReconcileEru) Reconcile(request reconcile.Request) (reconcile.Result, error) { erulog.Infof("Reconciling Eru,%v",request.String()) deployment_instance := &appsv1.Deployment{} eru_instance := &mtsrev1.Eru{}
deploymentExist := r.client.Get(context.TODO(), request.NamespacedName, deployment_instance) eruExist := r.client.Get(context.TODO(),request.NamespacedName,eru_instance)
if deploymentExist != nil && errors.IsNotFound(deploymentExist) { if eruExist == nil { erulog.Warningf("this deployment : %v not found,maybe deleted,so eru cr need to deleted",request.String()) if deletestatus ,err := r.DeleteEruCr(eru_instance); deletestatus { return reconcile.Result{},nil } else { return reconcile.Result{}, err } } }
deploymentAnnotation := deployment_instance.Annotations if _,ok := deploymentAnnotation["mtsre.meitu.com/eru"]; ok { if deploymentAnnotation["mtsre.meitu.com/eru"] == "true" && eruExist != nil { erulog.Infof("check annotation success,now to create cr resource with deployment : %v",request.String()) if _, createcrerror := r.CreateEruCr(deployment_instance); createcrerror != nil { erulog.Errorf("create cr resource with deployment : %v , failure : %v",request.String(),createcrerror) return reconcile.Result{}, createcrerror } erulog.Infof("create cr resource with deployment : %v success",request.String()) return reconcile.Result{},nil } else if deploymentAnnotation["mtsre.meitu.com/eru"] == "true" && eruExist == nil { erulog.Infof("Start checking eru for updates...") } else { erulog.Infof("annotations not exist or config error") return reconcile.Result{},nil } } else { return reconcile.Result{}, nil }
return reconcile.Result{}, nil }
func (r *ReconcileEru) CreateEruCr(deployment *appsv1.Deployment) (bool,error) { eru := eru_res.NewEru(deployment) if err := r.client.Create(context.TODO(),eru); err != nil { erulog.Errorf("create eru cr failure,reason: %v",err) failureCondition := mtsrev1.EruCondition{ Ready: false, Reason: "create eru cr failure", Message: "create eru cr failure", LastedTranslationTime: v1.Now(), } if updateerror := r.updateStatus(eru,failureCondition,mtsrev1.EruFailure); updateerror != nil { return false,updateerror } return false,err } deploymentData,enabledData,scheduleData := toString(eru) eru.Annotations = map[string]string{ "mtsre.meitu.com/deploymentName": deploymentData, "mtsre.meitu.com/enabled": enabledData, "mtsre.meitu.com/schedule": scheduleData, } r.UpdateAnnotations(eru) stopCondition := mtsrev1.EruCondition{ Ready: true, Reason: "create eru cr success,wait to start", Message: "create eru cr success,wait to start", LastedTranslationTime: v1.Time{}, } if updateerror := r.updateStatus(eru,stopCondition,mtsrev1.EruStoping); updateerror != nil { return false,updateerror } return true,nil }
func (r *ReconcileEru) DeleteEruCr(eru *mtsrev1.Eru) (bool, error) { retryError := retry.RetryOnConflict(retry.DefaultRetry, func() error { return r.client.Delete(context.TODO(), eru) }) if retryError != nil { erulog.Errorf("eru delete failure, %v/%v", eru.Namespace, eru.Name) return false, retryError } else { erulog.Infof("eru delete success, %v/%v", eru.Namespace, eru.Name) return true, nil } }
func (r *ReconcileEru) updateStatus(eru *mtsrev1.Eru, condition mtsrev1.EruCondition, phase mtsrev1.EruPhase) error { eru.Status.Phase = phase eru.Status.Conditions = append(eru.Status.Conditions, condition) return retry.RetryOnConflict(retry.DefaultRetry, func() error { return r.client.Status().Update(context.TODO(), eru) })
}
func (r *ReconcileEru) UpdateAnnotations(eru *mtsrev1.Eru) { _ = retry.RetryOnConflict(retry.DefaultRetry, func() error { return r.client.Update(context.TODO(), eru) }) }
func toString(eru *mtsrev1.Eru) (string,string,string) { deploymentData,_ := json.Marshal(eru.Spec.DeploymentName) enabledData,_ := json.Marshal(eru.Spec.Enabled) scheduleData,_ := json.Marshal(eru.Spec.Schedule) return string(deploymentData),string(enabledData),string(scheduleData) }
|