Skip to content

Commit

Permalink
add serviceaccount to service describe
Browse files Browse the repository at this point in the history
  • Loading branch information
Daisy Guo committed Nov 4, 2019
1 parent a269d29 commit 42ececf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
3 changes: 3 additions & 0 deletions pkg/kn/commands/service/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ func writeService(dw printers.PrefixWriter, service *v1alpha1.Service) {
url := service.Status.Address.GetURL()
dw.WriteAttribute("Address", url.String())
}
if (service.Spec.Template != nil) && (service.Spec.Template.Spec.ServiceAccountName != "") {
dw.WriteAttribute("ServiceAccount", service.Spec.Template.Spec.ServiceAccountName)
}
}

// Write out revisions associated with this service. By default only active
Expand Down
41 changes: 28 additions & 13 deletions pkg/kn/commands/service/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestServiceDescribeBasic(t *testing.T) {
// Recording:
r := client.Recorder()
// Prepare service
expectedService := createTestService("foo", []string{"rev1"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1"}, "default-sa", goodConditions())

// Get service & revision
r.GetService("foo", &expectedService, nil)
Expand All @@ -69,6 +69,7 @@ func TestServiceDescribeBasic(t *testing.T) {
assert.Assert(t, cmp.Regexp(`(?m)\s*Annotations:.*\.\.\.$`, output))
assert.Assert(t, util.ContainsAll(output, "Labels:", "label1=lval1, label2=lval2\n"))
assert.Assert(t, util.ContainsAll(output, "[1]"))
assert.Assert(t, cmp.Regexp("ServiceAccount: \\s+default-sa", output))

assert.Equal(t, strings.Count(output, "rev1"), 1)

Expand All @@ -80,7 +81,7 @@ func TestServiceDescribeSad(t *testing.T) {
client := knclient.NewMockKnClient(t)
r := client.Recorder()

expectedService := createTestService("foo", []string{"rev1"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1"}, "", goodConditions())
expectedService.Status.Conditions[0].Status = v1.ConditionFalse
r.GetService("foo", &expectedService, nil)
rev1 := createTestRevision("rev1", 1)
Expand All @@ -100,7 +101,7 @@ func TestServiceDescribeLatest(t *testing.T) {
client := knclient.NewMockKnClient(t)
r := client.Recorder()

expectedService := createTestService("foo", []string{"rev1"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1"}, "", goodConditions())
expectedService.Status.Traffic[0].LatestRevision = ptr.Bool(true)

// Get service & revision
Expand All @@ -125,7 +126,7 @@ func TestServiceDescribeLatestNotInTraffic(t *testing.T) {
// Recording:
r := client.Recorder()
// Prepare service
expectedService := createTestService("foo", []string{"rev1", "rev2"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1", "rev2"}, "", goodConditions())
expectedService.Status.Traffic = expectedService.Status.Traffic[:1]
expectedService.Status.Traffic[0].LatestRevision = ptr.Bool(false)
expectedService.Status.Traffic[0].Percent = ptr.Int64(int64(100))
Expand Down Expand Up @@ -156,7 +157,7 @@ func TestServiceDescribeEachNamedOnce(t *testing.T) {
// Recording:
r := client.Recorder()
// Prepare service
expectedService := createTestService("foo", []string{"rev1", "rev2"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1", "rev2"}, "", goodConditions())
expectedService.Status.Traffic = expectedService.Status.Traffic[:1]
expectedService.Status.Traffic[0].LatestRevision = ptr.Bool(false)
expectedService.Status.Traffic[0].Percent = ptr.Int64(int64(100))
Expand Down Expand Up @@ -188,7 +189,7 @@ func TestServiceDescribeLatestAndCurrentBothHaveTrafficEntries(t *testing.T) {
// Recording:
r := client.Recorder()
// Prepare service
expectedService := createTestService("foo", []string{"rev1", "rev1"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1", "rev1"}, "", goodConditions())
expectedService.Status.Traffic[0].LatestRevision = ptr.Bool(true)
expectedService.Status.Traffic[0].Tag = "latest"
expectedService.Status.Traffic[1].Tag = "current"
Expand Down Expand Up @@ -217,7 +218,7 @@ func TestServiceDescribeLatestCreatedIsBroken(t *testing.T) {
// Recording:
r := client.Recorder()
// Prepare service
expectedService := createTestService("foo", []string{"rev1"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1"}, "", goodConditions())
expectedService.Status.Traffic[0].LatestRevision = ptr.Bool(true)
expectedService.Status.LatestCreatedRevisionName = "rev2"

Expand Down Expand Up @@ -259,7 +260,7 @@ func TestServiceDescribeScaling(t *testing.T) {
r := client.Recorder()

// Prepare service
expectedService := createTestService("foo", []string{"rev1"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1"}, "", goodConditions())

// Get service & revision
r.GetService("foo", &expectedService, nil)
Expand Down Expand Up @@ -327,7 +328,7 @@ func TestServiceDescribeResources(t *testing.T) {
r := client.Recorder()

// Prepare service
expectedService := createTestService("foo", []string{"rev1"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1"}, "", goodConditions())

// Get service & revision
r.GetService("foo", &expectedService, nil)
Expand Down Expand Up @@ -370,7 +371,7 @@ func TestServiceDescribeUserImageVsImage(t *testing.T) {
r := client.Recorder()

// Prepare service
expectedService := createTestService("foo", []string{"rev1", "rev2", "rev3", "rev4"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1", "rev2", "rev3", "rev4"}, "", goodConditions())
r.GetService("foo", &expectedService, nil)

rev1 := createTestRevision("rev1", 1)
Expand Down Expand Up @@ -420,7 +421,7 @@ func TestServiceDescribeVerbose(t *testing.T) {
r := client.Recorder()

// Prepare service
expectedService := createTestService("foo", []string{"rev1", "rev2"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1", "rev2"}, "", goodConditions())
r.GetService("foo", &expectedService, nil)

rev1 := createTestRevision("rev1", 1)
Expand Down Expand Up @@ -475,7 +476,7 @@ func TestServiceDescribeMachineReadable(t *testing.T) {
r := client.Recorder()

// Prepare service
expectedService := createTestService("foo", []string{"rev1", "rev2"}, goodConditions())
expectedService := createTestService("foo", []string{"rev1", "rev2"}, "", goodConditions())
r.GetService("foo", &expectedService, nil)

output, err := executeServiceCommand(client, "describe", "foo", "-o", "yaml")
Expand All @@ -495,7 +496,7 @@ func validateServiceOutput(t *testing.T, service string, output string) {
assert.Assert(t, util.ContainsAll(output, "Ready", "RoutesReady", "OK", "TYPE", "AGE", "REASON"))
}

func createTestService(name string, revisionNames []string, conditions duckv1.Conditions) v1alpha1.Service {
func createTestService(name string, revisionNames []string, serviceaccount string, conditions duckv1.Conditions) v1alpha1.Service {

labelMap := make(map[string]string)
labelMap["label1"] = "lval1"
Expand Down Expand Up @@ -546,6 +547,20 @@ func createTestService(name string, revisionNames []string, conditions duckv1.Co
}
service.Status.Traffic = trafficTargets
}

if len(serviceaccount) > 0 {
template := v1alpha1.RevisionTemplateSpec{
Spec: v1alpha1.RevisionSpec{
RevisionSpec: servingv1.RevisionSpec{
PodSpec: v1.PodSpec{
ServiceAccountName: serviceaccount,
},
},
},
}
service.Spec.Template = &template
}

return service
}

Expand Down

0 comments on commit 42ececf

Please sign in to comment.