Skip to content

integration: use restricted RBAC credentials for some tests#1695

Merged
jazzdan merged 18 commits into
masterfrom
dmiller/ch2595/add-integration-test-for-cluster-w-namespace
May 31, 2019
Merged

integration: use restricted RBAC credentials for some tests#1695
jazzdan merged 18 commits into
masterfrom
dmiller/ch2595/add-integration-test-for-cluster-w-namespace

Conversation

@jazzdan

@jazzdan jazzdan commented May 29, 2019

Copy link
Copy Markdown
Contributor

No description provided.

@jazzdan
jazzdan marked this pull request as ready for review May 29, 2019 15:50
@jazzdan
jazzdan requested review from maiamcc and nicks May 29, 2019 15:51
@jazzdan

jazzdan commented May 29, 2019

Copy link
Copy Markdown
Contributor Author

For an explanation of what I'm scripting here see this article.

@maiamcc maiamcc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I uh... THINK this lgtm? (def try to get some of the tests to go red to make sure you're testing what you think you're testing.)

this is all opaque and weird enough that i'd recommend a lotttt of comments in the setup code about what's going on and why we have to do it this way, otherwise this'll be hell on the next person who has to change it

Comment thread integration/k8s_fixture_test.go Outdated

func (f *k8sFixture) getSecrets() {
outWriter := bytes.NewBuffer(nil)
cmdStr := `kubectl get secrets -n tilt-integration -o json | jq -r '.items[] | select(.metadata.name | startswith("tilt-integration-user-token-")) | .data.token'`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff between these two blocks is not immediately apparent, consider comments for readability

Comment thread integration/k8s_fixture_test.go Outdated
f.t.Fatalf("Error getting secrets: %v. Cmd: %s", err, cmdStr)
}

cmdStr = `kubectl get secrets -n tilt-integration -o json | jq -r '.items[] | select(.metadata.name | startswith("tilt-integration-user-token-")) | .data["ca.crt"]'`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this kubectl command takes longer than a few hundred ms, might make sense to run the kubectl get secrets bit only once and run the output of that through jq -- i don't want to add multiple seconds of overhead to every integration test if we can help it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is only called if you enable restricted credentials in the test, so it's opt in.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah gotcha 👍

Comment thread integration/k8s_fixture_test.go Outdated
func (f *k8sFixture) SetRestrictedCredentials() {
f.getSecrets()

err := ioutil.WriteFile("/tmp/cert", []byte(f.cert), 0644)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth a note about why we have to write it to disk instead of passing it to the cmd

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...also it makes me a little sad that we're doing this unnecessarily for local integration tests, but 🤷‍♀

@nicks nicks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered making all the namespace setup into a separate reusable script, instead of baking it into the integration test fixture?

I expect we'll want to do this in other places. And other teams will want to use it for their tiltfile tests

@jazzdan

jazzdan commented May 29, 2019

Copy link
Copy Markdown
Contributor Author

@nicks I didn't, but I think that's a good idea!

Comment thread integration/k8s_fixture_test.go Outdated
f.t.Fatalf("Error writing cert: %v", err)
}
outWriter := bytes.NewBuffer(nil)
cmd := exec.CommandContext(f.ctx, "kubectl", "config", "set-credentials", "tilt-integration-user", "--client-certificate=/tmp/cert", fmt.Sprintf("--token=%s", f.token))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modifying the user's kubeconfig seems very dangerous and likely to break something (e.g., if the user ctrl-c's a test)

have you considered setting the KUBECONFIG env var to use a custom kubeconfig?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that would be a better approach and once I get this working at all I will do it that way instead.

Comment thread integration/k8s_fixture_test.go Outdated
func (f *k8sFixture) SetRestrictedCredentials() {
f.getSecrets()

err := ioutil.WriteFile("/tmp/cert", []byte(f.cert), 0644)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you aware of client-certificate-data so you can embed the data in the kubeconfig file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but you can't specify that via kubectl config set-credentials and I'm trying to avoid having to parse/write a kubeconfig.

I don't think I can avoid it though, tragically.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tried kubectl config set? its instructions explicitly mention certificate-authority-data

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, no! Thank you for the tip

Comment thread internal/k8s/watch.go Outdated
o.LabelSelector = ls.String()
}))

logger.Get(ctx).Infof("DERP namespace: %s", ns)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore this for now 😬

@jazzdan
jazzdan force-pushed the dmiller/ch2595/add-integration-test-for-cluster-w-namespace branch from 531d417 to 2bf4f1c Compare May 29, 2019 20:48
@jazzdan

jazzdan commented May 29, 2019

Copy link
Copy Markdown
Contributor Author

This is still very much a work in progress, but it should be much different than the last time you looked at it @nicks @maiamcc, but I have to head out for the day.

@maiamcc maiamcc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh this IS much better! 🙌

Comment thread integration/k8s_fixture_test.go Outdated
func (f *k8sFixture) CreateNamespaceIfNecessary() {
outWriter := bytes.NewBuffer(nil)
cmd := exec.CommandContext(f.ctx, "kubectl", "apply", "-f", "namespace.yaml")
if f.usingOverriddenConfig {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having to remember this every time you make a new Command object, consider an f.CommandContext(...) method that does this automatically (and maybe even passes the ctx for you). Otherwise, seems like a recipe of someone invoking a command wrong and breaking stuff in mysterious ways

Comment thread integration/k8s_fixture_test.go Outdated
f.t.Fatalf("Error getting secrets: %v. Cmd: %s", err, cmdStr)
}

cmdStr = `kubectl get secrets -n tilt-integration -o json | jq -r '.items[] | select(.metadata.name | startswith("tilt-integration-user-token-")) | .data["ca.crt"]'`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah gotcha 👍

@jazzdan
jazzdan force-pushed the dmiller/ch2595/add-integration-test-for-cluster-w-namespace branch 2 times, most recently from 233ff6f to b73e785 Compare May 30, 2019 15:16
@jazzdan
jazzdan merged commit 0952f54 into master May 31, 2019
@nicks
nicks deleted the dmiller/ch2595/add-integration-test-for-cluster-w-namespace branch February 3, 2022 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants