seaweedfs-operator/controllers/controller_filer.go

103 lines
3.2 KiB
Go
Raw Normal View History

2020-08-06 04:11:40 +00:00
package controllers
import (
"context"
"k8s.io/apimachinery/pkg/runtime"
2020-08-06 04:11:40 +00:00
appsv1 "k8s.io/api/apps/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2020-08-06 04:11:40 +00:00
seaweedv1 "github.com/seaweedfs/seaweedfs-operator/api/v1"
)
func (r *SeaweedReconciler) ensureFilerServers(seaweedCR *seaweedv1.Seaweed) (done bool, result ctrl.Result, err error) {
_ = context.Background()
_ = r.Log.WithValues("seaweed", seaweedCR.Name)
if done, result, err = r.ensureFilerPeerService(seaweedCR); done {
2020-10-17 20:51:07 +00:00
return
}
if done, result, err = r.ensureFilerService(seaweedCR); done {
2020-10-15 04:45:53 +00:00
return
2020-08-06 04:11:40 +00:00
}
if done, result, err = r.ensureFilerConfigMap(seaweedCR); done {
return
}
2020-10-17 07:59:38 +00:00
if done, result, err = r.ensureFilerStatefulSet(seaweedCR); done {
2020-10-15 04:45:53 +00:00
return
2020-08-06 04:11:40 +00:00
}
2020-10-15 04:45:53 +00:00
return
2020-08-06 04:11:40 +00:00
}
func (r *SeaweedReconciler) ensureFilerStatefulSet(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
2020-10-18 08:59:38 +00:00
log := r.Log.WithValues("sw-filer-statefulset", seaweedCR.Name)
filerStatefulSet := r.createFilerStatefulSet(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, filerStatefulSet, r.Scheme); err != nil {
return ReconcileResult(err)
}
2020-10-18 08:59:38 +00:00
_, err := r.CreateOrUpdate(filerStatefulSet, func(existing, desired runtime.Object) error {
existingStatefulSet := existing.(*appsv1.StatefulSet)
desiredStatefulSet := desired.(*appsv1.StatefulSet)
existingStatefulSet.Spec.Replicas = desiredStatefulSet.Spec.Replicas
existingStatefulSet.Spec.Template.Spec.Containers[0].Image = desiredStatefulSet.Spec.Template.Spec.Containers[0].Image
return nil
})
2020-10-18 08:59:38 +00:00
log.Info("ensure filer stateful set " + filerStatefulSet.Name)
2020-10-15 04:45:53 +00:00
return ReconcileResult(err)
2020-08-06 04:11:40 +00:00
}
func (r *SeaweedReconciler) ensureFilerPeerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
2020-10-18 08:59:38 +00:00
log := r.Log.WithValues("sw-filer-peer-service", seaweedCR.Name)
2020-10-18 08:59:38 +00:00
filerPeerService := r.createFilerPeerService(seaweedCR)
2020-10-30 02:40:34 +00:00
if err := controllerutil.SetControllerReference(seaweedCR, filerPeerService, r.Scheme); err != nil {
return ReconcileResult(err)
}
2020-10-18 08:59:38 +00:00
2020-10-30 02:40:34 +00:00
_, err := r.CreateOrUpdateService(filerPeerService)
log.Info("ensure filer peer service " + filerPeerService.Name)
2020-10-18 08:59:38 +00:00
return ReconcileResult(err)
2020-10-17 20:51:07 +00:00
}
func (r *SeaweedReconciler) ensureFilerService(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
2020-10-18 08:59:38 +00:00
log := r.Log.WithValues("sw-filer-service", seaweedCR.Name)
filerService := r.createFilerService(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, filerService, r.Scheme); err != nil {
return ReconcileResult(err)
}
2020-10-18 08:59:38 +00:00
_, err := r.CreateOrUpdateService(filerService)
log.Info("ensure filer service " + filerService.Name)
return ReconcileResult(err)
2020-10-17 20:51:07 +00:00
}
func (r *SeaweedReconciler) ensureFilerConfigMap(seaweedCR *seaweedv1.Seaweed) (bool, ctrl.Result, error) {
log := r.Log.WithValues("sw-filer-configmap", seaweedCR.Name)
filerConfigMap := r.createFilerConfigMap(seaweedCR)
if err := controllerutil.SetControllerReference(seaweedCR, filerConfigMap, r.Scheme); err != nil {
return ReconcileResult(err)
}
_, err := r.CreateOrUpdateConfigMap(filerConfigMap)
log.Info("Get filer ConfigMap " + filerConfigMap.Name)
return ReconcileResult(err)
}
2020-10-17 20:51:07 +00:00
func labelsForFiler(name string) map[string]string {
return map[string]string{"app": "seaweedfs", "role": "filer", "name": name}
}