Add cmd for downgrade (#173)

This commit is contained in:
Sandeep Rajan 2019-05-21 11:38:22 -04:00 committed by Chris O'Haver
parent 38ae5466f7
commit 617ccc1151
5 changed files with 63 additions and 4 deletions

View file

@ -12,6 +12,7 @@ Usage:
corefile-tool default --corefile <path> [--k8sversion <k8s-ver>]
corefile-tool deprecated --from <coredns-ver> --to <coredns-ver> --corefile <path>
corefile-tool migrate --from <coredns-ver> --to <coredns-ver> --corefile <path> [--deprecations <true|false>]
corefile-tool downgrade --from <coredns-ver> --to <coredns-ver> --corefile <path>
corefile-tool released --dockerImageId <id>
corefile-tool unsupported --from <coredns-ver> --to <coredns-ver> --corefile <path>
corefile-tool validversions
@ -26,7 +27,9 @@ The following operations are supported:
- `deprecated`: returns a list of plugins/options in the Corefile that have been deprecated, removed, ignored or is a new default plugin/option.
- `migrate`: updates your CoreDNS corefile to be compatible with the `-to` version. Setting the `--deprecations` flag to `true` will migrate plugins/options as soon as they are announced as deprecated. Setting the `--deprecations` flag to `false` will migrate plugins/options only once they are removed (or made a no-op). The default is `false`.
- `migrate`: updates your CoreDNS corefile to be compatible with the `-to` version. Setting the `--deprecations` flag to `true` will migrate plugins/options as soon as they are announced as deprecated. Setting the `--deprecations` flag to `false` will migrate plugins/options only once they are removed (or made a no-op). The default is `false`.
- `downgrade` : downgrades your CoreDNS corefile to be compatible with the `-to` version. It will not restore plugins/options that might have been removed or altered during an upward migration.
- `released`: determines if the `--dockerImageID` was an official CoreDNS release or not. Only official releases of CoreDNS are supported by the tool.
@ -63,4 +66,8 @@ corefile-tool migrate --from 1.4.0 --to 1.5.0 --corefile /path/to/Corefile --de
# that are present in the current Corefile.
corefile-tool migrate --from 1.2.2 --to 1.3.1 --corefile /path/to/Corefile --deprecations false
```
```bash
# Downgrade CoreDNS from v1.5.0 to v1.4.0
corefile-tool downgrade --from 1.5.0 --to 1.4.0 --corefile /path/to/Corefile
```

View file

@ -0,0 +1,51 @@
package cmd
import (
"fmt"
"github.com/coredns/deployment/kubernetes/migration"
"github.com/spf13/cobra"
)
// NewDowngradeCmd represents the downgrade command
func NewDowngradeCmd() *cobra.Command {
var migrateCmd = &cobra.Command{
Use: "downgrade",
Short: "Downgrade your CoreDNS corefile to a previous version",
Example: `# Downgrade CoreDNS from v1.5.0 to v1.4.0.
corefile-tool downgrade --from 1.5.0 --to 1.4.0 --corefile /path/to/Corefile`,
RunE: func(cmd *cobra.Command, args []string) error {
from, _ := cmd.Flags().GetString("from")
to, _ := cmd.Flags().GetString("to")
corefile, _ := cmd.Flags().GetString("corefile")
migrated, err := downgradeCorefileFromPath(from, to, corefile)
if err != nil {
return fmt.Errorf("error while migration: %v \n", err)
}
fmt.Println(migrated)
return nil
},
}
migrateCmd.Flags().String("from", "", "Required: The version you are migrating from. ")
migrateCmd.MarkFlagRequired("from")
migrateCmd.Flags().String("to", "", "Required: The version you are migrating to.")
migrateCmd.MarkFlagRequired("to")
migrateCmd.Flags().String("corefile", "", "Required: The path where your Corefile is located.")
migrateCmd.MarkFlagRequired("corefile")
migrateCmd.Flags().Bool("deprecations", false, "Specify whether you want to handle plugin deprecations. [True | False] ")
return migrateCmd
}
// downgradeCorefileFromPath takes the path where the Corefile is located and downgrades the Corefile to the
// desrired version.
func downgradeCorefileFromPath(fromCoreDNSVersion, toCoreDNSVersion, corefilePath string) (string, error) {
fileBytes, err := getCorefileFromPath(corefilePath)
if err != nil {
return "", err
}
corefileStr := string(fileBytes)
return migration.MigrateDown(fromCoreDNSVersion, toCoreDNSVersion, corefileStr)
}

View file

@ -43,8 +43,8 @@ corefile-tool migrate --from 1.2.2 --to 1.3.1 --corefile /path/to/Corefile --de
return migrateCmd
}
// migrateCorefileFromPath takes the path where the Corefile is located and returns the deprecated plugins or directives
// present in the Corefile.
// migrateCorefileFromPath takes the path where the Corefile is located and migrates the Corefile to the
// desrired version.
func migrateCorefileFromPath(fromCoreDNSVersion, toCoreDNSVersion, corefilePath string, deprecations bool) (string, error) {
fileBytes, err := getCorefileFromPath(corefilePath)
if err != nil {

View file

@ -27,6 +27,7 @@ func CorefileTool() *cobra.Command {
`),
}
rootCmd.AddCommand(NewMigrateCmd())
rootCmd.AddCommand(NewDowngradeCmd())
rootCmd.AddCommand(NewDefaultCmd())
rootCmd.AddCommand(NewDeprecatedCmd())
rootCmd.AddCommand(NewUnsupportedCmd())

View file

@ -40,7 +40,7 @@ corefile-tool unsupported --from 1.4.0 --to 1.5.0 --corefile /path/to/Corefile`,
return unsupportedCmd
}
// unsupportedCorefileFromPath takes the path where the Corefile is located and returns a list of plugins
// unsupportedCorefileFromPath takes the path where the Corefile is located and returns a list of plugins
// that have been removed.
func unsupportedCorefileFromPath(fromCoreDNSVersion, toCoreDNSVersion, corefilePath string) ([]migration.Notice, error) {
fileBytes, err := getCorefileFromPath(corefilePath)