generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement legacy ResourceQuota and LimitRange generation
Replaces https://hub.syn.tools/appuio-cloud/references/policies/11_generate_quota_limit_range_in_ns.html. Also includes a webhook to deny edits to the synced resources.
- Loading branch information
Showing
12 changed files
with
606 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"go.uber.org/multierr" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/builder" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
// LegacyResourceQuotaReconciler reconciles namespaces and synchronizes their resource quotas | ||
type LegacyResourceQuotaReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
Recorder record.EventRecorder | ||
|
||
OrganizationLabel string | ||
|
||
ResourceQuotaAnnotationBase string | ||
DefaultResourceQuotas map[string]corev1.ResourceQuotaSpec | ||
|
||
LimitRangeName string | ||
DefaultLimitRange corev1.LimitRangeSpec | ||
} | ||
|
||
func (r *LegacyResourceQuotaReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
l := log.FromContext(ctx) | ||
l.Info("Reconciling Namespace") | ||
|
||
var ns corev1.Namespace | ||
if err := r.Get(ctx, req.NamespacedName, &ns); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
if ns.DeletionTimestamp != nil { | ||
l.Info("Namespace is being deleted, skipping reconciliation") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
if _, ok := ns.Labels[r.OrganizationLabel]; !ok { | ||
l.Info("Namespace does not have organization label, skipping reconciliation") | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
var errs []error | ||
for name, s := range r.DefaultResourceQuotas { | ||
spec := *s.DeepCopy() | ||
|
||
var storageQuotas corev1.ResourceList | ||
if sqa := ns.Annotations[fmt.Sprintf("%s/%s.storageclasses", r.ResourceQuotaAnnotationBase, name)]; sqa != "" { | ||
err := json.Unmarshal([]byte(ns.Annotations[fmt.Sprintf("%s/%s.storageclasses", r.ResourceQuotaAnnotationBase, name)]), &storageQuotas) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("failed to unmarshal storage classes: %w", err)) | ||
storageQuotas = make(corev1.ResourceList) | ||
} | ||
} else { | ||
storageQuotas = make(corev1.ResourceList) | ||
} | ||
|
||
rq := &corev1.ResourceQuota{ | ||
ObjectMeta: ctrl.ObjectMeta{ | ||
Name: name, | ||
Namespace: ns.Name, | ||
}, | ||
} | ||
op, err := controllerutil.CreateOrUpdate(ctx, r.Client, rq, func() error { | ||
for k := range spec.Hard { | ||
an := fmt.Sprintf("%s/%s.%s", r.ResourceQuotaAnnotationBase, name, strings.ReplaceAll(string(k), "/", "_")) | ||
if strings.Contains(string(k), "storageclass.storage.k8s.io") { | ||
if _, ok := storageQuotas[k]; ok { | ||
spec.Hard[k] = storageQuotas[k] | ||
} | ||
} else if a := ns.Annotations[an]; a != "" { | ||
po, err := resource.ParseQuantity(a) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("failed to parse quantity %s=%s: %w", an, a, err)) | ||
continue | ||
} | ||
spec.Hard[k] = po | ||
} | ||
} | ||
|
||
rq.Spec = spec | ||
return controllerutil.SetControllerReference(&ns, rq, r.Scheme) | ||
}) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("failed to reconcile ResourceQuota %s: %w", name, err)) | ||
} | ||
if op != controllerutil.OperationResultNone { | ||
l.Info("Reconciled ResourceQuota", "name", name, "operation", op) | ||
} | ||
} | ||
|
||
lr := &corev1.LimitRange{ | ||
ObjectMeta: ctrl.ObjectMeta{ | ||
Name: r.LimitRangeName, | ||
Namespace: ns.Name, | ||
}, | ||
} | ||
op, err := controllerutil.CreateOrUpdate(ctx, r.Client, lr, func() error { | ||
lr.Spec = *r.DefaultLimitRange.DeepCopy() | ||
return controllerutil.SetControllerReference(&ns, lr, r.Scheme) | ||
}) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("failed to reconcile LimitRange %s: %w", r.LimitRangeName, err)) | ||
} | ||
if op != controllerutil.OperationResultNone { | ||
l.Info("Reconciled LimitRange", "name", r.LimitRangeName, "operation", op) | ||
} | ||
|
||
if err := multierr.Combine(errs...); err != nil { | ||
r.Recorder.Eventf(&ns, corev1.EventTypeWarning, "ReconcileError", "Failed to reconcile ResourceQuotas and LimitRanges: %s", err.Error()) | ||
return ctrl.Result{}, fmt.Errorf("failed to reconcile ResourceQuotas and LimitRanges: %w", err) | ||
} | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *LegacyResourceQuotaReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
orgPredicate, err := labelExistsPredicate(r.OrganizationLabel) | ||
if err != nil { | ||
return fmt.Errorf("failed to create organization label predicate: %w", err) | ||
} | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&corev1.Namespace{}, builder.WithPredicates(orgPredicate)). | ||
Owns(&corev1.ResourceQuota{}). | ||
Owns(&corev1.LimitRange{}). | ||
Complete(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/go-logr/logr/testr" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/utils/ptr" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
func Test_LegacyResourceQuotaReconciler_Reconcile(t *testing.T) { | ||
t.Parallel() | ||
|
||
subjectNamespace := newNamespace("test", map[string]string{"organization": "testorg"}, nil) | ||
|
||
c, scheme, recorder := prepareClient(t, subjectNamespace) | ||
ctx := log.IntoContext(context.Background(), testr.New(t)) | ||
|
||
subject := LegacyResourceQuotaReconciler{ | ||
Client: c, | ||
Scheme: scheme, | ||
Recorder: recorder, | ||
|
||
OrganizationLabel: "organization", | ||
|
||
ResourceQuotaAnnotationBase: "resourcequota.example.com", | ||
DefaultResourceQuotas: map[string]corev1.ResourceQuotaSpec{ | ||
"orgq": { | ||
Hard: corev1.ResourceList{ | ||
corev1.ResourceLimitsCPU: resource.MustParse("10"), | ||
corev1.ResourceRequestsMemory: resource.MustParse("10Gi"), | ||
"count/services.loadbalancers": resource.MustParse("10"), | ||
"localblock-storage.storageclass.storage.k8s.io/persistentvolumeclaims": resource.MustParse("10"), | ||
"cephfs-fspool-cluster.storageclass.storage.k8s.io/requests.storage": resource.MustParse("10"), | ||
"openshift.io/imagestreamtags": resource.MustParse("10"), | ||
}, | ||
}, | ||
}, | ||
|
||
LimitRangeName: "limitrange", | ||
DefaultLimitRange: corev1.LimitRangeSpec{ | ||
Limits: []corev1.LimitRangeItem{ | ||
{ | ||
Type: corev1.LimitTypeContainer, | ||
Default: corev1.ResourceList{ | ||
corev1.ResourceLimitsCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
_, err := subject.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: subjectNamespace.Name}}) | ||
require.NoError(t, err) | ||
|
||
var syncedRQ corev1.ResourceQuota | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: "orgq", Namespace: "test"}, &syncedRQ)) | ||
require.Equal(t, subject.DefaultResourceQuotas["orgq"], syncedRQ.Spec) | ||
|
||
var syncedLR corev1.LimitRange | ||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: "limitrange", Namespace: "test"}, &syncedLR)) | ||
require.Equal(t, subject.DefaultLimitRange, syncedLR.Spec) | ||
|
||
subjectNamespace.Annotations = map[string]string{ | ||
"resourcequota.example.com/orgq.storageclasses": `{"cephfs-fspool-cluster.storageclass.storage.k8s.io/requests.storage":"5"}`, | ||
"resourcequota.example.com/orgq.limits.cpu": "5", | ||
"resourcequota.example.com/orgq.count_services.loadbalancers": "5", | ||
"resourcequota.example.com/orgq.openshift.io_imagestreamtags": "5", | ||
} | ||
require.NoError(t, c.Update(ctx, subjectNamespace)) | ||
|
||
_, err = subject.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: subjectNamespace.Name}}) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, c.Get(ctx, types.NamespacedName{Name: "orgq", Namespace: "test"}, &syncedRQ)) | ||
assert.Equal(t, "5", ptr.To(syncedRQ.Spec.Hard[corev1.ResourceLimitsCPU]).String()) | ||
assert.Equal(t, "5", ptr.To(syncedRQ.Spec.Hard["count/services.loadbalancers"]).String()) | ||
assert.Equal(t, "5", ptr.To(syncedRQ.Spec.Hard["openshift.io/imagestreamtags"]).String()) | ||
assert.Equal(t, "5", ptr.To(syncedRQ.Spec.Hard["cephfs-fspool-cluster.storageclass.storage.k8s.io/requests.storage"]).String()) | ||
assert.Equal(t, "10", ptr.To(syncedRQ.Spec.Hard["localblock-storage.storageclass.storage.k8s.io/persistentvolumeclaims"]).String()) | ||
assert.Equal(t, "10Gi", ptr.To(syncedRQ.Spec.Hard[corev1.ResourceRequestsMemory]).String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.