@@ -952,65 +952,91 @@ func (c *HeadlampConfig) getConfig(w http.ResponseWriter, r *http.Request) {
952
952
}
953
953
}
954
954
955
- func (c * HeadlampConfig ) addClusterSetupRoute (r * mux.Router ) {
956
- // We do not support this feature when in-cluster
957
- if c .useInCluster {
955
+ func (c * HeadlampConfig ) addCluster (w http.ResponseWriter , r * http.Request ) {
956
+ clusterReq := ClusterReq {}
957
+ if err := json .NewDecoder (r .Body ).Decode (& clusterReq ); err != nil {
958
+ fmt .Println (err )
959
+ http .Error (w , "Error decoding cluster info" , http .StatusBadRequest )
960
+
958
961
return
959
962
}
960
963
961
- r .HandleFunc ("/cluster" , func (w http.ResponseWriter , r * http.Request ) {
962
- clusterReq := ClusterReq {}
963
- if err := json .NewDecoder (r .Body ).Decode (& clusterReq ); err != nil {
964
- fmt .Println (err )
965
- http .Error (w , "Error decoding cluster info" , http .StatusBadRequest )
966
- return
967
- }
968
-
969
- if clusterReq .Name == "" || clusterReq .Server == "" {
970
- http .Error (w , "Error creating cluster with invalid info; please provide a 'name' and 'server' fields at least." ,
971
- http .StatusBadRequest )
972
- return
973
- }
964
+ if clusterReq .Name == "" || clusterReq .Server == "" {
965
+ http .Error (w , "Error creating cluster with invalid info; please provide a 'name' and 'server' fields at least." ,
966
+ http .StatusBadRequest )
967
+ return
968
+ }
974
969
975
- context := Context {
976
- Name : clusterReq .Name ,
977
- cluster : Cluster {
978
- Name : clusterReq .Name ,
979
- Server : clusterReq .Server ,
980
- config : & clientcmdapi.Cluster {
981
- Server : clusterReq .Server ,
982
- InsecureSkipTLSVerify : clusterReq .InsecureSkipTLSVerify ,
983
- CertificateAuthorityData : clusterReq .CertificateAuthorityData ,
984
- },
985
- Metadata : clusterReq .Metadata ,
970
+ context := Context {
971
+ Name : clusterReq .Name ,
972
+ cluster : Cluster {
973
+ Name : clusterReq .Name ,
974
+ Server : clusterReq .Server ,
975
+ config : & clientcmdapi.Cluster {
976
+ Server : clusterReq .Server ,
977
+ InsecureSkipTLSVerify : clusterReq .InsecureSkipTLSVerify ,
978
+ CertificateAuthorityData : clusterReq .CertificateAuthorityData ,
986
979
},
987
- }
980
+ Metadata : clusterReq .Metadata ,
981
+ },
982
+ }
988
983
989
- proxy , err := c .createProxyForContext (context )
990
- if err != nil {
991
- log .Printf ("Error creating proxy for cluster %s: %s" , clusterReq .Name , err )
992
- http .Error (w , "Error setting up cluster" , http .StatusBadRequest )
993
- return
994
- }
984
+ proxy , err := c .createProxyForContext (context )
985
+ if err != nil {
986
+ log .Printf ("Error creating proxy for cluster %s: %s" , clusterReq .Name , err )
987
+ http .Error (w , "Error setting up cluster" , http .StatusBadRequest )
995
988
996
- _ , isReplacement := c .contextProxies [clusterReq .Name ]
989
+ return
990
+ }
997
991
998
- c .contextProxies [clusterReq .Name ] = contextProxy {
999
- & context ,
1000
- proxy ,
1001
- DynamicCluster ,
1002
- }
992
+ _ , isReplacement := c .contextProxies [clusterReq .Name ]
1003
993
1004
- if isReplacement {
1005
- fmt .Printf ("Replaced cluster \" %s\" proxy by:\n " , context .Name )
1006
- } else {
1007
- fmt .Println ("Created new cluster proxy:" )
1008
- }
1009
- fmt .Printf ("\t localhost:%d%s%s/{api...} -> %s\n " , c .port , c .baseURL , "/clusters/" + context .Name , clusterReq .Server )
994
+ c .contextProxies [clusterReq .Name ] = contextProxy {
995
+ & context ,
996
+ proxy ,
997
+ DynamicCluster ,
998
+ }
1010
999
1011
- w .WriteHeader (http .StatusCreated )
1012
- c .getConfig (w , r )
1013
- }).Methods ("POST" )
1000
+ if isReplacement {
1001
+ fmt .Printf ("Replaced cluster \" %s\" proxy by:\n " , context .Name )
1002
+ } else {
1003
+ fmt .Println ("Created new cluster proxy:" )
1004
+ }
1005
+
1006
+ fmt .Printf ("\t localhost:%d%s%s/{api...} -> %s\n " , c .port , c .baseURL , "/clusters/" + context .Name , clusterReq .Server )
1007
+
1008
+ w .WriteHeader (http .StatusCreated )
1009
+ c .getConfig (w , r )
1010
+ }
1011
+
1012
+ func (c * HeadlampConfig ) deleteCluster (w http.ResponseWriter , r * http.Request ) {
1013
+ name := mux .Vars (r )["name" ]
1014
+ if _ , ok := c .contextProxies [name ]; ! ok {
1015
+ http .Error (w , "Cluster not found" , http .StatusNotFound )
1016
+ return
1017
+ }
1018
+
1019
+ if c .contextProxies [name ].source != DynamicCluster {
1020
+ http .Error (w , "Cannot delete a static cluster" , http .StatusForbidden )
1021
+ return
1022
+ }
1023
+
1024
+ delete (c .contextProxies , name )
1025
+ fmt .Printf ("Removed cluster \" %s\" proxy\n " , name )
1026
+
1027
+ c .getConfig (w , r )
1028
+ }
1029
+
1030
+ func (c * HeadlampConfig ) addClusterSetupRoute (r * mux.Router ) {
1031
+ // We do not support this feature when in-cluster
1032
+ if c .useInCluster {
1033
+ return
1034
+ }
1035
+
1036
+ r .HandleFunc ("/cluster" , c .addCluster ).Methods ("POST" )
1037
+
1038
+ // Delete a cluster
1039
+ r .HandleFunc ("/cluster/{name}" , c .deleteCluster ).Methods ("DELETE" )
1014
1040
}
1015
1041
1016
1042
func absPath (path string ) (string , error ) {
0 commit comments