[AIR-137] Add isogen subcommand for bootstrap

This is just a frame for command that will execute docker container
with ISO builder scripts

Change-Id: I59a5c494785af3cbcffd3b9f6a488d93f73f4878
This commit is contained in:
Dmitry Ukov 2019-08-13 07:17:22 +00:00
parent 1c999e2095
commit 1f2e20e106
11 changed files with 179 additions and 10 deletions

View File

@ -1,8 +1,6 @@
package bootstrap package bootstrap
import ( import (
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
@ -12,12 +10,11 @@ import (
func NewBootstrapCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { func NewBootstrapCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command {
bootstrapRootCmd := &cobra.Command{ bootstrapRootCmd := &cobra.Command{
Use: "bootstrap", Use: "bootstrap",
Short: "bootstraps airshipctl", Short: "Bootstrap ephemeral Kubernetes cluster",
Run: func(cmd *cobra.Command, args []string) {
out := cmd.OutOrStdout()
fmt.Fprintf(out, "Under construction\n")
},
} }
ISOGenCmd := NewISOGenCommand(bootstrapRootCmd, rootSettings)
bootstrapRootCmd.AddCommand(ISOGenCmd)
return bootstrapRootCmd return bootstrapRootCmd
} }

View File

@ -0,0 +1,24 @@
package bootstrap
import (
"github.com/spf13/cobra"
"opendev.org/airship/airshipctl/pkg/bootstrap/isogen"
"opendev.org/airship/airshipctl/pkg/environment"
)
// NewISOGenCommand creates a new command for ISO image creation
func NewISOGenCommand(parent *cobra.Command, rootSettings *environment.AirshipCTLSettings) *cobra.Command {
settings := &isogen.Settings{AirshipCTLSettings: rootSettings}
imageGen := &cobra.Command{
Use: "isogen",
Short: "Generate bootstrap ISO image",
RunE: func(cmd *cobra.Command, args []string) error {
return isogen.GenerateBootstrapIso(settings, args, cmd.OutOrStdout())
},
}
settings.InitFlags(imageGen)
return imageGen
}

View File

@ -0,0 +1,26 @@
package bootstrap_test
import (
"testing"
"opendev.org/airship/airshipctl/cmd/bootstrap"
"opendev.org/airship/airshipctl/testutil"
)
func TestBootstrap(t *testing.T) {
tests := []*testutil.CmdTest{
{
Name: "bootstrap-cmd-with-defaults",
CmdLine: "",
Cmd: bootstrap.NewBootstrapCommand(nil),
},
{
Name: "bootstrap-isogen-cmd-with-help",
CmdLine: "isogen --help",
Cmd: bootstrap.NewBootstrapCommand(nil),
},
}
for _, tt := range tests {
testutil.RunTest(t, tt)
}
}

View File

@ -0,0 +1,13 @@
Bootstrap ephemeral Kubernetes cluster
Usage:
bootstrap [command]
Available Commands:
help Help about any command
isogen Generate bootstrap ISO image
Flags:
-h, --help help for bootstrap
Use "bootstrap [command] --help" for more information about a command.

View File

@ -0,0 +1,8 @@
Generate bootstrap ISO image
Usage:
bootstrap isogen [flags]
Flags:
-c, --config string Configuration file path for ISO builder container.
-h, --help help for isogen

View File

@ -5,7 +5,7 @@ Usage:
Available Commands: Available Commands:
argo argo is the command line interface to Argo argo argo is the command line interface to Argo
bootstrap bootstraps airshipctl bootstrap Bootstrap ephemeral Kubernetes cluster
completion Generate autocompletions script for the specified shell (bash or zsh) completion Generate autocompletions script for the specified shell (bash or zsh)
help Help about any command help Help about any command
kubeadm kubeadm: easily bootstrap a secure Kubernetes cluster kubeadm kubeadm: easily bootstrap a secure Kubernetes cluster

View File

@ -4,7 +4,7 @@ Usage:
airshipctl [command] airshipctl [command]
Available Commands: Available Commands:
bootstrap bootstraps airshipctl bootstrap Bootstrap ephemeral Kubernetes cluster
help Help about any command help Help about any command
version Show the version number of airshipctl version Show the version number of airshipctl

2
go.mod
View File

@ -110,6 +110,6 @@ require (
k8s.io/metrics v0.0.0-20190516231729-8b83d5daaa8f // indirect k8s.io/metrics v0.0.0-20190516231729-8b83d5daaa8f // indirect
k8s.io/utils v0.0.0-20190529001817-6999998975a7 // indirect k8s.io/utils v0.0.0-20190529001817-6999998975a7 // indirect
sigs.k8s.io/kustomize v2.0.3+incompatible // indirect sigs.k8s.io/kustomize v2.0.3+incompatible // indirect
sigs.k8s.io/yaml v1.1.0 // indirect sigs.k8s.io/yaml v1.1.0
vbom.ml/util v0.0.0-20180919145318-efcd4e0f9787 // indirect vbom.ml/util v0.0.0-20180919145318-efcd4e0f9787 // indirect
) )

View File

@ -0,0 +1,28 @@
package isogen
import (
"errors"
"fmt"
"io"
"opendev.org/airship/airshipctl/pkg/util"
)
// ErrNotImplemented returned for not implemented features
var ErrNotImplemented = errors.New("Error. Not implemented")
// GenerateBootstrapIso will generate data for cloud init and start ISO builder container
func GenerateBootstrapIso(settings *Settings, args []string, out io.Writer) error {
if settings.IsogenConfigFile == "" {
fmt.Fprintln(out, "Reading config file location from global settings is not supported")
return ErrNotImplemented
}
cfg := Config{}
if err := util.ReadYAMLFile(settings.IsogenConfigFile, &cfg); err != nil {
return err
}
fmt.Println("Under construction")
return nil
}

View File

@ -0,0 +1,56 @@
package isogen
import (
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
"opendev.org/airship/airshipctl/pkg/environment"
)
// Settings settings for isogen command
type Settings struct {
*environment.AirshipCTLSettings
// Configuration file (YAML-formatted) path for ISO builder container.
IsogenConfigFile string
}
// InitFlags adds falgs and their default settings for isogen command
func (i *Settings) InitFlags(cmd *cobra.Command) {
flags := cmd.Flags()
flags.StringVarP(&i.IsogenConfigFile, "config", "c", "", "Configuration file path for ISO builder container.")
}
// Config ISO builder container configuration
type Config struct {
// Configuration parameters for container
Container Container `json:"container,omitempty"`
// Configuration parameters for ISO builder
Builder Builder `json:"builder,omitempty"`
}
// Container parameters
type Container struct {
// Container volume directory binding.
Volume string `json:"volume,omitempty"`
// ISO generator container image URL
Image string `json:"image,omitempty"`
// Container Runtime Interface driver
ContainerRuntime string `json:"containerRuntime,omitempty"`
}
// Builder parameters
type Builder struct {
// Cloud Init user-data file name placed to the container volume root
UserDataFileName string `json:"userDataFileName,omitempty"`
// Cloud Init network-config file name placed to the container volume root
NetworkConfigFileName string `json:"networkConfigFileName,omitempty"`
// File name for output metadata
OutputMetadataFileName string `json:"outputMetadataFileName,omitempty"`
}
// ToYAML serializes confid to YAML
func (c *Config) ToYAML() ([]byte, error) {
return yaml.Marshal(c)
}

17
pkg/util/configreader.go Normal file
View File

@ -0,0 +1,17 @@
package util
import (
"io/ioutil"
"sigs.k8s.io/yaml"
)
// ReadYAMLFile reads YAML-formatted configuration file and
// de-serializes it to a given object
func ReadYAMLFile(filePath string, cfg interface{}) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
return yaml.Unmarshal(data, cfg)
}