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

feat: add action for plugin #28

Merged
merged 1 commit into from
Feb 21, 2020
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
2 changes: 2 additions & 0 deletions cmd/vela-kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/sirupsen/logrus"
)

const applyAction = "apply"

// Apply represents the plugin configuration for Apply config information.
type Apply struct {
// enables pretending to apply the files
Expand Down
2 changes: 2 additions & 0 deletions cmd/vela-kubernetes/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func TestKubernetes_Apply_Command(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
Expand Down Expand Up @@ -49,6 +50,7 @@ func TestKubernetes_Apply_Command(t *testing.T) {
func TestKubernetes_Apply_Exec_Error(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
Expand Down
15 changes: 11 additions & 4 deletions cmd/vela-kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@ var appFS = afero.NewOsFs()

// Config represents the plugin configuration for Kubernetes information.
type Config struct {
// cluster configuration file to interact with Kubernetes
// action to perform against Kubernetes
Action string
// configuration file for communication with Kubernetes
File string
// name of the configuration cluster to use for interactions with Kubernetes
// name of the configuration cluster from file
Cluster string
// name of the configuration context to use for interactions with Kubernetes
// name of the configuration context from file
Context string
// name of the configuration namespace to use for interactions with Kubernetes
// name of the configuration namespace from file
Namespace string
}

// Validate verifies the Config is properly configured.
func (c *Config) Validate() error {
logrus.Trace("validating config configuration")

// verify action is provided
if len(c.Action) == 0 {
return fmt.Errorf("no config action provided")
}

// verify file is provided
if len(c.File) == 0 {
return fmt.Errorf("no config file provided")
Expand Down
20 changes: 20 additions & 0 deletions cmd/vela-kubernetes/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import (
func TestKubernetes_Config_Validate(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
Namespace: "namespace",
}

err := c.Validate()
if err != nil {
t.Errorf("Validate returned err: %v", err)
}
}

func TestKubernetes_Config_Validate_NoAction(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
Expand All @@ -28,6 +45,7 @@ func TestKubernetes_Config_Validate(t *testing.T) {
func TestKubernetes_Config_Validate_NoFile(t *testing.T) {
// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
Namespace: "namespace",
Expand All @@ -45,6 +63,7 @@ func TestKubernetes_Config_Write(t *testing.T) {

// setup types
c := &Config{
Action: "apply",
File: "file",
Cluster: "cluster",
Context: "context",
Expand All @@ -63,6 +82,7 @@ func TestKubernetes_Config_Write_NoFile(t *testing.T) {

// setup types
c := &Config{
Action: "apply",
Cluster: "cluster",
Context: "context",
Namespace: "namespace",
Expand Down
6 changes: 6 additions & 0 deletions cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func main() {

// Config Flags

cli.StringFlag{
EnvVar: "PARAMETER_ACTION,CONFIG_ACTION,KUBE_ACTION",
Name: "config.action",
Usage: "action to perform against Kubernetes",
},
cli.StringFlag{
EnvVar: "PARAMETER_CONFIG,CONFIG_FILE,KUBE_CONFIG",
Name: "config.file",
Expand Down Expand Up @@ -176,6 +181,7 @@ func run(c *cli.Context) error {
},
// config configuration
Config: &Config{
Action: c.String("config.action"),
File: c.String("config.file"),
Cluster: c.String("config.cluster"),
Context: c.String("config.context"),
Expand Down
2 changes: 2 additions & 0 deletions cmd/vela-kubernetes/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/sirupsen/logrus"
)

const patchAction = "patch"

// cronPatch represents the pattern needed to
// patch a Kubernetes CronJob with a new image.
//
Expand Down
2 changes: 2 additions & 0 deletions cmd/vela-kubernetes/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func TestKubernetes_Patch_Command(t *testing.T) {
// setup types
c := &Config{
Action: "patch",
File: "file",
Cluster: "cluster",
Context: "context",
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestKubernetes_Patch_Command(t *testing.T) {
func TestKubernetes_Patch_Exec_Error(t *testing.T) {
// setup types
c := &Config{
Action: "patch",
File: "file",
Cluster: "cluster",
Context: "context",
Expand Down
89 changes: 48 additions & 41 deletions cmd/vela-kubernetes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
package main

import (
"errors"
"fmt"

"github.com/sirupsen/logrus"
)

var (
// ErrInvalidAction defines the error type when the
// Action provided to the Plugin is unsupported.
ErrInvalidAction = errors.New("invalid action provided")
)

// Plugin represents the configuration loaded for the plugin.
type Plugin struct {
// apply arguments loaded for the plugin
Expand Down Expand Up @@ -36,28 +45,27 @@ func (p *Plugin) Exec() error {
return err
}

// check if containers were provided to patch
if len(p.Patch.Containers) > 0 {
// patch the Kubernetes resource files
err = p.Patch.Exec(p.Config)
if err != nil {
return err
}
} else { // default back to apply if no containers to patch
// apply the Kubernetes resource files
err = p.Apply.Exec(p.Config)
if err != nil {
return err
}
}

// watch the status for the specified resources
err = p.Status.Exec(p.Config)
if err != nil {
return err
// execute action specific configuration
switch p.Config.Action {
case applyAction:
// execute apply action
return p.Apply.Exec(p.Config)
case patchAction:
// execute patch action
return p.Patch.Exec(p.Config)
case statusAction:
// execute status action
return p.Status.Exec(p.Config)
default:
return fmt.Errorf(
"%s: %s (Valid actions: %s, %s, %s)",
ErrInvalidAction,
p.Config.Action,
applyAction,
patchAction,
statusAction,
)
}

return nil
}

// Validate verifies the plugin is properly configured.
Expand All @@ -70,26 +78,25 @@ func (p *Plugin) Validate() error {
return err
}

// check if containers were provided to patch
if len(p.Patch.RawContainers) > 0 {
// validate patch configuration
err = p.Patch.Validate()
if err != nil {
return err
}
} else { // default back to apply if no containers to patch
// validate action specific configuration
switch p.Config.Action {
case applyAction:
// validate apply configuration
err = p.Apply.Validate()
if err != nil {
return err
}
}

// validate status configuration
err = p.Status.Validate()
if err != nil {
return err
return p.Apply.Validate()
case patchAction:
// validate patch configuration
return p.Patch.Validate()
case statusAction:
// validate status configuration
return p.Status.Validate()
default:
return fmt.Errorf(
"%s: %s (Valid actions: %s, %s, %s)",
ErrInvalidAction,
p.Config.Action,
applyAction,
patchAction,
statusAction,
)
}

return nil
}
Loading