2014-08-06 17:15:14 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-08-19 22:40:46 +00:00
|
|
|
package vagrant_cloud
|
2014-08-06 17:15:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2014-09-05 16:33:52 +00:00
|
|
|
"fmt"
|
2014-09-03 21:12:20 +00:00
|
|
|
"io"
|
2014-08-06 17:15:14 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
neturl "net/url"
|
|
|
|
"sort"
|
2014-08-19 22:40:46 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
2014-08-06 17:15:14 +00:00
|
|
|
)
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// VagrantCloud is an implementation of Interface, TCPLoadBalancer and Instances for developer managed Vagrant cluster.
|
2014-08-06 17:15:14 +00:00
|
|
|
type VagrantCloud struct {
|
|
|
|
saltURL string
|
|
|
|
saltUser string
|
|
|
|
saltPass string
|
|
|
|
saltAuth string
|
|
|
|
}
|
|
|
|
|
2014-08-19 23:40:47 +00:00
|
|
|
func init() {
|
2014-09-03 21:12:20 +00:00
|
|
|
cloudprovider.RegisterCloudProvider("vagrant", func(config io.Reader) (cloudprovider.Interface, error) { return newVagrantCloud() })
|
2014-08-19 23:40:47 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// SaltToken is an authorization token required by Salt REST API.
|
2014-08-06 17:15:14 +00:00
|
|
|
type SaltToken struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
User string `json:"user"`
|
|
|
|
EAuth string `json:"eauth"`
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// SaltLoginResponse is the response object for a /login operation against Salt REST API.
|
2014-08-06 17:15:14 +00:00
|
|
|
type SaltLoginResponse struct {
|
|
|
|
Data []SaltToken `json:"return"`
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// SaltMinion is a machine managed by the Salt service.
|
2014-08-06 17:15:14 +00:00
|
|
|
type SaltMinion struct {
|
|
|
|
Roles []string `json:"roles"`
|
2014-09-05 16:33:52 +00:00
|
|
|
IP string `json:"node_ip"`
|
2014-08-06 17:15:14 +00:00
|
|
|
Host string `json:"host"`
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// SaltMinions is a map of minion name to machine information.
|
2014-08-06 17:15:14 +00:00
|
|
|
type SaltMinions map[string]SaltMinion
|
|
|
|
|
|
|
|
// SaltMinionsResponse is the response object for a /minions operation against Salt REST API
|
|
|
|
type SaltMinionsResponse struct {
|
|
|
|
Minions []SaltMinions `json:"return"`
|
|
|
|
}
|
|
|
|
|
2014-08-19 23:40:47 +00:00
|
|
|
// newVagrantCloud creates a new instance of VagrantCloud configured to talk to the Salt REST API.
|
|
|
|
func newVagrantCloud() (*VagrantCloud, error) {
|
2014-08-06 17:15:14 +00:00
|
|
|
return &VagrantCloud{
|
|
|
|
saltURL: "http://127.0.0.1:8000",
|
|
|
|
saltUser: "vagrant",
|
|
|
|
saltPass: "vagrant",
|
|
|
|
saltAuth: "pam",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// TCPLoadBalancer returns an implementation of TCPLoadBalancer for Vagrant cloud.
|
2014-08-19 22:40:46 +00:00
|
|
|
func (v *VagrantCloud) TCPLoadBalancer() (cloudprovider.TCPLoadBalancer, bool) {
|
2014-08-06 17:15:14 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Instances returns an implementation of Instances for Vagrant cloud.
|
2014-08-19 22:40:46 +00:00
|
|
|
func (v *VagrantCloud) Instances() (cloudprovider.Instances, bool) {
|
2014-08-06 17:15:14 +00:00
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// Zones returns an implementation of Zones for Vagrant cloud.
|
2014-08-19 22:40:46 +00:00
|
|
|
func (v *VagrantCloud) Zones() (cloudprovider.Zones, bool) {
|
2014-08-06 17:15:14 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// IPAddress returns the address of a particular machine instance.
|
2014-08-06 17:15:14 +00:00
|
|
|
func (v *VagrantCloud) IPAddress(instance string) (net.IP, error) {
|
2014-09-05 16:33:52 +00:00
|
|
|
token, err := v.saltLogin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
minions, err := v.saltMinions(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filteredMinions := v.saltMinionsByRole(minions, "kubernetes-pool")
|
|
|
|
for _, minion := range filteredMinions {
|
|
|
|
fmt.Println("Minion: ", minion.Host, " , ", instance, " IP: ", minion.IP)
|
|
|
|
if minion.Host == instance {
|
|
|
|
return net.ParseIP(minion.IP), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unable to find IP address for instance:", instance)
|
2014-08-06 17:15:14 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// saltMinionsByRole filters a list of minions that have a matching role.
|
2014-08-06 17:15:14 +00:00
|
|
|
func (v *VagrantCloud) saltMinionsByRole(minions []SaltMinion, role string) []SaltMinion {
|
|
|
|
var filteredMinions []SaltMinion
|
|
|
|
for _, value := range minions {
|
|
|
|
sort.Strings(value.Roles)
|
|
|
|
if pos := sort.SearchStrings(value.Roles, role); pos < len(value.Roles) {
|
|
|
|
filteredMinions = append(filteredMinions, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredMinions
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// saltMinions invokes the Salt API for minions using provided token.
|
2014-08-06 17:15:14 +00:00
|
|
|
func (v *VagrantCloud) saltMinions(token SaltToken) ([]SaltMinion, error) {
|
|
|
|
var minions []SaltMinion
|
|
|
|
|
|
|
|
url := v.saltURL + "/minions"
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
req.Header.Add("X-Auth-Token", token.Token)
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return minions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var minionsResp SaltMinionsResponse
|
|
|
|
if err = json.Unmarshal(body, &minionsResp); err != nil {
|
|
|
|
return minions, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range minionsResp.Minions[0] {
|
|
|
|
minions = append(minions, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return minions, nil
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// saltLogin invokes the Salt API to get an authorization token.
|
2014-08-06 17:15:14 +00:00
|
|
|
func (v *VagrantCloud) saltLogin() (SaltToken, error) {
|
|
|
|
url := v.saltURL + "/login"
|
|
|
|
data := neturl.Values{
|
|
|
|
"username": {v.saltUser},
|
|
|
|
"password": {v.saltPass},
|
|
|
|
"eauth": {v.saltAuth},
|
|
|
|
}
|
|
|
|
|
|
|
|
var token SaltToken
|
|
|
|
resp, err := http.PostForm(url, data)
|
|
|
|
if err != nil {
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var loginResp SaltLoginResponse
|
|
|
|
if err := json.Unmarshal(body, &loginResp); err != nil {
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(loginResp.Data) == 0 {
|
|
|
|
return token, errors.New("No token found in response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return loginResp.Data[0], nil
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// List enumerates the set of minions instances known by the cloud provider.
|
2014-08-06 17:15:14 +00:00
|
|
|
func (v *VagrantCloud) List(filter string) ([]string, error) {
|
|
|
|
token, err := v.saltLogin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
minions, err := v.saltMinions(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredMinions := v.saltMinionsByRole(minions, "kubernetes-pool")
|
|
|
|
var instances []string
|
|
|
|
for _, instance := range filteredMinions {
|
2014-09-05 16:33:52 +00:00
|
|
|
instances = append(instances, instance.Host)
|
2014-08-06 17:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return instances, nil
|
|
|
|
}
|