Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Use a single namespace via kube-namespace flag #56

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
49 changes: 33 additions & 16 deletions engine/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import (
)

type kubeEngine struct {
client *kubernetes.Clientset
node string
client *kubernetes.Clientset
node string
namespace string
}

// NewFile returns a new Kubernetes engine from a
// Kubernetes configuration file (~/.kube/config).
func NewFile(url, path, node string) (engine.Engine, error) {
func NewFile(url, path, node, namespace string) (engine.Engine, error) {
config, err := clientcmd.BuildConfigFromFlags(url, path)
if err != nil {
return nil, err
Expand All @@ -40,18 +41,25 @@ func NewFile(url, path, node string) (engine.Engine, error) {
if err != nil {
return nil, err
}
return &kubeEngine{client: client, node: node}, nil
return &kubeEngine{client: client, node: node, namespace: namespace}, nil
}

func (e *kubeEngine) Setup(ctx context.Context, spec *engine.Spec) error {
if e.namespace != "" {
spec.Metadata.Namespace = e.namespace
}

ns := toNamespace(spec)

// create the project namespace. all pods and
// containers are created within the namespace, and
// are removed when the pipeline execution completes.
_, err := e.client.CoreV1().Namespaces().Create(ns)
if err != nil {
return err
// Create namespace if no default one given
if e.namespace == "" {
// create the project namespace. all pods and
// containers are created within the namespace, and
// are removed when the pipeline execution completes.
_, err := e.client.CoreV1().Namespaces().Create(ns)
if err != nil {
return err
}
}

// create all secrets
Expand Down Expand Up @@ -124,6 +132,11 @@ func (e *kubeEngine) Create(_ context.Context, _ *engine.Spec, _ *engine.Step) e
}

func (e *kubeEngine) Start(ctx context.Context, spec *engine.Spec, step *engine.Step) error {
if e.namespace != "" {
spec.Metadata.Namespace = e.namespace
step.Metadata.Namespace = e.namespace
}

pod := toPod(spec, step)
if len(step.Docker.Ports) != 0 {
service := toService(spec, step)
Expand Down Expand Up @@ -268,10 +281,14 @@ func (e *kubeEngine) Destroy(ctx context.Context, spec *engine.Spec) error {
),
)

// deleting the namespace should destroy all secrets,
// volumes, configuration files and more.
return e.client.CoreV1().Namespaces().Delete(
spec.Metadata.Namespace,
&metav1.DeleteOptions{},
)
if e.namespace == "" {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!=?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, nvm. The code should be more more self-decriptive or documented in my opinion. Consider adding a bool useSignleNamespace = e.namespace != "" and rely on it in the checks.

// deleting the namespace should destroy all secrets,
// volumes, configuration files and more.
return e.client.CoreV1().Namespaces().Delete(
spec.Metadata.Namespace,
&metav1.DeleteOptions{},
)
}

return nil
}
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
u := flag.String("kube-url", "", "")
n := flag.String("kube-node", "", "")
d := flag.Bool("kube-debug", false, "")
ns := flag.String("kube-namespace", "drone", "")
t := flag.Duration("timeout", time.Hour, "")
h := flag.Bool("help", false, "")

Expand Down Expand Up @@ -73,7 +74,7 @@ func main() {
log.Fatalln(err)
}
} else {
engine, err = kube.NewFile(*u, *k, *n)
engine, err = kube.NewFile(*u, *k, *n, *ns)
if err != nil {
log.Fatalln(err)
}
Expand Down Expand Up @@ -108,6 +109,7 @@ func usage() {
--kube-config loads a kubernetes config file
--kube-url sets a kubernetes endpoint
--kube-debug writes a kubernetes configuration to stdout
--kube-namespace sets the kubernetes namespace to run in
--timeout sets an execution timeout
-h, --help display this help and exit`)
}