diff --git a/changelog/v0.5.16/loop-demo.yaml b/changelog/v0.5.16/loop-demo.yaml new file mode 100644 index 0000000..89fa3f4 --- /dev/null +++ b/changelog/v0.5.16/loop-demo.yaml @@ -0,0 +1,3 @@ +changelog: + - type: NON_USER_FACING + description: Add a version of the calculator (service2) that produces 500s when the sum is in the range 500-600. diff --git a/contrib/example/service2-500/Dockerfile b/contrib/example/service2-500/Dockerfile new file mode 100644 index 0000000..e33ab72 --- /dev/null +++ b/contrib/example/service2-500/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine:3.5 + +COPY service2ise /service2ise + +ENTRYPOINT ["/service2ise"] + +EXPOSE 8080 diff --git a/contrib/example/service2-500/Makefile b/contrib/example/service2-500/Makefile new file mode 100644 index 0000000..852be52 --- /dev/null +++ b/contrib/example/service2-500/Makefile @@ -0,0 +1,12 @@ +DOCKER_REPO ?= soloio +VERSION ?= dev + +.PHONY: build +build: + GOOS=linux CGO_ENABLED=0 go build -o service2ise -gcflags "-N -l" main.go + docker build -t $(DOCKER_REPO)/example-service2ise:$(VERSION) . + +.PHONY: push +push: build + docker push $(DOCKER_REPO)/example-service2ise:$(VERSION) + diff --git a/contrib/example/service2-500/main.go b/contrib/example/service2-500/main.go new file mode 100644 index 0000000..8a8f9c2 --- /dev/null +++ b/contrib/example/service2-500/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + + "github.com/solo-io/go-utils/contextutils" +) + +type Calculator struct { + Op1, Op2 int + IsAdd bool +} + +func main() { + http.HandleFunc("/calculate", calchandler) + + contextutils.LoggerFrom(context.TODO()).Fatal(http.ListenAndServe(":8080", nil)) +} + +func calchandler(w http.ResponseWriter, r *http.Request) { + var req Calculator + dec := json.NewDecoder(r.Body) + err := dec.Decode(&req) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + } + + isadd := req.IsAdd + op1 := req.Op1 + op2 := req.Op2 + sum, err := computeSum(isadd, op1, op2) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "%v", err) + } + + fmt.Fprintf(w, "%d", sum) +} + +func computeSum(isadd bool, op1, op2 int) (int, error) { + var val int + if isadd { + val = op1 + op2 + } else { + val = op1 - op2 + } + // Happy 5**th birthday Kubernetes! :D + if val >= 500 && val < 600 { + return 0, fmt.Errorf("result is a 500") + } + + return val, nil +} diff --git a/contrib/example/service2-500/service2.yml b/contrib/example/service2-500/service2.yml new file mode 100644 index 0000000..188b049 --- /dev/null +++ b/contrib/example/service2-500/service2.yml @@ -0,0 +1,33 @@ +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + name: example-service2 +spec: + replicas: 1 + selector: + matchLabels: + app: example-service2 + template: + metadata: + labels: + app: example-service2 + spec: + containers: + - name: example-service2 + image: soloio/example-service2ise:0.1.0 + ports: + - containerPort: 8080 + protocol: TCP +--- +kind: Service +apiVersion: v1 +metadata: + name: example-service2 +spec: + selector: + app: example-service2 + ports: + - name: http + protocol: TCP + port: 80 + targetPort: 8080