From 8524b0992caf3274274194db41a2414be3d86fd1 Mon Sep 17 00:00:00 2001 From: Yao Wu Date: Tue, 8 May 2018 10:17:02 -0700 Subject: [PATCH 1/2] enable sidecar injection for activator --- activator.yaml | 2 ++ namespace.yaml | 2 ++ pkg/activator/activator.go | 31 ++++++++++++++++++++----------- pkg/activator/activator_test.go | 2 +- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/activator.yaml b/activator.yaml index 207b8afa04d8..d629d7eec6b8 100644 --- a/activator.yaml +++ b/activator.yaml @@ -23,6 +23,8 @@ spec: replicas: 1 template: metadata: + annotations: + sidecar.istio.io/inject: "true" labels: app: ela-activator role: ela-activator diff --git a/namespace.yaml b/namespace.yaml index fe929502b09f..d5341e90628c 100644 --- a/namespace.yaml +++ b/namespace.yaml @@ -15,4 +15,6 @@ apiVersion: v1 kind: Namespace metadata: + labels: + istio-injection: enabled name: ela-system diff --git a/pkg/activator/activator.go b/pkg/activator/activator.go index 866688041f79..38223010b814 100644 --- a/pkg/activator/activator.go +++ b/pkg/activator/activator.go @@ -20,6 +20,7 @@ import ( "net/url" "strings" "sync" + "time" "github.com/elafros/elafros/pkg/apis/ela/v1alpha1" clientset "github.com/elafros/elafros/pkg/client/clientset/versioned" @@ -92,34 +93,42 @@ func getRevisionNameFromKey(key string) (namespace string, name string, err erro } func (a *Activator) getRevisionTargetURL(revision *v1alpha1.Revision) (*url.URL, error) { - endpoint, err := a.kubeClient.CoreV1().Endpoints(revision.GetNamespace()).Get( - controller.GetElaK8SServiceNameForRevision(revision), metav1.GetOptions{}) + services := a.kubeClient.CoreV1().Services(revision.GetNamespace()) + svc, err := services.Get(controller.GetElaK8SServiceNameForRevision(revision), metav1.GetOptions{}) if err != nil { if apierrors.IsNotFound(err) { return nil, nil } return nil, err } - if len(endpoint.Subsets[0].Ports) != 1 { - return nil, fmt.Errorf("need just one port. Found %v ports", len(endpoint.Subsets[0].Ports)) + svc, err = services.Get(controller.GetElaK8SServiceNameForRevision(revision), metav1.GetOptions{}) + if len(svc.Spec.Ports) != 1 { + return nil, fmt.Errorf("need just one port. Found %v ports", len(svc.Spec.Ports)) } - // TODO: figure out why do we need to use the pod IP directly to avoid the delay. - // We should be able to use the k8s service cluster IP. - // https://github.com/elafros/elafros/issues/660 - ip := endpoint.Subsets[0].Addresses[0].IP - port := endpoint.Subsets[0].Ports[0].Port u := &url.URL{ Scheme: "http", - Host: fmt.Sprintf("%s:%d", ip, port), + Host: fmt.Sprintf("%s.%s.svc.cluster.local:%d", + controller.GetElaK8SServiceNameForRevision(revision), revision.Namespace, svc.Spec.Ports[0].Port), } return u, nil } func (a *Activator) proxyRequest(revRequest RevisionRequest, serviceURL *url.URL) { - glog.Infof("Sending a proxy request to %q", serviceURL) + glog.Infof("Sending the request to %q", serviceURL) + // TODO: We need to wait a bit after the revision is marked ready. + // See https://github.com/elafros/elafros/issues/660: Mark a revision ready at the right time. + time.Sleep(2 * time.Second) proxy := httputil.NewSingleHostReverseProxy(serviceURL) proxy.Transport = a.tripper + + // We are passing host header as a hack in tests, so the request can be matched to the right route. + // https://github.com/elafros/elafros/blob/2b3ee4f3c46118b3759aa95d9e6c41747c32d6c5/pkg/controller/route/ela_ingress.go#L38 + // Since host values like "route-example.default.demo-domain.com" do not exist, we need to + // clear it to make istio sidecar happy if it's injected. + revRequest.r.Host = "" + proxy.ServeHTTP(revRequest.w, revRequest.r) + // Make sure the handler function exits after ServeHTTP function. revRequest.doneCh <- struct{}{} glog.Info("End proxy request") diff --git a/pkg/activator/activator_test.go b/pkg/activator/activator_test.go index b1a24ae3c785..1572ee5ff581 100644 --- a/pkg/activator/activator_test.go +++ b/pkg/activator/activator_test.go @@ -173,7 +173,7 @@ func TestGetRevisionTargetURL(t *testing.T) { if err != nil { t.Errorf("Error in getRevisionTargetURL %v", err) } - expectedURL := "http://abc:1234" + expectedURL := "http://test-rev-service.default.svc.cluster.local:1234" if targetURL.String() != expectedURL { t.Errorf("getRevisionTargetURL returned unexpected url %s, expected %s", targetURL, expectedURL) } From 8861351b23a36bb73f88671f079787b2b4111dc1 Mon Sep 17 00:00:00 2001 From: Yao Wu Date: Tue, 8 May 2018 15:41:30 -0700 Subject: [PATCH 2/2] why 404? --- elaconfig.yaml | 4 ++-- pkg/activator/activator.go | 4 ++-- sample/helloworld/helloworld.go | 5 ++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/elaconfig.yaml b/elaconfig.yaml index 3db1019ccb92..44d0502ee47a 100644 --- a/elaconfig.yaml +++ b/elaconfig.yaml @@ -62,13 +62,13 @@ data: autoscale.concurrency-quantum-of-time: "100ms" # Scale to zero feature flag - autoscale.enable-scale-to-zero: "false" + autoscale.enable-scale-to-zero: "true" # Dynamic parameters (take effect when config map is updated): # Scale to zero threshold is the time a revision must be idle before # it is scaled to zero. - autoscale.scale-to-zero-threshold: "5m" + autoscale.scale-to-zero-threshold: "1m" # LOGGING CONFIGURATION diff --git a/pkg/activator/activator.go b/pkg/activator/activator.go index 38223010b814..55c06b43eeab 100644 --- a/pkg/activator/activator.go +++ b/pkg/activator/activator.go @@ -101,7 +101,8 @@ func (a *Activator) getRevisionTargetURL(revision *v1alpha1.Revision) (*url.URL, } return nil, err } - svc, err = services.Get(controller.GetElaK8SServiceNameForRevision(revision), metav1.GetOptions{}) + // TODO: in the future, the target service could have more than one ports. + // https://github.com/elafros/elafros/issues/837 if len(svc.Spec.Ports) != 1 { return nil, fmt.Errorf("need just one port. Found %v ports", len(svc.Spec.Ports)) } @@ -114,7 +115,6 @@ func (a *Activator) getRevisionTargetURL(revision *v1alpha1.Revision) (*url.URL, } func (a *Activator) proxyRequest(revRequest RevisionRequest, serviceURL *url.URL) { - glog.Infof("Sending the request to %q", serviceURL) // TODO: We need to wait a bit after the revision is marked ready. // See https://github.com/elafros/elafros/issues/660: Mark a revision ready at the right time. time.Sleep(2 * time.Second) diff --git a/sample/helloworld/helloworld.go b/sample/helloworld/helloworld.go index e3948aeb3fff..629729edf228 100644 --- a/sample/helloworld/helloworld.go +++ b/sample/helloworld/helloworld.go @@ -25,7 +25,10 @@ import ( ) func handler(w http.ResponseWriter, r *http.Request) { - log.Print("Hello world received a request.") + log.Println("Hello world received a request.") + log.Printf("request\n%+v", r) + log.Printf("request.Host\n%+v", r.Host) + log.Printf("request.URL.Host\n%+v", r.URL.Host) target := os.Getenv("TARGET") if target == "" { target = "NOT SPECIFIED"