From 1b4d75943123538c9612953b2f51fd7c03f86bb2 Mon Sep 17 00:00:00 2001 From: Dann Bohn Date: Thu, 28 Mar 2019 15:45:49 -0400 Subject: [PATCH 1/6] pulls in KUBE_RUNNER_ANNOTATONS and puts it on the job spec --- engine/kube/util.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/engine/kube/util.go b/engine/kube/util.go index 479fc37..5354d83 100644 --- a/engine/kube/util.go +++ b/engine/kube/util.go @@ -7,12 +7,14 @@ package kube import ( "path" "path/filepath" - "strings" "strconv" + "strings" "github.com/drone/drone-runtime/engine" + "github.com/drone/drone/cmd/drone-controller/config" + "github.com/sirupsen/logrus" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" @@ -236,6 +238,13 @@ func toResources(step *engine.Step) v1.ResourceRequirements { // helper function returns a kubernetes pod for the // given step and specification. func toPod(spec *engine.Spec, step *engine.Step) *v1.Pod { + config, err := config.Environ() + + if err != nil { + logrus.WithError(err).Fatalln("invalid configuration") + + } + var volumes []v1.Volume volumes = append(volumes, toVolumes(spec, step)...) volumes = append(volumes, toConfigVolumes(spec, step)...) @@ -253,9 +262,10 @@ func toPod(spec *engine.Spec, step *engine.Step) *v1.Pod { return &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ - Name: step.Metadata.UID, - Namespace: step.Metadata.Namespace, - Labels: step.Metadata.Labels, + Name: step.Metadata.UID, + Namespace: step.Metadata.Namespace, + Labels: step.Metadata.Labels, + Annotations: config.Runner.Annotations, }, Spec: v1.PodSpec{ AutomountServiceAccountToken: boolptr(false), From cbecfa4629a07b6d5f6206d4ed0d478ec486daa0 Mon Sep 17 00:00:00 2001 From: Dann Bohn Date: Thu, 28 Mar 2019 21:51:06 -0400 Subject: [PATCH 2/6] adds annotations to Runners Step --- engine/spec.go | 1 + 1 file changed, 1 insertion(+) diff --git a/engine/spec.go b/engine/spec.go index e0579d4..ae315fb 100644 --- a/engine/spec.go +++ b/engine/spec.go @@ -40,6 +40,7 @@ type ( // Step defines a pipeline step. Step struct { Metadata Metadata `json:"metadata,omitempty"` + Annotations map[string]string `json:annotations,omitempty"` Detach bool `json:"detach,omitempty"` DependsOn []string `json:"depends_on,omitempty"` Devices []*VolumeDevice `json:"devices,omitempty"` From 710f49b69173716e62c4eb7b9a06a8cf5aa260a8 Mon Sep 17 00:00:00 2001 From: Dann Bohn Date: Thu, 28 Mar 2019 21:51:31 -0400 Subject: [PATCH 3/6] pass annotations to engine --- engine/kube/kube.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/engine/kube/kube.go b/engine/kube/kube.go index 73860e2..258af23 100644 --- a/engine/kube/kube.go +++ b/engine/kube/kube.go @@ -14,7 +14,7 @@ import ( "github.com/drone/drone-runtime/engine" "github.com/drone/drone-runtime/engine/docker/auth" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/informers" @@ -25,13 +25,14 @@ import ( ) type kubeEngine struct { - client *kubernetes.Clientset - node string + client *kubernetes.Clientset + node string + annotations map[string]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 string, annotations map[string]string) (engine.Engine, error) { config, err := clientcmd.BuildConfigFromFlags(url, path) if err != nil { return nil, err @@ -40,7 +41,7 @@ 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, annotations: annotations}, nil } func (e *kubeEngine) Setup(ctx context.Context, spec *engine.Spec) error { @@ -133,6 +134,10 @@ func (e *kubeEngine) Start(ctx context.Context, spec *engine.Spec, step *engine. } } + if len(e.annotations) != 0 { + pod.ObjectMeta.Annotations = e.annotations + } + if e.node != "" { pod.Spec.Affinity = &v1.Affinity{ NodeAffinity: &v1.NodeAffinity{ From dada22666549040a83ecfc728fc2a890b069304c Mon Sep 17 00:00:00 2001 From: Dann Bohn Date: Thu, 28 Mar 2019 21:52:19 -0400 Subject: [PATCH 4/6] pulls in annottions from step --- engine/kube/util.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/engine/kube/util.go b/engine/kube/util.go index 5354d83..763e226 100644 --- a/engine/kube/util.go +++ b/engine/kube/util.go @@ -11,8 +11,6 @@ import ( "strings" "github.com/drone/drone-runtime/engine" - "github.com/drone/drone/cmd/drone-controller/config" - "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -24,6 +22,7 @@ import ( // helper function converts environment variable // string data to kubernetes variables. + func toEnv(spec *engine.Spec, step *engine.Step) []v1.EnvVar { var to []v1.EnvVar for k, v := range step.Envs { @@ -238,12 +237,6 @@ func toResources(step *engine.Step) v1.ResourceRequirements { // helper function returns a kubernetes pod for the // given step and specification. func toPod(spec *engine.Spec, step *engine.Step) *v1.Pod { - config, err := config.Environ() - - if err != nil { - logrus.WithError(err).Fatalln("invalid configuration") - - } var volumes []v1.Volume volumes = append(volumes, toVolumes(spec, step)...) @@ -265,7 +258,7 @@ func toPod(spec *engine.Spec, step *engine.Step) *v1.Pod { Name: step.Metadata.UID, Namespace: step.Metadata.Namespace, Labels: step.Metadata.Labels, - Annotations: config.Runner.Annotations, + Annotations: step.Annotations, }, Spec: v1.PodSpec{ AutomountServiceAccountToken: boolptr(false), From c9c374d84e13f8760990c7e4d949a6c2e7eb27ff Mon Sep 17 00:00:00 2001 From: Dann Bohn Date: Thu, 28 Mar 2019 21:53:52 -0400 Subject: [PATCH 5/6] pass new annotations to NewFiew --- main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 329c765..d85e3e7 100644 --- a/main.go +++ b/main.go @@ -73,7 +73,9 @@ func main() { log.Fatalln(err) } } else { - engine, err = kube.NewFile(*u, *k, *n) + // send Empty annotatoins to NewFile + a := map[string]string{} + engine, err = kube.NewFile(*u, *k, *n, a) if err != nil { log.Fatalln(err) } From 1849bc5102e97f3d1fecefe44ccf16c38085a785 Mon Sep 17 00:00:00 2001 From: Dann Bohn Date: Thu, 28 Mar 2019 21:57:39 -0400 Subject: [PATCH 6/6] fix syntax error --- engine/spec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/spec.go b/engine/spec.go index ae315fb..7dd03eb 100644 --- a/engine/spec.go +++ b/engine/spec.go @@ -40,7 +40,7 @@ type ( // Step defines a pipeline step. Step struct { Metadata Metadata `json:"metadata,omitempty"` - Annotations map[string]string `json:annotations,omitempty"` + Annotations map[string]string `json:"annotations,omitempty"` Detach bool `json:"detach,omitempty"` DependsOn []string `json:"depends_on,omitempty"` Devices []*VolumeDevice `json:"devices,omitempty"`