Skip to content

Commit

Permalink
go version
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcalleja committed Aug 2, 2022
1 parent 8890241 commit d9246e7
Show file tree
Hide file tree
Showing 13 changed files with 1,505 additions and 189 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
export VERSION=$(docker run --rm alpine/semver semver -c -i patch $(cat VERSION))
echo $VERSION > VERSION
echo ::debug ::relase version ${VERSION}
echo ::set-output name=tag_name::v${VERSION}
echo ::set-output name=tag_name::${VERSION}
- name: Update version
id: updatev
run: |
Expand All @@ -41,7 +41,7 @@ jobs:
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'content-type: application/json' \
--data '{
"tag_name": "${{ steps.release.outputs.tag_name }}",
"tag_name": "v${{ steps.release.outputs.tag_name }}",
"body": "docker pull rafaelcalleja/keda-upstream-deployment-scaler:${{ steps.release.outputs.tag_name }}",
"name": "Release ${{ steps.release.outputs.tag_name }}",
"draft": false,
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
GPBMetadata/
Externalscaler/
vendor/
.idea/

91 changes: 8 additions & 83 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,91 +1,16 @@
FROM composer as composer
FROM golang:1.18 as builder

FROM php:7.4-buster as grpc-base
WORKDIR /src

RUN apt-get -qq update && apt-get -qq install -y \
autoconf automake curl git libtool \
pkg-config unzip zlib1g-dev
COPY . .

ARG MAKEFLAGS=-j8
RUN go mod tidy && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o external-scaler cmd/main.go

WORKDIR /tmp

RUN curl -sSL https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/\
protoc-3.14.0-linux-x86_64.zip -o /tmp/protoc.zip && \
unzip -qq protoc.zip && \
cp /tmp/bin/protoc /usr/local/bin/protoc && \
cp -R /tmp/include /usr/local/
FROM scratch

WORKDIR /

WORKDIR /github/grpc

RUN git clone --depth 1 --branch v1.34.0 https://github.com/grpc/grpc . && \
git submodule update --init && \
cd third_party/protobuf && git submodule update --init

RUN make grpc_php_plugin

RUN pecl install grpc
RUN pecl install protobuf

FROM debian:buster as sprial

RUN apt-get -qq update && apt-get -qq install -y curl wget git

RUN wget https://github.com/spiral/php-grpc/releases/download/v1.4.1/protoc-gen-php-grpc-1.4.1-linux-amd64.tar.gz -O protoc-gen-php-grpc-1.4.1-linux-amd64.tar.gz && \
wget https://github.com/spiral/php-grpc/releases/download/v1.4.1/rr-grpc-1.4.1-linux-amd64.tar.gz -O rr-grpc-1.4.1-linux-amd64.tar.gz && \
tar xvfz protoc-gen-php-grpc-1.4.1-linux-amd64.tar.gz && \
tar xvfz rr-grpc-1.4.1-linux-amd64.tar.gz && \
cp protoc-gen-php-grpc-1.4.1-linux-amd64/protoc-gen-php-grpc /usr/local/bin/ && \
cp rr-grpc-1.4.1-linux-amd64/rr-grpc /usr/local/bin/

FROM php:7.4-buster

RUN apt-get -qq update && apt-get -qq install -y git unzip libzip-dev

COPY --from=composer /usr/bin/composer /usr/bin/composer

COPY --from=grpc-base /usr/local/bin/protoc /usr/local/bin/protoc

COPY --from=grpc-base /usr/local/include/google /usr/local/include/google

COPY --from=grpc-base /github/grpc/bins/opt/grpc_php_plugin /usr/local/bin/protoc-gen-grpc

COPY --from=grpc-base \
/usr/local/lib/php/extensions/no-debug-non-zts-20190902/grpc.so \
/usr/local/lib/php/extensions/no-debug-non-zts-20190902/grpc.so

COPY --from=grpc-base \
/usr/local/lib/php/extensions/no-debug-non-zts-20190902/protobuf.so \
/usr/local/lib/php/extensions/no-debug-non-zts-20190902/protobuf.so

COPY --from=sprial /usr/local/bin/protoc-gen-php-grpc /usr/local/bin/protoc-gen-php-grpc
COPY --from=sprial /usr/local/bin/rr-grpc /usr/local/bin/rr-grpc

RUN docker-php-ext-install zip
RUN docker-php-ext-enable grpc protobuf

ENV KUBECTL_VERSION=1.19.0
RUN cd /tmp && curl -sLf https://storage.googleapis.com/kubernetes-release/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl \
&& chmod +x /usr/local/bin/kubectl \
&& rm -rf /tmp/*

WORKDIR /var/www/html

COPY composer.json .
RUN composer install

COPY ./protos/externalscaler.proto .
RUN protoc --proto_path=./ --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/protoc-gen-php-grpc ./externalscaler.proto

COPY src/.rr.yaml ./
COPY src/worker.php .
COPY src/ExternalScaler.php .

RUN chmod 644 worker.php

EXPOSE 9001

CMD ["rr-grpc", "serve", "-v", "-d"]
#docker run -it --rm --net=host -v $(pwd):/app -w /app -p 8080:8080 fullstorydev/grpcui -plaintext -proto /app/protos/externalscaler.proto -import-path /app/protos/ localhost:9001
COPY --from=builder /src/external-scaler .

ENTRYPOINT ["/external-scaler"]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.28
0.1.0
135 changes: 135 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package main

import (
"context"
"fmt"
pb "github.com/rafaelcalleja/keda-upstream-deployment-scaler/externalscaler"
"log"
"net"
"os"
"strconv"

"time"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/jenkins-x/jx-kube-client/v3/pkg/kubeclient"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

type ExternalScaler struct{}

func (e *ExternalScaler) IsActive(ctx context.Context, scaledObject *pb.ScaledObjectRef) (*pb.IsActiveResponse, error) {
namespace := scaledObject.ScalerMetadata["upstreamDeploymentNamespace"]
deployment := scaledObject.ScalerMetadata["upstreamDeploymentName"]

if len(namespace) == 0 {
return nil, status.Error(codes.InvalidArgument, "upstreamDeploymentNamespace must be specified")
}

if len(deployment) == 0 {
return nil, status.Error(codes.InvalidArgument, "upstreamDeploymentName must be specified")
}

options, err := kubeClient.AppsV1().Deployments(namespace).GetScale(ctx, deployment, metav1.GetOptions{})
if err != nil {
return &pb.IsActiveResponse{
Result: false,
}, err
}

return &pb.IsActiveResponse{
Result: options.Spec.Replicas > 0,
}, nil
}

func (e *ExternalScaler) GetMetricSpec(context.Context, *pb.ScaledObjectRef) (*pb.GetMetricSpecResponse, error) {
return &pb.GetMetricSpecResponse{
MetricSpecs: []*pb.MetricSpec{{
MetricName: "targetSize",
TargetSize: 1,
}},
}, nil
}

func (e *ExternalScaler) GetMetrics(_ context.Context, metricRequest *pb.GetMetricsRequest) (*pb.GetMetricsResponse, error) {
replicaCount := metricRequest.ScaledObjectRef.ScalerMetadata["replicaCount"]

if len(replicaCount) == 0 {
return nil, status.Error(codes.InvalidArgument, "replicaCount must be specified")
}

replicaCountInt64, err := strconv.ParseInt(replicaCount, 10, 64)
if nil != err {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

return &pb.GetMetricsResponse{
MetricValues: []*pb.MetricValue{{
MetricName: "targetSize",
MetricValue: replicaCountInt64,
}},
}, nil
}

func (e *ExternalScaler) StreamIsActive(scaledObject *pb.ScaledObjectRef, epsServer pb.ExternalScaler_StreamIsActiveServer) error {
namespace := scaledObject.ScalerMetadata["upstreamDeploymentNamespace"]
deployment := scaledObject.ScalerMetadata["upstreamDeploymentName"]

if len(namespace) == 0 {
return status.Error(codes.InvalidArgument, "upstreamDeploymentNamespace must be specified")
}

if len(deployment) == 0 {
return status.Error(codes.InvalidArgument, "upstreamDeploymentName must be specified")
}

for {
select {
case <-epsServer.Context().Done():
// call cancelled
return nil
case <-time.Tick(time.Minute * 10):
options, err := kubeClient.AppsV1().Deployments(namespace).GetScale(context.Background(), deployment, metav1.GetOptions{})

if err != nil {
err = epsServer.Send(&pb.IsActiveResponse{
Result: false,
})
} else if options.Spec.Replicas > 0 {
err = epsServer.Send(&pb.IsActiveResponse{
Result: true,
})
}
}
}
}

var kubeClient *kubernetes.Clientset

func main() {
f := kubeclient.NewFactory()
cfg, err := f.CreateKubeConfig()
if err != nil {
log.Fatal(fmt.Sprintf("failed to get kubernetes config: %v", err))
os.Exit(1)
}

kubeClient, err = kubernetes.NewForConfig(cfg)
if err != nil {
log.Fatal(fmt.Sprintf("error building kubernetes clientset: %v", err))
os.Exit(1)
}

grpcServer := grpc.NewServer()
lis, _ := net.Listen("tcp", ":9001")
pb.RegisterExternalScalerServer(grpcServer, &ExternalScaler{})

fmt.Println("listenting on :9001")
if err := grpcServer.Serve(lis); err != nil {
log.Fatal(err)
}
}
13 changes: 0 additions & 13 deletions composer.json

This file was deleted.

Loading

0 comments on commit d9246e7

Please sign in to comment.