Skip to content

Commit

Permalink
feat(gateway): add platform info api (#1736)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoryu committed Jan 6, 2022
1 parent e927b92 commit 9345fd1
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/gateway/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"github.com/emicklei/go-restful"
"golang.org/x/oauth2"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"tkestack.io/tke/pkg/apiserver/authentication/authenticator/oidc"
gatewayconfig "tkestack.io/tke/pkg/gateway/apis/config"
"tkestack.io/tke/pkg/gateway/requestheader"
Expand All @@ -44,5 +46,15 @@ func RegisterRoute(container *restful.Container, cfg *gatewayconfig.GatewayConfi
}
registerSysInfoRoute(container, cfg)
registerLogoutRoute(container)
config, err := rest.InClusterConfig()
if err != nil {
return err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
registerPlatformInfoRoute(container, oidcAuthenticator, client)

return nil
}
72 changes: 72 additions & 0 deletions pkg/gateway/api/platforminfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2022 Tencent. 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package api

import (
"fmt"
"net/http"

"github.com/emicklei/go-restful"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
"k8s.io/client-go/kubernetes"
"tkestack.io/tke/pkg/apiserver/authentication/authenticator/oidc"
"tkestack.io/tke/pkg/gateway/token"
"tkestack.io/tke/pkg/util/log"
)

func registerPlatformInfoRoute(container *restful.Container, oidcAuthenticator *oidc.Authenticator, client kubernetes.Interface) {
ws := new(restful.WebService)
ws.Path(fmt.Sprintf("/apis/%s/%s/platforminfo", GroupName, Version))
ws.Produces(restful.MIME_JSON)
ws.Consumes(restful.MIME_JSON, restful.MIME_OCTET)
ws.Route(ws.
GET("/").
Doc("get platform info of TKE").
Operation("getPlatformInfo").
Returns(http.StatusOK, "Ok", v1.ConfigMap{}).
Returns(http.StatusUnauthorized, "Unauthorized", v1.ConfigMap{}).
To(handlePlatformInfoFunc(oidcAuthenticator, client)))
container.Add(ws)
}

func handlePlatformInfoFunc(oidcAuthenticator *oidc.Authenticator, client kubernetes.Interface) func(*restful.Request, *restful.Response) {
return func(request *restful.Request, response *restful.Response) {
t, err := token.RetrieveToken(request.Request)
if err != nil {
responsewriters.WriteRawJSON(http.StatusUnauthorized, errors.NewUnauthorized(err.Error()), response.ResponseWriter)
return
}
_, authenticated, _ := oidcAuthenticator.AuthenticateToken(request.Request.Context(), t.ID)

if !authenticated {
responsewriters.WriteRawJSON(http.StatusUnauthorized, errors.NewUnauthorized("invalid token"), response.ResponseWriter)
return
}

cm, err := client.CoreV1().ConfigMaps("kube-public").Get(request.Request.Context(), "cluster-info", metav1.GetOptions{})
if err != nil {
log.Errorf("get cluster-info failed: %v", err)
cm = &v1.ConfigMap{}
}
responsewriters.WriteRawJSON(http.StatusOK, *cm, response.ResponseWriter)
}
}

0 comments on commit 9345fd1

Please sign in to comment.