Skip to content
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

Solves #1966. Disable HTTP/2 when a SPDY request is detected in the okteto deploy proxy #1967

Merged
merged 3 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 43 additions & 4 deletions cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"k8s.io/client-go/rest"
)

const headerUpgrade = "Upgrade"

var tempKubeConfigTemplate = "%s/.okteto/kubeconfig-%s"

// Options options for deploy command
Expand Down Expand Up @@ -212,19 +214,37 @@ func (dc *deployCommand) cleanUp(ctx context.Context) {
}
}

func newProtocolTransport(clusterConfig *rest.Config, disableHTTP2 bool) (http.RoundTripper, error) {
copiedConfig := &rest.Config{}
*copiedConfig = *clusterConfig

if disableHTTP2 {
// According to https://pkg.go.dev/k8s.io/client-go/rest#TLSClientConfig, this is the way to disable HTTP/2
copiedConfig.TLSClientConfig.NextProtos = []string{"http/1.1"}
}

return rest.TransportFor(copiedConfig)
}

func isSPDY(r *http.Request) bool {
return strings.HasPrefix(strings.ToLower(r.Header.Get(headerUpgrade)), "spdy/")
}

func getProxyHandler(name, token string, clusterConfig *rest.Config) (http.Handler, error) {
trans, err := rest.TransportFor(clusterConfig)
// By default we don't disable HTTP/2
trans, err := newProtocolTransport(clusterConfig, false)
if err != nil {
log.Errorf("could not get http transport from config: %s", err)
return nil, err
}

handler := http.NewServeMux()

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
destinationURL := &url.URL{
Host: strings.TrimPrefix(clusterConfig.Host, "https://"),
Scheme: "https",
})
}
proxy := httputil.NewSingleHostReverseProxy(destinationURL)
proxy.Transport = trans

log.Debugf("forwarding host: %s", clusterConfig.Host)
Expand All @@ -246,6 +266,20 @@ func getProxyHandler(name, token string, clusterConfig *rest.Config) (http.Handl
r.Header.Del("Authorization")
}

reverseProxy := proxy
if isSPDY(r) {
log.Debugf("detected SPDY request, disabling HTTP/2 for request %s %s", r.Method, r.URL.String())
// In case of a SPDY request, we create a new proxy with HTTP/2 disabled
t, err := newProtocolTransport(clusterConfig, true)
if err != nil {
log.Errorf("could not disabled HTTP/2: %s", err)
rw.WriteHeader(500)
return
}
reverseProxy = httputil.NewSingleHostReverseProxy(destinationURL)
reverseProxy.Transport = t
}

// Modify all resources updated or created to include the label.
if r.Method == "PUT" || r.Method == "POST" {
b, err := io.ReadAll(r.Body)
Expand All @@ -255,6 +289,11 @@ func getProxyHandler(name, token string, clusterConfig *rest.Config) (http.Handl
return
}

if len(b) == 0 {
reverseProxy.ServeHTTP(rw, r)
return
}

var body map[string]json.RawMessage
if err := json.Unmarshal(b, &body); err != nil {
log.Errorf("could not unmarshal request: %s", err)
Expand Down Expand Up @@ -303,7 +342,7 @@ func getProxyHandler(name, token string, clusterConfig *rest.Config) (http.Handl
}

// Redirect request to the k8s server (based on the transport HTTP generated from the config)
proxy.ServeHTTP(rw, r)
reverseProxy.ServeHTTP(rw, r)
})

return handler, nil
Expand Down
8 changes: 8 additions & 0 deletions pkg/k8s/namespaces/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
const (
parallelism int64 = 10
volumeKind = "PersistentVolumeClaim"
jobKind = "Job"
)

// DeleteAllOptions options for delete all operation
Expand Down Expand Up @@ -103,6 +104,13 @@ func (n *Namespaces) DestroyWithLabel(ctx context.Context, ns string, opts Delet
return err
}
deleteOpts := metav1.DeleteOptions{}

// It seems that by default, client-go don't delete pods scheduled by jobs, so we need to set the propation policy
if gvk.Kind == jobKind {
deletePropagation := metav1.DeletePropagationBackground
deleteOpts.PropagationPolicy = &deletePropagation
}
Comment on lines +109 to +112
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pchico83 with the example provided by Tere in the issue, the pod generated by a Job is not being deleted. As it is created by the job controller, it does not have the deployed-by label, and it seems that by default, client-go don't include the propagation policy (while kubectl does). So I'm including it in case of jobs (I see we already have this propagation policy on our code). For now I just added it for Jobs, should I include it for other Kinds?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ifbyol nothing else comes to my mind


err = n.dynClient.
Resource(mapping.Resource).
Namespace(ns).
Expand Down