Skip to content

Commit

Permalink
Put versioned API of cluster into state store
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed May 31, 2020
1 parent c718852 commit f0db5c0
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 16 deletions.
2 changes: 2 additions & 0 deletions cmd/kops-controller/controllers/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (r *NodeReconciler) patchNodeLabels(ctx context.Context, node *corev1.Node,
// getClusterForNode returns the kops.Cluster object for the node
// The cluster is actually loaded when we first start
func (r *NodeReconciler) getClusterForNode(node *corev1.Node) (*kops.Cluster, error) {
// todo have to read the versioned format
clusterPath := r.configBase.Join(registry.PathClusterCompleted)
cluster, err := r.loadCluster(clusterPath)
if err != nil {
Expand Down Expand Up @@ -261,6 +262,7 @@ func (r *NodeReconciler) loadNamedInstanceGroup(name string) (*kops.InstanceGrou
return nil, fmt.Errorf("error loading InstanceGroup %q: %v", p, err)
}

// todo bug: The on-disk format is the versioned API but this is deserializing as the unversioned API
instanceGroup := &kops.InstanceGroup{}
if err := utils.YamlUnmarshal(b, instanceGroup); err != nil {
return nil, fmt.Errorf("error parsing InstanceGroup %q: %v", p, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
return fmt.Errorf("error writing updated configuration: %v", err)
}

err = registry.WriteConfigDeprecated(cluster, configBase.Join(registry.PathClusterCompleted), fullCluster)
err = registry.WriteConfigDeprecated(cluster, configBase.Join(registry.PathLegacyClusterCompleted), fullCluster)
if err != nil {
return fmt.Errorf("error writing completed cluster spec: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/edit_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func RunEditCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, ar
return preservedFile(err, file, out)
}

err = registry.WriteConfigDeprecated(newCluster, configBase.Join(registry.PathClusterCompleted), fullCluster)
err = registry.WriteConfigDeprecated(newCluster, configBase.Join(registry.PathLegacyClusterCompleted), fullCluster)
if err != nil {
return preservedFile(fmt.Errorf("error writing completed cluster spec: %v", err), file, out)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/apis/kops/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const (
// Path for the user-specified cluster spec
PathCluster = "config"
// Path for completed cluster spec in the state store
PathClusterCompleted = "cluster.spec"
PathClusterCompleted = "cluster-completed.spec"
// Path for unversioned completed cluster spec in the state store
PathLegacyClusterCompleted = "cluster.spec"
)

func ConfigBase(c *api.Cluster) (vfs.Path, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/kops/registry/statestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/kops/util/pkg/vfs"
)

// todo remove this function
func ReadConfigDeprecated(configPath vfs.Path, config interface{}) error {
data, err := configPath.ReadFile()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/bundle/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (b *Builder) Build(cluster *kops.Cluster, ig *kops.InstanceGroup) (*Data, e
return nil, fmt.Errorf("error building ConfigBase for cluster: %v", err)
}

// todo have to read the versioned format
p := configBase.Join(registry.PathClusterCompleted)

b, err := p.ReadFile()
Expand All @@ -87,13 +88,13 @@ func (b *Builder) Build(cluster *kops.Cluster, ig *kops.InstanceGroup) (*Data, e
var files []*DataFile

{
data, err := utils.YamlMarshal(fullCluster)
data, err := kopscodecs.ToVersionedYaml(fullCluster)
if err != nil {
return nil, fmt.Errorf("error marshaling configuration: %v", err)
}

file := &DataFile{}
file.Header.Name = "cluster.spec"
file.Header.Name = "cluster-completed.spec"
file.Header.Size = int64(len(data))
file.Header.Mode = 0644
file.Data = data
Expand Down
6 changes: 6 additions & 0 deletions pkg/client/simple/api/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (c *RESTClientset) UpdateCluster(ctx context.Context, cluster *kops.Cluster
return c.KopsClient.Clusters(namespace).Update(ctx, cluster, metav1.UpdateOptions{})
}

// UpdateCompletedCluster implements the UpdateCompletedCluster method of Clientset for a kubernetes-API state store
func (c *RESTClientset) UpdateCompletedCluster(ctx context.Context, cluster *kops.Cluster) error {
namespace := restNamespaceForClusterName(cluster.Name)
return c.KopsClient.Clusters(namespace).UpdateCompleted(ctx, cluster, metav1.UpdateOptions{})
}

// ConfigBaseFor implements the ConfigBaseFor method of Clientset for a kubernetes-API state store
func (c *RESTClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) {
if cluster.Spec.ConfigBase != "" {
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/simple/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Clientset interface {
// UpdateCluster updates a cluster
UpdateCluster(ctx context.Context, cluster *kops.Cluster, status *kops.ClusterStatus) (*kops.Cluster, error)

// UpdateCompletedCluster updates a completed cluster.
UpdateCompletedCluster(ctx context.Context, cluster *kops.Cluster) error

// ListClusters returns all clusters
ListClusters(ctx context.Context, options metav1.ListOptions) (*kops.ClusterList, error)

Expand Down
7 changes: 6 additions & 1 deletion pkg/client/simple/vfsclientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (c *VFSClientset) UpdateCluster(ctx context.Context, cluster *kops.Cluster,
return c.clusters().Update(cluster, status)
}

// UpdateCompletedCluster implements the UpdateCluster method of simple.Clientset for a VFS-backed state store.
func (c *VFSClientset) UpdateCompletedCluster(ctx context.Context, cluster *kops.Cluster) error {
return c.clusters().UpdateCompleted(cluster)
}

// CreateCluster implements the CreateCluster method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) CreateCluster(ctx context.Context, cluster *kops.Cluster) (*kops.Cluster, error) {
return c.clusters().Create(cluster)
Expand Down Expand Up @@ -117,7 +122,7 @@ func DeleteAllClusterState(basePath vfs.Path) error {
continue
}

if relativePath == "config" || relativePath == "cluster.spec" {
if relativePath == "config" || relativePath == "cluster.spec" || relativePath == "cluster-completed.spec" {
continue
}
if strings.HasPrefix(relativePath, "addons/") {
Expand Down
16 changes: 16 additions & 0 deletions pkg/client/simple/vfsclientset/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ func (r *ClusterVFS) Update(c *api.Cluster, status *api.ClusterStatus) (*api.Clu
return c, nil
}

func (r *ClusterVFS) UpdateCompleted(c *api.Cluster) error {
clusterName := c.ObjectMeta.Name
if clusterName == "" {
return field.Required(field.NewPath("objectMeta", "name"), "clusterName is required")
}

if err := r.writeConfig(c, r.basePath.Join(clusterName, registry.PathClusterCompleted), c); err != nil {
if os.IsNotExist(err) {
return err
}
return fmt.Errorf("error writing Cluster: %v", err)
}

return nil
}

// List returns a slice containing all the cluster names
// It skips directories that don't look like clusters
func (r *ClusterVFS) listNames() ([]string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/alimodel/policy_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (b *PolicyBuilder) AddOSSPermissions(p *Policy) (*Policy, error) {
} else if b.Role == kops.InstanceGroupRoleNode {
resources := []string{
strings.Join([]string{b.RAMPrefix(), ":oss:*:*:", ramOSSPath, "/addons/*"}, ""),
strings.Join([]string{b.RAMPrefix(), ":oss:*:*:", ramOSSPath, "/cluster.spec"}, ""),
strings.Join([]string{b.RAMPrefix(), ":oss:*:*:", ramOSSPath, "/cluster-completed.spec"}, ""),
strings.Join([]string{b.RAMPrefix(), ":oss:*:*:", ramOSSPath, "/config"}, ""),
strings.Join([]string{b.RAMPrefix(), ":oss:*:*:", ramOSSPath, "/instancegroup/*"}, ""),
strings.Join([]string{b.RAMPrefix(), ":oss:*:*:", ramOSSPath, "/pki/issued/*"}, ""),
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/iam/iam_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (b *PolicyBuilder) AddS3Permissions(p *Policy) (*Policy, error) {
} else if b.Role == kops.InstanceGroupRoleNode {
resources := []string{
strings.Join([]string{b.IAMPrefix(), ":s3:::", iamS3Path, "/addons/*"}, ""),
strings.Join([]string{b.IAMPrefix(), ":s3:::", iamS3Path, "/cluster.spec"}, ""),
strings.Join([]string{b.IAMPrefix(), ":s3:::", iamS3Path, "/cluster-completed.spec"}, ""),
strings.Join([]string{b.IAMPrefix(), ":s3:::", iamS3Path, "/config"}, ""),
strings.Join([]string{b.IAMPrefix(), ":s3:::", iamS3Path, "/instancegroup/*"}, ""),
strings.Join([]string{b.IAMPrefix(), ":s3:::", iamS3Path, "/pki/issued/*"}, ""),
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/iam/tests/iam_builder_node_strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
],
"Resource": [
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/addons/*",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/cluster.spec",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/cluster-completed.spec",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/config",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/instancegroup/*",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/pki/issued/*",
Expand Down
2 changes: 1 addition & 1 deletion pkg/model/iam/tests/iam_builder_node_strict_ecr.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
],
"Resource": [
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/addons/*",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/cluster.spec",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/cluster-completed.spec",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/config",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/instancegroup/*",
"arn:aws:s3:::kops-tests/iam-builder-test.k8s.local/pki/issued/*",
Expand Down
7 changes: 6 additions & 1 deletion upup/pkg/fi/cloudup/apply_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,11 +924,16 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {
c.Target = target

if !dryRun {
err = registry.WriteConfigDeprecated(cluster, configBase.Join(registry.PathClusterCompleted), c.Cluster)
err = c.Clientset.UpdateCompletedCluster(ctx, c.Cluster)
if err != nil {
return fmt.Errorf("error writing completed cluster spec: %v", err)
}

err = registry.WriteConfigDeprecated(cluster, configBase.Join(registry.PathLegacyClusterCompleted), c.Cluster)
if err != nil {
return fmt.Errorf("error writing legacy completed cluster spec: %v", err)
}

vfsMirror := vfsclientset.NewInstanceGroupMirror(cluster, configBase)

for _, g := range c.InstanceGroups {
Expand Down
2 changes: 2 additions & 0 deletions upup/pkg/fi/nodeup/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
p = configBase.Join(registry.PathClusterCompleted)
}

// todo have to read the versioned format
b, err := p.ReadFile()
if err != nil {
return fmt.Errorf("error loading Cluster %q: %v", p, err)
Expand All @@ -155,6 +156,7 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
return fmt.Errorf("error loading InstanceGroup %q: %v", instanceGroupLocation, err)
}

// todo bug: The on-disk format is the versioned API but this is deserializing as the unversioned API
if err = utils.YamlUnmarshal(b, c.instanceGroup); err != nil {
return fmt.Errorf("error parsing InstanceGroup %q: %v", instanceGroupLocation, err)
}
Expand Down
6 changes: 1 addition & 5 deletions upup/pkg/kutil/convert_kubeup_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,7 @@ func (x *ConvertKubeupCluster) Upgrade(ctx context.Context) error {
}

// TODO: No longer needed?
err = registry.WriteConfigDeprecated(cluster, newConfigBase.Join(registry.PathClusterCompleted), fullCluster)
if err != nil {
return fmt.Errorf("error writing completed cluster spec: %v", err)
}

err = registry.WriteConfigDeprecated(cluster, newConfigBase.Join(registry.PathLegacyClusterCompleted), fullCluster)
if err != nil {
return fmt.Errorf("error writing completed cluster spec: %v", err)
}
Expand Down

0 comments on commit f0db5c0

Please sign in to comment.