Skip to content

Commit

Permalink
Add billing to services
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Saratura committed Oct 7, 2024
1 parent 7dd0eb1 commit 2525b50
Show file tree
Hide file tree
Showing 20 changed files with 220 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apis/vshn/v1/dbaas_vshn_keycloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"fmt"
v1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -334,3 +336,7 @@ func (v *VSHNKeycloak) GetPDBLabels() map[string]string {
func (v *VSHNKeycloak) GetSecurity() *Security {
return &v.Spec.Parameters.Security
}

func (v *VSHNKeycloak) GetServiceKubeType() client.Object {
return &v1.StatefulSet{}
}
6 changes: 6 additions & 0 deletions apis/vshn/v1/dbaas_vshn_mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"fmt"
v1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -273,3 +275,7 @@ func (v *VSHNMariaDB) GetPDBLabels() map[string]string {
func (v *VSHNMariaDB) GetSecurity() *Security {
return &v.Spec.Parameters.Security
}

func (v *VSHNMariaDB) GetServiceKubeType() client.Object {
return &v1.StatefulSet{}
}
6 changes: 6 additions & 0 deletions apis/vshn/v1/dbaas_vshn_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"fmt"
v1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
sgv1 "github.com/vshn/appcat/v4/apis/stackgres/v1"
Expand Down Expand Up @@ -397,3 +399,7 @@ func (v *VSHNPostgreSQL) GetPDBLabels() map[string]string {
func (v *VSHNPostgreSQL) GetSecurity() *Security {
return &v.Spec.Parameters.Security
}

func (v *VSHNPostgreSQL) GetServiceKubeType() client.Object {
return &v1.StatefulSet{}
}
6 changes: 6 additions & 0 deletions apis/vshn/v1/dbaas_vshn_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"fmt"
v1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -298,3 +300,7 @@ func (v *VSHNRedis) GetPDBLabels() map[string]string {
func (v *VSHNRedis) GetSecurity() *Security {
return &v.Spec.Parameters.Security
}

func (v *VSHNRedis) GetServiceKubeType() client.Object {
return &v1.StatefulSet{}
}
6 changes: 6 additions & 0 deletions apis/vshn/v1/vshn_minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"fmt"
v1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -247,3 +249,7 @@ func (v *VSHNMinio) GetPDBLabels() map[string]string {
func (v *VSHNMinio) GetSecurity() *Security {
return &v.Spec.Parameters.Security
}

func (v *VSHNMinio) GetServiceKubeType() client.Object {
return &v1.StatefulSet{}
}
6 changes: 6 additions & 0 deletions apis/vshn/v1/vshn_nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"fmt"
v1 "k8s.io/api/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -304,3 +306,7 @@ func (v *VSHNNextcloud) GetPDBLabels() map[string]string {
func (v *VSHNNextcloud) GetSecurity() *Security {
return &v.Spec.Parameters.Security
}

func (v *VSHNNextcloud) GetServiceKubeType() client.Object {
return &v1.Deployment{}
}
39 changes: 39 additions & 0 deletions pkg/comp-functions/functions/common/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package common

import (
"fmt"
v12 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
xkube "github.com/vshn/appcat/v4/apis/kubernetes/v1alpha2"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

const billingLabel = "appcat.io/billing"

func InjectBillingLabelToObjects(svc *runtime.ServiceRuntime, comp InfoGetter) *xfnproto.Result {
s := comp.GetServiceKubeType()
s.SetName(comp.GetName())
s.SetNamespace(comp.GetInstanceNamespace())

_ = svc.GetObservedKubeObject(s, s.GetName()+"-service")
mp := v12.ManagementPolicies{v12.ManagementActionObserve}
_, exists := s.GetLabels()[billingLabel]
if !s.GetCreationTimestamp().Time.IsZero() {
if !exists {
labels := s.GetLabels()
labels[billingLabel] = "true"
s.SetLabels(labels)
mp = append(mp, v12.ManagementActionCreate, v12.ManagementActionUpdate)
}
}

err := svc.SetDesiredKubeObject(s, s.GetName()+"-service", func(obj *xkube.Object) {
obj.Spec.ManagementPolicies = mp
})

if err != nil && !exists {
runtime.NewWarningResult(fmt.Sprintf("cannot add billing to service object %s", s.GetName()))
}

return runtime.NewNormalResult("billing enabled")
}
1 change: 1 addition & 0 deletions pkg/comp-functions/functions/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type InfoGetter interface {
GetSecurity() *vshnv1.Security
InstanceNamespaceInfo
GetPDBLabels() map[string]string
GetServiceKubeType() client.Object
}

// InstanceNamespaceInfo provides all the necessary information to create
Expand Down
20 changes: 20 additions & 0 deletions pkg/comp-functions/functions/vshnkeycloak/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package vshnkeycloak

import (
"context"
"fmt"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

// AddServiceBillingLabel adds billingLabel to all the objects of a services that must be billed
func AddServiceBillingLabel(_ context.Context, comp *v1.VSHNKeycloak, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

return common.InjectBillingLabelToObjects(svc, comp)
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnkeycloak/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func init() {
Name: "pdb",
Execute: common.AddPDBSettings[*vshnv1.VSHNKeycloak],
},
{
Name: "billing",
Execute: AddServiceBillingLabel,
},
},
})
}
20 changes: 20 additions & 0 deletions pkg/comp-functions/functions/vshnmariadb/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package vshnmariadb

import (
"context"
"fmt"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

// AddServiceBillingLabel adds billingLabel to all the objects of a services that must be billed
func AddServiceBillingLabel(_ context.Context, comp *v1.VSHNMariaDB, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

return common.InjectBillingLabelToObjects(svc, comp)
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnmariadb/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func init() {
Name: "non-sla-prometheus-rules",
Execute: nonsla.GenerateNonSLAPromRules[*vshnv1.VSHNMariaDB](nonsla.NewAlertSetBuilder("mariadb", "mariadb").AddAll().GetAlerts()),
},
{
Name: "billing",
Execute: AddServiceBillingLabel,
},
},
})
}
20 changes: 20 additions & 0 deletions pkg/comp-functions/functions/vshnminio/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package vshnminio

import (
"context"
"fmt"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

// AddServiceBillingLabel adds billingLabel to all the objects of a services that must be billed
func AddServiceBillingLabel(_ context.Context, comp *v1.VSHNMinio, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

return common.InjectBillingLabelToObjects(svc, comp)
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnminio/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func init() {
Name: "pdb",
Execute: common.AddPDBSettings[*vshnv1.VSHNMinio],
},
{
Name: "billing",
Execute: AddServiceBillingLabel,
},
},
})
}
20 changes: 20 additions & 0 deletions pkg/comp-functions/functions/vshnnextcloud/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package vshnnextcloud

import (
"context"
"fmt"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

// AddServiceBillingLabel adds billingLabel to all the objects of a services that must be billed
func AddServiceBillingLabel(_ context.Context, comp *v1.VSHNNextcloud, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

return common.InjectBillingLabelToObjects(svc, comp)
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnnextcloud/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func init() {
Name: "backup",
Execute: AddBackup,
},
{
Name: "billing",
Execute: AddServiceBillingLabel,
},
},
})
}
20 changes: 20 additions & 0 deletions pkg/comp-functions/functions/vshnpostgres/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package vshnpostgres

import (
"context"
"fmt"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

// AddServiceBillingLabel adds billingLabel to all the objects of a services that must be billed
func AddServiceBillingLabel(_ context.Context, comp *v1.VSHNPostgreSQL, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

return common.InjectBillingLabelToObjects(svc, comp)
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnpostgres/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func init() {
Name: "pdb",
Execute: common.AddPDBSettings[*vshnv1.VSHNPostgreSQL],
},
{
Name: "billing",
Execute: AddServiceBillingLabel,
},
},
})
}
20 changes: 20 additions & 0 deletions pkg/comp-functions/functions/vshnredis/billing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package vshnredis

import (
"context"
"fmt"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
v1 "github.com/vshn/appcat/v4/apis/vshn/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

// AddServiceBillingLabel adds billingLabel to all the objects of a services that must be billed
func AddServiceBillingLabel(_ context.Context, comp *v1.VSHNRedis, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

return common.InjectBillingLabelToObjects(svc, comp)
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/vshnredis/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func init() {
Name: "non-sla-prometheus-rules",
Execute: nonsla.GenerateNonSLAPromRules[*vshnv1.VSHNRedis](nonsla.NewAlertSetBuilder("redis", "redis").AddAll().GetAlerts()),
},
{
Name: "billing",
Execute: AddServiceBillingLabel,
},
},
})
}

0 comments on commit 2525b50

Please sign in to comment.