Skip to content

Instantly share code, notes, and snippets.

@velotiotech
Created June 25, 2020 09:54
Show Gist options
  • Save velotiotech/ace211bab96b23aeabdcefa2980e6b25 to your computer and use it in GitHub Desktop.
Save velotiotech/ace211bab96b23aeabdcefa2980e6b25 to your computer and use it in GitHub Desktop.
func (r *ReconcileBookStore) BookStore(bookstore *blogv1alpha1.BookStore) error {
reqLogger := log.WithValues("Namespace", bookstore.Namespace)
mongoDBSvc := getmongoDBSvc(bookstore)
msvc := &corev1.Service{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: "mongodb-service", Namespace: bookstore.Namespace}, msvc)
if err != nil {
if errors.IsNotFound(err) {
controllerutil.SetControllerReference(bookstore, mongoDBSvc, r.scheme)
err = r.client.Create(context.TODO(), mongoDBSvc)
if err != nil { return err }
} else { return err }
} else if !reflect.DeepEqual(mongoDBSvc.Spec, msvc.Spec) {
mongoDBSvc.ObjectMeta = msvc.ObjectMeta
controllerutil.SetControllerReference(bookstore, mongoDBSvc, r.scheme)
err = r.client.Update(context.TODO(), mongoDBSvc)
if err != nil { return err }
reqLogger.Info("mongodb-service updated")
}
mongoDBSS := getMongoDBStatefulsets(bookstore)
mss := &appsv1.StatefulSet{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: "mongodb", Namespace: bookstore.Namespace}, mss)
if err != nil {
if errors.IsNotFound(err) {
reqLogger.Info("mongodb statefulset not found, will be created")
controllerutil.SetControllerReference(bookstore, mongoDBSS, r.scheme)
err = r.client.Create(context.TODO(), mongoDBSS)
if err != nil { return err }
} else {
reqLogger.Info("failed to get mongodb statefulset")
return err
}
} else if !reflect.DeepEqual(mongoDBSS.Spec, mss.Spec) {
r.UpdateVolume(bookstore)
mongoDBSS.ObjectMeta = mss.ObjectMeta
mongoDBSS.Spec.VolumeClaimTemplates = mss.Spec.VolumeClaimTemplates
controllerutil.SetControllerReference(bookstore, mongoDBSS, r.scheme)
err = r.client.Update(context.TODO(), mongoDBSS)
if err != nil { return err }
reqLogger.Info("mongodb statefulset updated")
}
bookStoreSvc := getBookStoreAppSvc(bookstore)
bsvc := &corev1.Service{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: "bookstore-svc", Namespace: bookstore.Namespace}, bsvc)
if err != nil {
if errors.IsNotFound(err) {
controllerutil.SetControllerReference(bookstore, bookStoreSvc, r.scheme)
err = r.client.Create(context.TODO(), bookStoreSvc)
if err != nil { return err }
} else {
reqLogger.Info("failed to get bookstore service")
return err
}
} else if !reflect.DeepEqual(bookStoreSvc.Spec, bsvc.Spec) {
bookStoreSvc.ObjectMeta = bsvc.ObjectMeta
bookStoreSvc.Spec.ClusterIP = bsvc.Spec.ClusterIP
controllerutil.SetControllerReference(bookstore, bookStoreSvc, r.scheme)
err = r.client.Update(context.TODO(), bookStoreSvc)
if err != nil { return err }
reqLogger.Info("bookstore service updated")
}
bookStoreDep := getBookStoreDeploy(bookstore)
bsdep := &appsv1.Deployment{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: "bookstore", Namespace: bookstore.Namespace}, bsdep)
if err != nil {
if errors.IsNotFound(err) {
controllerutil.SetControllerReference(bookstore, bookStoreDep, r.scheme)
err = r.client.Create(context.TODO(), bookStoreDep)
if err != nil { return err }
} else {
reqLogger.Info("failed to get bookstore deployment")
return err
}
} else if !reflect.DeepEqual(bookStoreDep.Spec, bsdep.Spec) {
bookStoreDep.ObjectMeta = bsdep.ObjectMeta
controllerutil.SetControllerReference(bookstore, bookStoreDep, r.scheme)
err = r.client.Update(context.TODO(), bookStoreDep)
if err != nil { return err }
reqLogger.Info("bookstore deployment updated")
}
r.client.Status().Update(context.TODO(), bookstore)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment