Skip to content

Commit

Permalink
BUILD-87: Secret Volume Mounts in Builds
Browse files Browse the repository at this point in the history
Adding `oc describe` output for build volumes
Adding tests for `oc describe` output for build volumes
Adding `_tmp` to .gitignore
  • Loading branch information
coreydaley committed Jul 14, 2021
1 parent 25c2060 commit 93d3c39
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@
/genman
/_output
.idea/
_tmp/
29 changes: 29 additions & 0 deletions pkg/helpers/describe/describer.go
Expand Up @@ -433,6 +433,7 @@ func describeSourceStrategy(s *buildv1.SourceBuildStrategy, out *tabwriter.Write
if s.ForcePull {
formatString(out, "Force Pull", "yes")
}
describeBuildVolumes(out, s.Volumes)
}

func describeDockerStrategy(s *buildv1.DockerBuildStrategy, out *tabwriter.Writer) {
Expand All @@ -451,6 +452,7 @@ func describeDockerStrategy(s *buildv1.DockerBuildStrategy, out *tabwriter.Write
if s.ForcePull {
formatString(out, "Force Pull", "true")
}
describeBuildVolumes(out, s.Volumes)
}

func describeCustomStrategy(s *buildv1.CustomBuildStrategy, out *tabwriter.Writer) {
Expand Down Expand Up @@ -544,6 +546,33 @@ func describeBuildTriggers(triggers []buildv1.BuildTriggerPolicy, name, namespac
}
}

// describeBuildVolumes returns the description of a slice of build volumes
func describeBuildVolumes(w *tabwriter.Writer, volumes []buildv1.BuildVolume) {
if len(volumes) == 0 {
formatString(w, "Volumes", "<none>")
return
}
formatString(w, "Volumes", " ")
fmt.Fprint(w, "\tName\tSource Type\tSource\tMounts\n")
for _, v := range volumes {
var sourceName string
switch v.Source.Type {
case buildv1.BuildVolumeSourceTypeSecret:
sourceName = v.Source.Secret.SecretName
case buildv1.BuildVolumeSourceTypeConfigMap:
sourceName = v.Source.ConfigMap.Name
default:
sourceName = fmt.Sprintf("InvalidSourceType: %q", v.Source.Type)
}

var mounts []string
for _, m := range v.Mounts {
mounts = append(mounts, m.DestinationPath)
}
fmt.Fprintf(w, "\t%s\t%s\t%s\t%s\n", v.Name, v.Source.Type, sourceName, strings.Join(mounts, "\n\t\t\t\t"))
}
}

// Describe returns the description of a buildConfig
func (d *BuildConfigDescriber) Describe(namespace, name string, settings describe.DescriberSettings) (string, error) {
c := d.buildClient.BuildConfigs(namespace)
Expand Down
124 changes: 115 additions & 9 deletions pkg/helpers/describe/describer_test.go
Expand Up @@ -3,21 +3,14 @@ package describe
import (
"bytes"
"fmt"
v1 "github.com/openshift/api/quota/v1"
"k8s.io/apimachinery/pkg/api/resource"
"reflect"
"regexp"
"strings"
"testing"
"text/tabwriter"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake"
kubernetesscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
v1 "github.com/openshift/api/quota/v1"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/openshift/api"
appsv1 "github.com/openshift/api/apps/v1"
Expand All @@ -30,6 +23,13 @@ import (
projectv1 "github.com/openshift/api/project/v1"
securityv1 "github.com/openshift/api/security/v1"
templatev1 "github.com/openshift/api/template/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake"
kubernetesscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)

type describeClient struct {
Expand Down Expand Up @@ -618,3 +618,109 @@ func TestDescribeClusterQuota(t *testing.T) {
})
}
}

func Test_describeBuildVolumes(t *testing.T) {
var InvalidSourceType buildv1.BuildVolumeSourceType = "InvalidType"
tests := []struct {
name string
volumes []buildv1.BuildVolume
want []string
}{
{
name: "invalid source type",
volumes: []buildv1.BuildVolume{
{
Name: "my-secret-volume",
Source: buildv1.BuildVolumeSource{
Type: InvalidSourceType,
Secret: &corev1.SecretVolumeSource{
SecretName: "my-secret",
},
},
Mounts: []buildv1.BuildVolumeMount{
{
DestinationPath: "/my/secret/destination/path",
},
{
DestinationPath: "/my/secret/destination/path/two",
},
},
},
},
want: []string{
"InvalidSourceType: \"InvalidType\"",
},
},
{
name: "secret build volume",
volumes: []buildv1.BuildVolume{
{
Name: "my-secret-volume",
Source: buildv1.BuildVolumeSource{
Type: buildv1.BuildVolumeSourceTypeSecret,
Secret: &corev1.SecretVolumeSource{
SecretName: "my-secret",
},
},
Mounts: []buildv1.BuildVolumeMount{
{
DestinationPath: "/my/secret/destination/path",
},
{
DestinationPath: "/my/secret/destination/path/two",
},
},
},
},
want: []string{
"my-secret-volume",
"my-secret",
"/my/secret/destination/path",
"/my/secret/destination/path/two",
},
},
{
name: "config map build volume",
volumes: []buildv1.BuildVolume{
{
Name: "my-configmap-volume",
Source: buildv1.BuildVolumeSource{
Type: buildv1.BuildVolumeSourceTypeConfigMap,
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: "my-configmap",
},
},
},
Mounts: []buildv1.BuildVolumeMount{
{
DestinationPath: "/my/configmap/destination/path",
},
{
DestinationPath: "/my/configmap/destination/path/two",
},
},
},
},
want: []string{
"my-configmap-volume",
"my-configmap",
"/my/configmap/destination/path",
"/my/configmap/destination/path/two",
},
},
}
for _, tt := range tests {
var b bytes.Buffer
out := tabwriter.NewWriter(&b, 0, 8, 0, '\t', 0)
t.Run(tt.name, func(t *testing.T) {
describeBuildVolumes(out, tt.volumes)
out.Flush()
for _, match := range tt.want {
if got := b.String(); !regexp.MustCompile(match).MatchString(got) {
t.Errorf("%s\nshould contain %q", got, match)
}
}
})
}
}

0 comments on commit 93d3c39

Please sign in to comment.