diff --git a/example/server/deployments/argocd.yaml b/example/server1/deployments/argocd.yaml similarity index 80% rename from example/server/deployments/argocd.yaml rename to example/server1/deployments/argocd.yaml index 2449632..7544f48 100644 --- a/example/server/deployments/argocd.yaml +++ b/example/server1/deployments/argocd.yaml @@ -1,7 +1,7 @@ apiVersion: argoproj.io/v1alpha1 kind: Application metadata: - name: xds-grpc-server-example + name: xds-grpc-server-example-1 namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.io @@ -11,7 +11,7 @@ spec: server: "https://kubernetes.default.svc" project: xds-control-plane source: - path: example/server/deployments/helm/xds-grpc-server-example + path: example/server1/deployments/helm/xds-grpc-server-example repoURL: https://github.com/mohammadVatandoost/xds-control-plane targetRevision: HEAD helm: diff --git a/example/server/deployments/helm/xds-grpc-server-example/.helmignore b/example/server1/deployments/helm/xds-grpc-server-example/.helmignore similarity index 100% rename from example/server/deployments/helm/xds-grpc-server-example/.helmignore rename to example/server1/deployments/helm/xds-grpc-server-example/.helmignore diff --git a/example/server/deployments/helm/xds-grpc-server-example/Chart.lock b/example/server1/deployments/helm/xds-grpc-server-example/Chart.lock similarity index 100% rename from example/server/deployments/helm/xds-grpc-server-example/Chart.lock rename to example/server1/deployments/helm/xds-grpc-server-example/Chart.lock diff --git a/example/server/deployments/helm/xds-grpc-server-example/Chart.yaml b/example/server1/deployments/helm/xds-grpc-server-example/Chart.yaml similarity index 79% rename from example/server/deployments/helm/xds-grpc-server-example/Chart.yaml rename to example/server1/deployments/helm/xds-grpc-server-example/Chart.yaml index c3b1fb3..c8113c6 100644 --- a/example/server/deployments/helm/xds-grpc-server-example/Chart.yaml +++ b/example/server1/deployments/helm/xds-grpc-server-example/Chart.yaml @@ -1,7 +1,7 @@ apiVersion: v2 # Name of the chart, aka service -name: xds-grpc-server-example -description: xds-grpc-server-example service +name: xds-grpc-server-example-1 +description: xds-grpc-server-example-1 service # Version of the chart version: 0.1.0 # This should be equal to your service image version. diff --git a/example/server/deployments/helm/xds-grpc-server-example/templates/NOTES.txt b/example/server1/deployments/helm/xds-grpc-server-example/templates/NOTES.txt similarity index 100% rename from example/server/deployments/helm/xds-grpc-server-example/templates/NOTES.txt rename to example/server1/deployments/helm/xds-grpc-server-example/templates/NOTES.txt diff --git a/example/server/deployments/helm/xds-grpc-server-example/templates/_helpers.tpl b/example/server1/deployments/helm/xds-grpc-server-example/templates/_helpers.tpl similarity index 100% rename from example/server/deployments/helm/xds-grpc-server-example/templates/_helpers.tpl rename to example/server1/deployments/helm/xds-grpc-server-example/templates/_helpers.tpl diff --git a/example/server/deployments/helm/xds-grpc-server-example/templates/configmaps.yaml b/example/server1/deployments/helm/xds-grpc-server-example/templates/configmaps.yaml similarity index 100% rename from example/server/deployments/helm/xds-grpc-server-example/templates/configmaps.yaml rename to example/server1/deployments/helm/xds-grpc-server-example/templates/configmaps.yaml diff --git a/example/server/deployments/helm/xds-grpc-server-example/templates/deployment.yaml b/example/server1/deployments/helm/xds-grpc-server-example/templates/deployment.yaml similarity index 100% rename from example/server/deployments/helm/xds-grpc-server-example/templates/deployment.yaml rename to example/server1/deployments/helm/xds-grpc-server-example/templates/deployment.yaml diff --git a/example/server/deployments/helm/xds-grpc-server-example/templates/service.yaml b/example/server1/deployments/helm/xds-grpc-server-example/templates/service.yaml similarity index 100% rename from example/server/deployments/helm/xds-grpc-server-example/templates/service.yaml rename to example/server1/deployments/helm/xds-grpc-server-example/templates/service.yaml diff --git a/example/server/deployments/helm/xds-grpc-server-example/values.yaml b/example/server1/deployments/helm/xds-grpc-server-example/values.yaml similarity index 96% rename from example/server/deployments/helm/xds-grpc-server-example/values.yaml rename to example/server1/deployments/helm/xds-grpc-server-example/values.yaml index 306c076..3c7d176 100644 --- a/example/server/deployments/helm/xds-grpc-server-example/values.yaml +++ b/example/server1/deployments/helm/xds-grpc-server-example/values.yaml @@ -16,7 +16,7 @@ global: imagePullSecrets: [name: registry-secret, name: mvatandoost] serverExample: - imageName: "cp-example-server" + imageName: "cp-example-server-1" nameOverride: "" diff --git a/example/server/main.go b/example/server1/main.go similarity index 100% rename from example/server/main.go rename to example/server1/main.go diff --git a/example/server2/deployments/argocd.yaml b/example/server2/deployments/argocd.yaml new file mode 100644 index 0000000..96c92e6 --- /dev/null +++ b/example/server2/deployments/argocd.yaml @@ -0,0 +1,19 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: xds-grpc-server-example-2 + namespace: argocd + finalizers: + - resources-finalizer.argocd.argoproj.io +spec: + destination: + namespace: test + server: "https://kubernetes.default.svc" + project: xds-control-plane + source: + path: example/server2/deployments/helm/xds-grpc-server-example + repoURL: https://github.com/mohammadVatandoost/xds-control-plane + targetRevision: HEAD + helm: + valueFiles: + - values.yaml diff --git a/example/server2/deployments/helm/xds-grpc-server-example/.helmignore b/example/server2/deployments/helm/xds-grpc-server-example/.helmignore new file mode 100644 index 0000000..f7e5d4c --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/.helmignore @@ -0,0 +1,24 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ +.charts/ diff --git a/example/server2/deployments/helm/xds-grpc-server-example/Chart.lock b/example/server2/deployments/helm/xds-grpc-server-example/Chart.lock new file mode 100644 index 0000000..cf59876 --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/Chart.lock @@ -0,0 +1,6 @@ +dependencies: +- name: common + repository: s3://helm-charts/divar + version: 0.1.0 +digest: sha256:b6175141b45595a2c62f2a1e38f47d9a7bcf37a845f2ba3f9fdc64da92618118 +generated: "2022-03-02T09:22:26.132202454+03:30" diff --git a/example/server2/deployments/helm/xds-grpc-server-example/Chart.yaml b/example/server2/deployments/helm/xds-grpc-server-example/Chart.yaml new file mode 100644 index 0000000..887e3c0 --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/Chart.yaml @@ -0,0 +1,13 @@ +apiVersion: v2 +# Name of the chart, aka service +name: xds-grpc-server-example-2 +description: xds-grpc-server-example-2 service +# Version of the chart +version: 0.1.0 +# This should be equal to your service image version. +appVersion: 0.1.0 +type: application +# dependencies: +# - name: common +# version: 0.1.0 +# repository: https://mohammadVatandoost.github.io/helm-chart/ \ No newline at end of file diff --git a/example/server2/deployments/helm/xds-grpc-server-example/templates/NOTES.txt b/example/server2/deployments/helm/xds-grpc-server-example/templates/NOTES.txt new file mode 100644 index 0000000..c6aef4e --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/templates/NOTES.txt @@ -0,0 +1 @@ +Successful \ No newline at end of file diff --git a/example/server2/deployments/helm/xds-grpc-server-example/templates/_helpers.tpl b/example/server2/deployments/helm/xds-grpc-server-example/templates/_helpers.tpl new file mode 100644 index 0000000..9b0b3c3 --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "MY_CHART.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "MY_CHART.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "MY_CHART.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "MY_CHART.labels" -}} +helm.sh/chart: {{ include "MY_CHART.chart" . }} +{{ include "MY_CHART.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "MY_CHART.selectorLabels" -}} +name: {{ .Release.Name }} +app: {{ .Chart.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "MY_CHART.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "MY_CHART.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/example/server2/deployments/helm/xds-grpc-server-example/templates/configmaps.yaml b/example/server2/deployments/helm/xds-grpc-server-example/templates/configmaps.yaml new file mode 100644 index 0000000..dbbe030 --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/templates/configmaps.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name }}-config +data: + {{- range $key,$val := .Values.configMap.data }} + {{ $key }}: {{ $val | quote }} + {{- end }} diff --git a/example/server2/deployments/helm/xds-grpc-server-example/templates/deployment.yaml b/example/server2/deployments/helm/xds-grpc-server-example/templates/deployment.yaml new file mode 100644 index 0000000..132220c --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/templates/deployment.yaml @@ -0,0 +1,52 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ .Release.Name }} + labels: + {{- include "MY_CHART.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "MY_CHART.selectorLabels" . | nindent 6 }} + template: + metadata: + annotations: + rollme: {{ randAlphaNum 5 | quote }} + {{- with .Values.podAnnotations }} + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "MY_CHART.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.global.image.registry }}/{{.Values.serverExample.imageName}}:{{ .Values.global.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.global.image.pullPolicy }} + ports: + {{- range .Values.service.ports }} + - containerPort: {{ .port }} + name: {{ .name }} + protocol: {{ .protocol }} + {{- end }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + envFrom: + - configMapRef: + name: {{ .Release.Name }}-config + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/example/server2/deployments/helm/xds-grpc-server-example/templates/service.yaml b/example/server2/deployments/helm/xds-grpc-server-example/templates/service.yaml new file mode 100644 index 0000000..65289eb --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/templates/service.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Release.Name }}-headless + labels: + {{- range $key,$val := .Values.service.labels }} + {{ $key }}: {{ $val | quote }} + {{- end }} +spec: + {{- if eq .Values.service.headless true }} + clusterIP: None + {{- end}} + ports: + {{- range .Values.service.ports }} + - port: {{ .port }} + name: {{ .name }} + targetPort: {{ .targetPort }} + protocol: {{ .protocol }} + {{- end }} + selector: + name: {{ .Release.Name }} diff --git a/example/server2/deployments/helm/xds-grpc-server-example/values.yaml b/example/server2/deployments/helm/xds-grpc-server-example/values.yaml new file mode 100644 index 0000000..221c5f2 --- /dev/null +++ b/example/server2/deployments/helm/xds-grpc-server-example/values.yaml @@ -0,0 +1,60 @@ +# Default values for MY_CHART. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 2 + + +global: + image: + # -- Default registry for all control plane Images + registry: "mvatandoost" + # -- The default tag for all images, which itself defaults to .Chart.AppVersion + tag: 886210 + pullPolicy: IfNotPresent + # -- Add `imagePullSecrets` to all the service accounts used for components + imagePullSecrets: [name: registry-secret, name: mvatandoost] + +serverExample: + imageName: "cp-example-server-2" + + +nameOverride: "" +fullnameOverride: "" + + +podAnnotations: {} + +service: + labels: + monitoring-app: xds-apps + xds/portName: grpc + type: clusterIP + headless: true + ports: + - name: grpc + port: 8888 + targetPort: 8888 + protocol: TCP + - name: metrics + port: 9000 + targetPort: 9000 + protocol: TCP + + +resources: + limits: + cpu: 200m + memory: 100Mi + requests: + cpu: 200m + memory: 100Mi + +configMap: + data: + METRIC_LISTENPORT: 9000 + REST_LISTENPORT: 8080 + LOGGER_LEVEL: "info" + GRPC_LISTENPORT: 8888 + GRPC_TIMEOUT: 5 + XDS_PORT: 5678 diff --git a/example/server2/main.go b/example/server2/main.go new file mode 100644 index 0000000..d1f2012 --- /dev/null +++ b/example/server2/main.go @@ -0,0 +1,114 @@ +package main + +import ( + "math/rand" + "net" + "net/http" + "strconv" + "strings" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/status" + + "github.com/mohammadVatandoost/interfaces/golang/echo" + "github.com/sirupsen/logrus" + "github.com/spf13/viper" + "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/health" + healthpb "google.golang.org/grpc/health/grpc_health_v1" +) + +type Config struct { + GRPCPort int + ServerName string +} + +var ( + hs *health.Server + conn *grpc.ClientConn +) + +type server struct { + ServerName string +} + +func isGrpcRequest(r *http.Request) bool { + return r.ProtoMajor == 2 && strings.HasPrefix(r.Header.Get("Content-Type"), "application/grpc") +} + +func (s *server) SayHello(ctx context.Context, in *echo.EchoRequest) (*echo.EchoReply, error) { + + logrus.Println("Got rpc: --> ", in.Name) + + return &echo.EchoReply{Message: "Hello " + in.Name + " from " + s.ServerName}, nil +} + +func (s *server) SayHelloStream(in *echo.EchoRequest, stream echo.EchoServer_SayHelloStreamServer) error { + + logrus.Println("Got stream: --> ") + stream.Send(&echo.EchoReply{Message: "Hello " + in.Name}) + stream.Send(&echo.EchoReply{Message: "Hello " + in.Name}) + + return nil +} + +type healthServer struct{} + +func (s *healthServer) Check(ctx context.Context, in *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) { + logrus.Printf("Handling grpc Check request") + return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_SERVING}, nil +} + +func (s *healthServer) Watch(in *healthpb.HealthCheckRequest, srv healthpb.Health_WatchServer) error { + return status.Error(codes.Unimplemented, "Watch is not implemented") +} + +func main() { + + // Setting defaults for this application + + viper.SetDefault("GRPCPort", 8888) + + viper.SetDefault("ServerName", "server1") + + // Read Config from ENV + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viper.AutomaticEnv() + + logrus.SetFormatter(&logrus.JSONFormatter{ + FieldMap: logrus.FieldMap{ + logrus.FieldKeyTime: "@timestamp", + logrus.FieldKeyMsg: "message", + }, + }) + logrus.SetLevel(logrus.TraceLevel) + + var config Config + + err := viper.Unmarshal(&config) + if err != nil { + logrus.Fatalf("failed to read configs: %v", err) + } + + lis, err := net.Listen("tcp", ":"+strconv.Itoa(config.GRPCPort)) + if err != nil { + logrus.Fatalf("failed to listen: %v", err) + } + + creds := insecure.NewCredentials() + sopts := []grpc.ServerOption{grpc.MaxConcurrentStreams(10), grpc.Creds(creds)} + // s := xds.NewGRPCServer(sopts...) + s := grpc.NewServer(sopts...) + // Generate a random number between 0 and 100. + randomNumber := rand.Intn(101) + name := config.ServerName + "-" + strconv.Itoa(randomNumber) + + echo.RegisterEchoServerServer(s, &server{ServerName: name}) + + healthpb.RegisterHealthServer(s, &healthServer{}) + logrus.Infof("Starting grpcServer on Port: %v with servername: %v", config.GRPCPort, name) + s.Serve(lis) + +} diff --git a/mk/build.mk b/mk/build.mk index 5814877..6c8a29d 100644 --- a/mk/build.mk +++ b/mk/build.mk @@ -59,9 +59,13 @@ build/artifacts-$(1)-$(2)/control-plane: build/artifacts-$(1)-$(2)/cp-example-client: $(Build_Go_Application) ./example/client -.PHONY: build/artifacts-$(1)-$(2)/cp-example-server -build/artifacts-$(1)-$(2)/cp-example-server: - $(Build_Go_Application) ./example/server +.PHONY: build/artifacts-$(1)-$(2)/cp-example-server-1 +build/artifacts-$(1)-$(2)/cp-example-server-1: + $(Build_Go_Application) ./example/server1 + +.PHONY: build/artifacts-$(1)-$(2)/cp-example-server-2 +build/artifacts-$(1)-$(2)/cp-example-server-2: + $(Build_Go_Application) ./example/server2 endef $(foreach goos,$(SUPPORTED_GOOSES),$(foreach goarch,$(SUPPORTED_GOARCHES),$(eval $(call BUILD_TARGET,$(goos),$(goarch))))) diff --git a/mk/kind.mk b/mk/kind.mk index 1135718..7c904af 100644 --- a/mk/kind.mk +++ b/mk/kind.mk @@ -82,7 +82,11 @@ kind/deploy/control-plane: kind/load KUBECONFIG=$(KIND_KUBECONFIG) helm upgrade --install --namespace $(EXAMPLE_NAMESPACE) --create-namespace \ --set global.image.registry="$(DOCKER_REGISTRY)" \ --set global.image.tag="$(BUILD_INFO_VERSION)" \ - xds-grpc-server-example ./example/server/deployments/helm/xds-grpc-server-example + xds-grpc-server-example-1 ./example/server1/deployments/helm/xds-grpc-server-example + KUBECONFIG=$(KIND_KUBECONFIG) helm upgrade --install --namespace $(EXAMPLE_NAMESPACE) --create-namespace \ + --set global.image.registry="$(DOCKER_REGISTRY)" \ + --set global.image.tag="$(BUILD_INFO_VERSION)" \ + xds-grpc-server-example-2 ./example/server2/deployments/helm/xds-grpc-server-example .PHONY: kind/delete/control-plane diff --git a/mk/microk8s.mk b/mk/microk8s.mk index 24d323d..faf9635 100644 --- a/mk/microk8s.mk +++ b/mk/microk8s.mk @@ -12,7 +12,11 @@ microk8s/deploy/control-plane: microk8s/load microk8s helm upgrade --install --namespace $(EXAMPLE_NAMESPACE) --create-namespace \ --set global.image.registry="$(DOCKER_REGISTRY)" \ --set global.image.tag="$(BUILD_INFO_VERSION)" \ - xds-grpc-server-example ./example/server/deployments/helm/xds-grpc-server-example + xds-grpc-server-example-1 ./example/server1/deployments/helm/xds-grpc-server-example + microk8s helm upgrade --install --namespace $(EXAMPLE_NAMESPACE) --create-namespace \ + --set global.image.registry="$(DOCKER_REGISTRY)" \ + --set global.image.tag="$(BUILD_INFO_VERSION)" \ + xds-grpc-server-example-2 ./example/server2/deployments/helm/xds-grpc-server-example .PHONY: microk8s/load/images microk8s/load/images: