diff --git a/cncd/pipeline/pipec/exec_test.go b/cncd/pipeline/pipec/exec_test.go index 0f5a1a64b..35afb453c 100644 --- a/cncd/pipeline/pipec/exec_test.go +++ b/cncd/pipeline/pipec/exec_test.go @@ -46,7 +46,8 @@ func TestExec(t *testing.T) { } func TestKubeExec(t *testing.T) { - reader, err := os.Open("../samples/sample_1/pipeline.json") + // reader, err := os.Open("../samples/sample_1/pipeline.json") + reader, err := os.Open("../samples/sample_7_redis/pipeline.json") if err != nil { t.Errorf("Could not read pipeline %f", err) } diff --git a/cncd/pipeline/pipeline/backend/kubernetes/kubernetes.go b/cncd/pipeline/pipeline/backend/kubernetes/kubernetes.go index bd5268700..8bdb38f61 100644 --- a/cncd/pipeline/pipeline/backend/kubernetes/kubernetes.go +++ b/cncd/pipeline/pipeline/backend/kubernetes/kubernetes.go @@ -103,15 +103,17 @@ func (e *engine) Exec(ctx context.Context, step *backend.Step) error { return err } - // for _, n := range proc.Networks { - // svc := Service(e.namespace, n.Aliases[0], pod.Name, nil) - // if svc == nil { - // continue - // } - // if _, err := e.kubeClient.CoreV1().Services(e.namespace).Create(svc); err != nil { - // return err - // } - // } + for _, n := range step.Networks { + if len(n.Aliases) > 0 { + svc := Service(e.namespace, n.Aliases[0], pod.Name, step.Ports) + if svc == nil { + continue + } + if _, err := e.kubeClient.CoreV1().Services(e.namespace).Create(svc); err != nil { + return err + } + } + } _, err = e.kubeClient.CoreV1().Pods(e.namespace).Create(pod) return err @@ -249,15 +251,15 @@ func (e *engine) Destroy(ctx context.Context, conf *backend.Config) error { return err } - // for _, n := range step.Networks { - // svc := Service(e.namespace, n.Aliases[0], step.Alias, n.Ports) - // if svc == nil { - // continue - // } - // if err := e.client.CoreV1().Services(e.namespace).Delete(svc.Name, deleteOpts); err != nil { - // return err - // } - // } + for _, n := range step.Networks { + svc := Service(e.namespace, n.Aliases[0], step.Alias, step.Ports) + if svc == nil { + continue + } + if err := e.kubeClient.CoreV1().Services(e.namespace).Delete(svc.Name, deleteOpts); err != nil { + return err + } + } } } diff --git a/cncd/pipeline/pipeline/backend/kubernetes/pod.go b/cncd/pipeline/pipeline/backend/kubernetes/pod.go index e1ed7510e..810618ac2 100644 --- a/cncd/pipeline/pipeline/backend/kubernetes/pod.go +++ b/cncd/pipeline/pipeline/backend/kubernetes/pod.go @@ -10,24 +10,27 @@ import ( ) func Pod(namespace string, step *backend.Step) (*v1.Pod, error) { + var vols []v1.Volume var volMounts []v1.VolumeMount - for _, vol := range step.Volumes { - vols = append(vols, v1.Volume{ - Name: volumeName(vol), - VolumeSource: v1.VolumeSource{ - PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ - ClaimName: volumeName(vol), - ReadOnly: false, + if step.WorkingDir != "" { + for _, vol := range step.Volumes { + vols = append(vols, v1.Volume{ + Name: volumeName(vol), + VolumeSource: v1.VolumeSource{ + PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ + ClaimName: volumeName(vol), + ReadOnly: false, + }, }, - }, - }) + }) - volMounts = append(volMounts, v1.VolumeMount{ - Name: volumeName(vol), - MountPath: volumeMountPath(step.WorkingDir), - //MountPath: volumeMountPath(vol.Target), - }) + volMounts = append(volMounts, v1.VolumeMount{ + Name: volumeName(vol), + MountPath: volumeMountPath(step.WorkingDir), + //MountPath: volumeMountPath(vol.Target), + }) + } } pullPolicy := v1.PullIfNotPresent @@ -39,7 +42,7 @@ func Pod(namespace string, step *backend.Step) (*v1.Pod, error) { args := step.Command envs := mapToEnvVars(step.Environment) - if !strings.HasSuffix(step.Name, "_clone_0") { + if _, hasScript := step.Environment["CI_SCRIPT"]; !strings.HasSuffix(step.Name, "_clone") && hasScript { command = []string{"/bin/sh", "-c"} args = []string{"echo $CI_SCRIPT | base64 -d | /bin/sh -e"} } diff --git a/cncd/pipeline/pipeline/backend/kubernetes/services.go b/cncd/pipeline/pipeline/backend/kubernetes/services.go index 589cbb563..6ff2e5206 100644 --- a/cncd/pipeline/pipeline/backend/kubernetes/services.go +++ b/cncd/pipeline/pipeline/backend/kubernetes/services.go @@ -1,7 +1,7 @@ package kubernetes import ( - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" ) diff --git a/cncd/pipeline/pipeline/backend/kubernetes/volumes.go b/cncd/pipeline/pipeline/backend/kubernetes/volumes.go index 751f76547..6db134c39 100644 --- a/cncd/pipeline/pipeline/backend/kubernetes/volumes.go +++ b/cncd/pipeline/pipeline/backend/kubernetes/volumes.go @@ -3,7 +3,7 @@ package kubernetes import ( "strings" - "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" ) @@ -27,7 +27,7 @@ func PersistentVolume(node, namespace, name string) *v1.PersistentVolume { StorageClassName: "local-storage", PersistentVolumeSource: v1.PersistentVolumeSource{ Local: &v1.LocalVolumeSource{ - Path: "/tmp", + Path: "/tmp/drone", }, }, NodeAffinity: &v1.VolumeNodeAffinity{ diff --git a/cncd/pipeline/pipeline/backend/types.go b/cncd/pipeline/pipeline/backend/types.go index e9132d41b..63972dbd8 100644 --- a/cncd/pipeline/pipeline/backend/types.go +++ b/cncd/pipeline/pipeline/backend/types.go @@ -48,6 +48,7 @@ type ( NetworkMode string `json:"network_mode,omitempty"` IpcMode string `json:"ipc_mode,omitempty"` Sysctls map[string]string `json:"sysctls,omitempty"` + Ports []int `json:"ports,omitempty"` } // Auth defines registry authentication credentials. diff --git a/cncd/pipeline/samples/sample_2/pipeline.json b/cncd/pipeline/samples/sample_2/pipeline.json index af52677ce..37b5b96c4 100644 --- a/cncd/pipeline/samples/sample_2/pipeline.json +++ b/cncd/pipeline/samples/sample_2/pipeline.json @@ -75,6 +75,7 @@ "alias": "database", "image": "mysql:latest", "detach": true, + "ports": [ 3306 ], "environment": { "CI": "pipec", "CI_BUILD_CREATED": "1486119586", @@ -143,7 +144,7 @@ { "name": "pipeline_step_0", "alias": "build", - "image": "golang:1.7", + "image": "golang:1.11", "working_dir": "/go/src/github.com/go-sql-driver/mysql", "environment": { "CI": "pipec", diff --git a/cncd/pipeline/samples/sample_2/pipeline.yml b/cncd/pipeline/samples/sample_2/pipeline.yml index 57e26d9d6..7d75ff349 100644 --- a/cncd/pipeline/samples/sample_2/pipeline.yml +++ b/cncd/pipeline/samples/sample_2/pipeline.yml @@ -4,7 +4,7 @@ workspace: pipeline: build: - image: golang:1.7 + image: golang:1.11 environment: MYSQL_TEST_ADDR: database:3306 commands: diff --git a/cncd/pipeline/samples/sample_7_redis/pipeline.json b/cncd/pipeline/samples/sample_7_redis/pipeline.json index 41b50dd4c..13bcc3e2b 100644 --- a/cncd/pipeline/samples/sample_7_redis/pipeline.json +++ b/cncd/pipeline/samples/sample_7_redis/pipeline.json @@ -8,6 +8,7 @@ "name": "pipeline_services_0", "alias": "redis1", "image": "redis:3.0", + "ports": [ 6379 ], "detach": true, "environment": { "CI": "drone", @@ -23,7 +24,7 @@ "DRONE_WORKSPACE": "/go/src/github.com/drone/envsubst" }, "volumes": [ - "/Users/bradrydzewski/code/src/github.com/laszlocph/drone-oss-08/cncd/pipeline/samples/sample_7_redis:/go/src/github.com/drone/envsubst" + "pipeline_default:/go/src/github.com/drone/envsubst" ], "networks": [ { @@ -40,6 +41,7 @@ "name": "pipeline_services_1", "alias": "redis2", "image": "redis:3.0", + "ports": [ 6379 ], "detach": true, "environment": { "CI": "drone", @@ -55,7 +57,7 @@ "DRONE_WORKSPACE": "/go/src/github.com/drone/envsubst" }, "volumes": [ - "/Users/bradrydzewski/code/src/github.com/laszlocph/drone-oss-08/cncd/pipeline/samples/sample_7_redis:/go/src/github.com/drone/envsubst" + "pipeline_default:/go/src/github.com/drone/envsubst" ], "networks": [ { @@ -103,7 +105,7 @@ "echo $CI_SCRIPT | base64 -d | /bin/sh -e" ], "volumes": [ - "/Users/bradrydzewski/code/src/github.com/laszlocph/drone-oss-08/cncd/pipeline/samples/sample_7_redis:/go/src/github.com/drone/envsubst" + "pipeline_default:/go/src/github.com/drone/envsubst" ], "networks": [ {