Skip to content

Commit

Permalink
Add utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lllamnyp committed Sep 17, 2024
1 parent e00e6cb commit 012a1bc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
31 changes: 31 additions & 0 deletions internal/controller/etcdcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (r *EtcdClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

state := observables{}
state.instance = instance

// create two services and the pdb
err = r.ensureUnconditionalObjects(ctx, instance)
Expand Down Expand Up @@ -661,3 +662,33 @@ func (r *EtcdClusterReconciler) ensureUnconditionalObjects(ctx context.Context,
}
return nil
}

func (r *EtcdClusterReconciler) patchOrCreateObject(ctx context.Context, obj client.Object) error {
err := r.Patch(ctx, obj, client.Apply, &client.PatchOptions{FieldManager: "etcd-operator"}, client.ForceOwnership)
if err == nil {
return nil
}
if client.IgnoreNotFound(err) == nil {
err = r.Create(ctx, obj)
}
return err
}

// TODO!
func (r *EtcdClusterReconciler) createClusterFromScratch(ctx context.Context, state *observables) (ctrl.Result, error) {
cm := factory.TemplateClusterStateConfigMap(state.instance, "new", state.desiredReplicas())

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s previous version

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s latest version

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s penultimate version

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / test on k8s penultimate version

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / test on k8s latest version

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / test on k8s previous version

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / pre-commit

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / pre-commit

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)

Check failure on line 679 in internal/controller/etcdcluster_controller.go

View workflow job for this annotation

GitHub Actions / pre-commit

state.desiredReplicas undefined (type *observables has no field or method desiredReplicas)
err := ctrl.SetControllerReference(state.instance, cm, r.Scheme)
if err != nil {
return ctrl.Result{}, err
}
err = r.patchOrCreateObject(ctx, cm)
if err != nil {
return ctrl.Result{}, err
}
panic("not yet implemented")
}

// TODO!
func (r *EtcdClusterReconciler) scaleUpFromZero(ctx context.Context, state *observables) (ctrl.Result, error) {
panic("not yet implemented")
}
18 changes: 18 additions & 0 deletions internal/controller/factory/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ import (
"k8s.io/apimachinery/pkg/types"
)

func PVCLabels(cluster *etcdaenixiov1alpha1.EtcdCluster) map[string]string {
labels := PodLabels(cluster)
for key, value := range cluster.Spec.Storage.VolumeClaimTemplate.Labels {
labels[key] = value
}
return labels
}

func GetPVCName(cluster *etcdaenixiov1alpha1.EtcdCluster) string {
if len(cluster.Spec.Storage.VolumeClaimTemplate.Name) > 0 {
return cluster.Spec.Storage.VolumeClaimTemplate.Name
Expand All @@ -38,6 +46,16 @@ func GetPVCName(cluster *etcdaenixiov1alpha1.EtcdCluster) string {
return "data"
}

func PVCs(ctx context.Context, cluster *etcdaenixiov1alpha1.EtcdCluster, cli client.Client) ([]corev1.PersistentVolumeClaim, error) {
labels := PVCLabels(cluster)
pvcs := corev1.PersistentVolumeClaimList{}
err := cli.List(ctx, &pvcs, client.MatchingLabels(labels))
if err != nil {
return nil, err
}
return pvcs.Items, nil
}

// UpdatePersistentVolumeClaims checks and updates the sizes of PVCs in an EtcdCluster if the specified storage size is larger than the current.
func UpdatePersistentVolumeClaims(ctx context.Context, cluster *etcdaenixiov1alpha1.EtcdCluster, rclient client.Client) error {
labelSelector := labels.SelectorFromSet(labels.Set{
Expand Down
25 changes: 18 additions & 7 deletions internal/controller/factory/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,30 @@ const (
defaultBackendQuotaBytesFraction = 0.95
)

// TODO!
func TemplateStatefulSet() *appsv1.StatefulSet {
panic("not yet implemented")
}

func PodLabels(cluster *etcdaenixiov1alpha1.EtcdCluster) map[string]string {
labels := NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy()

if cluster.Spec.PodTemplate.Labels != nil {
for key, value := range cluster.Spec.PodTemplate.Labels {
labels[key] = value
}
}

return labels
}

func CreateOrUpdateStatefulSet(
ctx context.Context,
cluster *etcdaenixiov1alpha1.EtcdCluster,
rclient client.Client,
) error {
podMetadata := metav1.ObjectMeta{
Labels: NewLabelsBuilder().WithName().WithInstance(cluster.Name).WithManagedBy(),
}

if cluster.Spec.PodTemplate.Labels != nil {
for key, value := range cluster.Spec.PodTemplate.Labels {
podMetadata.Labels[key] = value
}
Labels: PodLabels(cluster),
}

if cluster.Spec.PodTemplate.Annotations != nil {
Expand Down
9 changes: 7 additions & 2 deletions internal/controller/observables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package controller

import (
"context"
"strconv"

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s previous version

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s latest version

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s penultimate version

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s penultimate version

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s latest version

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s previous version

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / pre-commit

"strconv" imported and not used

Check failure on line 5 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / pre-commit

"strconv" imported and not used
"strings"

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s previous version

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s latest version

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s penultimate version

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s penultimate version

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s latest version

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s previous version

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / pre-commit

"strings" imported and not used

Check failure on line 6 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / pre-commit

"strings" imported and not used
"sync"

"github.com/aenix-io/etcd-operator/api/v1alpha1"
"github.com/aenix-io/etcd-operator/pkg/set"

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / nilaway-lint

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s previous version

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s latest version

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test-e2e on k8s penultimate version

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s penultimate version

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s latest version

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / test on k8s previous version

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / pre-commit

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used

Check failure on line 10 in internal/controller/observables.go

View workflow job for this annotation

GitHub Actions / pre-commit

"github.com/aenix-io/etcd-operator/pkg/set" imported and not used) (typecheck)
clientv3 "go.etcd.io/etcd/client/v3"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -22,13 +26,14 @@ type etcdStatus struct {
// observables stores observations that the operator can make about
// states of objects in kubernetes
type observables struct {
instance *v1alpha1.EtcdCluster
statefulSet appsv1.StatefulSet
stsExists bool
endpoints []string
endpointsFound bool
etcdStatuses []etcdStatus
clusterID uint64
_ int
_ []corev1.PersistentVolumeClaim
pvcs []corev1.PersistentVolumeClaim
}

// setClusterID populates the clusterID field based on etcdStatuses
Expand Down

0 comments on commit 012a1bc

Please sign in to comment.