Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions pkg/comp-functions/functions/spks/spksredis/haproxy_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package spksredis

import (
"context"
"encoding/json"
"fmt"

xfnproto "github.com/crossplane/function-sdk-go/proto/v1"
helmv1beta1 "github.com/vshn/appcat/v4/apis/helm/release/v1beta1"
spksv1alpha1 "github.com/vshn/appcat/v4/apis/syntools/v1alpha1"
"github.com/vshn/appcat/v4/pkg/comp-functions/functions/common"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
)

func HaproxyMetrics(ctx context.Context, comp *spksv1alpha1.CompositeRedisInstance, svc *runtime.ServiceRuntime) *xfnproto.Result {
err := svc.GetObservedComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("failed to parse composite: %w", err))
}

release := &helmv1beta1.Release{}
err = svc.GetDesiredComposedResourceByName(release, redisRelease)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot get redis release from desired iof: %w", err))
}

values, err := common.GetReleaseValues(release)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot parse release values from desired release: %w", err))
}

haproxyExtraConfig := fmt.Sprintf(`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just do this with a string format PnT patch? https://docs.crossplane.io/latest/guides/function-patch-and-transform/#string-format

Copy link
Copy Markdown
Member Author

@TheBigLee TheBigLee Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but that would result in a huge unreadable patch. Would you prefer doing it with pnt patch?

Copy link
Copy Markdown
Contributor

@Kidswiss Kidswiss Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could build the patch via Jsonnet, so only when it's rendered out it's not readable.

I would prefer to minimize the amount of comp functions for SPKS and only use it if there's no other way. Because the comp functions add another layer of complexity.

But let's see what @mdnix and @zugao think.

Copy link
Copy Markdown
Collaborator

@zugao zugao Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we decided on that, we use comp-functions for SPKS only when necessary, we keep PnT as base as it used to be!

Update: One thing that I am investigating with our comp function architecture is to get out all the code that we can be configured either with project syn or another configuration manager such as KCL. So this approach here might be true for our appcat comp-functions as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Thanks for the input. I'll close this PR as I've integrated this logic directly into PnT.

backend filterproxy
mode http
http-request set-query namespace=%s
server filter exporter-filterproxy.syn-exporter-filterproxy.svc.cluster.local:8080

frontend redisMetrics
mode http
bind *:9090
option httplog
use_backend redis-node-metrics-0 if { path_beg /redis/0 }
use_backend redis-node-metrics-1 if { path_beg /redis/1 }
use_backend redis-node-metrics-2 if { path_beg /redis/2 }
use_backend filterproxy

backend redis-node-metrics-0
mode http
http-request set-path /metrics
server node-0 redis-announce-0:9121 init-addr none check
backend redis-node-metrics-1
mode http
http-request set-path /metrics
server node-1 redis-announce-1:9121 init-addr none check
backend redis-node-metrics-2
mode http
http-request set-path /metrics
server node-2 redis-announce-2:9121 init-addr none check`, comp.GetName())

if err := common.SetNestedObjectValue(values, []string{"haproxy", "extraConfig"}, haproxyExtraConfig); err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot set helm values: %w", err))
}

vb, err := json.Marshal(values)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot marshal release values: %w", err))
}

release.Spec.ForProvider.Values.Raw = vb

if err := svc.SetDesiredComposedResourceWithName(release, redisRelease); err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot set new release: %w", err))
}

return nil
}
4 changes: 4 additions & 0 deletions pkg/comp-functions/functions/spks/spksredis/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
func init() {
runtime.RegisterService("redis-k8s", runtime.Service[*v1alpha1.CompositeRedisInstance]{
Steps: []runtime.Step[*v1alpha1.CompositeRedisInstance]{
{
Name: "haproxyMetrics",
Execute: HaproxyMetrics,
},
{
Name: "resizePVC",
Execute: ResizeSpksPVCs,
Expand Down
Loading