Skip to content

Commit

Permalink
Merge pull request #127 from vshn/fix/empty_org
Browse files Browse the repository at this point in the history
Don't handle the organization label in quotas
  • Loading branch information
Kidswiss authored Jan 25, 2024
2 parents a6b61d6 + 18c0461 commit b7f2147
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
18 changes: 13 additions & 5 deletions pkg/comp-functions/functions/common/instance_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func BootstrapInstanceNs(ctx context.Context, comp InstanceNamespaceInfo, servic
return nil
}

func getOrg(instance string, svc *runtime.ServiceRuntime) string {
func getOrg(instance string, svc *runtime.ServiceRuntime) (string, error) {
ns := &corev1.Namespace{}

err := svc.GetObservedKubeObject(ns, instance+claimNsObserverSuffix)
if err != nil {
return ""
return "", err
}
return ns.GetLabels()[utils.OrgLabelName]
return ns.GetLabels()[utils.OrgLabelName], nil
}

func createNamespaceObserver(ctx context.Context, claimNs string, instance string, svc *runtime.ServiceRuntime) error {
Expand All @@ -82,7 +82,11 @@ func createNamespaceObserver(ctx context.Context, claimNs string, instance strin
// Create the namespace for the service instance
func createInstanceNamespace(ctx context.Context, serviceName, compName, claimNamespace, instanceNamespace, namespaceResName, claimName string, svc *runtime.ServiceRuntime) error {

org := getOrg(compName, svc)
org, err := getOrg(compName, svc)
if err != nil {
return fmt.Errorf("cannot get claim namespace: %w", err)
}

ns := &corev1.Namespace{

ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -115,7 +119,11 @@ func createNamespacePermissions(ctx context.Context, instance string, instanceNs
}
}

org := getOrg(instance, svc)
org, err := getOrg(instance, svc)
if err != nil {
return fmt.Errorf("cannot get claim namespace: %w", err)
}

if org == "" {
return nil
}
Expand Down
27 changes: 5 additions & 22 deletions pkg/comp-functions/functions/common/namespace-quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,22 @@ func AddInitialNamespaceQuotas(namespaceKon string) func(context.Context, *runti
if err == runtime.ErrNotFound {
err = svc.GetDesiredKubeObject(ns, namespaceKon)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot get namespace: %w", err))
return runtime.NewWarningResult(fmt.Sprintf("cannot get namespace: %s", err))
}
// Make sure we don't touch this, if there's no name in the namespace.
if ns.GetName() == "" {
return runtime.NewWarningResult("namespace doesn't yet have a name")
}
} else {
return runtime.NewFatalResult(fmt.Errorf("cannot get namespace: %w", err))
return runtime.NewWarningResult(fmt.Sprintf("cannot get namespace: %s", err))
}
}

orgAdded := false
objectMeta := &metadata.MetadataOnlyObject{}

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

if value, ok := ns.GetLabels()[utils.OrgLabelName]; !ok || value == "" {
objectMeta := &metadata.MetadataOnlyObject{}

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

if ns.Labels == nil {
ns.Labels = map[string]string{}
}

ns.Labels[utils.OrgLabelName] = objectMeta.GetLabels()[utils.OrgLabelName]
orgAdded = true
return runtime.NewWarningResult(fmt.Sprintf("cannot get composite meta: %s", err))
}

s, err := utils.FetchSidecarsFromConfig(ctx, svc)
Expand All @@ -70,10 +53,10 @@ func AddInitialNamespaceQuotas(namespaceKon string) func(context.Context, *runti

// 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) || orgAdded {
if quotas.AddInitalNamespaceQuotas(ctx, ns, s, objectMeta.TypeMeta.Kind) {
err = svc.SetDesiredKubeObjectWithName(ns, ns.GetName(), namespaceKon)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot save namespace quotas: %w", err))
return runtime.NewWarningResult(fmt.Sprintf("cannot save namespace quotas: %s", err))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func DeployPostgreSQL(ctx context.Context, svc *runtime.ServiceRuntime) *xfnprot
err = common.BootstrapInstanceNs(ctx, comp, "postgresql", "namespace-conditions", svc)
if err != nil {
err = fmt.Errorf("cannot bootstrap instance namespace: %w", err)
return runtime.NewFatalResult(err)
return runtime.NewWarningResult(err.Error())
}

return nil
Expand Down

0 comments on commit b7f2147

Please sign in to comment.