mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
Support persistent volume on Photon Controller platform
1. Enable Photon Controller as cloud provider 2. Support Photon persistent disk as volume source/persistent volume source
This commit is contained in:
@@ -22,6 +22,7 @@ go_library(
|
||||
"//pkg/cloudprovider/providers/mesos:go_default_library",
|
||||
"//pkg/cloudprovider/providers/openstack:go_default_library",
|
||||
"//pkg/cloudprovider/providers/ovirt:go_default_library",
|
||||
"//pkg/cloudprovider/providers/photon:go_default_library",
|
||||
"//pkg/cloudprovider/providers/rackspace:go_default_library",
|
||||
"//pkg/cloudprovider/providers/vsphere:go_default_library",
|
||||
],
|
||||
|
||||
37
pkg/cloudprovider/providers/photon/BUILD
Normal file
37
pkg/cloudprovider/providers/photon/BUILD
Normal file
@@ -0,0 +1,37 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["photon.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/cloudprovider:go_default_library",
|
||||
"//pkg/types:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
"//vendor:github.com/vmware/photon-controller-go-sdk/photon",
|
||||
"//vendor:gopkg.in/gcfg.v1",
|
||||
],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["photon_test.go"],
|
||||
library = "go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/cloudprovider:go_default_library",
|
||||
"//pkg/types:go_default_library",
|
||||
"//pkg/util/rand:go_default_library",
|
||||
],
|
||||
)
|
||||
4
pkg/cloudprovider/providers/photon/OWNERS
Normal file
4
pkg/cloudprovider/providers/photon/OWNERS
Normal file
@@ -0,0 +1,4 @@
|
||||
maintainers:
|
||||
- luomiao
|
||||
- kerneltime
|
||||
- abrarshivani
|
||||
573
pkg/cloudprovider/providers/photon/photon.go
Normal file
573
pkg/cloudprovider/providers/photon/photon.go
Normal file
@@ -0,0 +1,573 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
// This version of Photon cloud provider supports the disk interface
|
||||
// for Photon persistent disk volume plugin. LoadBalancer, Routes, and
|
||||
// Zones are currently not supported.
|
||||
// The use of Photon cloud provider requires to start kubelet, kube-apiserver,
|
||||
// and kube-controller-manager with config flag: '--cloud-provider=photon
|
||||
// --cloud-config=[path_to_config_file]'. When running multi-node kubernetes
|
||||
// using docker, the config file should be located inside /etc/kubernetes.
|
||||
package photon
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/golang/glog"
|
||||
"github.com/vmware/photon-controller-go-sdk/photon"
|
||||
"gopkg.in/gcfg.v1"
|
||||
"io"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/cloudprovider"
|
||||
k8stypes "k8s.io/kubernetes/pkg/types"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
ProviderName = "photon"
|
||||
DiskSpecKind = "persistent-disk"
|
||||
)
|
||||
|
||||
// Global variable pointing to photon client
|
||||
var photonClient *photon.Client
|
||||
var logger *log.Logger = nil
|
||||
|
||||
// overrideIP indicates if the hostname is overriden by IP address, such as when
|
||||
// running multi-node kubernetes using docker. In this case the user should set
|
||||
// overrideIP = true in cloud config file. Default value is false.
|
||||
var overrideIP bool = false
|
||||
|
||||
// Photon is an implementation of the cloud provider interface for Photon Controller.
|
||||
type PCCloud struct {
|
||||
cfg *PCConfig
|
||||
// InstanceID of the server where this PCCloud object is instantiated.
|
||||
localInstanceID string
|
||||
// local $HOSTNAME
|
||||
localHostname string
|
||||
// hostname from K8S, could be overridden
|
||||
localK8sHostname string
|
||||
// Photon project ID. We assume that there is only one Photon Controller project
|
||||
// in the environment per current Photon Controller deployment methodology.
|
||||
projID string
|
||||
cloudprovider.Zone
|
||||
}
|
||||
|
||||
type PCConfig struct {
|
||||
Global struct {
|
||||
// the Photon Controller endpoint IP address
|
||||
CloudTarget string `gcfg:"target"`
|
||||
// when the Photon Controller authentication is enabled, set to true;
|
||||
// otherwise, set to false.
|
||||
IgnoreCertificate bool `gcfg:"ignoreCertificate"`
|
||||
// Photon Controller tenant name
|
||||
Tenant string `gcfg:"tenant"`
|
||||
// Photon Controller project name
|
||||
Project string `gcfg:"project"`
|
||||
// when kubelet is started with '--hostname-override=${IP_ADDRESS}', set to true;
|
||||
// otherwise, set to false.
|
||||
OverrideIP bool `gcfg:"overrideIP"`
|
||||
}
|
||||
}
|
||||
|
||||
// Disks is interface for manipulation with PhotonController Persistent Disks.
|
||||
type Disks interface {
|
||||
// AttachDisk attaches given disk to given node. Current node
|
||||
// is used when nodeName is empty string.
|
||||
AttachDisk(pdID string, nodeName k8stypes.NodeName) error
|
||||
|
||||
// DetachDisk detaches given disk to given node. Current node
|
||||
// is used when nodeName is empty string.
|
||||
DetachDisk(pdID string, nodeName k8stypes.NodeName) error
|
||||
|
||||
// DiskIsAttached checks if a disk is attached to the given node.
|
||||
DiskIsAttached(pdID string, nodeName k8stypes.NodeName) (bool, error)
|
||||
|
||||
// DisksAreAttached is a batch function to check if a list of disks are attached
|
||||
// to the node with the specified NodeName.
|
||||
DisksAreAttached(pdIDs []string, nodeName k8stypes.NodeName) (map[string]bool, error)
|
||||
|
||||
// CreateDisk creates a new PD with given properties.
|
||||
CreateDisk(volumeOptions *VolumeOptions) (pdID string, err error)
|
||||
|
||||
// DeleteDisk deletes PD.
|
||||
DeleteDisk(pdID string) error
|
||||
}
|
||||
|
||||
// VolumeOptions specifies capacity, tags, name and flavorID for a volume.
|
||||
type VolumeOptions struct {
|
||||
CapacityGB int
|
||||
Tags map[string]string
|
||||
Name string
|
||||
Flavor string
|
||||
}
|
||||
|
||||
func logError(msg string, err error) error {
|
||||
s := "Photon Cloud Provider: " + msg + ". Error [" + err.Error() + "]"
|
||||
glog.Errorf(s)
|
||||
return fmt.Errorf(s)
|
||||
}
|
||||
|
||||
func readConfig(config io.Reader) (PCConfig, error) {
|
||||
if config == nil {
|
||||
err := fmt.Errorf("cloud provider config file is missing. Please restart kubelet with --cloud-provider=photon --cloud-config=[path_to_config_file]")
|
||||
return PCConfig{}, err
|
||||
}
|
||||
|
||||
var cfg PCConfig
|
||||
err := gcfg.ReadInto(&cfg, config)
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
func init() {
|
||||
cloudprovider.RegisterCloudProvider(ProviderName, func(config io.Reader) (cloudprovider.Interface, error) {
|
||||
cfg, err := readConfig(config)
|
||||
if err != nil {
|
||||
return nil, logError("failed to read in cloud provider config file", err)
|
||||
}
|
||||
return newPCCloud(cfg)
|
||||
})
|
||||
}
|
||||
|
||||
// Retrieve the Photon VM ID from the Photon Controller endpoint based on the node name
|
||||
func getVMIDbyNodename(project string, nodeName string) (string, error) {
|
||||
vmList, err := photonClient.Projects.GetVMs(project, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to GetVMs from project %s with nodeName %s, error: [%v]", project, nodeName, err)
|
||||
}
|
||||
|
||||
for _, vm := range vmList.Items {
|
||||
if vm.Name == nodeName {
|
||||
if vm.State == "STARTED" {
|
||||
return vm.ID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("No matching started VM is found with name %s", nodeName)
|
||||
}
|
||||
|
||||
// Retrieve the Photon VM ID from the Photon Controller endpoint based on the IP address
|
||||
func getVMIDbyIP(project string, IPAddress string) (string, error) {
|
||||
vmList, err := photonClient.Projects.GetVMs(project, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to GetVMs for project %s, error [%v]", project, err)
|
||||
}
|
||||
|
||||
for _, vm := range vmList.Items {
|
||||
task, err := photonClient.VMs.GetNetworks(vm.ID)
|
||||
if err != nil {
|
||||
glog.Warningf("Photon Cloud Provider: GetNetworks failed for vm.ID %s, error [%v]", vm.ID, err)
|
||||
} else {
|
||||
task, err = photonClient.Tasks.Wait(task.ID)
|
||||
if err != nil {
|
||||
glog.Warning("Photon Cloud Provider: Wait task for GetNetworks failed for vm.ID %s, error [%v]", vm.ID, err)
|
||||
} else {
|
||||
networkConnections := task.ResourceProperties.(map[string]interface{})
|
||||
networks := networkConnections["networkConnections"].([]interface{})
|
||||
for _, nt := range networks {
|
||||
network := nt.(map[string]interface{})
|
||||
if val, ok := network["ipAddress"]; ok && val != nil {
|
||||
ipAddr := val.(string)
|
||||
if ipAddr == IPAddress {
|
||||
return vm.ID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("No matching VM is found with IP %s", IPAddress)
|
||||
}
|
||||
|
||||
// Retrieve the the Photon project ID from the Photon Controller endpoint based on the project name
|
||||
func getProjIDbyName(tenantName, projName string) (string, error) {
|
||||
tenants, err := photonClient.Tenants.GetAll()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("GetAll tenants failed with error [%v]", err)
|
||||
}
|
||||
|
||||
for _, tenant := range tenants.Items {
|
||||
if tenant.Name == tenantName {
|
||||
projects, err := photonClient.Tenants.GetProjects(tenant.ID, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to GetProjects for tenant %s, error [%v]", tenantName, err)
|
||||
}
|
||||
|
||||
for _, project := range projects.Items {
|
||||
if project.Name == projName {
|
||||
return project.ID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("No matching tenant/project name is found with %s/%s", tenantName, projName)
|
||||
}
|
||||
|
||||
func newPCCloud(cfg PCConfig) (*PCCloud, error) {
|
||||
if len(cfg.Global.CloudTarget) == 0 {
|
||||
return nil, fmt.Errorf("Photon Controller endpoint was not specified.")
|
||||
}
|
||||
|
||||
//TODO: add handling of certification enabled situation
|
||||
options := &photon.ClientOptions{
|
||||
IgnoreCertificate: cfg.Global.IgnoreCertificate,
|
||||
}
|
||||
|
||||
photonClient = photon.NewClient(cfg.Global.CloudTarget, options, logger)
|
||||
status, err := photonClient.Status.Get()
|
||||
if err != nil {
|
||||
return nil, logError("new client creation failed", err)
|
||||
}
|
||||
glog.V(2).Info("Photon Cloud Provider: Status of the new photon controller client: %v", status)
|
||||
|
||||
// Get Photon Controller project ID for future use
|
||||
projID, err := getProjIDbyName(cfg.Global.Tenant, cfg.Global.Project)
|
||||
if err != nil {
|
||||
return nil, logError("getProjIDbyName failed when creating new Photon Controller client", err)
|
||||
}
|
||||
|
||||
// Get local hostname for localInstanceID
|
||||
cmd := exec.Command("bash", "-c", `echo $HOSTNAME`)
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return nil, logError("get local hostname bash command failed", err)
|
||||
}
|
||||
if out.Len() == 0 {
|
||||
return nil, logError("unable to retrieve hostname for Instance ID", nil)
|
||||
}
|
||||
hostname := strings.TrimRight(out.String(), "\n")
|
||||
vmID, err := getVMIDbyNodename(projID, hostname)
|
||||
if err != nil {
|
||||
return nil, logError("getVMIDbyNodename failed when creating new Photon Controller client", err)
|
||||
}
|
||||
|
||||
pc := PCCloud{
|
||||
cfg: &cfg,
|
||||
localInstanceID: vmID,
|
||||
localHostname: hostname,
|
||||
localK8sHostname: "",
|
||||
projID: projID,
|
||||
}
|
||||
|
||||
overrideIP = cfg.Global.OverrideIP
|
||||
|
||||
return &pc, nil
|
||||
}
|
||||
|
||||
// Instances returns an implementation of Instances for Photon Controller.
|
||||
func (pc *PCCloud) Instances() (cloudprovider.Instances, bool) {
|
||||
return pc, true
|
||||
}
|
||||
|
||||
// List is an implementation of Instances.List.
|
||||
func (pc *PCCloud) List(filter string) ([]k8stypes.NodeName, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// NodeAddresses is an implementation of Instances.NodeAddresses.
|
||||
func (pc *PCCloud) NodeAddresses(nodeName k8stypes.NodeName) ([]api.NodeAddress, error) {
|
||||
addrs := []api.NodeAddress{}
|
||||
name := string(nodeName)
|
||||
|
||||
var vmID string
|
||||
var err error
|
||||
if name == pc.localK8sHostname {
|
||||
vmID = pc.localInstanceID
|
||||
} else {
|
||||
vmID, err = getInstanceID(name, pc.projID)
|
||||
if err != nil {
|
||||
return addrs, logError("getInstanceID failed for NodeAddresses", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve the Photon VM's IP addresses from the Photon Controller endpoint based on the VM ID
|
||||
vmList, err := photonClient.Projects.GetVMs(pc.projID, nil)
|
||||
if err != nil {
|
||||
return addrs, fmt.Errorf("Photon Cloud Provider: Failed to GetVMs for project %s, error [%v]", pc.projID, err)
|
||||
}
|
||||
|
||||
for _, vm := range vmList.Items {
|
||||
if vm.ID == vmID {
|
||||
task, err := photonClient.VMs.GetNetworks(vm.ID)
|
||||
if err != nil {
|
||||
return addrs, logError("GetNetworks failed for node "+name+" with vm.ID "+vm.ID, err)
|
||||
} else {
|
||||
task, err = photonClient.Tasks.Wait(task.ID)
|
||||
if err != nil {
|
||||
return addrs, logError("Wait task for GetNetworks failed for node"+name+" with vm.ID "+vm.ID, err)
|
||||
} else {
|
||||
networkConnections := task.ResourceProperties.(map[string]interface{})
|
||||
networks := networkConnections["networkConnections"].([]interface{})
|
||||
for _, nt := range networks {
|
||||
network := nt.(map[string]interface{})
|
||||
if val, ok := network["ipAddress"]; ok && val != nil {
|
||||
ipAddr := val.(string)
|
||||
if ipAddr != "-" {
|
||||
api.AddToNodeAddresses(&addrs,
|
||||
api.NodeAddress{
|
||||
// TODO: figure out the type of the IP
|
||||
Type: api.NodeInternalIP,
|
||||
Address: ipAddr,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
return addrs, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return addrs, logError("Failed to find the node "+name+" from Photon Controller endpoint", nil)
|
||||
}
|
||||
|
||||
func (pc *PCCloud) AddSSHKeyToAllInstances(user string, keyData []byte) error {
|
||||
return errors.New("unimplemented")
|
||||
}
|
||||
|
||||
func (pc *PCCloud) CurrentNodeName(hostname string) (k8stypes.NodeName, error) {
|
||||
pc.localK8sHostname = hostname
|
||||
return k8stypes.NodeName(hostname), nil
|
||||
}
|
||||
|
||||
func getInstanceID(name string, projID string) (string, error) {
|
||||
var vmID string
|
||||
var err error
|
||||
|
||||
if overrideIP == true {
|
||||
vmID, err = getVMIDbyIP(projID, name)
|
||||
} else {
|
||||
vmID, err = getVMIDbyNodename(projID, name)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if vmID == "" {
|
||||
err = cloudprovider.InstanceNotFound
|
||||
}
|
||||
|
||||
return vmID, err
|
||||
}
|
||||
|
||||
// ExternalID returns the cloud provider ID of the specified instance (deprecated).
|
||||
func (pc *PCCloud) ExternalID(nodeName k8stypes.NodeName) (string, error) {
|
||||
name := string(nodeName)
|
||||
if name == pc.localK8sHostname {
|
||||
return pc.localInstanceID, nil
|
||||
} else {
|
||||
ID, err := getInstanceID(name, pc.projID)
|
||||
if err != nil {
|
||||
return ID, logError("getInstanceID failed for ExternalID", err)
|
||||
} else {
|
||||
return ID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// InstanceID returns the cloud provider ID of the specified instance.
|
||||
func (pc *PCCloud) InstanceID(nodeName k8stypes.NodeName) (string, error) {
|
||||
name := string(nodeName)
|
||||
if name == pc.localK8sHostname {
|
||||
return pc.localInstanceID, nil
|
||||
} else {
|
||||
ID, err := getInstanceID(name, pc.projID)
|
||||
if err != nil {
|
||||
return ID, logError("getInstanceID failed for InstanceID", err)
|
||||
} else {
|
||||
return ID, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *PCCloud) InstanceType(nodeName k8stypes.NodeName) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (pc *PCCloud) Clusters() (cloudprovider.Clusters, bool) {
|
||||
return nil, true
|
||||
}
|
||||
|
||||
// ProviderName returns the cloud provider ID.
|
||||
func (pc *PCCloud) ProviderName() string {
|
||||
return ProviderName
|
||||
}
|
||||
|
||||
// LoadBalancer returns an implementation of LoadBalancer for Photon Controller.
|
||||
func (pc *PCCloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Zones returns an implementation of Zones for Photon Controller.
|
||||
func (pc *PCCloud) Zones() (cloudprovider.Zones, bool) {
|
||||
glog.V(4).Info("Claiming to support Zones")
|
||||
return pc, true
|
||||
}
|
||||
|
||||
func (pc *PCCloud) GetZone() (cloudprovider.Zone, error) {
|
||||
return pc.Zone, nil
|
||||
}
|
||||
|
||||
// Routes returns a false since the interface is not supported for photon controller.
|
||||
func (pc *PCCloud) Routes() (cloudprovider.Routes, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// ScrubDNS filters DNS settings for pods.
|
||||
func (pc *PCCloud) ScrubDNS(nameservers, searches []string) (nsOut, srchOut []string) {
|
||||
return nameservers, searches
|
||||
}
|
||||
|
||||
// Attaches given virtual disk volume to the compute running kubelet.
|
||||
func (pc *PCCloud) AttachDisk(pdID string, nodeName k8stypes.NodeName) error {
|
||||
operation := &photon.VmDiskOperation{
|
||||
DiskID: pdID,
|
||||
}
|
||||
|
||||
vmID, err := pc.InstanceID(nodeName)
|
||||
if err != nil {
|
||||
return logError("pc.InstanceID failed for AttachDisk", err)
|
||||
}
|
||||
|
||||
task, err := photonClient.VMs.AttachDisk(vmID, operation)
|
||||
if err != nil {
|
||||
return logError("Failed to attach disk with pdID "+pdID, err)
|
||||
}
|
||||
|
||||
_, err = photonClient.Tasks.Wait(task.ID)
|
||||
if err != nil {
|
||||
return logError("Failed to wait for task to attach disk with pdID "+pdID, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Detaches given virtual disk volume from the compute running kubelet.
|
||||
func (pc *PCCloud) DetachDisk(pdID string, nodeName k8stypes.NodeName) error {
|
||||
operation := &photon.VmDiskOperation{
|
||||
DiskID: pdID,
|
||||
}
|
||||
|
||||
vmID, err := pc.InstanceID(nodeName)
|
||||
if err != nil {
|
||||
return logError("pc.InstanceID failed for DetachDisk", err)
|
||||
}
|
||||
|
||||
task, err := photonClient.VMs.DetachDisk(vmID, operation)
|
||||
if err != nil {
|
||||
return logError("Failed to detach disk with pdID "+pdID, err)
|
||||
}
|
||||
|
||||
_, err = photonClient.Tasks.Wait(task.ID)
|
||||
if err != nil {
|
||||
return logError("Failed to wait for task to detach disk with pdID "+pdID, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DiskIsAttached returns if disk is attached to the VM using controllers supported by the plugin.
|
||||
func (pc *PCCloud) DiskIsAttached(pdID string, nodeName k8stypes.NodeName) (bool, error) {
|
||||
disk, err := photonClient.Disks.Get(pdID)
|
||||
if err != nil {
|
||||
return false, logError("Failed to Get disk with pdID "+pdID, err)
|
||||
}
|
||||
|
||||
vmID, err := pc.InstanceID(nodeName)
|
||||
if err != nil {
|
||||
return false, logError("pc.InstanceID failed for DiskIsAttached", err)
|
||||
}
|
||||
|
||||
for _, vm := range disk.VMs {
|
||||
if strings.Compare(vm, vmID) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// DisksAreAttached returns if disks are attached to the VM using controllers supported by the plugin.
|
||||
func (pc *PCCloud) DisksAreAttached(pdIDs []string, nodeName k8stypes.NodeName) (map[string]bool, error) {
|
||||
attached := make(map[string]bool)
|
||||
for _, pdID := range pdIDs {
|
||||
attached[pdID] = false
|
||||
}
|
||||
|
||||
vmID, err := pc.InstanceID(nodeName)
|
||||
if err != nil {
|
||||
return attached, logError("pc.InstanceID failed for DiskIsAttached", err)
|
||||
}
|
||||
|
||||
for _, pdID := range pdIDs {
|
||||
disk, err := photonClient.Disks.Get(pdID)
|
||||
if err != nil {
|
||||
glog.Warningf("Photon Cloud Provider: failed to get VMs for persistent disk %s, err [%v]", pdID, err)
|
||||
} else {
|
||||
for _, vm := range disk.VMs {
|
||||
if strings.Compare(vm, vmID) == 0 {
|
||||
attached[pdID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return attached, nil
|
||||
}
|
||||
|
||||
// Create a volume of given size (in GB).
|
||||
func (pc *PCCloud) CreateDisk(volumeOptions *VolumeOptions) (pdID string, err error) {
|
||||
diskSpec := photon.DiskCreateSpec{}
|
||||
diskSpec.Name = volumeOptions.Name
|
||||
diskSpec.Flavor = volumeOptions.Flavor
|
||||
diskSpec.CapacityGB = volumeOptions.CapacityGB
|
||||
diskSpec.Kind = DiskSpecKind
|
||||
|
||||
task, err := photonClient.Projects.CreateDisk(pc.projID, &diskSpec)
|
||||
if err != nil {
|
||||
return "", logError("Failed to CreateDisk", err)
|
||||
}
|
||||
|
||||
waitTask, err := photonClient.Tasks.Wait(task.ID)
|
||||
if err != nil {
|
||||
return "", logError("Failed to wait for task to CreateDisk", err)
|
||||
}
|
||||
|
||||
return waitTask.Entity.ID, nil
|
||||
}
|
||||
|
||||
// Deletes a volume given volume name.
|
||||
func (pc *PCCloud) DeleteDisk(pdID string) error {
|
||||
task, err := photonClient.Disks.Delete(pdID)
|
||||
if err != nil {
|
||||
return logError("Failed to DeleteDisk", err)
|
||||
}
|
||||
|
||||
_, err = photonClient.Tasks.Wait(task.ID)
|
||||
if err != nil {
|
||||
return logError("Failed to wait for task to DeleteDisk", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
216
pkg/cloudprovider/providers/photon/photon_test.go
Normal file
216
pkg/cloudprovider/providers/photon/photon_test.go
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package photon
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/cloudprovider"
|
||||
"k8s.io/kubernetes/pkg/types"
|
||||
"k8s.io/kubernetes/pkg/util/rand"
|
||||
)
|
||||
|
||||
func configFromEnv() (TestVM string, TestFlavor string, cfg PCConfig, ok bool) {
|
||||
var IgnoreCertificate bool
|
||||
var OverrideIP bool
|
||||
var err error
|
||||
cfg.Global.CloudTarget = os.Getenv("PHOTON_TARGET")
|
||||
cfg.Global.Tenant = os.Getenv("PHOTON_TENANT")
|
||||
cfg.Global.Project = os.Getenv("PHOTON_PROJECT")
|
||||
if os.Getenv("PHOTON_IGNORE_CERTIFICATE") != "" {
|
||||
IgnoreCertificate, err = strconv.ParseBool(os.Getenv("PHOTON_IGNORE_CERTIFICATE"))
|
||||
} else {
|
||||
IgnoreCertificate = false
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
cfg.Global.IgnoreCertificate = IgnoreCertificate
|
||||
if os.Getenv("PHOTON_OVERRIDE_IP") != "" {
|
||||
OverrideIP, err = strconv.ParseBool(os.Getenv("PHOTON_OVERRIDE_IP"))
|
||||
} else {
|
||||
OverrideIP = false
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
cfg.Global.OverrideIP = OverrideIP
|
||||
|
||||
TestVM = os.Getenv("PHOTON_TEST_VM")
|
||||
if os.Getenv("PHOTON_TEST_FLAVOR") != "" {
|
||||
TestFlavor = os.Getenv("PHOTON_TEST_FLAVOR")
|
||||
} else {
|
||||
TestFlavor = ""
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ok = (cfg.Global.CloudTarget != "" &&
|
||||
cfg.Global.Tenant != "" &&
|
||||
cfg.Global.Project != "" &&
|
||||
TestVM != "")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func TestReadConfig(t *testing.T) {
|
||||
_, err := readConfig(nil)
|
||||
if err == nil {
|
||||
t.Errorf("Should fail when no config is provided: %s", err)
|
||||
}
|
||||
|
||||
cfg, err := readConfig(strings.NewReader(`
|
||||
[Global]
|
||||
target = 0.0.0.0
|
||||
ignoreCertificate = true
|
||||
tenant = tenant
|
||||
project = project
|
||||
overrideIP = false
|
||||
`))
|
||||
if err != nil {
|
||||
t.Fatalf("Should succeed when a valid config is provided: %s", err)
|
||||
}
|
||||
|
||||
if cfg.Global.CloudTarget != "0.0.0.0" {
|
||||
t.Errorf("incorrect photon target ip: %s", cfg.Global.CloudTarget)
|
||||
}
|
||||
|
||||
if cfg.Global.Tenant != "tenant" {
|
||||
t.Errorf("incorrect tenant: %s", cfg.Global.Tenant)
|
||||
}
|
||||
|
||||
if cfg.Global.Project != "project" {
|
||||
t.Errorf("incorrect project: %s", cfg.Global.Project)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPCCloud(t *testing.T) {
|
||||
_, _, cfg, ok := configFromEnv()
|
||||
if !ok {
|
||||
t.Skipf("No config found in environment")
|
||||
}
|
||||
|
||||
_, err := newPCCloud(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create new Photon client: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstances(t *testing.T) {
|
||||
testVM, _, cfg, ok := configFromEnv()
|
||||
if !ok {
|
||||
t.Skipf("No config found in environment")
|
||||
}
|
||||
NodeName := types.NodeName(testVM)
|
||||
|
||||
pc, err := newPCCloud(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create new Photon client: %s", err)
|
||||
}
|
||||
|
||||
i, ok := pc.Instances()
|
||||
if !ok {
|
||||
t.Fatalf("Instances() returned false")
|
||||
}
|
||||
|
||||
externalId, err := i.ExternalID(NodeName)
|
||||
if err != nil {
|
||||
t.Fatalf("Instances.ExternalID(%s) failed: %s", testVM, err)
|
||||
}
|
||||
t.Logf("Found ExternalID(%s) = %s\n", testVM, externalId)
|
||||
|
||||
nonExistingVM := types.NodeName(rand.String(15))
|
||||
externalId, err = i.ExternalID(nonExistingVM)
|
||||
if err == cloudprovider.InstanceNotFound {
|
||||
t.Logf("VM %s was not found as expected\n", nonExistingVM)
|
||||
} else if err == nil {
|
||||
t.Fatalf("Instances.ExternalID did not fail as expected, VM %s was found", nonExistingVM)
|
||||
} else {
|
||||
t.Fatalf("Instances.ExternalID did not fail as expected, err: %v", err)
|
||||
}
|
||||
|
||||
instanceId, err := i.InstanceID(NodeName)
|
||||
if err != nil {
|
||||
t.Fatalf("Instances.InstanceID(%s) failed: %s", testVM, err)
|
||||
}
|
||||
t.Logf("Found InstanceID(%s) = %s\n", testVM, instanceId)
|
||||
|
||||
instanceId, err = i.InstanceID(nonExistingVM)
|
||||
if err == cloudprovider.InstanceNotFound {
|
||||
t.Logf("VM %s was not found as expected\n", nonExistingVM)
|
||||
} else if err == nil {
|
||||
t.Fatalf("Instances.InstanceID did not fail as expected, VM %s was found", nonExistingVM)
|
||||
} else {
|
||||
t.Fatalf("Instances.InstanceID did not fail as expected, err: %v", err)
|
||||
}
|
||||
|
||||
addrs, err := i.NodeAddresses(NodeName)
|
||||
if err != nil {
|
||||
t.Fatalf("Instances.NodeAddresses(%s) failed: %s", testVM, err)
|
||||
}
|
||||
t.Logf("Found NodeAddresses(%s) = %s\n", testVM, addrs)
|
||||
}
|
||||
|
||||
func TestVolumes(t *testing.T) {
|
||||
testVM, testFlavor, cfg, ok := configFromEnv()
|
||||
if !ok {
|
||||
t.Skipf("No config found in environment")
|
||||
}
|
||||
|
||||
pc, err := newPCCloud(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create new Photon client: %s", err)
|
||||
}
|
||||
|
||||
NodeName := types.NodeName(testVM)
|
||||
|
||||
volumeOptions := &VolumeOptions{
|
||||
CapacityGB: 2,
|
||||
Tags: nil,
|
||||
Name: "kubernetes-test-volume-" + rand.String(10),
|
||||
Flavor: testFlavor}
|
||||
|
||||
pdID, err := pc.CreateDisk(volumeOptions)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot create a Photon persistent disk: %v", err)
|
||||
}
|
||||
|
||||
err = pc.AttachDisk(pdID, NodeName)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot attach persistent disk(%s) to VM(%s): %v", pdID, testVM, err)
|
||||
}
|
||||
|
||||
_, err = pc.DiskIsAttached(pdID, NodeName)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot attach persistent disk(%s) to VM(%s): %v", pdID, testVM, err)
|
||||
}
|
||||
|
||||
err = pc.DetachDisk(pdID, NodeName)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot detach persisten disk(%s) from VM(%s): %v", pdID, testVM, err)
|
||||
}
|
||||
|
||||
err = pc.DeleteDisk(pdID)
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot delete persisten disk(%s): %v", pdID, err)
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
_ "k8s.io/kubernetes/pkg/cloudprovider/providers/mesos"
|
||||
_ "k8s.io/kubernetes/pkg/cloudprovider/providers/openstack"
|
||||
_ "k8s.io/kubernetes/pkg/cloudprovider/providers/ovirt"
|
||||
_ "k8s.io/kubernetes/pkg/cloudprovider/providers/photon"
|
||||
_ "k8s.io/kubernetes/pkg/cloudprovider/providers/rackspace"
|
||||
_ "k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user