|
| 1 | +# vSphere Shared Session capability |
| 2 | + |
| 3 | +One problem that can be found when provisioning a large amount of clusters using |
| 4 | +vSphere CSI 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 CSI. 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 CSI with the usage of |
| 15 | +the following CSI configuration: |
| 16 | + |
| 17 | +```shell |
| 18 | +[Global] |
| 19 | +ca-file = "/etc/ssl/certs/trusted-certificates.crt" |
| 20 | +[VirtualCenter "your-vcenter-host"] |
| 21 | +datacenters = "datacenter1" |
| 22 | +vc-session-manager-url = "https://some-session-manager/session" |
| 23 | +vc-session-manager-token = "a-secret-token" |
| 24 | +``` |
| 25 | + |
| 26 | +The configuration above will make CSI call the shared session rest API and use the |
| 27 | +provided token to authenticate against vSphere, instead of using a username/password. |
| 28 | + |
| 29 | +The parameter provider at `vc-session-manager-token` is sent as a `Authorization: Bearer` token |
| 30 | +to the session manager, and in case this directive is not configured CSI will send the |
| 31 | +Pod Service Account token instead. |
| 32 | + |
| 33 | +Below is an example implementation of a shared session manager rest API. Starting the |
| 34 | +program below and calling `http://127.0.0.1:18080/session` should return a JSON that is expected |
| 35 | +by CSI using session manager to work: |
| 36 | + |
| 37 | +```shell |
| 38 | +$ curl 127.0.0.1:18080/session |
| 39 | +{"token":"cst-VCT-52f8d061-aace-4506-f4e6-fca78293a93f-....."} |
| 40 | +``` |
| 41 | + |
| 42 | +**NOTE**: Below implementation is **NOT PRODUCTION READY** and does not implement |
| 43 | +any kind of authentication! |
| 44 | + |
| 45 | +```go |
| 46 | +package main |
| 47 | + |
| 48 | +import ( |
| 49 | + "context" |
| 50 | + "encoding/json" |
| 51 | + "log" |
| 52 | + "net/http" |
| 53 | + "net/url" |
| 54 | + |
| 55 | + "github.com/vmware/govmomi" |
| 56 | + "github.com/vmware/govmomi/session" |
| 57 | + "github.com/vmware/govmomi/vim25" |
| 58 | + "github.com/vmware/govmomi/vim25/soap" |
| 59 | +) |
| 60 | + |
| 61 | +const ( |
| 62 | + vcURL = "https://my-vc.tld" |
| 63 | + |
| 64 | + vcPassword = "somepassword" |
| 65 | +) |
| 66 | + |
| 67 | +var ( |
| 68 | + userPassword = url.UserPassword(vcUsername, vcPassword) |
| 69 | +) |
| 70 | + |
| 71 | +// SharedSessionResponse is the expected response of CPI when using Shared session manager |
| 72 | +type SharedSessionResponse struct { |
| 73 | + Token string `json:"token"` |
| 74 | +} |
| 75 | + |
| 76 | +func main() { |
| 77 | + ctx := context.Background() |
| 78 | + vcURL, err := soap.ParseURL(vcURL) |
| 79 | + if err != nil { |
| 80 | + panic(err) |
| 81 | + } |
| 82 | + soapClient := soap.NewClient(vcURL, false) |
| 83 | + c, err := vim25.NewClient(ctx, soapClient) |
| 84 | + if err != nil { |
| 85 | + panic(err) |
| 86 | + } |
| 87 | + client := &govmomi.Client{ |
| 88 | + Client: c, |
| 89 | + SessionManager: session.NewManager(c), |
| 90 | + } |
| 91 | + if err := client.SessionManager.Login(ctx, userPassword); err != nil { |
| 92 | + panic(err) |
| 93 | + } |
| 94 | + |
| 95 | + vcsession := func(w http.ResponseWriter, r *http.Request) { |
| 96 | + clonedtoken, err := client.SessionManager.AcquireCloneTicket(ctx) |
| 97 | + if err != nil { |
| 98 | + w.WriteHeader(http.StatusForbidden) |
| 99 | + return |
| 100 | + } |
| 101 | + token := &SharedSessionResponse{Token: clonedtoken} |
| 102 | + jsonT, err := json.Marshal(token) |
| 103 | + if err != nil { |
| 104 | + w.WriteHeader(http.StatusInternalServerError) |
| 105 | + return |
| 106 | + } |
| 107 | + w.WriteHeader(http.StatusOK) |
| 108 | + w.Write(jsonT) |
| 109 | + } |
| 110 | + |
| 111 | + http.HandleFunc("/session", vcsession) |
| 112 | + log.Printf("starting webserver on port 18080") |
| 113 | + http.ListenAndServe(":18080", nil) |
| 114 | +} |
| 115 | +``` |
0 commit comments