Skip to content

Commit

Permalink
Merge pull request #893 from headlamp-k8s/delete_dynamic_cluster_endp…
Browse files Browse the repository at this point in the history
…oint

backend: Add Delete dynamic cluster endpoint
  • Loading branch information
joaquimrocha authored Jan 10, 2023
2 parents 04d58f3 + de1d90e commit 081bf6b
Showing 1 changed file with 75 additions and 49 deletions.
124 changes: 75 additions & 49 deletions backend/cmd/headlamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,65 +952,91 @@ func (c *HeadlampConfig) getConfig(w http.ResponseWriter, r *http.Request) {
}
}

func (c *HeadlampConfig) addClusterSetupRoute(r *mux.Router) {
// We do not support this feature when in-cluster
if c.useInCluster {
func (c *HeadlampConfig) addCluster(w http.ResponseWriter, r *http.Request) {
clusterReq := ClusterReq{}
if err := json.NewDecoder(r.Body).Decode(&clusterReq); err != nil {
fmt.Println(err)
http.Error(w, "Error decoding cluster info", http.StatusBadRequest)

return
}

r.HandleFunc("/cluster", func(w http.ResponseWriter, r *http.Request) {
clusterReq := ClusterReq{}
if err := json.NewDecoder(r.Body).Decode(&clusterReq); err != nil {
fmt.Println(err)
http.Error(w, "Error decoding cluster info", http.StatusBadRequest)
return
}

if clusterReq.Name == "" || clusterReq.Server == "" {
http.Error(w, "Error creating cluster with invalid info; please provide a 'name' and 'server' fields at least.",
http.StatusBadRequest)
return
}
if clusterReq.Name == "" || clusterReq.Server == "" {
http.Error(w, "Error creating cluster with invalid info; please provide a 'name' and 'server' fields at least.",
http.StatusBadRequest)
return
}

context := Context{
Name: clusterReq.Name,
cluster: Cluster{
Name: clusterReq.Name,
Server: clusterReq.Server,
config: &clientcmdapi.Cluster{
Server: clusterReq.Server,
InsecureSkipTLSVerify: clusterReq.InsecureSkipTLSVerify,
CertificateAuthorityData: clusterReq.CertificateAuthorityData,
},
Metadata: clusterReq.Metadata,
context := Context{
Name: clusterReq.Name,
cluster: Cluster{
Name: clusterReq.Name,
Server: clusterReq.Server,
config: &clientcmdapi.Cluster{
Server: clusterReq.Server,
InsecureSkipTLSVerify: clusterReq.InsecureSkipTLSVerify,
CertificateAuthorityData: clusterReq.CertificateAuthorityData,
},
}
Metadata: clusterReq.Metadata,
},
}

proxy, err := c.createProxyForContext(context)
if err != nil {
log.Printf("Error creating proxy for cluster %s: %s", clusterReq.Name, err)
http.Error(w, "Error setting up cluster", http.StatusBadRequest)
return
}
proxy, err := c.createProxyForContext(context)
if err != nil {
log.Printf("Error creating proxy for cluster %s: %s", clusterReq.Name, err)
http.Error(w, "Error setting up cluster", http.StatusBadRequest)

_, isReplacement := c.contextProxies[clusterReq.Name]
return
}

c.contextProxies[clusterReq.Name] = contextProxy{
&context,
proxy,
DynamicCluster,
}
_, isReplacement := c.contextProxies[clusterReq.Name]

if isReplacement {
fmt.Printf("Replaced cluster \"%s\" proxy by:\n", context.Name)
} else {
fmt.Println("Created new cluster proxy:")
}
fmt.Printf("\tlocalhost:%d%s%s/{api...} -> %s\n", c.port, c.baseURL, "/clusters/"+context.Name, clusterReq.Server)
c.contextProxies[clusterReq.Name] = contextProxy{
&context,
proxy,
DynamicCluster,
}

w.WriteHeader(http.StatusCreated)
c.getConfig(w, r)
}).Methods("POST")
if isReplacement {
fmt.Printf("Replaced cluster \"%s\" proxy by:\n", context.Name)
} else {
fmt.Println("Created new cluster proxy:")
}

fmt.Printf("\tlocalhost:%d%s%s/{api...} -> %s\n", c.port, c.baseURL, "/clusters/"+context.Name, clusterReq.Server)

w.WriteHeader(http.StatusCreated)
c.getConfig(w, r)
}

func (c *HeadlampConfig) deleteCluster(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
if _, ok := c.contextProxies[name]; !ok {
http.Error(w, "Cluster not found", http.StatusNotFound)
return
}

if c.contextProxies[name].source != DynamicCluster {
http.Error(w, "Cannot delete a static cluster", http.StatusForbidden)
return
}

delete(c.contextProxies, name)
fmt.Printf("Removed cluster \"%s\" proxy\n", name)

c.getConfig(w, r)
}

func (c *HeadlampConfig) addClusterSetupRoute(r *mux.Router) {
// We do not support this feature when in-cluster
if c.useInCluster {
return
}

r.HandleFunc("/cluster", c.addCluster).Methods("POST")

// Delete a cluster
r.HandleFunc("/cluster/{name}", c.deleteCluster).Methods("DELETE")
}

func absPath(path string) (string, error) {
Expand Down

0 comments on commit 081bf6b

Please sign in to comment.