-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.go
45 lines (38 loc) · 1.24 KB
/
lookup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package k8slookup
import (
"net/http"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clusterrouter"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/apis/management.cattle.io/v3/schema"
client "github.com/rancher/types/client/management/v3"
"github.com/rancher/types/config"
)
func New(context *config.ScaledContext, validate bool) clusterrouter.ClusterLookup {
return &lookup{
clusterLister: context.Management.Clusters("").Controller().Lister(),
schemas: context.Schemas,
validate: validate,
}
}
type lookup struct {
clusterLister v3.ClusterLister
schemas *types.Schemas
validate bool
}
func (l *lookup) Lookup(req *http.Request) (*v3.Cluster, error) {
apiContext := types.NewAPIContext(req, nil, l.schemas)
clusterID := clusterrouter.GetClusterID(req)
if clusterID == "" {
return nil, httperror.NewAPIError(httperror.NotFound, "failed to find cluster")
}
if l.validate {
// check access
if err := access.ByID(apiContext, &schema.Version, client.ClusterType, clusterID, &client.Cluster{}); err != nil {
return nil, err
}
}
return l.clusterLister.Get("", clusterID)
}