forked from canonical/k8s-snap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow access to the microcluster app from rest endpoints (canonical#110)
- Loading branch information
1 parent
a787f52
commit a5831b3
Showing
11 changed files
with
165 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
package v1 | ||
|
||
// JoinNodeRequest is used to request to add a node to the cluster. | ||
type JoinNodeRequest struct { | ||
// JoinClusterRequest is used to request to add a node to the cluster. | ||
type JoinClusterRequest struct { | ||
Name string `json:"name"` | ||
Address string `json:"address"` | ||
Token string `json:"token"` | ||
} | ||
|
||
// JoinNodeResponse is the response from "POST 1.0/k8sd/cluster/{node}" | ||
type JoinNodeResponse struct{} | ||
// JoinClusterResponse is the response from "POST 1.0/k8sd/cluster/{node}" | ||
type JoinClusterResponse struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
apiv1 "github.com/canonical/k8s/api/v1" | ||
"github.com/canonical/k8s/pkg/k8sd/types" | ||
"github.com/canonical/lxd/lxd/response" | ||
"github.com/canonical/microcluster/microcluster" | ||
"github.com/canonical/microcluster/state" | ||
) | ||
|
||
func postClusterJoin(m *microcluster.MicroCluster, s *state.State, r *http.Request) response.Response { | ||
req := apiv1.JoinClusterRequest{} | ||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
return response.BadRequest(fmt.Errorf("failed to parse request: %w", err)) | ||
} | ||
|
||
// differentiate between control plane and worker node tokens | ||
info := &types.InternalWorkerNodeToken{} | ||
if info.Decode(req.Token) == nil { | ||
// valid worker node token | ||
if err := m.NewCluster(req.Name, req.Address, map[string]string{"workerToken": req.Token}, time.Second*180); err != nil { | ||
return response.InternalError(fmt.Errorf("failed to join k8sd cluster as worker: %w", err)) | ||
} | ||
} else { | ||
if err := m.JoinCluster(req.Name, req.Address, req.Token, nil, time.Second*180); err != nil { | ||
return response.InternalError(fmt.Errorf("failed to join k8sd cluster as control plane: %w", err)) | ||
} | ||
} | ||
|
||
return response.SyncResponse(true, &apiv1.JoinClusterResponse{}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/canonical/lxd/lxd/response" | ||
"github.com/canonical/microcluster/microcluster" | ||
"github.com/canonical/microcluster/state" | ||
) | ||
|
||
// handler is the handler type for microcluster endpoints. | ||
type handler func(*state.State, *http.Request) response.Response | ||
|
||
// handlerWithMicroCluster is the handler type for endpoints that also need access to the microcluster instance. | ||
type handlerWithMicroCluster func(*microcluster.MicroCluster, *state.State, *http.Request) response.Response | ||
|
||
// wrapHandlerWithMicroCluster creates a microcluster handler from a handlerWithMicroCluster by capturing the microcluster instance. | ||
func wrapHandlerWithMicroCluster(m *microcluster.MicroCluster, handler handlerWithMicroCluster) handler { | ||
return func(s *state.State, r *http.Request) response.Response { | ||
return handler(m, s, r) | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.