mirror of https://github.com/kubernetes/kops.git
API example
This commit is contained in:
parent
f7fa324858
commit
5d255d17cb
|
@ -146,12 +146,6 @@ func (c *CreateClusterCmd) Run(args []string) error {
|
|||
|
||||
cluster = &api.Cluster{}
|
||||
|
||||
configBase, err := clientset.Clusters().(*vfsclientset.ClusterVFS).ConfigBase(clusterName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error building ConfigBase for cluster: %v", err)
|
||||
}
|
||||
cluster.Spec.ConfigBase = configBase.Path()
|
||||
|
||||
channel, err := api.LoadChannel(c.Channel)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -161,7 +155,11 @@ func (c *CreateClusterCmd) Run(args []string) error {
|
|||
}
|
||||
cluster.Spec.Channel = c.Channel
|
||||
|
||||
var instanceGroups []*api.InstanceGroup
|
||||
configBase, err := clientset.Clusters().(*vfsclientset.ClusterVFS).ConfigBase(clusterName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error building ConfigBase for cluster: %v", err)
|
||||
}
|
||||
cluster.Spec.ConfigBase = configBase.Path()
|
||||
|
||||
cluster.Spec.Networking = &api.NetworkingSpec{}
|
||||
switch c.Networking {
|
||||
|
@ -195,14 +193,7 @@ func (c *CreateClusterCmd) Run(args []string) error {
|
|||
|
||||
var masters []*api.InstanceGroup
|
||||
var nodes []*api.InstanceGroup
|
||||
|
||||
for _, group := range instanceGroups {
|
||||
if group.IsMaster() {
|
||||
masters = append(masters, group)
|
||||
} else {
|
||||
nodes = append(nodes, group)
|
||||
}
|
||||
}
|
||||
var instanceGroups []*api.InstanceGroup
|
||||
|
||||
if c.MasterZones == "" {
|
||||
if len(masters) == 0 {
|
||||
|
@ -244,7 +235,7 @@ func (c *CreateClusterCmd) Run(args []string) error {
|
|||
|
||||
if len(cluster.Spec.EtcdClusters) == 0 {
|
||||
zones := sets.NewString()
|
||||
for _, group := range instanceGroups {
|
||||
for _, group := range masters {
|
||||
for _, zone := range group.Spec.Zones {
|
||||
zones.Insert(zone)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"flag"
|
||||
"k8s.io/kops/util/pkg/vfs"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// registryBase is the base path where state files are kept (the state store)
|
||||
var registryBase vfs.Path
|
||||
|
||||
// clusterName is the name of the cluster to create
|
||||
var clusterName string
|
||||
|
||||
// nodeZones is the set of zones in which we will run nodes
|
||||
var nodeZones []string
|
||||
// masterZones is the set of zones in which we will run masters
|
||||
var masterZones []string
|
||||
|
||||
var sshPublicKey = "~/.ssh/id_rsa.pub"
|
||||
|
||||
var flagRegistryBase = flag.String("registry", os.Getenv("KOPS_STATE_STORE"), "VFS path where files are kept")
|
||||
var flagClusterName = flag.String("name", "", "Name of cluster to create")
|
||||
var flagZones = flag.String("zones", "", "Comma separated list of zones to create")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
err := parseFlags()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = up()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func parseFlags() error {
|
||||
var err error
|
||||
|
||||
registryBase, err = vfs.Context.BuildVfsPath(*flagRegistryBase)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing registry path %q: %v", *flagRegistryBase, err)
|
||||
}
|
||||
|
||||
clusterName = *flagClusterName
|
||||
if clusterName == "" {
|
||||
return fmt.Errorf("Must pass -name with cluster name")
|
||||
}
|
||||
|
||||
if *flagZones == "" {
|
||||
return fmt.Errorf("Must pass -zones with comma-separated list of zones")
|
||||
}
|
||||
nodeZones = strings.Split(*flagZones, ",")
|
||||
masterZones = []string{nodeZones[0] }
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"k8s.io/kops/pkg/client/simple/vfsclientset"
|
||||
"k8s.io/kops/upup/pkg/api"
|
||||
"k8s.io/kops/upup/pkg/fi/cloudup"
|
||||
"k8s.io/kops/upup/pkg/fi"
|
||||
"k8s.io/kops/upup/pkg/fi/utils"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"k8s.io/kops/upup/pkg/api/registry"
|
||||
)
|
||||
|
||||
func up() error {
|
||||
clientset := vfsclientset.NewVFSClientset(registryBase)
|
||||
|
||||
cluster := &api.Cluster{}
|
||||
|
||||
cluster.Name = clusterName
|
||||
cluster.Spec = api.ClusterSpec{
|
||||
Channel: "stable",
|
||||
CloudProvider: "aws",
|
||||
ConfigBase: registryBase.Join(cluster.Name).Path(),
|
||||
}
|
||||
|
||||
for _, z := range nodeZones {
|
||||
cluster.Spec.Zones = append(cluster.Spec.Zones, &api.ClusterZoneSpec{
|
||||
Name: z,
|
||||
})
|
||||
}
|
||||
|
||||
for _, etcdClusterName := range cloudup.EtcdClusters {
|
||||
etcdCluster := &api.EtcdClusterSpec{
|
||||
Name: etcdClusterName,
|
||||
}
|
||||
for _, masterZone := range masterZones {
|
||||
etcdMember := &api.EtcdMemberSpec{
|
||||
Name: masterZone,
|
||||
Zone: fi.String(masterZone),
|
||||
}
|
||||
etcdCluster.Members = append(etcdCluster.Members, etcdMember)
|
||||
}
|
||||
cluster.Spec.EtcdClusters = append(cluster.Spec.EtcdClusters, etcdCluster)
|
||||
}
|
||||
|
||||
if err := cluster.PerformAssignments(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := clientset.Clusters().Create(cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create master ig
|
||||
{
|
||||
ig := &api.InstanceGroup{}
|
||||
ig.Name = "master"
|
||||
ig.Spec = api.InstanceGroupSpec{
|
||||
Role: api.InstanceGroupRoleMaster,
|
||||
Zones: masterZones,
|
||||
}
|
||||
_, err := clientset.InstanceGroups(cluster.Name).Create(ig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Create node ig
|
||||
{
|
||||
ig := &api.InstanceGroup{}
|
||||
ig.Name = "nodes"
|
||||
ig.Spec = api.InstanceGroupSpec{
|
||||
Role: api.InstanceGroupRoleNode,
|
||||
Zones: nodeZones,
|
||||
}
|
||||
|
||||
_, err := clientset.InstanceGroups(cluster.Name).Create(ig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
keyStore, err := registry.KeyStore(cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add a public key
|
||||
{
|
||||
f := utils.ExpandPath(sshPublicKey)
|
||||
pubKey, err := ioutil.ReadFile(f)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading SSH key file %q: %v", f, err)
|
||||
}
|
||||
err = keyStore.AddSSHPublicKey(fi.SecretNameSSHPrimary, pubKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error addding SSH public key: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -44,7 +44,7 @@ func (c *InstanceGroupVFS) List(options k8sapi.ListOptions) (*api.InstanceGroupL
|
|||
}
|
||||
|
||||
func (c *InstanceGroupVFS) Create(g *api.InstanceGroup) (*api.InstanceGroup, error) {
|
||||
err := g.Validate(false)
|
||||
err := g.Validate(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ func (c *InstanceGroupVFS) Create(g *api.InstanceGroup) (*api.InstanceGroup, err
|
|||
}
|
||||
|
||||
func (c *InstanceGroupVFS) Update(g *api.InstanceGroup) (*api.InstanceGroup, error) {
|
||||
err := g.Validate(false)
|
||||
err := g.Validate(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ func (c *Cluster) Validate(strict bool) error {
|
|||
specPath := field.NewPath("Cluster").Child("Spec")
|
||||
|
||||
if c.Name == "" {
|
||||
return fmt.Errorf("Cluster Name is required (e.g. --name=mycluster.myzone.com)")
|
||||
return field.Required(field.NewPath("Name"), "Cluster Name is required (e.g. --name=mycluster.myzone.com)")
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -69,11 +69,11 @@ func (c *Cluster) Validate(strict bool) error {
|
|||
{
|
||||
networkCIDRString := c.Spec.NetworkCIDR
|
||||
if networkCIDRString == "" {
|
||||
return fmt.Errorf("Cluster did not have NetworkCIDR set")
|
||||
return field.Required(specField.Child("NetworkCIDR"), "Cluster did not have NetworkCIDR set")
|
||||
}
|
||||
_, networkCIDR, err = net.ParseCIDR(networkCIDRString)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cluster had an invalid NetworkCIDR: %q", networkCIDRString)
|
||||
return field.Invalid(specField.Child("NetworkCIDR"), networkCIDRString, fmt.Sprintf("Cluster had an invalid NetworkCIDR"))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -267,8 +267,8 @@ func (c *Cluster) Validate(strict bool) error {
|
|||
|
||||
// Etcd
|
||||
{
|
||||
if strict && len(c.Spec.EtcdClusters) == 0 {
|
||||
return fmt.Errorf("EtcdClusters not configured")
|
||||
if len(c.Spec.EtcdClusters) == 0 {
|
||||
return field.Required(specField.Child("EtcdClusters"), "")
|
||||
}
|
||||
for _, etcd := range c.Spec.EtcdClusters {
|
||||
if etcd.Name == "" {
|
||||
|
|
Loading…
Reference in New Issue