You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: shell_handler.go
    path of file: ./repos/erda/internal/apps/cmp/steve/middleware
    
    the code of the file until where you have to start completion: // Copyright (c) 2021 Terminus, Inc.
//
// 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 middleware

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"time"

	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
	authenticationv1 "k8s.io/api/authentication/v1"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apiserver/pkg/endpoints/request"
	"k8s.io/client-go/kubernetes"
	corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
	"k8s.io/utils/pointer"

	"github.com/erda-project/erda/apistructs"
	"github.com/erda-project/erda/internal/apps/cmp/steve/predefined"
	"github.com/erda-project/erda/pkg/k8sclient"
)

var (
	defaultShellTokenExpirationTime = 2 * time.Hour.Seconds()
)

type ShellHandler struct {
	ctx context.Context
}

// NewShellHandler create a new ShellHandler
func NewShellHandler(ctx context.Context) *ShellHandler {
	return &ShellHandler{ctx: ctx}
}

// HandleShell forwards the request to cluster-agent pod
func (s *ShellHandler) HandleShell(next http.Handler) http.Handler {
	return http.HandlerFunc(func(resp http.ResponseWriter, req
    
func (s *ShellHandler) HandleShell(next http.Handler) http.Handler {
	return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
		vars, _ := req.Context().Value(varsKey).(map[string]string)
		if vars["kubectl-shell"] == "" {
			next.ServeHTTP(resp, req)
			return
		}
		user, ok := request.UserFrom(req.Context())
		if !ok || user.GetName() == "system:unauthenticated" {
			resp.WriteHeader(http.StatusForbidden)
			resp.Write(apistructs.NewSteveError(apistructs.PermissionDenied, "access denied").JSON())
			return
		}

		clusterName := vars["clusterName"]
		client, err := k8sclient.New(clusterName)
		if err != nil {
			logrus.Errorf("failed to get k8s client for cluster %s in steve handle shell, %v", clusterName, err)
			resp.WriteHeader(http.StatusInternalServerError)
			resp.Write(apistructs.NewSteveError(apistructs.ServerError, "server error").JSON())
			return
		}

		group := user.GetGroups()[0]
		userGroup, ok := predefined.UserGroups[group]
		if !ok {
			resp.WriteHeader(http.StatusForbidden)
			resp.Write(apistructs.NewSteveError(apistructs.PermissionDenied, "access denied").JSON())
			return
		}

		token, err := s.getAuthToken(client.ClientSet, userGroup)
		if err != nil {
			logrus.Error(err)
			resp.WriteHeader(http.StatusInternalServerError)
			resp.Write(apistructs.NewSteveError(apistructs.ServerError, "interval server error").JSON())
			return
		}

		podClient := client.ClientSet.CoreV1().Pods("")

		for _, pod := range s.getAgentPods(podClient) {
			if !isPodReady(&pod) {
				continue
			}
			vars := url.Values{}
			vars.Add("container", "cluster-agent")
			vars.Add("stdout", "1")
			vars.Add("stdout", "1")
			vars.Add("stdin", "1")
			vars.Add("stderr", "1")
			vars.Add("tty", "1")
			vars.Add("command", "kubectl-shell.sh")
			vars.Add("command", token)

			path := fmt.Sprintf("/api/k8s/clusters/%s/api/v1/namespaces/%s/pods/%s/exec", clusterName, pod.Namespace, pod.Name)

			req.URL.Path = path
			req.URL.RawQuery = vars.Encode()
			next.ServeHTTP(resp, req)
			return
		}

		logrus.Errorf("failed to find a ready cluster-agent pod for cluster %s", clusterName)
		resp.WriteHeader(http.StatusInternalServerError)
		resp.Write(apistructs.NewSteveError(apistructs.ServerError,
			fmt.Sprintf("cluster %s does not have a ready agent pod", clusterName)).JSON())
	})
}