Merge pull request #3240 from justinsb/create_and_update_functions

Automatic merge from submit-queue

Explicit CreateCluster & UpdateCluster functions
This commit is contained in:
Kubernetes Submit Queue 2017-08-25 06:37:34 -07:00 committed by GitHub
commit d661e27a99
9 changed files with 50 additions and 22 deletions

View File

@ -159,7 +159,7 @@ func RunCreate(f *util.Factory, out io.Writer, c *CreateOptions) error {
if err != nil { if err != nil {
return fmt.Errorf("error populating configuration: %v", err) return fmt.Errorf("error populating configuration: %v", err)
} }
_, err = clientset.ClustersFor(v).Create(v) _, err = clientset.CreateCluster(v)
if err != nil { if err != nil {
if apierrors.IsAlreadyExists(err) { if apierrors.IsAlreadyExists(err) {
return fmt.Errorf("cluster %q already exists", v.ObjectMeta.Name) return fmt.Errorf("cluster %q already exists", v.ObjectMeta.Name)

View File

@ -237,7 +237,7 @@ func RunEditCluster(f *util.Factory, cmd *cobra.Command, args []string, out io.W
} }
// Note we perform as much validation as we can, before writing a bad config // Note we perform as much validation as we can, before writing a bad config
_, err = clientset.ClustersFor(newCluster).Update(newCluster) _, err = clientset.UpdateCluster(newCluster)
if err != nil { if err != nil {
return preservedFile(err, file, out) return preservedFile(err, file, out)
} }

View File

@ -115,7 +115,7 @@ func RunReplace(f *util.Factory, cmd *cobra.Command, out io.Writer, c *replaceOp
} }
case *kopsapi.Cluster: case *kopsapi.Cluster:
_, err = clientset.ClustersFor(v).Update(v) _, err = clientset.UpdateCluster(v)
if err != nil { if err != nil {
return fmt.Errorf("error replacing cluster: %v", err) return fmt.Errorf("error replacing cluster: %v", err)
} }

View File

@ -301,7 +301,7 @@ func (c *UpgradeClusterCmd) Run(args []string) error {
} }
// Note we perform as much validation as we can, before writing a bad config // Note we perform as much validation as we can, before writing a bad config
_, err = clientset.ClustersFor(cluster).Update(cluster) _, err = clientset.UpdateCluster(cluster)
if err != nil { if err != nil {
return err return err
} }

View File

@ -67,7 +67,7 @@ func up() error {
return err return err
} }
_, err := clientset.ClustersFor(cluster).Create(cluster) _, err := clientset.CreateCluster(cluster)
if err != nil { if err != nil {
return err return err
} }

View File

@ -37,7 +37,7 @@ func CreateClusterConfig(clientset simple.Clientset, cluster *api.Cluster, group
} }
} }
_, err := clientset.ClustersFor(cluster).Create(cluster) _, err := clientset.CreateCluster(cluster)
if err != nil { if err != nil {
return err return err
} }

View File

@ -26,12 +26,15 @@ import (
) )
type Clientset interface { type Clientset interface {
// ClustersFor returns the ClusterInterface bound to the namespace for a particular Cluster
ClustersFor(cluster *kops.Cluster) kopsinternalversion.ClusterInterface
// GetCluster reads a cluster by name // GetCluster reads a cluster by name
GetCluster(name string) (*kops.Cluster, error) GetCluster(name string) (*kops.Cluster, error)
// CreateCluster creates a cluster
CreateCluster(cluster *kops.Cluster) (*kops.Cluster, error)
// UpdateCluster updates a cluster
UpdateCluster(cluster *kops.Cluster) (*kops.Cluster, error)
// ListClusters returns all clusters // ListClusters returns all clusters
ListClusters(options metav1.ListOptions) (*kops.ClusterList, error) ListClusters(options metav1.ListOptions) (*kops.ClusterList, error)
@ -57,31 +60,43 @@ type RESTClientset struct {
KopsClient kopsinternalversion.KopsInterface KopsClient kopsinternalversion.KopsInterface
} }
func (c *RESTClientset) ClustersFor(cluster *kops.Cluster) kopsinternalversion.ClusterInterface { // GetCluster implements the GetCluster method of Clientset for a kubernetes-API state store
namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.Clusters(namespace)
}
func (c *RESTClientset) GetCluster(name string) (*kops.Cluster, error) { func (c *RESTClientset) GetCluster(name string) (*kops.Cluster, error) {
namespace := restNamespaceForClusterName(name) namespace := restNamespaceForClusterName(name)
return c.KopsClient.Clusters(namespace).Get(name, metav1.GetOptions{}) return c.KopsClient.Clusters(namespace).Get(name, metav1.GetOptions{})
} }
// CreateCluster implements the CreateCluster method of Clientset for a kubernetes-API state store
func (c *RESTClientset) CreateCluster(cluster *kops.Cluster) (*kops.Cluster, error) {
namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.Clusters(namespace).Create(cluster)
}
// UpdateCluster implements the UpdateCluster method of Clientset for a kubernetes-API state store
func (c *RESTClientset) UpdateCluster(cluster *kops.Cluster) (*kops.Cluster, error) {
namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.Clusters(namespace).Update(cluster)
}
// ConfigBaseFor implements the ConfigBaseFor method of Clientset for a kubernetes-API state store
func (c *RESTClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) { func (c *RESTClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) {
// URL for clusters looks like https://<server>/apis/kops/v1alpha2/namespaces/<cluster>/clusters/<cluster> // URL for clusters looks like https://<server>/apis/kops/v1alpha2/namespaces/<cluster>/clusters/<cluster>
// We probably want to add a subresource for full resources // We probably want to add a subresource for full resources
return vfs.Context.BuildVfsPath(c.BaseURL.String()) return vfs.Context.BuildVfsPath(c.BaseURL.String())
} }
// ListClusters implements the ListClusters method of Clientset for a kubernetes-API state store
func (c *RESTClientset) ListClusters(options metav1.ListOptions) (*kops.ClusterList, error) { func (c *RESTClientset) ListClusters(options metav1.ListOptions) (*kops.ClusterList, error) {
return c.KopsClient.Clusters(metav1.NamespaceAll).List(options) return c.KopsClient.Clusters(metav1.NamespaceAll).List(options)
} }
// InstanceGroupsFor implements the InstanceGroupsFor method of Clientset for a kubernetes-API state store
func (c *RESTClientset) InstanceGroupsFor(cluster *kops.Cluster) kopsinternalversion.InstanceGroupInterface { func (c *RESTClientset) InstanceGroupsFor(cluster *kops.Cluster) kopsinternalversion.InstanceGroupInterface {
namespace := restNamespaceForClusterName(cluster.Name) namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.InstanceGroups(namespace) return c.KopsClient.InstanceGroups(namespace)
} }
// FederationsFor implements the FederationsFor method of Clientset for a kubernetes-API state store
func (c *RESTClientset) FederationsFor(federation *kops.Federation) kopsinternalversion.FederationInterface { func (c *RESTClientset) FederationsFor(federation *kops.Federation) kopsinternalversion.FederationInterface {
// Unsure if this should be namespaced or not - probably, so that we can RBAC it... // Unsure if this should be namespaced or not - probably, so that we can RBAC it...
panic("Federations are curently not supported by the server API") panic("Federations are curently not supported by the server API")
@ -89,10 +104,12 @@ func (c *RESTClientset) FederationsFor(federation *kops.Federation) kopsinternal
//return c.KopsClient.Federations(namespace) //return c.KopsClient.Federations(namespace)
} }
// ListFederations implements the ListFederations method of Clientset for a kubernetes-API state store
func (c *RESTClientset) ListFederations(options metav1.ListOptions) (*kops.FederationList, error) { func (c *RESTClientset) ListFederations(options metav1.ListOptions) (*kops.FederationList, error) {
return c.KopsClient.Federations(metav1.NamespaceAll).List(options) return c.KopsClient.Federations(metav1.NamespaceAll).List(options)
} }
// GetFederation implements the GetFederation method of Clientset for a kubernetes-API state store
func (c *RESTClientset) GetFederation(name string) (*kops.Federation, error) { func (c *RESTClientset) GetFederation(name string) (*kops.Federation, error) {
namespace := restNamespaceForFederationName(name) namespace := restNamespaceForFederationName(name)
return c.KopsClient.Federations(namespace).Get(name, metav1.GetOptions{}) return c.KopsClient.Federations(namespace).Get(name, metav1.GetOptions{})

View File

@ -30,26 +30,36 @@ type VFSClientset struct {
var _ simple.Clientset = &VFSClientset{} var _ simple.Clientset = &VFSClientset{}
func (c *VFSClientset) ClustersFor(cluster *kops.Cluster) kopsinternalversion.ClusterInterface {
return c.clusters()
}
func (c *VFSClientset) clusters() *ClusterVFS { func (c *VFSClientset) clusters() *ClusterVFS {
return newClusterVFS(c.basePath) return newClusterVFS(c.basePath)
} }
// GetCluster implements the GetCluster method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) GetCluster(name string) (*kops.Cluster, error) { func (c *VFSClientset) GetCluster(name string) (*kops.Cluster, error) {
return c.clusters().Get(name, metav1.GetOptions{}) return c.clusters().Get(name, metav1.GetOptions{})
} }
// UpdateCluster implements the UpdateCluster method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) UpdateCluster(cluster *kops.Cluster) (*kops.Cluster, error) {
return c.clusters().Update(cluster)
}
// CreateCluster implements the CreateCluster method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) CreateCluster(cluster *kops.Cluster) (*kops.Cluster, error) {
return c.clusters().Create(cluster)
}
// ListClusters implements the ListClusters method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) ListClusters(options metav1.ListOptions) (*kops.ClusterList, error) { func (c *VFSClientset) ListClusters(options metav1.ListOptions) (*kops.ClusterList, error) {
return c.clusters().List(options) return c.clusters().List(options)
} }
// ConfigBaseFor implements the ConfigBaseFor method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) { func (c *VFSClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) {
return c.clusters().configBase(cluster.Name) return c.clusters().configBase(cluster.Name)
} }
// InstanceGroupsFor implements the InstanceGroupsFor method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) InstanceGroupsFor(cluster *kops.Cluster) kopsinternalversion.InstanceGroupInterface { func (c *VFSClientset) InstanceGroupsFor(cluster *kops.Cluster) kopsinternalversion.InstanceGroupInterface {
clusterName := cluster.Name clusterName := cluster.Name
return newInstanceGroupVFS(c, clusterName) return newInstanceGroupVFS(c, clusterName)
@ -59,14 +69,17 @@ func (c *VFSClientset) federations() kopsinternalversion.FederationInterface {
return newFederationVFS(c) return newFederationVFS(c)
} }
// FederationsFor implements the FederationsFor method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) FederationsFor(federation *kops.Federation) kopsinternalversion.FederationInterface { func (c *VFSClientset) FederationsFor(federation *kops.Federation) kopsinternalversion.FederationInterface {
return c.federations() return c.federations()
} }
// ListFederations implements the ListFederations method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) ListFederations(options metav1.ListOptions) (*kops.FederationList, error) { func (c *VFSClientset) ListFederations(options metav1.ListOptions) (*kops.FederationList, error) {
return c.federations().List(options) return c.federations().List(options)
} }
// GetFederation implements the GetFederation method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) GetFederation(name string) (*kops.Federation, error) { func (c *VFSClientset) GetFederation(name string) (*kops.Federation, error) {
return c.federations().Get(name, metav1.GetOptions{}) return c.federations().Get(name, metav1.GetOptions{})
} }

View File

@ -21,12 +21,12 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/watch" "k8s.io/apimachinery/pkg/watch"
api "k8s.io/kops/pkg/apis/kops" api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/registry" "k8s.io/kops/pkg/apis/kops/registry"
"k8s.io/kops/pkg/apis/kops/v1alpha1" "k8s.io/kops/pkg/apis/kops/v1alpha1"
"k8s.io/kops/pkg/apis/kops/validation" "k8s.io/kops/pkg/apis/kops/validation"
kopsinternalversion "k8s.io/kops/pkg/client/clientset_generated/clientset/typed/kops/internalversion"
"k8s.io/kops/util/pkg/vfs" "k8s.io/kops/util/pkg/vfs"
"os" "os"
"strings" "strings"
@ -45,8 +45,6 @@ func newClusterVFS(basePath vfs.Path) *ClusterVFS {
return c return c
} }
var _ kopsinternalversion.ClusterInterface = &ClusterVFS{}
func (c *ClusterVFS) Get(name string, options metav1.GetOptions) (*api.Cluster, error) { func (c *ClusterVFS) Get(name string, options metav1.GetOptions) (*api.Cluster, error) {
if options.ResourceVersion != "" { if options.ResourceVersion != "" {
return nil, fmt.Errorf("ResourceVersion not supported in ClusterVFS::Get") return nil, fmt.Errorf("ResourceVersion not supported in ClusterVFS::Get")
@ -121,7 +119,7 @@ func (r *ClusterVFS) Update(c *api.Cluster) (*api.Cluster, error) {
clusterName := c.ObjectMeta.Name clusterName := c.ObjectMeta.Name
if clusterName == "" { if clusterName == "" {
return nil, fmt.Errorf("clusterName is required") return nil, field.Required(field.NewPath("Name"), "clusterName is required")
} }
if err := r.writeConfig(r.basePath.Join(clusterName, registry.PathCluster), c, vfs.WriteOptionOnlyIfExists); err != nil { if err := r.writeConfig(r.basePath.Join(clusterName, registry.PathCluster), c, vfs.WriteOptionOnlyIfExists); err != nil {