From 9e589be71ad8f2f4d26107d867b2c33662979058 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 21 Feb 2019 17:10:27 -0800 Subject: [PATCH] added image pull secrets --- yaml/from_secret.go | 50 ------------------- yaml/from_secret_test.go | 50 ------------------- yaml/pipeline.go | 7 +-- yaml/pretty/pipeline.go | 5 ++ yaml/pretty/pipeline_test.go | 9 ++++ .../testdata/pipeline_image_pull_secrets.yml | 11 ++++ .../pipeline_image_pull_secrets.yml.golden | 18 +++++++ 7 files changed, 47 insertions(+), 103 deletions(-) delete mode 100644 yaml/from_secret.go delete mode 100644 yaml/from_secret_test.go create mode 100644 yaml/pretty/testdata/pipeline_image_pull_secrets.yml create mode 100644 yaml/pretty/testdata/pipeline_image_pull_secrets.yml.golden diff --git a/yaml/from_secret.go b/yaml/from_secret.go deleted file mode 100644 index dc69d31..0000000 --- a/yaml/from_secret.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2019 Drone IO, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package yaml - -type ( - // FromSecret defines a secret sources from an external value. - // This could be a named secret stored in the Drone database, - // or a secret stored at an external path in a key value - // system such as Vault. - FromSecret struct { - Name string `json:"name"` - Path string `json:"path"` - } - - // fromSecret is a tempoary type used to unmarshal - // from_secret which may be short form vs long form. - fromSecret struct { - Name string `json:"name"` - Path string `json:"path"` - } -) - -// UnmarshalYAML implements yaml unmarshalling. -func (v *FromSecret) UnmarshalYAML(unmarshal func(interface{}) error) error { - err := unmarshal(&v.Name) - if err == nil { - return nil - } - - s := struct { - Name string - Path string - }{} - err = unmarshal(&s) - v.Name = s.Name - v.Path = s.Path - return err -} diff --git a/yaml/from_secret_test.go b/yaml/from_secret_test.go deleted file mode 100644 index 5336eac..0000000 --- a/yaml/from_secret_test.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2019 Drone.IO Inc. All rights reserved. -// Use of this source code is governed by the Drone Non-Commercial License -// that can be found in the LICENSE file. - -package yaml - -import ( - "testing" - - "gopkg.in/yaml.v2" -) - -func TestFromSecret(t *testing.T) { - tests := []struct { - yaml string - name string - path string - }{ - { - yaml: "bar", - name: "bar", - path: "", - }, - { - yaml: "{ name: bar }", - name: "bar", - path: "", - }, - { - yaml: "{ name: bar, path: foo }", - name: "bar", - path: "foo", - }, - } - for _, test := range tests { - in := []byte(test.yaml) - out := new(FromSecret) - err := yaml.Unmarshal(in, out) - if err != nil { - t.Error(err) - return - } - if got, want := out.Name, test.name; got != want { - t.Errorf("Want from_secret name %q, got %q", want, got) - } - if got, want := out.Path, test.path; got != want { - t.Errorf("Want from_secret path %q, got %q", want, got) - } - } -} diff --git a/yaml/pipeline.go b/yaml/pipeline.go index 228b498..4c6fed3 100644 --- a/yaml/pipeline.go +++ b/yaml/pipeline.go @@ -1,11 +1,11 @@ // Copyright 2019 Drone IO, Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -27,6 +27,7 @@ type Pipeline struct { DependsOn []string `json:"depends_on,omitempty" yaml:"depends_on" ` Node map[string]string `json:"node,omitempty" yaml:"node"` Platform Platform `json:"platform,omitempty"` + PullSecrets []string `json:"image_pull_secrets,omitempty" yaml:"image_pull_secrets"` Services []*Container `json:"services,omitempty"` Steps []*Container `json:"steps,omitempty"` Trigger Conditions `json:"trigger,omitempty"` diff --git a/yaml/pretty/pipeline.go b/yaml/pretty/pipeline.go index 2787476..1bb7d4e 100644 --- a/yaml/pretty/pipeline.go +++ b/yaml/pretty/pipeline.go @@ -67,6 +67,11 @@ func printPipeline(w writer, v *yaml.Pipeline) { w.WriteByte('\n') } + if len(v.PullSecrets) > 0 { + w.WriteTagValue("image_pull_secrets", v.PullSecrets) + w.WriteByte('\n') + } + if len(v.Node) > 0 { printNode(w, v.Node) w.WriteByte('\n') diff --git a/yaml/pretty/pipeline_test.go b/yaml/pretty/pipeline_test.go index a38c8cd..f60b3ad 100644 --- a/yaml/pretty/pipeline_test.go +++ b/yaml/pretty/pipeline_test.go @@ -151,3 +151,12 @@ func TestPipeline_Workspace(t *testing.T) { t.Errorf("Unepxected formatting") } } + +func TestPipeline_ImagePullSecrets(t *testing.T) { + ok, err := diff("testdata/pipeline_image_pull_secrets.yml") + if err != nil { + t.Error(err) + } else if !ok { + t.Errorf("Unepxected formatting") + } +} diff --git a/yaml/pretty/testdata/pipeline_image_pull_secrets.yml b/yaml/pretty/testdata/pipeline_image_pull_secrets.yml new file mode 100644 index 0000000..0a8e9a6 --- /dev/null +++ b/yaml/pretty/testdata/pipeline_image_pull_secrets.yml @@ -0,0 +1,11 @@ +kind: pipeline +name: default + +steps: + - name: build + image: golang + commands: + - go build + +image_pull_secrets: +- dockerhub diff --git a/yaml/pretty/testdata/pipeline_image_pull_secrets.yml.golden b/yaml/pretty/testdata/pipeline_image_pull_secrets.yml.golden new file mode 100644 index 0000000..3d891da --- /dev/null +++ b/yaml/pretty/testdata/pipeline_image_pull_secrets.yml.golden @@ -0,0 +1,18 @@ +--- +kind: pipeline +name: default + +platform: + os: linux + arch: amd64 + +steps: +- name: build + image: golang + commands: + - go build + +image_pull_secrets: +- dockerhub + +...