|
| 1 | +# vSphere Shared Session capability |
| 2 | + |
| 3 | +One problem that can be found when provisioning a large amount of clusters using |
| 4 | +vSphere Cloud Provider is vCenter session exhaustion. This happens because every |
| 5 | +workload cluster needs to request a new session to vSphere to do proper reconciliation. |
| 6 | + |
| 7 | +vSphere 8.0U3 and up uses a new approach of session management, that allows the |
| 8 | +creation and sharing of the sessions among different clusters. |
| 9 | + |
| 10 | +A cluster admin can implement a rest API that, once called, requests a new vCenter |
| 11 | +session and shares with CPI. This session will not count on the total generated |
| 12 | +sessions of vSphere, and instead will be a child derived session. |
| 13 | + |
| 14 | +This configuration can be applied on vSphere Cloud Provider with the usage of |
| 15 | +the following secret/credentials, instead of vSphere Username/password: |
| 16 | + |
| 17 | +```yaml |
| 18 | +apiVersion: v1 |
| 19 | +kind: Secret |
| 20 | +metadata: |
| 21 | + namespace: kube-system |
| 22 | + name: vsphere-cloud-secret |
| 23 | +stringData: |
| 24 | + your-vcenter-host.vc-session-manager-url: "https://shared-session-service.tld/session" |
| 25 | + your-vcenter-host.vc-session-manager-token: "authenticationtoken" |
| 26 | +``` |
| 27 | +
|
| 28 | +The configuration above will make CPI call the shared session rest API and use the |
| 29 | +provided token to authenticate against vSphere, instead of using a username/password. |
| 30 | +
|
| 31 | +The parameter provider at `vc-session-manager-token` is sent as a `Authorization: Bearer` token |
| 32 | +to the session manager, and in case this directive is not configured CPI will send the |
| 33 | +Pod Service Account token instead. |
| 34 | + |
| 35 | +Below is an example implementation of a shared session manager rest API. Starting the |
| 36 | +program below and calling "http://127.0.0.1:18080/session" should return a JSON that is expected |
| 37 | +by CPI using session manager to work: |
| 38 | +``` |
| 39 | +$ curl 127.0.0.1:18080/session |
| 40 | +{"token":"cst-VCT-52f8d061-aace-4506-f4e6-fca78293a93f-....."} |
| 41 | +``` |
| 42 | + |
| 43 | +**NOTE**: Below implementation is **NOT PRODUCTION READY** and does not implement |
| 44 | +any kind of authentication! |
| 45 | + |
| 46 | +```go |
| 47 | +package main |
| 48 | +
|
| 49 | +import ( |
| 50 | + "context" |
| 51 | + "encoding/json" |
| 52 | + "log" |
| 53 | + "net/http" |
| 54 | + "net/url" |
| 55 | +
|
| 56 | + "github.com/vmware/govmomi" |
| 57 | + "github.com/vmware/govmomi/session" |
| 58 | + "github.com/vmware/govmomi/vim25" |
| 59 | + "github.com/vmware/govmomi/vim25/soap" |
| 60 | +) |
| 61 | +
|
| 62 | +const ( |
| 63 | + vcURL = "https://my-vc.tld" |
| 64 | + |
| 65 | + vcPassword = "somepassword" |
| 66 | +) |
| 67 | +
|
| 68 | +var ( |
| 69 | + userPassword = url.UserPassword(vcUsername, vcPassword) |
| 70 | +) |
| 71 | +
|
| 72 | +// SharedSessionResponse is the expected response of CPI when using Shared session |
| 73 | +// manager |
| 74 | +type SharedSessionResponse struct { |
| 75 | + Token string `json:"token"` |
| 76 | +} |
| 77 | + |
| 78 | +func main() { |
| 79 | + ctx := context.Background() |
| 80 | + vcURL, err := soap.ParseURL(vcURL) |
| 81 | + if err != nil { |
| 82 | + panic(err) |
| 83 | + } |
| 84 | + soapClient := soap.NewClient(vcURL, false) |
| 85 | + c, err := vim25.NewClient(ctx, soapClient) |
| 86 | + if err != nil { |
| 87 | + panic(err) |
| 88 | + } |
| 89 | + client := &govmomi.Client{ |
| 90 | + Client: c, |
| 91 | + SessionManager: session.NewManager(c), |
| 92 | + } |
| 93 | + if err := client.SessionManager.Login(ctx, userPassword); err != nil { |
| 94 | + panic(err) |
| 95 | + } |
| 96 | + |
| 97 | + vcsession := func(w http.ResponseWriter, r *http.Request) { |
| 98 | + clonedtoken, err := client.SessionManager.AcquireCloneTicket(ctx) |
| 99 | + if err != nil { |
| 100 | + w.WriteHeader(http.StatusForbidden) |
| 101 | + return |
| 102 | + } |
| 103 | + token := &SharedSessionResponse{Token: clonedtoken} |
| 104 | + jsonT, err := json.Marshal(token) |
| 105 | + if err != nil { |
| 106 | + w.WriteHeader(http.StatusInternalServerError) |
| 107 | + return |
| 108 | + } |
| 109 | + w.WriteHeader(http.StatusOK) |
| 110 | + w.Write(jsonT) |
| 111 | + } |
| 112 | + |
| 113 | + http.HandleFunc("/session", vcsession) |
| 114 | + log.Printf("starting webserver on port 18080") |
| 115 | + http.ListenAndServe(":18080", nil) |
| 116 | +} |
| 117 | +``` |
| 118 | + |
0 commit comments