-
Notifications
You must be signed in to change notification settings - Fork 630
Channel CRDs #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Channel CRDs #66
Changes from all commits
5085643
b2b526c
c819788
0cac79b
9bf905c
d18efa6
e2d423a
ad3970d
04da06a
1069cda
dcbab6a
e39fd26
efd068e
33866d6
4b5eb8f
c9118fa
9f3fa72
20d1088
de07ce8
a45911d
0972a93
9262da3
ac6c8ff
41887ee
0695c25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Copyright 2018 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "flag" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/go-martini/martini" | ||
| "github.com/golang/glog" | ||
| channelsv1alpha1 "github.com/knative/eventing/pkg/apis/channels/v1alpha1" | ||
| clientset "github.com/knative/eventing/pkg/client/clientset/versioned" | ||
| informers "github.com/knative/eventing/pkg/client/informers/externalversions" | ||
| "github.com/knative/eventing/pkg/signals" | ||
| "github.com/knative/eventing/pkg/subscription" | ||
| "k8s.io/client-go/tools/clientcmd" | ||
| ) | ||
|
|
||
| var ( | ||
| masterURL string | ||
| kubeconfig string | ||
|
|
||
| bus = os.Getenv("BUS_NAME") | ||
| forwardHeaders = []string{ | ||
| "content-type", | ||
| "x-request-id", | ||
| "x-b3-traceid", | ||
| "x-b3-spanid", | ||
| "x-b3-parentspanid", | ||
| "x-b3-sampled", | ||
| "x-b3-flags", | ||
| "x-ot-span-context", | ||
| } | ||
| ) | ||
|
|
||
| func splitChannelName(host string) (string, string) { | ||
| chunks := strings.Split(host, ".") | ||
| channel := chunks[0] | ||
| namespace := chunks[1] | ||
| return channel, namespace | ||
| } | ||
|
|
||
| func main() { | ||
| flag.Parse() | ||
|
|
||
| // set up signals so we handle the first shutdown signal gracefully | ||
| stopCh := signals.SetupSignalHandler() | ||
|
|
||
| cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfig) | ||
| if err != nil { | ||
| glog.Fatalf("Error building kubeconfig: %s", err.Error()) | ||
| } | ||
|
|
||
| client, err := clientset.NewForConfig(cfg) | ||
| if err != nil { | ||
| glog.Fatalf("Error building clientset: %s", err.Error()) | ||
| } | ||
|
|
||
| informerFactory := informers.NewSharedInformerFactory(client, time.Second*30) | ||
| monitor := subscription.NewMonitor(bus, informerFactory, subscription.MonitorEventHandlerFuncs{ | ||
| ProvisionFunc: func(channel channelsv1alpha1.Channel) { | ||
| glog.Infof("Provision channel %q\n", channel.Name) | ||
| }, | ||
| UnprovisionFunc: func(channel channelsv1alpha1.Channel) { | ||
| glog.Infof("Unprovision channel %q\n", channel.Name) | ||
| }, | ||
| SubscribeFunc: func(subscription channelsv1alpha1.Subscription) { | ||
| glog.Infof("Subscribe %q to %q channel\n", subscription.Spec.Subscriber, subscription.Spec.Channel) | ||
| }, | ||
| UnsubscribeFunc: func(subscription channelsv1alpha1.Subscription) { | ||
| glog.Infof("Unubscribe %q from %q channel\n", subscription.Spec.Subscriber, subscription.Spec.Channel) | ||
| }, | ||
| }) | ||
| go informerFactory.Start(stopCh) | ||
|
|
||
| m := createServer(monitor) | ||
| m.Run() | ||
|
|
||
| glog.Flush() | ||
| } | ||
|
|
||
| func createServer(monitor *subscription.Monitor) *martini.ClassicMartini { | ||
| m := martini.Classic() | ||
|
|
||
| m.Post("/", func(req *http.Request, res http.ResponseWriter) { | ||
| host := req.Host | ||
| glog.Infof("Recieved request for %s\n", host) | ||
| channel, namespace := splitChannelName(host) | ||
| subscriptions := monitor.Subscriptions(channel, namespace) | ||
| if subscriptions == nil { | ||
| res.WriteHeader(http.StatusNotFound) | ||
| return | ||
| } | ||
|
|
||
| body, err := ioutil.ReadAll(req.Body) | ||
| if err != nil { | ||
| res.WriteHeader(http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| res.WriteHeader(http.StatusAccepted) | ||
| go func() { | ||
| if len(*subscriptions) == 0 { | ||
| glog.Warningf("No subscribers for channel %q\n", channel) | ||
| } | ||
|
|
||
| // make upstream requests | ||
| client := &http.Client{} | ||
|
|
||
| for _, subscription := range *subscriptions { | ||
| go func(subscriber string) { | ||
| glog.Infof("Sending to %q for %q\n", subscriber, channel) | ||
|
|
||
| url := fmt.Sprintf("http://%s/", subscriber) | ||
| request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body)) | ||
| if err != nil { | ||
| glog.Errorf("Unable to create subscriber request %v", err) | ||
| } | ||
| request.Header.Set("x-bus", bus) | ||
| request.Header.Set("x-channel", channel) | ||
| for _, header := range forwardHeaders { | ||
| if value := req.Header.Get(header); value != "" { | ||
| request.Header.Set(header, value) | ||
| } | ||
| } | ||
| _, err = client.Do(request) | ||
| if err != nil { | ||
| glog.Errorf("Unable to complete subscriber request %v", err) | ||
| } | ||
| }(subscription.Subscriber) | ||
| } | ||
| }() | ||
| }) | ||
|
|
||
| return m | ||
| } | ||
|
|
||
| func init() { | ||
| flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") | ||
| flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2018 Google, Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| apiVersion: apiextensions.k8s.io/v1beta1 | ||
| kind: CustomResourceDefinition | ||
| metadata: | ||
| name: buses.channels.knative.dev | ||
| spec: | ||
| scope: Namespaced | ||
| group: channels.knative.dev | ||
| version: v1alpha1 | ||
| names: | ||
| kind: Bus | ||
| plural: buses | ||
| singular: bus |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2018 Google, Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| apiVersion: channels.knative.dev/v1alpha1 | ||
| kind: Bus | ||
| metadata: | ||
| name: stub | ||
| spec: | ||
| dispatcher: | ||
| name: dispatcher | ||
| image: github.com/knative/eventing/cmd/stub-bus | ||
| args: [ | ||
| "-logtostderr", | ||
| "-stderrthreshold", "INFO", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2018 Google, Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| apiVersion: apiextensions.k8s.io/v1beta1 | ||
| kind: CustomResourceDefinition | ||
| metadata: | ||
| name: channels.channels.knative.dev | ||
| spec: | ||
| scope: Namespaced | ||
| group: channels.knative.dev | ||
| version: v1alpha1 | ||
| names: | ||
| kind: Channel | ||
| plural: channels | ||
| singular: channel |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Copyright 2018 Google, Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| kind: ClusterRole | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| metadata: | ||
| name: knative-channels-bus | ||
| rules: | ||
| - apiGroups: ["channels.knative.dev"] | ||
| resources: ["buses", "channels", "subscriptions"] | ||
| verbs: ["get", "watch", "list"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2018 Google, Inc. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| apiVersion: apiextensions.k8s.io/v1beta1 | ||
| kind: CustomResourceDefinition | ||
| metadata: | ||
| name: subscriptions.channels.knative.dev | ||
| spec: | ||
| scope: Namespaced | ||
| group: channels.knative.dev | ||
| version: v1alpha1 | ||
| names: | ||
| kind: Subscription | ||
| plural: subscriptions | ||
| singular: subscription |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ CODEGEN_PKG=${CODEGEN_PKG:-$(cd ${SCRIPT_ROOT}; ls -d -1 ./vendor/k8s.io/code-ge | |
| # instead of the $GOPATH directly. For normal projects this can be dropped. | ||
| ${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \ | ||
| github.com/knative/eventing/pkg/client github.com/knative/eventing/pkg/apis \ | ||
| feeds:v1alpha1 \ | ||
| "channels:v1alpha1 feeds:v1alpha1 istio:v1alpha2" \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see Istio being used anywhere but perhaps I missed it? Rest of this seems fine, but just trying to understand why we need to import Istio in.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope to get rid of the custom Istio client in the near future. I would have used the client created by serving, except that it doesn't have the struct for rewriting the http host in a RouteRule. I can drive the PR in serving to completion and then drop this client.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There it was ! :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /cc @tcnghia who is working on importing the Istio 0.8 types into serving. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I filed a bug, with the hope that we only need to vendor/ this stuff in the future. istio/istio#6084 In the mean time, for istio v1alpha3 we will need to do the same (cloning _types.go from *.proto files and run codegen) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you use the client in /serving you'll benefit from my istio/v1alpha3 PR (coming). I am translating everything, so you should have access to all features, unlike the version we hae for istio/v1alpha2
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tcnghia Thanks. I'll keep a look out for the PR in serving. |
||
| --go-header-file ${SCRIPT_ROOT}/hack/boilerplate/boilerplate.go.txt | ||
|
|
||
| # Make sure our dependencies are up-to-date | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| Copyright 2018 Google, Inc. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package channels | ||
|
|
||
| const ( | ||
| GroupName = "channels.knative.dev" | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, missing newline here.