1
1
package cache
2
2
3
3
import (
4
+ "encoding/json"
5
+ "fmt"
4
6
"io"
5
7
"net/http"
6
8
@@ -9,6 +11,7 @@ import (
9
11
"k8s.io/klog/v2"
10
12
11
13
"k8s.io/dashboard/client/args"
14
+ "k8s.io/dashboard/errors"
12
15
"k8s.io/dashboard/helpers"
13
16
"k8s.io/dashboard/types"
14
17
)
@@ -18,6 +21,13 @@ import (
18
21
// multi-cluster resources.
19
22
var contextCache * theine.Cache [string , string ]
20
23
24
+ func init () {
25
+ var err error
26
+ if contextCache , err = theine.NewBuilder [string , string ](int64 (args .CacheSize ())).Build (); err != nil {
27
+ panic (err )
28
+ }
29
+ }
30
+
21
31
// key is an internal structure used for creating
22
32
// a unique cache key SHA. It is used when
23
33
// `cluster-context-enabled=false`.
@@ -34,10 +44,21 @@ type key struct {
34
44
35
45
// SHA calculates sha based on the internal key fields.
36
46
func (k key ) SHA () (string , error ) {
37
- k .opts = metav1.ListOptions {LabelSelector : k .opts .LabelSelector , FieldSelector : k .opts .FieldSelector }
38
47
return helpers .HashObject (k )
39
48
}
40
49
50
+ func (k key ) MarshalJSON () ([]byte , error ) {
51
+ return json .Marshal (struct {
52
+ Kind types.ResourceKind
53
+ Namespace string
54
+ Opts metav1.ListOptions
55
+ }{
56
+ Kind : k .kind ,
57
+ Namespace : k .namespace ,
58
+ Opts : metav1.ListOptions {LabelSelector : k .opts .LabelSelector , FieldSelector : k .opts .FieldSelector },
59
+ })
60
+ }
61
+
41
62
// Key embeds an internal key structure and extends it with the support
42
63
// for the multi-cluster cache key creation. It is used when
43
64
// `cluster-context-enabled=true`.
@@ -53,6 +74,16 @@ type Key struct {
53
74
context string
54
75
}
55
76
77
+ func (k Key ) MarshalJSON () ([]byte , error ) {
78
+ return json .Marshal (struct {
79
+ K key
80
+ Context string
81
+ }{
82
+ K : k .key ,
83
+ Context : k .context ,
84
+ })
85
+ }
86
+
56
87
// SHA calculates sha based on the internal struct fields.
57
88
// It is also responsible for exchanging the [Key.Token] for
58
89
// the context identifier with the external source of truth
@@ -72,8 +103,6 @@ func (k Key) SHA() (sha string, err error) {
72
103
contextCache .SetWithTTL (k .token , contextKey , 1 , args .CacheTTL ())
73
104
}
74
105
75
- k .opts = metav1.ListOptions {LabelSelector : k .opts .LabelSelector , FieldSelector : k .opts .FieldSelector }
76
- k .token = ""
77
106
k .context = contextKey
78
107
return helpers .HashObject (k )
79
108
}
@@ -100,6 +129,15 @@ func exchangeToken(token string) (string, error) {
100
129
return "" , err
101
130
}
102
131
132
+ if response .StatusCode == http .StatusUnauthorized || response .StatusCode == http .StatusForbidden {
133
+ return "" , errors .NewUnauthorized (fmt .Sprintf ("could not exchange token: %s" , response .Status ))
134
+ }
135
+
136
+ if response .StatusCode != http .StatusOK {
137
+ klog .ErrorS (errors .NewBadRequest (response .Status ), "could not exchange token" , "url" , args .TokenExchangeEndpoint ())
138
+ return "" , errors .NewBadRequest (response .Status )
139
+ }
140
+
103
141
defer func (body io.ReadCloser ) {
104
142
if err := body .Close (); err != nil {
105
143
klog .V (3 ).ErrorS (err , "could not close response body writer" )
0 commit comments