diff --git a/docs/eventing/samples/helloworld/helloworld-go/README.md b/docs/eventing/samples/helloworld/helloworld-go/README.md index 59edebc09..2f7ad7c6a 100644 --- a/docs/eventing/samples/helloworld/helloworld-go/README.md +++ b/docs/eventing/samples/helloworld/helloworld-go/README.md @@ -31,9 +31,7 @@ cd knative-docs/docs/eventing/samples/helloworld/helloworld-go ## Before you begin -- A Kubernetes cluster with - [Knative Eventing](../../../getting-started.md#installing-knative-eventing) - installed. +- A Kubernetes cluster with [Knative Eventing](../../../../admin/install) installed. - [Docker](https://www.docker.com) installed and running on your local machine, and a Docker Hub account configured (we'll use it for a container registry). @@ -42,105 +40,105 @@ cd knative-docs/docs/eventing/samples/helloworld/helloworld-go 1. Create a new file named `helloworld.go` and paste the following code. This code creates a basic web server which listens on port 8080: - ```go - import ( - "context" - "log" + ```go + import ( + "context" + "log" - cloudevents "github.com/cloudevents/sdk-go/v2" - "github.com/google/uuid" - ) + cloudevents "github.com/cloudevents/sdk-go/v2" + "github.com/google/uuid" + ) - func receive(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, cloudevents.Result) { - // Here is where your code to process the event will go. - // In this example we will log the event msg - log.Printf("Event received. \n%s\n", event) - data := &HelloWorld{} - if err := event.DataAs(data); err != nil { - log.Printf("Error while extracting cloudevent Data: %s\n", err.Error()) - return nil, cloudevents.NewHTTPResult(400, "failed to convert data: %s", err) + func receive(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, cloudevents.Result) { + // Here is where your code to process the event will go. + // In this example we will log the event msg + log.Printf("Event received. \n%s\n", event) + data := &HelloWorld{} + if err := event.DataAs(data); err != nil { + log.Printf("Error while extracting cloudevent Data: %s\n", err.Error()) + return nil, cloudevents.NewHTTPResult(400, "failed to convert data: %s", err) + } + log.Printf("Hello World Message from received event %q", data.Msg) + + // Respond with another event (optional) + // This is optional and is intended to show how to respond back with another event after processing. + // The response will go back into the knative eventing system just like any other event + newEvent := cloudevents.NewEvent() + newEvent.SetID(uuid.New().String()) + newEvent.SetSource("knative/eventing/samples/hello-world") + newEvent.SetType("dev.knative.samples.hifromknative") + if err := newEvent.SetData(cloudevents.ApplicationJSON, HiFromKnative{Msg: "Hi from helloworld-go app!"}); err != nil { + return nil, cloudevents.NewHTTPResult(500, "failed to set response data: %s", err) + } + log.Printf("Responding with event\n%s\n", newEvent) + return &newEvent, nil } - log.Printf("Hello World Message from received event %q", data.Msg) - // Respond with another event (optional) - // This is optional and is intended to show how to respond back with another event after processing. - // The response will go back into the knative eventing system just like any other event - newEvent := cloudevents.NewEvent() - newEvent.SetID(uuid.New().String()) - newEvent.SetSource("knative/eventing/samples/hello-world") - newEvent.SetType("dev.knative.samples.hifromknative") - if err := newEvent.SetData(cloudevents.ApplicationJSON, HiFromKnative{Msg: "Hi from helloworld-go app!"}); err != nil { - return nil, cloudevents.NewHTTPResult(500, "failed to set response data: %s", err) + func main() { + log.Print("Hello world sample started.") + c, err := cloudevents.NewDefaultClient() + if err != nil { + log.Fatalf("failed to create client, %v", err) + } + log.Fatal(c.StartReceiver(context.Background(), receive)) } - log.Printf("Responding with event\n%s\n", newEvent) - return &newEvent, nil - } - - func main() { - log.Print("Hello world sample started.") - c, err := cloudevents.NewDefaultClient() - if err != nil { - log.Fatalf("failed to create client, %v", err) - } - log.Fatal(c.StartReceiver(context.Background(), receive)) - } - ``` + ``` 1. Create a new file named `eventschemas.go` and paste the following code. This defines the data schema of the CloudEvents. - ```go - package main + ```go + package main - // HelloWorld defines the Data of CloudEvent with type=dev.knative.samples.helloworld - type HelloWorld struct { - // Msg holds the message from the event - Msg string `json:"msg,omitempty,string"` - } + // HelloWorld defines the Data of CloudEvent with type=dev.knative.samples.helloworld + type HelloWorld struct { + // Msg holds the message from the event + Msg string `json:"msg,omitempty,string"` + } - // HiFromKnative defines the Data of CloudEvent with type=dev.knative.samples.hifromknative - type HiFromKnative struct { - // Msg holds the message from the event - Msg string `json:"msg,omitempty,string"` - } - ``` + // HiFromKnative defines the Data of CloudEvent with type=dev.knative.samples.hifromknative + type HiFromKnative struct { + // Msg holds the message from the event + Msg string `json:"msg,omitempty,string"` + } + ``` 1. In your project directory, create a file named `Dockerfile` and copy the code block below into it. For detailed instructions on dockerizing a Go app, see [Deploying Go servers with Docker](https://blog.golang.org/docker). - ```docker - # Use the official Golang image to create a build artifact. - # This is based on Debian and sets the GOPATH to /go. - # https://hub.docker.com/_/golang - FROM golang:1.14 as builder + ```docker + # Use the official Golang image to create a build artifact. + # This is based on Debian and sets the GOPATH to /go. + # https://hub.docker.com/_/golang + FROM golang:1.14 as builder - # Copy local code to the container image. - WORKDIR /app + # Copy local code to the container image. + WORKDIR /app - # Retrieve application dependencies using go modules. - # Allows container builds to reuse downloaded dependencies. - COPY go.* ./ - RUN go mod download + # Retrieve application dependencies using go modules. + # Allows container builds to reuse downloaded dependencies. + COPY go.* ./ + RUN go mod download - # Copy local code to the container image. - COPY . ./ + # Copy local code to the container image. + COPY . ./ - # Build the binary. - # -mod=readonly ensures immutable go.mod and go.sum in container builds. - RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o helloworld + # Build the binary. + # -mod=readonly ensures immutable go.mod and go.sum in container builds. + RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o helloworld - # Use a Docker multi-stage build to create a lean production image. - # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds - FROM alpine:3 - RUN apk add --no-cache ca-certificates + # Use a Docker multi-stage build to create a lean production image. + # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds + FROM alpine:3 + RUN apk add --no-cache ca-certificates - # Copy the binary to the production image from the builder stage. - COPY --from=builder /app/helloworld /helloworld + # Copy the binary to the production image from the builder stage. + COPY --from=builder /app/helloworld /helloworld - # Run the web service on container startup. - CMD ["/helloworld"] - ``` + # Run the web service on container startup. + CMD ["/helloworld"] + ``` 1. Create a new file, `sample-app.yaml` and copy the following service definition into the file. Make sure to replace `{username}` with your Docker @@ -218,9 +216,9 @@ cd knative-docs/docs/eventing/samples/helloworld/helloworld-go 1. Use the go tool to create a [`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest. - ```shell - go mod init github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go - ``` + ```shell + go mod init github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go + ``` ## Building and deploying the sample @@ -231,25 +229,24 @@ folder) you're ready to build and deploy the sample app. Docker Hub, run these commands replacing `{username}` with your Docker Hub username: - ```shell - # Build the container on your local machine - docker build -t {username}/helloworld-go . + ```shell + # Build the container on your local machine + docker build -t {username}/helloworld-go . - # Push the container to docker registry - docker push {username}/helloworld-go - ``` + # Push the container to docker registry + docker push {username}/helloworld-go + ``` 1. After the build has completed and the container is pushed to docker hub, you can deploy the sample application into your cluster. Ensure that the container image value in `sample-app.yaml` matches the container you built in the previous step. Apply the configuration using `kubectl`: - ```shell - kubectl apply --filename sample-app.yaml - ``` + ```shell + kubectl apply --filename sample-app.yaml + ``` - 1. Above command created a namespace `knative-samples` and create a default - Broker it. Verify using the following command: +1. Above command created a namespace `knative-samples` and create a default Broker it. Verify using the following command: ```shell kubectl get broker --namespace knative-samples @@ -258,9 +255,9 @@ folder) you're ready to build and deploy the sample app. **Note:** you can also use injection based on labels with the Eventing sugar controller. For how to install the Eventing sugar controller, see - [Install optional Eventing extensions](../../../../install/install-extensions.md#install-optional-eventing-extensions). + [Install optional Eventing extensions](../../../../admin/install/install-extensions#install-optional-eventing-extensions). - 1. It deployed the helloworld-go app as a K8s Deployment and created a K8s +1. It deployed the helloworld-go app as a K8s Deployment and created a K8s service names helloworld-go. Verify using the following command. ```shell @@ -269,8 +266,9 @@ folder) you're ready to build and deploy the sample app. kubectl --namespace knative-samples get svc helloworld-go ``` - 1. It created a Knative Eventing Trigger to route certain events to the +1. It created a Knative Eventing Trigger to route certain events to the helloworld-go application. Make sure that Ready=true + ```shell kubectl --namespace knative-samples get trigger helloworld-go ``` @@ -286,28 +284,31 @@ We can send an http request directly to the [Broker](../../../broker/) with correct CloudEvent headers set. 1. Deploy a curl pod and SSH into it - ```shell - kubectl --namespace knative-samples run curl --image=radial/busyboxplus:curl -it - ``` + + ```bash + kubectl -n knative-samples run curl --image=radial/busyboxplus:curl -it + ``` + 1. Get the Broker URL - ```shell - kubectl --namespace knative-samples get broker default - ``` + + ```bash + kubectl -n knative-samples get broker default + ``` + 1. Run the following in the SSH terminal. Please replace the URL with the URL of the default broker. - ```shell - curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/knative-samples/default" \ - -X POST \ - -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" \ - -H "Ce-Specversion: 1.0" \ - -H "Ce-Type: dev.knative.samples.helloworld" \ - -H "Ce-Source: dev.knative.samples/helloworldsource" \ - -H "Content-Type: application/json" \ - -d '{"msg":"Hello World from the curl pod."}' - - exit - ``` + ```bash + curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/knative-samples/default" \ + -X POST \ + -H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" \ + -H "Ce-Specversion: 1.0" \ + -H "Ce-Type: dev.knative.samples.helloworld" \ + -H "Ce-Source: dev.knative.samples/helloworldsource" \ + -H "Content-Type: application/json" \ + -d '{"msg":"Hello World from the curl pod."}' + exit + ``` ### Verify that event is received by helloworld-go app @@ -316,13 +317,13 @@ back with another event. 1. Display helloworld-go app logs - ```shell + ```bash kubectl --namespace knative-samples logs -l app=helloworld-go --tail=50 ``` You should see something similar to: - ```shell + ```bash Event received. Validation: valid Context Attributes, @@ -353,9 +354,7 @@ back with another event. ``` - Play around with the CloudEvent attributes in the curl command and the - trigger specification to understand how - [Triggers work](../../../broker/README.md#trigger). +Play around with the CloudEvent attributes in the curl command and the trigger specification to [understand how triggers work](../../../broker/triggers). ## Verify reply from helloworld-go app @@ -366,8 +365,8 @@ mesh via the Broker and can be delivered to other services using a Trigger 1. Deploy a pod that receives any CloudEvent and logs the event to its output. - ```shell - kubectl --namespace knative-samples apply --filename - << END + ```yaml + kubectl -n knative-samples apply -f - < - **Note:** Currently, [Ambassador](https://github.com/datawire/ambassador) is unsupported for use with Auto TLS. -- [cert-manager version `1.0.0` and higher](./installing-cert-manager.md). -- Your Knative cluster must be configured to use a - [custom domain](./using-a-custom-domain.md). -- Your DNS provider must be setup and configured to your domain. -- If you want to use HTTP-01 challenge, you need to configure your custom -domain to map to the IP of ingress. You can achieve this by adding a DNS A record to map the domain to the IP according to the instructions of your DNS provider. - ## Enabling Auto TLS -To enable support for Auto TLS in Knative: - -### Create cert-manager ClusterIssuer - -1. Create and add the `ClusterIssuer` configuration file to your Knative cluster -to define who issues the TLS certificates, how requests are validated, +1. Create and add the `ClusterIssuer` configuration file to your Knative cluster to define who issues the TLS certificates, how requests are validated, and which DNS provider validates those requests. - #### ClusterIssuer for DNS-01 challenge + ### ClusterIssuer for DNS-01 challenge Use the cert-manager reference to determine how to configure your `ClusterIssuer` file: @@ -78,7 +64,7 @@ and which DNS provider validates those requests. the Let's Encrypt account info, required `DNS-01` challenge type, and Cloud DNS provider info defined. For the complete Google Cloud DNS example, see - [Configuring HTTPS with cert-manager and Google Cloud DNS](./using-cert-manager-on-gcp.md). + [Configuring HTTPS with cert-manager and Google Cloud DNS](./using-cert-manager-on-gcp). ```shell apiVersion: cert-manager.io/v1 @@ -106,11 +92,11 @@ and which DNS provider validates those requests. key: key.json ``` - #### ClusterIssuer for HTTP-01 challenge + ### ClusterIssuer for HTTP-01 challenge Run the following command to apply the ClusterIssuer for HTT01 challenge: - ```shell + ```yaml kubectl apply -f - < --output yaml + ```bash + kubectl get clusterissuer -o yaml ``` Result: The `Status.Conditions` should include `Ready=True`. diff --git a/docs/serving/using-subroutes.md b/docs/serving/using-subroutes.md index 94e6bdc45..971fa1896 100644 --- a/docs/serving/using-subroutes.md +++ b/docs/serving/using-subroutes.md @@ -1,9 +1,3 @@ ---- -title: "Creating and using Subroutes" -weight: 20 -type: "docs" ---- - # Creating and using Subroutes Subroutes are most effective when used with multiple revisions. When defining a Knative service/route, the traffic section of the spec can split between the different revisions. For example: @@ -37,4 +31,4 @@ traffic: In the above example, you can access the staging target by accessing `staging-..`. The targets for `bar` and `baz` can only be accessed using the main route, `..`. -When a traffic target gets tagged, a new Kubernetes service is created for it so that other services can also access it within the cluster. From the above example, a new Kubernetes service called `staging-` will be created in the same namespace. This service has the ability to override the visibility of this specific route by applying the label `networking.knative.dev/visibility` with value `cluster-local`. See [cluster local routes](./cluster-local-route.md#label-a-service-to-be-cluster-local) for more information about how to restrict visibility on the specific route. +When a traffic target gets tagged, a new Kubernetes service is created for it so that other services can also access it within the cluster. From the above example, a new Kubernetes service called `staging-` will be created in the same namespace. This service has the ability to override the visibility of this specific route by applying the label `networking.knative.dev/visibility` with value `cluster-local`. See the documentation on [private services](../../../developer/serving/services/private-services) for more information about how to restrict visibility on the specific route. diff --git a/mkdocs.yml b/mkdocs.yml index e0372cb7a..bb2b674a5 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -82,6 +82,7 @@ nav: - Configuring scale bounds: serving/autoscaling/scale-bounds.md - Additional autoscaling configuration for Knative Pod Autoscaler: serving/autoscaling/kpa-specific.md - Autoscale Sample App - Go: serving/autoscaling/autoscale-go/index.md + # Administrator topics - Administrator Topics: - Deployment Configuration: serving/services/deployment.md - Kubernetes services: serving/knative-kubernetes-services.md @@ -149,6 +150,7 @@ nav: - Create a SinkBinding object: eventing/sources/sinkbinding/getting-started.md - SinkBinding reference: eventing/sources/sinkbinding/reference.md - Camel source: eventing/sources/apache-camel-source/README.md + - Kafka source: eventing/sources/kafka-source/README.md - Creating an event source: - Overview: eventing/sources/creating-event-sources/README.md - Writing an event source using Javascript: eventing/sources/creating-event-sources/writing-event-source-easy-way/README.md @@ -207,7 +209,6 @@ nav: - Overview: eventing/samples/kafka/README.md - Binding Example: eventing/samples/kafka/binding/index.md - Channel Example: eventing/samples/kafka/channel/README.md - - Source Example: eventing/sources/kafka-source/README.md - Parallel: - Overview: eventing/samples/parallel/README.md - Multiple Cases: eventing/samples/parallel/multiple-branches/README.md