Skip to content

Commit

Permalink
move some modules to internal dir
Browse files Browse the repository at this point in the history
Since kubebuilder's layout is now Standard Go Project Layout, some
modules have been re-located. Since there is no specification as
to where the constants should be located, they were moved to the
top directory, similar to TopoLVM.

Signed-off-by: Yuji Ito <[email protected]>
  • Loading branch information
llamerada-jp committed Jan 15, 2024
1 parent 4163b0f commit 51db0f7
Show file tree
Hide file tree
Showing 17 changed files with 26 additions and 23 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ WORKDIR /workspace
COPY go.mod go.mod
COPY go.sum go.sum
# Copy the go source
COPY constants.go constants.go
COPY cmd/ cmd/
COPY hooks/ hooks/
COPY metrics/ metrics/
COPY runners/ runners/
COPY internal/ internal/

# Build
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -a -o pvc-autoresizer cmd/*.go
Expand Down
4 changes: 2 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net"
"time"

"github.com/topolvm/pvc-autoresizer/hooks"
"github.com/topolvm/pvc-autoresizer/runners"
"github.com/topolvm/pvc-autoresizer/internal/hooks"
"github.com/topolvm/pvc-autoresizer/internal/runners"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
2 changes: 1 addition & 1 deletion runners/constants.go → constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runners
package pvcautoresizer

// AutoResizeEnabledKey is the key of flag that enables pvc-autoresizer.
const AutoResizeEnabledKey = "resize.topolvm.io/enabled"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"net/http"

"github.com/go-logr/logr"
"github.com/topolvm/pvc-autoresizer/runners"
pvcautoresizer "github.com/topolvm/pvc-autoresizer"
"github.com/topolvm/pvc-autoresizer/internal/runners"
admissionv1 "k8s.io/api/admission/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -35,7 +36,7 @@ func (m *persistentVolumeClaimMutator) Handle(ctx context.Context, req admission
if err := m.dec.Decode(req, pvc); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
groupLabelKey, ok := pvc.Annotations[runners.InitialResizeGroupByAnnotation]
groupLabelKey, ok := pvc.Annotations[pvcautoresizer.InitialResizeGroupByAnnotation]
if !ok || groupLabelKey == "" {
return admission.Allowed("annotation not set")
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/prometheus/client_golang/api"
prometheusv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/topolvm/pvc-autoresizer/metrics"
"github.com/topolvm/pvc-autoresizer/internal/metrics"
"k8s.io/apimachinery/pkg/types"
)

Expand Down
File renamed without changes.
19 changes: 10 additions & 9 deletions runners/pvc_autoresizer.go → internal/runners/pvc_autoresizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/topolvm/pvc-autoresizer/metrics"
pvcautoresizer "github.com/topolvm/pvc-autoresizer"
"github.com/topolvm/pvc-autoresizer/internal/metrics"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -166,15 +167,15 @@ func (w *pvcAutoresizer) reconcile(ctx context.Context) error {
func (w *pvcAutoresizer) resize(ctx context.Context, pvc *corev1.PersistentVolumeClaim, vs *VolumeStats) error {
log := w.log.WithName("resize").WithValues("namespace", pvc.Namespace, "name", pvc.Name)

threshold, err := convertSizeInBytes(pvc.Annotations[ResizeThresholdAnnotation], vs.CapacityBytes, DefaultThreshold)
threshold, err := convertSizeInBytes(pvc.Annotations[pvcautoresizer.ResizeThresholdAnnotation], vs.CapacityBytes, pvcautoresizer.DefaultThreshold)
if err != nil {
log.V(logLevelWarn).Info("failed to convert threshold annotation", "error", err.Error())
// lint:ignore nilerr ignores this because invalid annotations should be allowed.
return nil
}

annotation := pvc.Annotations[ResizeInodesThresholdAnnotation]
inodesThreshold, err := convertSize(annotation, vs.CapacityInodeSize, DefaultInodesThreshold)
annotation := pvc.Annotations[pvcautoresizer.ResizeInodesThresholdAnnotation]
inodesThreshold, err := convertSize(annotation, vs.CapacityInodeSize, pvcautoresizer.DefaultInodesThreshold)
if err != nil {
log.V(logLevelWarn).Info("failed to convert threshold annotation", "error", err.Error())
// lint:ignore nilerr ignores this because invalid annotations should be allowed.
Expand All @@ -191,13 +192,13 @@ func (w *pvcAutoresizer) resize(ctx context.Context, pvc *corev1.PersistentVolum
return nil
}

increase, err := convertSizeInBytes(pvc.Annotations[ResizeIncreaseAnnotation], cap.Value(), DefaultIncrease)
increase, err := convertSizeInBytes(pvc.Annotations[pvcautoresizer.ResizeIncreaseAnnotation], cap.Value(), pvcautoresizer.DefaultIncrease)
if err != nil {
log.V(logLevelWarn).Info("failed to convert increase annotation", "error", err.Error())
return nil
}

preCap, exist := pvc.Annotations[PreviousCapacityBytesAnnotation]
preCap, exist := pvc.Annotations[pvcautoresizer.PreviousCapacityBytesAnnotation]
if exist {
preCapInt64, err := strconv.ParseInt(preCap, 10, 64)
if err != nil {
Expand Down Expand Up @@ -232,7 +233,7 @@ func (w *pvcAutoresizer) resize(ctx context.Context, pvc *corev1.PersistentVolum
}

pvc.Spec.Resources.Requests[corev1.ResourceStorage] = *newReq
pvc.Annotations[PreviousCapacityBytesAnnotation] = strconv.FormatInt(vs.CapacityBytes, 10)
pvc.Annotations[pvcautoresizer.PreviousCapacityBytesAnnotation] = strconv.FormatInt(vs.CapacityBytes, 10)
err = w.client.Update(ctx, pvc)
if err != nil {
metrics.KubernetesClientFailTotal.Increment()
Expand All @@ -255,7 +256,7 @@ func (w *pvcAutoresizer) resize(ctx context.Context, pvc *corev1.PersistentVolum

func indexByResizeEnableAnnotation(obj client.Object) []string {
sc := obj.(*storagev1.StorageClass)
if val, ok := sc.Annotations[AutoResizeEnabledKey]; ok {
if val, ok := sc.Annotations[pvcautoresizer.AutoResizeEnabledKey]; ok {
return []string{val}
}

Expand Down Expand Up @@ -335,7 +336,7 @@ func calcSize(valStr string, capacity int64) (int64, error) {

func PvcStorageLimit(pvc *corev1.PersistentVolumeClaim) (resource.Quantity, error) {
// storage limit on the annotation has precedence
if annotation, ok := pvc.Annotations[StorageLimitAnnotation]; ok && annotation != "" {
if annotation, ok := pvc.Annotations[pvcautoresizer.StorageLimitAnnotation]; ok && annotation != "" {
return resource.ParseQuantity(annotation)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
pvcautoresizer "github.com/topolvm/pvc-autoresizer"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -447,18 +448,18 @@ func createPVC(ctx context.Context, ns, name, scName, threshold, inodesThreshold
}

if len(threshold) != 0 {
pvc.Annotations[ResizeThresholdAnnotation] = threshold
pvc.Annotations[pvcautoresizer.ResizeThresholdAnnotation] = threshold
}
if len(inodesThreshold) != 0 {
pvc.Annotations[ResizeInodesThresholdAnnotation] = inodesThreshold
pvc.Annotations[pvcautoresizer.ResizeInodesThresholdAnnotation] = inodesThreshold
}

if len(increase) != 0 {
pvc.Annotations[ResizeIncreaseAnnotation] = increase
pvc.Annotations[pvcautoresizer.ResizeIncreaseAnnotation] = increase
}

if limit != 0 {
pvc.Annotations[StorageLimitAnnotation] = strconv.FormatInt(limit, 10)
pvc.Annotations[pvcautoresizer.StorageLimitAnnotation] = strconv.FormatInt(limit, 10)
}

err := k8sClient.Create(ctx, &pvc)
Expand Down
3 changes: 2 additions & 1 deletion runners/suite_test.go → internal/runners/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
. "github.com/onsi/gomega"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
pvcautoresizer "github.com/topolvm/pvc-autoresizer"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -121,7 +122,7 @@ func createStorageClass(ctx context.Context, name, provisioner string) {
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: map[string]string{
AutoResizeEnabledKey: "true",
pvcautoresizer.AutoResizeEnabledKey: "true",
},
},
Provisioner: provisioner,
Expand Down

0 comments on commit 51db0f7

Please sign in to comment.