Skip to content

Commit

Permalink
support to set dkEndpointUrl by ScaledObject
Browse files Browse the repository at this point in the history
  • Loading branch information
ShotaKitazawa committed Nov 5, 2022
1 parent 26e7d04 commit ecf3fcc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dreamkast/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewClient(dkEndpointUrl string) (Client, error) {

func (c *ClientImpl) ListConferences(ctx context.Context) (ListConferencesResp, error) {
url := c.dkEndpointUrl
url.Path = filepath.Join(url.Path, "/v1/events")
url.Path = filepath.Join(url.Path, "/api/v1/events")
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
Expand Down
39 changes: 30 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,31 @@ var (
type ExternalScaler struct {
pb.UnimplementedExternalScalerServer

dkClient dreamkast.Client
defaultDkClient dreamkast.Client
}

func (e *ExternalScaler) IsActive(ctx context.Context, scaledObject *pb.ScaledObjectRef) (*pb.IsActiveResponse, error) {
result, err := e.isActive(ctx)
dkEndpointUrl := scaledObject.ScalerMetadata["dkEndpointUrl"]
var dkClient dreamkast.Client
if dkEndpointUrl != "" {
var err error
dkClient, err = dreamkast.NewClient(dkEndpointUrl)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "failed to connect to specified dkEndpointUrl")
}
}
result, err := e.isActive(ctx, dkClient)
if err != nil {
return nil, err
return nil, status.Error(codes.Internal, err.Error())
}
return &pb.IsActiveResponse{Result: result}, nil
}

func (e *ExternalScaler) isActive(ctx context.Context) (bool, error) {
conferences, err := e.dkClient.ListConferences(ctx)
func (e *ExternalScaler) isActive(ctx context.Context, dkClient dreamkast.Client) (bool, error) {
if dkClient == nil {
dkClient = e.defaultDkClient
}
conferences, err := dkClient.ListConferences(ctx)
if err != nil {
return false, status.Error(codes.Internal, err.Error())
}
Expand Down Expand Up @@ -70,9 +82,18 @@ func (e *ExternalScaler) GetMetricSpec(context.Context, *pb.ScaledObjectRef) (*p
}

func (e *ExternalScaler) GetMetrics(ctx context.Context, metricRequest *pb.GetMetricsRequest) (*pb.GetMetricsResponse, error) {
minReplicas := defaultDesiredReplicas
dkEndpointUrl := metricRequest.ScaledObjectRef.ScalerMetadata["dkEndpointUrl"]
var dkClient dreamkast.Client
if dkEndpointUrl != "" {
var err error
dkClient, err = dreamkast.NewClient(dkEndpointUrl)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "failed to connect to specified dkEndpointUrl")
}
}

active, err := e.isActive(ctx)
minReplicas := defaultDesiredReplicas
active, err := e.isActive(ctx, dkClient)
if err != nil {
return nil, err
} else if active {
Expand All @@ -97,15 +118,15 @@ func getenvOrDefault(key, defaultV string) string {
}

func main() {
dkUrl := getenvOrDefault("DK_ENDPOINT_URL", "https://event.cloudnativedays.jp/api/")
dkUrl := getenvOrDefault("DK_ENDPOINT_URL", "https://event.cloudnativedays.jp/")
dkClient, err := dreamkast.NewClient(dkUrl)
if err != nil {
panic(err)
}

s := grpc.NewServer()
pb.RegisterExternalScalerServer(s, &ExternalScaler{
dkClient: dkClient,
defaultDkClient: dkClient,
})

lis, _ := net.Listen("tcp", ":6000")
Expand Down

0 comments on commit ecf3fcc

Please sign in to comment.