1
1
package apiserver
2
2
3
3
import (
4
+ "encoding/json"
4
5
"net/http"
5
6
"net/url"
6
7
"path"
@@ -14,6 +15,7 @@ import (
14
15
"k8s.io/apimachinery/pkg/runtime"
15
16
"k8s.io/apimachinery/pkg/runtime/schema"
16
17
"k8s.io/apimachinery/pkg/runtime/serializer"
18
+ "k8s.io/apimachinery/pkg/version"
17
19
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
18
20
apirequest "k8s.io/apiserver/pkg/endpoints/request"
19
21
"k8s.io/apiserver/pkg/features"
@@ -22,11 +24,14 @@ import (
22
24
genericfilters "k8s.io/apiserver/pkg/server/filters"
23
25
genericmux "k8s.io/apiserver/pkg/server/mux"
24
26
utilfeature "k8s.io/apiserver/pkg/util/feature"
27
+ restclient "k8s.io/client-go/rest"
25
28
26
29
"github.com/openshift/api/webconsole/v1"
27
30
"github.com/openshift/origin-web-console-server/pkg/assets"
28
31
"github.com/openshift/origin-web-console-server/pkg/assets/java"
29
- "github.com/openshift/origin-web-console-server/pkg/version"
32
+ builtversion "github.com/openshift/origin-web-console-server/pkg/version"
33
+ "k8s.io/apimachinery/pkg/api/errors"
34
+ "k8s.io/client-go/kubernetes"
30
35
)
31
36
32
37
var (
@@ -60,6 +65,10 @@ const (
60
65
type ExtraConfig struct {
61
66
Options v1.WebConsoleConfiguration
62
67
PublicURL url.URL
68
+
69
+ KubeVersion string
70
+ OpenShiftVersion string
71
+ TemplateServiceBrokerEnabled * bool
63
72
}
64
73
65
74
type AssetServerConfig struct {
@@ -76,7 +85,12 @@ type AssetServer struct {
76
85
77
86
type completedConfig struct {
78
87
GenericConfig genericapiserver.CompletedConfig
79
- ExtraConfig * ExtraConfig
88
+
89
+ // ClientConfig holds the kubernetes client configuration.
90
+ // This value is set by RecommendedOptions.CoreAPI.ApplyTo called by RecommendedOptions.ApplyTo.
91
+ // By default in-cluster client config is used.
92
+ ClientConfig * restclient.Config
93
+ ExtraConfig * ExtraConfig
80
94
}
81
95
82
96
type CompletedConfig struct {
@@ -104,13 +118,57 @@ func NewAssetServerConfig(assetConfig v1.WebConsoleConfiguration) (*AssetServerC
104
118
}
105
119
106
120
// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
107
- func (c * AssetServerConfig ) Complete () completedConfig {
121
+ func (c * AssetServerConfig ) Complete () ( completedConfig , error ) {
108
122
cfg := completedConfig {
109
123
c .GenericConfig .Complete (),
124
+ c .GenericConfig .ClientConfig ,
110
125
& c .ExtraConfig ,
111
126
}
112
127
113
- return cfg
128
+ // In order to build the asset server, we need information that we can only retrieve from the master.
129
+ // We do this once during construction at the moment, but a clever person could set up a watch or poll technique
130
+ // to dynamically reload these bits of config without having the pod restart.
131
+ restClient , err := kubernetes .NewForConfig (c .GenericConfig .ClientConfig )
132
+ if err != nil {
133
+ return completedConfig {}, err
134
+ }
135
+ if len (cfg .ExtraConfig .KubeVersion ) == 0 {
136
+ resultBytes , err := restClient .RESTClient ().Get ().AbsPath ("/version" ).Do ().Raw ()
137
+ if err != nil {
138
+ return completedConfig {}, err
139
+ }
140
+ kubeVersion := & version.Info {}
141
+ if err := json .Unmarshal (resultBytes , kubeVersion ); err != nil {
142
+ return completedConfig {}, err
143
+ }
144
+ cfg .ExtraConfig .KubeVersion = kubeVersion .GitVersion
145
+ }
146
+ if len (cfg .ExtraConfig .OpenShiftVersion ) == 0 {
147
+ resultBytes , err := restClient .RESTClient ().Get ().AbsPath ("/version/openshift" ).Do ().Raw ()
148
+ if err != nil {
149
+ return completedConfig {}, err
150
+ }
151
+ openshiftVersion := & version.Info {}
152
+ if err := json .Unmarshal (resultBytes , openshiftVersion ); err != nil {
153
+ return completedConfig {}, err
154
+ }
155
+ cfg .ExtraConfig .OpenShiftVersion = openshiftVersion .GitVersion
156
+ }
157
+ if cfg .ExtraConfig .TemplateServiceBrokerEnabled == nil {
158
+ enabled := false
159
+ _ , err := restClient .RESTClient ().Get ().AbsPath ("/apis/servicecatalog.k8s.io/v1beta1/clusterservicebrokers/template-service-broker" ).Do ().Raw ()
160
+ if err == nil {
161
+ enabled = true
162
+ } else if errors .IsNotFound (err ) {
163
+ enabled = false
164
+ } else {
165
+ return completedConfig {}, err
166
+ }
167
+
168
+ cfg .ExtraConfig .TemplateServiceBrokerEnabled = & enabled
169
+ }
170
+
171
+ return cfg , nil
114
172
}
115
173
116
174
func (c completedConfig ) New (delegationTarget genericapiserver.DelegationTarget ) (* AssetServer , error ) {
@@ -187,11 +245,9 @@ func (c *completedConfig) addWebConsoleConfig(serverMux *genericmux.PathRecorder
187
245
MetricsURL : c .ExtraConfig .Options .MetricsPublicURL ,
188
246
}
189
247
190
- // TODO this looks incorrect. I'm guessing that we need to contact the master and locate the actual versions
191
- oVersionInfo := version .Get ()
192
248
versionInfo := assets.WebConsoleVersion {
193
- KubernetesVersion : oVersionInfo . GitVersion ,
194
- OpenShiftVersion : oVersionInfo . GitVersion ,
249
+ KubernetesVersion : c . ExtraConfig . KubeVersion ,
250
+ OpenShiftVersion : c . ExtraConfig . OpenShiftVersion ,
195
251
}
196
252
197
253
extensionProps := assets.WebConsoleExtensionProperties {
@@ -230,7 +286,7 @@ func (c completedConfig) buildAssetHandler() (http.Handler, error) {
230
286
231
287
// Cache control should happen after all Vary headers are added, but before
232
288
// any asset related routing (HTML5ModeHandler and FileServer)
233
- handler = assets .CacheControlHandler (version .Get ().GitCommit , handler )
289
+ handler = assets .CacheControlHandler (builtversion .Get ().GitCommit , handler )
234
290
235
291
handler = assets .SecurityHeadersHandler (handler )
236
292
0 commit comments