Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'config migrate' command #408

Merged
merged 16 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2019 The KubeOne 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 cmd

import (
"bytes"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
yaml "gopkg.in/yaml.v2"

kubeonev1alpha1 "github.com/kubermatic/kubeone/pkg/apis/kubeone/v1alpha1"
"github.com/kubermatic/kubeone/pkg/config"

kyaml "sigs.k8s.io/yaml"
)

type migrateOptions struct {
globalOptions
Manifest string
}

// configCmd setups the config command
func configCmd(rootFlags *pflag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Commands for working with the KubeOneCluster configuration manifests",
}

cmd.AddCommand(migrateCmd(rootFlags))

return cmd
}

// migrateCmd setups the migrate command
func migrateCmd(_ *pflag.FlagSet) *cobra.Command {
mOpts := &migrateOptions{}
cmd := &cobra.Command{
Use: "migrate <cluster-manifest>",
Short: "Migrate the pre-v0.6.0 configuration manifest to the KubeOneCluster manifest",
Long: `
Migrate the pre-v0.6.0 KubeOne configuration manifest to the KubeOneCluster manifest used as of v0.6.0.
The new manifest is printed on the standard output.
`,
Args: cobra.ExactArgs(1),
Example: `kubeone migrate mycluster.yaml`,
RunE: func(_ *cobra.Command, args []string) error {
mOpts.Manifest = args[0]
if mOpts.Manifest == "" {
return errors.New("no cluster config file given")
}

return runMigrate(mOpts)
},
}

return cmd
}

// runMigrate migrates the pre-v0.6.0 KubeOne API manifest to the KubeOneCluster manifest used as of v0.6.0
func runMigrate(migrateOptions *migrateOptions) error {
// Convert old config yaml to new config yaml
newConfigYAML, err := config.MigrateToKubeOneClusterAPI(migrateOptions.Manifest)
if err != nil {
return errors.Wrap(err, "unable to migrate the provided configuration")
}

// Validate new config by unmarshaling
var buffer bytes.Buffer
err = yaml.NewEncoder(&buffer).Encode(newConfigYAML)
if err != nil {
return errors.Wrap(err, "failed to encode new config as YAML")
}

newConfig := &kubeonev1alpha1.KubeOneCluster{}
err = kyaml.UnmarshalStrict(buffer.Bytes(), &newConfig)
if err != nil {
return errors.Wrap(err, "failed to decode new config")
}

// Print new config yaml
err = yaml.NewEncoder(os.Stdout).Encode(newConfigYAML)
if err != nil {
return errors.Wrap(err, "failed to encode new config as YAML")
}

return nil
}
1 change: 1 addition & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func newRoot() *cobra.Command {
upgradeCmd(fs),
resetCmd(fs),
kubeconfigCmd(fs),
configCmd(fs),
versionCmd(fs),
)

Expand Down
169 changes: 169 additions & 0 deletions pkg/config/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
Copyright 2019 The KubeOne 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 config

import (
"fmt"
"net"
"os"
"strconv"

"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"

"github.com/kubermatic/kubeone/pkg/util/yamled"
)

// MigrateToKubeOneClusterAPI migrates the old API manifest to the new KubeOneCluster API
func MigrateToKubeOneClusterAPI(oldConfigPath string) (interface{}, error) {
oldConfig, err := loadClusterConfig(oldConfigPath)
if err != nil {
return nil, errors.Wrap(err, "unable to parse the old config")
}

oldConfig.Set(yamled.Path{"apiVersion"}, "kubeone.io/v1alpha1")
oldConfig.Set(yamled.Path{"kind"}, "KubeOneCluster")

// basic root-level renames
rename(oldConfig, yamled.Path{}, "apiserver", "apiEndpoint")
rename(oldConfig, yamled.Path{}, "provider", "cloudProvider")
rename(oldConfig, yamled.Path{}, "network", "clusterNetwork")
rename(oldConfig, yamled.Path{}, "machine_controller", "machineController")

// camel-casing host fields
hosts, exists := oldConfig.GetArray(yamled.Path{"hosts"})
if exists {
total := len(hosts)

for i := 0; i < total; i++ {
path := yamled.Path{"hosts", i}

rename(oldConfig, path, "public_address", "publicAddress")
rename(oldConfig, path, "private_address", "privateAddress")
rename(oldConfig, path, "ssh_port", "sshPort")
rename(oldConfig, path, "ssh_username", "sshUsername")
rename(oldConfig, path, "ssh_private_key_file", "sshPrivateKeyFile")
rename(oldConfig, path, "ssh_agent_socket", "sshAgentSocket")
}
}

// separating host and port for api endpoints, turn it into an array
apiserver, exists := oldConfig.GetString(yamled.Path{"apiEndpoint", "address"})
if exists {
host, sport, err := net.SplitHostPort(apiserver)
if err != nil {
host = apiserver
sport = "6443"
}

port, err := strconv.Atoi(sport)
if err != nil {
return yaml.MapSlice{}, fmt.Errorf("invalid port specified for API server: %d", port)
}

oldConfig.Remove(yamled.Path{"apiEndpoint"})
oldConfig.Set(yamled.Path{"apiEndpoint", "host"}, host)
oldConfig.Set(yamled.Path{"apiEndpoint", "port"}, port)
}

// camel-casing cloudConfig
rename(oldConfig, yamled.Path{"cloudProvider"}, "cloud_config", "cloudConfig")

// camel-casing clusterNetwork
path := yamled.Path{"clusterNetwork"}
rename(oldConfig, path, "pod_subnet", "podSubnet")
rename(oldConfig, path, "service_subnet", "serviceSubnet")
rename(oldConfig, path, "node_port_range", "nodePortRange")

// camel-casing proxy
path = yamled.Path{"proxy"}
rename(oldConfig, path, "http_proxy", "http")
rename(oldConfig, path, "https_proxy", "https")
rename(oldConfig, path, "no_proxy", "noProxy")

// move machine-controller credentials to root level
credentials, exists := oldConfig.Get(yamled.Path{"machineController", "credentials"})
if exists {
oldConfig.Remove(yamled.Path{"machineController", "credentials"})
oldConfig.Set(yamled.Path{"credentials"}, credentials)
}

// camel-casing features
path = yamled.Path{"features"}
rename(oldConfig, path, "pod_security_policy", "podSecurityPolicy")
rename(oldConfig, path, "dynamic_audit_log", "dynamicAuditLog")
rename(oldConfig, path, "metrics_server", "metricsServer")
rename(oldConfig, path, "openid_connect", "openidConnect")
// migrate v0.5.0 features api
enablePSP, exists := oldConfig.Get(yamled.Path{"features", "enable_pod_security_policy"})
if exists {
oldConfig.Remove(yamled.Path{"features", "enable_pod_security_policy"})
oldConfig.Set(yamled.Path{"features", "podSecurityPolicy", "enable"}, enablePSP)
}
enableDynamicAuditLog, exists := oldConfig.Get(yamled.Path{"features", "enable_dynamic_audit_log"})
if exists {
oldConfig.Remove(yamled.Path{"features", "enable_dynamic_audit_log"})
oldConfig.Set(yamled.Path{"features", "dynamicAuditLog", "enable"}, enableDynamicAuditLog)
}

// camel-casing openidConnect
path = yamled.Path{"features", "openidConnect", "config"}
rename(oldConfig, path, "issuer_url", "issuerUrl")
rename(oldConfig, path, "client_id", "clientId")
rename(oldConfig, path, "username_claim", "usernameClaim")
rename(oldConfig, path, "username_prefix", "usernamePrefix")
rename(oldConfig, path, "groups_claim", "groupsClaim")
rename(oldConfig, path, "groups_prefix", "groupsPrefix")
rename(oldConfig, path, "signing_algs", "signingAlgs")
rename(oldConfig, path, "required_claim", "requiredClaim")
rename(oldConfig, path, "ca_file", "caFile")

// rename workers.config to providerSpec
workers, exists := oldConfig.GetArray(yamled.Path{"workers"})
if exists {
total := len(workers)

for i := 0; i < total; i++ {
rename(oldConfig, yamled.Path{"workers", i}, "config", "providerSpec")
}
}

return oldConfig.Root(), nil
}

// loadClusterConfig takes path to the Cluster Config (old API) and returns yamled.Document
func loadClusterConfig(oldConfigPath string) (*yamled.Document, error) {
f, err := os.Open(oldConfigPath)
if err != nil {
return nil, errors.Wrap(err, "failed to open file")
}
defer f.Close()

return yamled.Load(f)
}

// rename renames the YAML field
func rename(doc *yamled.Document, basePath yamled.Path, oldKey string, newKey string) {
oldPath := append(basePath, oldKey)
newPath := append(basePath, newKey)

data, exists := doc.Get(oldPath)
if exists {
doc.Remove(oldPath)
doc.Set(newPath, data)
}
}
Loading