Skip to content

Commit

Permalink
add logging (#179)
Browse files Browse the repository at this point in the history
Fix #177

Co-authored-by: Oksana Baranova <[email protected]>
  • Loading branch information
oxxenix and oxxenix authored Apr 20, 2024
1 parent b6ab36f commit 5f7dd36
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/controller/factory/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import (

func reconcileStatefulSet(ctx context.Context, rclient client.Client, crdName string, sts *appsv1.StatefulSet) error {
logger := log.FromContext(ctx)
logger.V(2).Info("statefulset reconciliation started")

currentSts := &appsv1.StatefulSet{}
logger.V(2).Info("statefulset found", "sts_name", currentSts.Name)

err := rclient.Get(ctx, types.NamespacedName{Namespace: sts.Namespace, Name: sts.Name}, currentSts)
if err != nil {
if errors.IsNotFound(err) {
Expand All @@ -43,14 +46,18 @@ func reconcileStatefulSet(ctx context.Context, rclient client.Client, crdName st
return fmt.Errorf("cannot get existing statefulset: %s, for crd_object: %s, err: %w", sts.Name, crdName, err)
}
sts.Annotations = labels.Merge(currentSts.Annotations, sts.Annotations)
logger.V(2).Info("statefulset annotations merged", "sts_annotations", sts.Annotations)
sts.Status = currentSts.Status
return rclient.Update(ctx, sts)
}

func reconcileConfigMap(ctx context.Context, rclient client.Client, crdName string, configMap *corev1.ConfigMap) error {
logger := log.FromContext(ctx)
logger.V(2).Info("configmap reconciliation started")

currentConfigMap := &corev1.ConfigMap{}
logger.V(2).Info("configmap found", "cm_name", currentConfigMap.Name)

err := rclient.Get(ctx, types.NamespacedName{Namespace: configMap.Namespace, Name: configMap.Name}, currentConfigMap)
if err != nil {
if errors.IsNotFound(err) {
Expand All @@ -60,13 +67,17 @@ func reconcileConfigMap(ctx context.Context, rclient client.Client, crdName stri
return fmt.Errorf("cannot get existing configMap: %s, for crd_object: %s, err: %w", configMap.Name, crdName, err)
}
configMap.Annotations = labels.Merge(currentConfigMap.Annotations, configMap.Annotations)
logger.V(2).Info("configmap annotations merged", "cm_annotations", configMap.Annotations)
return rclient.Update(ctx, configMap)
}

func reconcileService(ctx context.Context, rclient client.Client, crdName string, svc *corev1.Service) error {
logger := log.FromContext(ctx)
logger.V(2).Info("service reconciliation started")

currentSvc := &corev1.Service{}
logger.V(2).Info("service found", "svc_name", currentSvc.Name)

err := rclient.Get(ctx, types.NamespacedName{Namespace: svc.Namespace, Name: svc.Name}, currentSvc)
if err != nil {
if errors.IsNotFound(err) {
Expand All @@ -76,6 +87,7 @@ func reconcileService(ctx context.Context, rclient client.Client, crdName string
return fmt.Errorf("cannot get existing service: %s, for crd_object: %s, err: %w", svc.Name, crdName, err)
}
svc.Annotations = labels.Merge(currentSvc.Annotations, svc.Annotations)
logger.V(2).Info("service annotations merged", "svc_annotations", svc.Annotations)
svc.Status = currentSvc.Status
return rclient.Update(ctx, svc)
}
Expand All @@ -84,7 +96,10 @@ func reconcileService(ctx context.Context, rclient client.Client, crdName string
// pdb parameter should have at least metadata.name and metadata.namespace fields filled.
func deleteManagedPdb(ctx context.Context, rclient client.Client, pdb *v1.PodDisruptionBudget) error {
logger := log.FromContext(ctx)

currentPdb := &v1.PodDisruptionBudget{}
logger.V(2).Info("pdb found", "pdb_name", currentPdb.Name)

err := rclient.Get(ctx, types.NamespacedName{Namespace: pdb.Namespace, Name: pdb.Name}, currentPdb)
if err != nil {
logger.V(2).Info("error getting cluster PDB", "error", err)
Expand All @@ -101,7 +116,11 @@ func deleteManagedPdb(ctx context.Context, rclient client.Client, pdb *v1.PodDis

func reconcilePdb(ctx context.Context, rclient client.Client, crdName string, pdb *v1.PodDisruptionBudget) error {
logger := log.FromContext(ctx)
logger.V(2).Info("pdb reconciliation started")

currentPdb := &v1.PodDisruptionBudget{}
logger.V(2).Info("pdb found", "pdb_name", currentPdb.Name)

err := rclient.Get(ctx, types.NamespacedName{Namespace: pdb.Namespace, Name: pdb.Name}, currentPdb)
if err != nil {
if errors.IsNotFound(err) {
Expand All @@ -112,6 +131,7 @@ func reconcilePdb(ctx context.Context, rclient client.Client, crdName string, pd
return fmt.Errorf("cannot get existing pdb resource: %s for crd_object: %s, err: %w", pdb.Name, crdName, err)
}
pdb.Annotations = labels.Merge(currentPdb.Annotations, pdb.Annotations)
logger.V(2).Info("pdb annotations merged", "pdb_annotations", pdb.Annotations)
pdb.ResourceVersion = currentPdb.ResourceVersion
pdb.Status = currentPdb.Status
return rclient.Update(ctx, pdb)
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/factory/configMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/log"
)

func GetClusterStateConfigMapName(cluster *etcdaenixiov1alpha1.EtcdCluster) string {
Expand All @@ -50,6 +51,7 @@ func CreateOrUpdateClusterStateConfigMap(
)
}

logger := log.FromContext(ctx)
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Expand All @@ -64,8 +66,10 @@ func CreateOrUpdateClusterStateConfigMap(

if isEtcdClusterReady(cluster) {
// update cluster state to existing
logger.V(2).Info("updating cluster state", "cluster_name", cluster.Name)
configMap.Data["ETCD_INITIAL_CLUSTER_STATE"] = "existing"
}
logger.V(2).Info("configmap spec generated", "cm_name", configMap.Name, "cm_spec", configMap.Data)

if err := ctrl.SetControllerReference(cluster, configMap, rscheme); err != nil {
return fmt.Errorf("cannot set controller reference: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/factory/pdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

func CreateOrUpdatePdb(
Expand All @@ -44,6 +45,7 @@ func CreateOrUpdatePdb(
}})
}

logger := log.FromContext(ctx)
pdb := &v1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Namespace: cluster.Namespace,
Expand All @@ -63,6 +65,8 @@ func CreateOrUpdatePdb(
pdb.Spec.MinAvailable = ptr.To(intstr.FromInt32(int32(cluster.CalculateQuorumSize())))
}

logger.V(2).Info("pdb spec generated", "pdb_name", pdb.Name, "pdb_spec", pdb.Spec)

if err := ctrl.SetControllerReference(cluster, pdb, rscheme); err != nil {
return fmt.Errorf("cannot set controller reference: %w", err)
}
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/factory/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
"github.com/aenix-io/etcd-operator/internal/k8sutils"
"sigs.k8s.io/controller-runtime/pkg/log"
)

const (
Expand Down Expand Up @@ -102,6 +103,8 @@ func CreateOrUpdateStatefulSet(
VolumeClaimTemplates: volumeClaimTemplates,
},
}
logger := log.FromContext(ctx)
logger.V(2).Info("statefulset spec generated", "sts_name", statefulSet.Name, "sts_spec", statefulSet.Spec)

if err := ctrl.SetControllerReference(cluster, statefulSet, rscheme); err != nil {
return fmt.Errorf("cannot set controller reference: %w", err)
Expand Down
5 changes: 5 additions & 0 deletions internal/controller/factory/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/log"
)

func GetClientServiceName(cluster *etcdaenixiov1alpha1.EtcdCluster) string {
Expand All @@ -40,6 +41,7 @@ func CreateOrUpdateClusterService(
rclient client.Client,
rscheme *runtime.Scheme,
) error {
logger := log.FromContext(ctx)
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: cluster.Name,
Expand All @@ -57,6 +59,7 @@ func CreateOrUpdateClusterService(
PublishNotReadyAddresses: true,
},
}
logger.V(2).Info("cluster service spec generated", "svc_name", svc.Name, "svc_spec", svc.Spec)

if err := ctrl.SetControllerReference(cluster, svc, rscheme); err != nil {
return fmt.Errorf("cannot set controller reference: %w", err)
Expand All @@ -71,6 +74,7 @@ func CreateOrUpdateClientService(
rclient client.Client,
rscheme *runtime.Scheme,
) error {
logger := log.FromContext(ctx)
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: GetClientServiceName(cluster),
Expand All @@ -85,6 +89,7 @@ func CreateOrUpdateClientService(
Selector: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
},
}
logger.V(2).Info("client service spec generated", "svc_name", svc.Name, "svc_spec", svc.Spec)

if err := ctrl.SetControllerReference(cluster, svc, rscheme); err != nil {
return fmt.Errorf("cannot set controller reference: %w", err)
Expand Down

0 comments on commit 5f7dd36

Please sign in to comment.