Skip to content

Commit

Permalink
feat: add validate logic for plugin (#7)
Browse files Browse the repository at this point in the history
* feat: add validate logic for kubernetes flags

* feat: add validate logic for config flags
  • Loading branch information
jbrockopp authored Feb 11, 2020
1 parent a784e7e commit 18cf45e
Show file tree
Hide file tree
Showing 7 changed files with 361 additions and 4 deletions.
51 changes: 51 additions & 0 deletions cmd/vela-kubernetes/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"fmt"
"time"

"github.com/sirupsen/logrus"
)

// Config represents the plugin configuration for Kubernetes config information.
type Config struct {
// Kubernetes files or directories to apply
Files []string
// new container images from files to apply
Images []string
// Kubernetes resources to watch status of
Statuses []string
// total time allowed to watch Kubernetes resources
Timeout time.Duration
}

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

// verify files are provided
if len(c.Files) == 0 {
return fmt.Errorf("no config files provided")
}

// verify images are provided
if len(c.Images) == 0 {
return fmt.Errorf("no config images provided")
}

// verify statuses are provided
if len(c.Statuses) == 0 {
return fmt.Errorf("no config statuses provided")
}

// verify timeout is provided
if c.Timeout <= 0 {
return fmt.Errorf("no config timeout provided")
}

return nil
}
81 changes: 81 additions & 0 deletions cmd/vela-kubernetes/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"testing"
"time"
)

func TestKubernetes_Config_Validate(t *testing.T) {
// setup types
c := &Config{
Files: []string{"files"},
Images: []string{"images"},
Statuses: []string{"statuses"},
Timeout: 5 * time.Minute,
}

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

func TestKubernetes_Config_Validate_NoFiles(t *testing.T) {
// setup types
c := &Config{
Images: []string{"images"},
Statuses: []string{"statuses"},
Timeout: 5 * time.Minute,
}

err := c.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}

func TestKubernetes_Config_Validate_NoImages(t *testing.T) {
// setup types
c := &Config{
Files: []string{"files"},
Statuses: []string{"statuses"},
Timeout: 5 * time.Minute,
}

err := c.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}

func TestKubernetes_Config_Validate_NoStatuses(t *testing.T) {
// setup types
c := &Config{
Files: []string{"files"},
Images: []string{"images"},
Timeout: 5 * time.Minute,
}

err := c.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}

func TestKubernetes_Config_Validate_NoTimeout(t *testing.T) {
// setup types
c := &Config{
Files: []string{"files"},
Images: []string{"images"},
Statuses: []string{"statuses"},
}

err := c.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}
43 changes: 43 additions & 0 deletions cmd/vela-kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"fmt"

"github.com/sirupsen/logrus"
)

// Kubernetes represents the plugin configuration for Kubernetes information.
type Kubernetes struct {
// cluster configuration to use for interactions
Config string
// cluster context to use for interactions
Context string
// cluster namespace to use for interactions
Namespace string
}

// Validate verifies the Kubernetes is properly configured.
func (k *Kubernetes) Validate() error {
logrus.Trace("validating kubernetes configuration")

// verify config is provided
if len(k.Config) == 0 {
return fmt.Errorf("no kubernetes config provided")
}

// verify context is provided
if len(k.Context) == 0 {
return fmt.Errorf("no kubernetes context provided")
}

// verify namespace is provided
if len(k.Namespace) == 0 {
return fmt.Errorf("no kubernetes namespace provided")
}

return nil
}
62 changes: 62 additions & 0 deletions cmd/vela-kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2020 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

package main

import (
"testing"
)

func TestKubernetes_Kubernetes_Validate(t *testing.T) {
// setup types
k := &Kubernetes{
Config: "config",
Context: "context",
Namespace: "namespace",
}

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

func TestKubernetes_Kubernetes_Validate_NoConfig(t *testing.T) {
// setup types
k := &Kubernetes{
Context: "context",
Namespace: "namespace",
}

err := k.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}

func TestKubernetes_Kubernetes_Validate_NoContext(t *testing.T) {
// setup types
k := &Kubernetes{
Config: "config",
Namespace: "namespace",
}

err := k.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}

func TestKubernetes_Kubernetes_Validate_NoNamespace(t *testing.T) {
// setup types
k := &Kubernetes{
Config: "config",
Context: "context",
}

err := k.Validate()
if err == nil {
t.Errorf("Validate should have returned err")
}
}
59 changes: 57 additions & 2 deletions cmd/vela-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"os"

"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -46,6 +45,48 @@ func main() {
Usage: "set log level - options: (trace|debug|info|warn|error|fatal|panic)",
Value: "info",
},

// Config Flags

cli.StringSliceFlag{
EnvVar: "PARAMETER_FILES,CONFIG_FILES",
Name: "config.files",
Usage: "kubernetes files or directories to interact with",
},
cli.StringFlag{
EnvVar: "PARAMETER_IMAGES,CONFIG_IMAGES",
Name: "config.images",
Usage: "container images from files to interact with",
},
cli.StringSliceFlag{
EnvVar: "PARAMETER_STATUSES,CONFIG_STATUSES",
Name: "config.statuses",
Usage: "kubernetes resources to watch status on",
},
cli.DurationFlag{
EnvVar: "PARAMETER_TIMEOUT,CONFIG_TIMEOUT",
Name: "config.timeout",
Usage: "maximum duration to watch status on kubernetes resources",
Value: 5 * time.Minute,
},

// Kubernetes Flags

cli.StringFlag{
EnvVar: "PARAMETER_CONFIG,KUBE_CONFIG,KUBERNETES_CONFIG",
Name: "kubernetes.config",
Usage: "kubernetes cluster configuration",
},
cli.StringFlag{
EnvVar: "PARAMETER_CONTEXT,KUBE_CONTEXT,KUBERNETES_CONTEXT",
Name: "kubernetes.context",
Usage: "kubernetes cluster context to interact with",
},
cli.StringFlag{
EnvVar: "PARAMETER_NAMESPACE,KUBE_NAMESPACE,KUBERNETES_NAMESPACE",
Name: "kubernetes.namespace",
Usage: "kubernetes cluster namespace to interact with",
},
}

err := app.Run(os.Args)
Expand Down Expand Up @@ -83,7 +124,21 @@ func run(c *cli.Context) error {
}).Info("Vela Kubernetes Plugin")

// create the plugin
p := &Plugin{}
p := &Plugin{
// config configuration
Config: &Config{
Files: c.StringSlice("config.files"),
Images: c.StringSlice("config.images"),
Statuses: c.StringSlice("config.statuses"),
Timeout: c.Duration("config.timeout"),
},
// Kubernetes configuration
Kubernetes: &Kubernetes{
Config: c.String("kubernetes.config"),
Context: c.String("kubernetes.context"),
Namespace: c.String("kubernetes.namespace"),
},
}

// validate the plugin
err := p.Validate()
Expand Down
19 changes: 18 additions & 1 deletion cmd/vela-kubernetes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import (
)

// Plugin represents the configuration loaded for the plugin.
type Plugin struct{}
type Plugin struct {
// config arguments loaded for the plugin
Config *Config
// kubernetes arguments loaded for the plugin
Kubernetes *Kubernetes
}

// Command formats and outputs the command necessary for
// kubectl to manage Kubernetes resources.
Expand All @@ -35,5 +40,17 @@ func (p *Plugin) Exec() error {
func (p *Plugin) Validate() error {
logrus.Debug("validating plugin configuration")

// validate config configuration
err := p.Config.Validate()
if err != nil {
return err
}

// validate Kubernetes configuration
err = p.Kubernetes.Validate()
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 18cf45e

Please sign in to comment.