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{ diff --git a/engine/kube/util.go b/engine/kube/util.go index 479fc37..763e226 100644 --- a/engine/kube/util.go +++ b/engine/kube/util.go @@ -7,12 +7,12 @@ package kube import ( "path" "path/filepath" - "strings" "strconv" + "strings" "github.com/drone/drone-runtime/engine" - "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" @@ -22,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 { @@ -236,6 +237,7 @@ 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 { + var volumes []v1.Volume volumes = append(volumes, toVolumes(spec, step)...) volumes = append(volumes, toConfigVolumes(spec, step)...) @@ -253,9 +255,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: step.Annotations, }, Spec: v1.PodSpec{ AutomountServiceAccountToken: boolptr(false), diff --git a/engine/spec.go b/engine/spec.go index e0579d4..7dd03eb 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"` 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) }