Skip to content

Commit

Permalink
Use created namespace name during run --wait (#930)
Browse files Browse the repository at this point in the history
When using a manifest from a file and the `--wait` flag with `run`, a
segfault would happen because the value is retrieved from the
`config.Config` which is part of the `GenConfig` within the `RunConfig`.
When using a file, this config is nil, so an attempt to retrieve the
namespace value resulted in a segfault. Instead, we can capture the
namespace from the objects within the manifest file when they are being
created by the Kubernetes client.

Signed-off-by: Bridget McErlean <[email protected]>
  • Loading branch information
zubron authored and johnSchnake committed Oct 2, 2019
1 parent 15ab7f3 commit 7218d76
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ stress: sonobuoy

# Integration tests
int: DOCKER_FLAGS=-v $(KUBECONFIG):/root/.kube/kubeconfig --env KUBECONFIG=/root/.kube/kubeconfig --network host
int: TESTARGS= $(VERBOSE_FLAG) -timeout 3m
int: sonobuoy
$(DOCKER_BUILD) 'CGO_ENABLED=0 $(INT_TEST)'

Expand Down
11 changes: 10 additions & 1 deletion pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (c *SonobuoyClient) RunManifest(cfg *RunConfig, manifest []byte) error {
buf := bytes.NewBuffer(manifest)
d := yaml.NewYAMLOrJSONDecoder(buf, bufferSize)

var createdNamespace string
for {
ext := runtime.RawExtension{}
if err := d.Decode(&ext); err != nil {
Expand Down Expand Up @@ -84,6 +85,14 @@ func (c *SonobuoyClient) RunManifest(cfg *RunConfig, manifest []byte) error {
if err != nil {
return errors.Wrap(err, "could not get object namespace")
}

// The namespace value is required if Wait is enabled in order to check the status.
// It may not be available within the RunConfig but we can retrieve it from the
// namespace of objects within the manifest.
if createdNamespace == "" && namespace != "" {
createdNamespace = namespace
}

// err is used to determine output for user; but first extract resource
_, err = c.dynamicClient.CreateObject(obj)
resource, err2 := c.dynamicClient.ResourceVersion(obj)
Expand All @@ -101,7 +110,7 @@ func (c *SonobuoyClient) RunManifest(cfg *RunConfig, manifest []byte) error {
seenStatus := false
runCondition := func() (bool, error) {
// Get the Aggregator pod and check if its status is completed or terminated.
status, err := c.GetStatus(&StatusConfig{cfg.Config.Namespace})
status, err := c.GetStatus(&StatusConfig{Namespace: createdNamespace})
switch {
case err != nil && seenStatus:
return false, errors.Wrap(err, "failed to get status")
Expand Down
40 changes: 40 additions & 0 deletions test/integration/sonobuoy_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -71,6 +72,45 @@ func cleanup(t *testing.T, namespace string) {
}
}

func TestUseNamespaceFromManifest(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

ns := "sonobuoy-" + strings.ToLower(t.Name())
defer cleanup(t, ns)

genArgs := fmt.Sprintf("gen -p testImage/yaml/job-junit-passing-singlefile.yaml -n %v", ns)
err, genStdout, genStderr := runSonobuoyCommandWithContext(ctx, t, genArgs)

if err != nil {
t.Fatalf("Sonobuoy exited with an error: %q\n", err)
t.Log(genStderr.String())
}

// Write the contents of gen to a temp file
tmpfile, err := ioutil.TempFile("", "gen.*.yaml")
if err != nil {
t.Fatal(err)
}

defer os.Remove(tmpfile.Name()) // clean up

if _, err := tmpfile.Write(genStdout.Bytes()); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}

// Pass the gen output to sonobuoy run
runArgs := fmt.Sprintf("run --wait -f %v", tmpfile.Name())
err, _, runStderr := runSonobuoyCommandWithContext(ctx, t, runArgs)
if err != nil {
t.Errorf("Sonobuoy exited with an error: %q\n", err)
t.Log(runStderr.String())
}
}

// TestSimpleRun runs a simple plugin to check that it runs successfully
func TestSimpleRun(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
Expand Down

0 comments on commit 7218d76

Please sign in to comment.