Skip to content

Commit

Permalink
Remove old billing label from workloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Saratura committed Oct 25, 2024
1 parent e3d1f15 commit 414458d
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 74 deletions.
62 changes: 4 additions & 58 deletions pkg/comp-functions/functions/common/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,15 @@ import (
"context"
_ "embed"
"fmt"
v12 "github.com/crossplane/crossplane-runtime/apis/common/v1"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
xkube "github.com/vshn/appcat/v4/apis/kubernetes/v1alpha2"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"reflect"
controllerruntime "sigs.k8s.io/controller-runtime"
"strings"
"text/template"
)

var rawExpr = "sum (sum_over_time ((kube_namespace_labels{namespace=\"{{.Namespace}}\"} * {{.Instances}})[60m:1m])/60)"

// Billing contains required information for the billing record expression
type Billing struct {
Namespace string
Instances int
}

const billingLabel = "appcat.io/billing"

// TODO remove this function as soon as we have automated release pipeline in prod according to maintenance
// InjectBillingLabelToService adds billing label to a service (StatefulSet or Deployment).
// It uses a kube Object to achieve post provisioning labelling
func InjectBillingLabelToService(ctx context.Context, svc *runtime.ServiceRuntime, comp InfoGetter) *xfnproto.Result {

s := comp.GetWorkloadPodTemplateLabelsManager()
s.SetName(comp.GetWorkloadName())
s.SetNamespace(comp.GetInstanceNamespace())
kubeName := comp.GetName() + "-" + getType(s)

_ = svc.GetObservedKubeObject(s, kubeName)
mp := v12.ManagementPolicies{v12.ManagementActionObserve}
labels := s.GetPodTemplateLabels()
_, exists := labels[billingLabel]
if !s.GetCreationTimestamp().Time.IsZero() {
if !exists {
labels[billingLabel] = "true"
s.SetPodTemplateLabels(labels)
mp = append(mp, v12.ManagementActionCreate, v12.ManagementActionUpdate)
}
}

err := svc.SetDesiredKubeObject(s.GetObject(), kubeName, 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")
}

func getType(myvar interface{}) (res string) {
return strings.ToLower(reflect.TypeOf(myvar).Elem().Field(0).Name)
}
var rawExpr = "vector({{.}})"

// CreateBillingRecord creates a new prometheus rule per each instance namespace
// The rule is skipped for any secondary service such as postgresql instance for nextcloud
Expand All @@ -71,7 +22,7 @@ func CreateBillingRecord(ctx context.Context, svc *runtime.ServiceRuntime, comp
log := controllerruntime.LoggerFrom(ctx)
log.Info("Enabling billing for service", "service", comp.GetName())

expr, err := getExprFromTemplate(comp.GetInstanceNamespace(), comp.GetInstances())
expr, err := getExprFromTemplate(comp.GetInstances())
if err != nil {
runtime.NewWarningResult(fmt.Sprintf("cannot add billing to service %s", comp.GetName()))
}
Expand Down Expand Up @@ -146,19 +97,14 @@ func getLabels(org string, comp InfoGetter, svc *runtime.ServiceRuntime) map[str
return labels
}

func getExprFromTemplate(namespace string, i int) (string, error) {
b := Billing{
Instances: i,
Namespace: namespace,
}

func getExprFromTemplate(i int) (string, error) {
var buf bytes.Buffer
tmpl, err := template.New("billing").Parse(rawExpr)
if err != nil {
return "", err
}

err = tmpl.Execute(&buf, b)
err = tmpl.Execute(&buf, i)
if err != nil {
return "", err
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/comp-functions/functions/common/instance_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ func createInstanceNamespace(serviceName, compName, claimNamespace, instanceName
ObjectMeta: metav1.ObjectMeta{
Name: instanceNamespace,
Labels: map[string]string{
"appcat.vshn.io/servicename": serviceName + "-" + mode,
"appcat.vshn.io/claim-namespace": claimNamespace,
"appcat.vshn.io/claim-name": claimName,
"appuio.io/no-rbac-creation": "true",
"appuio.io/billing-name": billingName,
"appuio.io/organization": org,
"appcat.vshn.io/servicename": serviceName + "-" + mode,
"appcat.vshn.io/claim-namespace": claimNamespace,
"appcat.vshn.io/claim-name": claimName,
"appuio.io/no-rbac-creation": "true",
"appuio.io/billing-name": billingName,
"appuio.io/organization": org,
"openshift.io/cluster-monitoring": "true",
},
},
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/comp-functions/functions/vshnkeycloak/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ func AddServiceBillingLabel(ctx context.Context, comp *v1.VSHNKeycloak, svc *run
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

common.InjectBillingLabelToService(ctx, svc, comp)

return common.CreateBillingRecord(ctx, svc, comp)
}
2 changes: 0 additions & 2 deletions pkg/comp-functions/functions/vshnmariadb/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ func AddServiceBillingLabel(ctx context.Context, comp *v1.VSHNMariaDB, svc *runt
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

common.InjectBillingLabelToService(ctx, svc, comp)

return common.CreateBillingRecord(ctx, svc, comp)
}
2 changes: 0 additions & 2 deletions pkg/comp-functions/functions/vshnminio/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ func AddServiceBillingLabel(ctx context.Context, comp *v1.VSHNMinio, svc *runtim
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

common.InjectBillingLabelToService(ctx, svc, comp)

return common.CreateBillingRecord(ctx, svc, comp)
}
1 change: 1 addition & 0 deletions pkg/comp-functions/functions/vshnnextcloud/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

// AddServiceBillingLabel adds billingLabel to all the objects of a services that must be billed
func AddServiceBillingLabel(ctx 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))
Expand Down
2 changes: 0 additions & 2 deletions pkg/comp-functions/functions/vshnpostgres/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ func AddServiceBillingLabel(ctx context.Context, comp *v1.VSHNPostgreSQL, svc *r
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

common.InjectBillingLabelToService(ctx, svc, comp)

return common.CreateBillingRecord(ctx, svc, comp)
}
2 changes: 0 additions & 2 deletions pkg/comp-functions/functions/vshnredis/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ func AddServiceBillingLabel(ctx context.Context, comp *v1.VSHNRedis, svc *runtim
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}

common.InjectBillingLabelToService(ctx, svc, comp)

return common.CreateBillingRecord(ctx, svc, comp)
}

0 comments on commit 414458d

Please sign in to comment.