143 lines
3.2 KiB
Go
143 lines
3.2 KiB
Go
/*
|
|
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 pvecloud
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
|
|
lb "git.giftfish.de/ston1th/haproxy-lb/pkg/api/client"
|
|
lbclient "git.giftfish.de/ston1th/haproxy-lb/pkg/api/v1/client"
|
|
pve "git.giftfish.de/ston1th/pve-go"
|
|
cloudprovider "k8s.io/cloud-provider"
|
|
)
|
|
|
|
const (
|
|
ProviderName = pve.PVEScheme
|
|
scheme = pve.PVESchemeURL
|
|
)
|
|
|
|
type cloud struct {
|
|
pveClient *pve.Client
|
|
lbClient *lbclient.Client
|
|
instances cloudprovider.Instances
|
|
instancesV2 cloudprovider.InstancesV2
|
|
loadbalancer cloudprovider.LoadBalancer
|
|
}
|
|
|
|
type config struct {
|
|
PVE pve.Config `json:"pve"`
|
|
LB lb.Config `json:"lb"`
|
|
}
|
|
|
|
func newCloud(cr io.Reader) (c cloudprovider.Interface, err error) {
|
|
var (
|
|
pveOpts []pve.ClientOption
|
|
lbOpts []lb.ClientOption
|
|
)
|
|
buf, _ := ioutil.ReadAll(cr)
|
|
if len(buf) != 0 {
|
|
var cfg config
|
|
err = json.Unmarshal(buf, &cfg)
|
|
if err != nil {
|
|
return
|
|
}
|
|
pveOpts, err = pve.ClientOptionsFromConfig(cfg.PVE)
|
|
if err != nil {
|
|
return
|
|
}
|
|
lbOpts, err = lb.ClientOptionsFromConfig(cfg.LB)
|
|
if err != nil {
|
|
return
|
|
}
|
|
} else {
|
|
pveOpts, err = pve.ClientOptionsFromEnv()
|
|
if err != nil {
|
|
return
|
|
}
|
|
lbOpts, err = lb.ClientOptionsFromEnv()
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
if len(pveOpts) == 0 {
|
|
err = errors.New("missing pve config")
|
|
return
|
|
}
|
|
if len(lbOpts) == 0 {
|
|
err = errors.New("missing lb config")
|
|
return
|
|
}
|
|
|
|
pveClient := pve.NewClient(pveOpts...)
|
|
lbClient, err := lbclient.NewClient(lb.NewClient(lbOpts...))
|
|
|
|
c = &cloud{
|
|
pveClient: pveClient,
|
|
lbClient: lbClient,
|
|
instances: newInstances(pveClient),
|
|
instancesV2: newInstancesV2(pveClient),
|
|
loadbalancer: newLoadbalancer(lbClient),
|
|
//zones: newZones(client, nodeName),
|
|
//routes: nil,
|
|
//network: network,
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *cloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
|
|
}
|
|
|
|
func (c *cloud) Instances() (cloudprovider.Instances, bool) {
|
|
return c.instances, true
|
|
}
|
|
|
|
func (c *cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
|
|
return c.instancesV2, true
|
|
}
|
|
|
|
func (c *cloud) Zones() (cloudprovider.Zones, bool) {
|
|
return nil, false
|
|
}
|
|
|
|
func (c *cloud) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
|
|
return c.loadbalancer, true
|
|
}
|
|
|
|
func (c *cloud) Clusters() (cloudprovider.Clusters, bool) {
|
|
return nil, false
|
|
}
|
|
|
|
func (c *cloud) Routes() (cloudprovider.Routes, bool) {
|
|
return nil, false
|
|
}
|
|
|
|
func (c *cloud) ProviderName() string {
|
|
return ProviderName
|
|
}
|
|
|
|
func (c *cloud) ScrubDNS(nameservers, searches []string) (nsOut, srchOut []string) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (c *cloud) HasClusterID() bool {
|
|
return true
|
|
}
|
|
|
|
func init() {
|
|
cloudprovider.RegisterCloudProvider(ProviderName, func(config io.Reader) (cloudprovider.Interface, error) {
|
|
return newCloud(config)
|
|
})
|
|
}
|