-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from vshn/add-service-billing-label
Add billing to services
- Loading branch information
Showing
21 changed files
with
307 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package v1 | ||
|
||
import ( | ||
v1 "k8s.io/api/apps/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// +kubebuilder:skip | ||
// +kubebuilder:skipclient | ||
// +kubebuilder:skipdeepcopy | ||
// +kubebuilder:object:generate=false | ||
type PodTemplateLabelsManager interface { | ||
SetPodTemplateLabels(map[string]string) | ||
GetPodTemplateLabels() map[string]string | ||
GetObject() client.Object | ||
client.Object | ||
} | ||
|
||
// +kubebuilder:skip | ||
// +kubebuilder:skipclient | ||
// +kubebuilder:skipdeepcopy | ||
// +kubebuilder:object:generate=false | ||
type DeploymentManager struct { | ||
v1.Deployment | ||
} | ||
|
||
// +kubebuilder:skip | ||
// +kubebuilder:skipclient | ||
// +kubebuilder:skipdeepcopy | ||
// +kubebuilder:object:generate=false | ||
type StatefulSetManager struct { | ||
v1.StatefulSet | ||
} | ||
|
||
func (d *DeploymentManager) SetPodTemplateLabels(labels map[string]string) { | ||
d.Spec.Template.SetLabels(labels) | ||
} | ||
|
||
func (s *StatefulSetManager) SetPodTemplateLabels(labels map[string]string) { | ||
s.Spec.Template.SetLabels(labels) | ||
} | ||
|
||
func (d *DeploymentManager) GetPodTemplateLabels() map[string]string { | ||
return d.Spec.Template.GetLabels() | ||
} | ||
|
||
func (s *StatefulSetManager) GetPodTemplateLabels() map[string]string { | ||
return s.Spec.Template.GetLabels() | ||
} | ||
|
||
func (d *DeploymentManager) GetObject() client.Object { | ||
return &d.Deployment | ||
} | ||
|
||
func (d *StatefulSetManager) GetObject() client.Object { | ||
return &d.StatefulSet | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"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" | ||
"reflect" | ||
controllerruntime "sigs.k8s.io/controller-runtime" | ||
"strings" | ||
) | ||
|
||
const billingLabel = "appcat.io/billing" | ||
|
||
// 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 { | ||
log := controllerruntime.LoggerFrom(ctx) | ||
log.Info("Enabling billing for service", "service", comp.GetName()) | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ctx 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.InjectBillingLabelToService(ctx, svc, comp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ctx 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.InjectBillingLabelToService(ctx, svc, comp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ctx 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.InjectBillingLabelToService(ctx, svc, comp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(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)) | ||
} | ||
|
||
return common.InjectBillingLabelToService(ctx, svc, comp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(ctx 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.InjectBillingLabelToService(ctx, svc, comp) | ||
} |
Oops, something went wrong.