Skip to content

Commit

Permalink
Merge pull request #183 from vshn/add/generic_quota
Browse files Browse the repository at this point in the history
Ensure all services deploy quotas
  • Loading branch information
Kidswiss authored Jun 20, 2024
2 parents 03f2f97 + 460c6df commit 3410a63
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 74 deletions.
60 changes: 60 additions & 0 deletions pkg/comp-functions/functions/common/instance_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"

"github.com/vshn/appcat/v4/apis/metadata"
"github.com/vshn/appcat/v4/pkg/common/quotas"
"github.com/vshn/appcat/v4/pkg/common/utils"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -60,6 +62,12 @@ func BootstrapInstanceNs(ctx context.Context, comp Composite, serviceName, names
return fmt.Errorf("cannot add instance namespace to composite status: %w", err)
}

l.Info("Add default quotas to namespace")
err = addInitialNamespaceQuotas(ctx, svc, namespaceResName)
if err != nil {
return fmt.Errorf("canot add default quotas to namespace: %w", err)
}

return nil
}

Expand Down Expand Up @@ -242,3 +250,55 @@ func setInstanceNamespaceStatus(svc *runtime.ServiceRuntime, comp Composite) err

return svc.SetDesiredCompositeStatus(comp)
}

// addInitialNamespaceQuotas will add the default quotas to a namespace if they are not yet set.
// This function takes the name of the namespace resource as it appears in the functionIO, it then returns the actual
// function that implements the composition function step.
func addInitialNamespaceQuotas(ctx context.Context, svc *runtime.ServiceRuntime, namespaceKon string) error {
if !svc.GetBoolFromCompositionConfig("quotasEnabled") {
svc.Log.Info("Quotas disabled, skipping")
svc.AddResult(runtime.NewNormalResult("Quotas disabled, skipping"))
return nil
}

ns := &corev1.Namespace{}

err := svc.GetObservedKubeObject(ns, namespaceKon)
if err != nil {
if err == runtime.ErrNotFound {
err = svc.GetDesiredKubeObject(ns, namespaceKon)
if err != nil {
return fmt.Errorf("cannot get namespace: %w", err)
}
// Make sure we don't touch this, if there's no name in the namespace.
if ns.GetName() == "" {
return fmt.Errorf("namespace doesn't yet have a name")
}
} else {
return fmt.Errorf("cannot get namespace: %w", err)
}
}

objectMeta := &metadata.MetadataOnlyObject{}

err = svc.GetObservedComposite(objectMeta)
if err != nil {
return fmt.Errorf("cannot get composite meta: %w", err)
}

s, err := utils.FetchSidecarsFromConfig(ctx, svc)
if err != nil {
s = &utils.Sidecars{}
}

// We only act if either the quotas were missing or the organization label is not on the
// namespace. Otherwise we ignore updates. This is to prevent any unwanted overwriting.
if quotas.AddInitalNamespaceQuotas(ctx, ns, s, objectMeta.TypeMeta.Kind) {
err = svc.SetDesiredKubeObjectWithName(ns, ns.GetName(), namespaceKon)
if err != nil {
return fmt.Errorf("cannot save namespace quotas: %w", err)
}
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAddInitialNamespaceQuotas(t *testing.T) {
svc := commontest.LoadRuntimeFromFile(t, "common/quotas/01_default.yaml")
ctx := context.TODO()

res := AddInitialNamespaceQuotas("namespace")(ctx, svc)
res := addInitialNamespaceQuotas(ctx, svc, "namespace")
assert.Nil(t, res)

ns := &corev1.Namespace{}
Expand Down
65 changes: 0 additions & 65 deletions pkg/comp-functions/functions/common/namespace-quotas.go

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/comp-functions/functions/vshnpostgres/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ func init() {
Name: "load-balancer",
Execute: AddPrimaryService,
},
{
Name: "namespaceQuotas",
Execute: common.AddInitialNamespaceQuotas("namespace-conditions"),
},
{
Name: "delay-cluster-deployment",
Execute: DelayClusterDeployment,
Expand Down
4 changes: 0 additions & 4 deletions pkg/comp-functions/functions/vshnredis/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ func init() {
Name: "resizePVC",
Execute: ResizePVCs,
},
{
Name: "namespaceQuotas",
Execute: common.AddInitialNamespaceQuotas("namespace-conditions"),
},
{
Name: "redis_url",
Execute: AddUrlToConnectionDetails,
Expand Down

0 comments on commit 3410a63

Please sign in to comment.