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 24, 2024
1 parent e3d1f15 commit b406e85
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 53 deletions.
43 changes: 0 additions & 43 deletions pkg/comp-functions/functions/common/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ 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"
)

Expand All @@ -25,45 +21,6 @@ type Billing struct {
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)
}

// CreateBillingRecord creates a new prometheus rule per each instance namespace
// The rule is skipped for any secondary service such as postgresql instance for nextcloud
// The skipping is based on whether label appuio.io/billing-name is set or not on instance namespace
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 b406e85

Please sign in to comment.