Skip to content

Feature/setup project existing sa #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

ehmm
Copy link
Contributor

@ehmm ehmm commented Aug 15, 2018

Allowing use of existing sa for deploy-project/driver and checking that sa has needed roles.

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please sign in with your organization's credentials at https://identity.linuxfoundation.org/projects/cncf to be authorized.
  • If you have done the above and are still having issues with the CLA being reported as unsigned, please email the CNCF helpdesk: [email protected]

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Aug 15, 2018
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 15, 2018
@msau42
Copy link
Contributor

msau42 commented Aug 15, 2018

/ok-to-test
/assign @davidz627

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 15, 2018
Copy link
Contributor

@davidz627 davidz627 left a comment

Choose a reason for hiding this comment

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

Thanks for your PR! Just a couple suggestions and comments. Also please sign the CLA!

@@ -15,6 +15,30 @@ set -o errexit

readonly PKGDIR="${GOPATH}/src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver"
readonly KUBEDEPLOY="${PKGDIR}/deploy/kubernetes"
#readonly BIND_ROLES="roles/compute.storageAdmin roles/iam.serviceAccountUser projects/${PROJECT}/roles/gcp_compute_persistent_disk_csi_driver_custom_role"

function check_service_account()
Copy link
Contributor

Choose a reason for hiding this comment

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

can you move the function definition to the bottom of the file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cleaned

@@ -15,6 +15,30 @@ set -o errexit

readonly PKGDIR="${GOPATH}/src/sigs.k8s.io/gcp-compute-persistent-disk-csi-driver"
readonly KUBEDEPLOY="${PKGDIR}/deploy/kubernetes"
#readonly BIND_ROLES="roles/compute.storageAdmin roles/iam.serviceAccountUser projects/${PROJECT}/roles/gcp_compute_persistent_disk_csi_driver_custom_role"
Copy link
Contributor

Choose a reason for hiding this comment

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

why did this get moved? can it be moved back?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to first extract the project sadly

function check_service_account()
{
# Using bash magic to parse JSON for IAM
IAM_NAME=$(grep -Po '"client_email": *\K"[^"]*"' ${GCE_PD_SA_DIR}/cloud-sa.json | tr -d '"')
Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively:
jq '.client_email' ${GCE_PD_SA_DIR}/cloud-sa.json | tr -d '"'
I'm not sure if every system has jq installed but mine certainly does and I would prefer using a JSON parser for JSON over bash magic :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two of my standard dev machines don't have jq by default, that's why I offered to convert to python (can also be useful for Windows K8S deployments).
I also don't have a problem converting to jq and adding a message if it is missing if you prefer

fi
done
if [ "$MISSING_ROLES" = true ]; then
echo Cannot deploy with missing roles in service account, stopping
Copy link
Contributor

Choose a reason for hiding this comment

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

print out suggestion to use setup-project.sh to remedy this issue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

gcloud iam roles create gcp_compute_persistent_disk_csi_driver_custom_role --quiet \
--project "${PROJECT}" \
--file "${PKGDIR}/deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml"
function create_new_sa()
Copy link
Contributor

Choose a reason for hiding this comment

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

move function definitions to bottom of file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Undid the extraction to a function as bash needs functions before usage, but it's not so bad inline


# Check if SA exists
CREATE_SA=true
SA_JSON=$(gcloud iam service-accounts list --filter="name:$IAM_NAME" --format="json")
Copy link
Contributor

Choose a reason for hiding this comment

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

could we just do a get instead of a list and filter since we're only looking for one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I personally prefer this as trying to describe (get) instead of list prints an error to stdout if it fails, but I can still change it if you prefer

# Delete Service Account
gcloud iam service-accounts delete "${IAM_NAME}" --project "${PROJECT}" --quiet || true

# Create new Service Account and Keys
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Create new Service Account

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 15, 2018
Copy link
Contributor

@davidz627 davidz627 left a comment

Choose a reason for hiding this comment

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

just a bunch of nits

fi
done
if [ "$MISSING_ROLES" = true ]; then
echo Cannot deploy with missing roles in service account, please run setup-project.sh to setup Service Account
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: quotes around strings printed by echo

# Using bash magic to parse JSON for IAM
IAM_NAME=$(grep -Po '"client_email": *\K"[^"]*"' ${GCE_PD_SA_DIR}/cloud-sa.json | tr -d '"')
PROJECT=$(grep -Po '.*@\K[^.]+'<<<$IAM_NAME)
GOTTEN_BIND_ROLES=$(gcloud projects get-iam-policy $PROJECT --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:$IAM_NAME")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: all of these variables are readonly. I don't really know bash style guidelines but I'd like it to be at least internally consistent.

function check_service_account()
{
# Using bash magic to parse JSON for IAM
IAM_NAME=$(grep -Po '"client_email": *\K"[^"]*"' ${GCE_PD_SA_DIR}/cloud-sa.json | tr -d '"')
Copy link
Contributor

Choose a reason for hiding this comment

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

could you document in a comment what this Regex is doing approximately

GOTTEN_BIND_ROLES=$(gcloud projects get-iam-policy $PROJECT --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:$IAM_NAME")
readonly BIND_ROLES="roles/compute.storageAdmin roles/iam.serviceAccountUser projects/${PROJECT}/roles/gcp_compute_persistent_disk_csi_driver_custom_role"
MISSING_ROLES=false
for role in $BIND_ROLES
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: all variables in "${var}" form for consistency. I think the quotes prevent/fix some sort of multi-part variable issues

CREATE_SA=false
echo "Service account $IAM_NAME exists. Would you like to create a new one (y) or reuse the existing one (n)"
read -p "(y/n)" -n 1 -r REPLY
echo # (optional) move to a new line
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: indentation

# Check if SA exists
CREATE_SA=true
SA_JSON=$(gcloud iam service-accounts list --filter="name:$IAM_NAME" --format="json")
if [ "[]" != "$SA_JSON" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: then on new-line for file consistency
or fix the other one to be on the same line

# Create new Service Account and Keys
gcloud iam service-accounts create "${GCE_PD_SA_NAME}" --project "${PROJECT}"
# Bind service account to roles

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove space

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 15, 2018
@davidz627
Copy link
Contributor

davidz627 commented Aug 15, 2018

/lgtm
/hold
Please sign CLA and Squash commits.
Thanks again for the PR! And you're right, these bash scripts are getting really elaborate. We might want to invest in converting all of this to Python at some point in the future (#89).

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Aug 15, 2018
Check that service account has needed roles
@ehmm ehmm force-pushed the feature/setupProjectExistingSa branch from ae4c3b0 to 33681f9 Compare August 15, 2018 23:23
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 15, 2018
@davidz627
Copy link
Contributor

/hold cancel
/lgtm

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 15, 2018
@davidz627
Copy link
Contributor

davidz627 commented Aug 16, 2018

/approve

1 similar comment
@davidz627
Copy link
Contributor

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: davidz627, ehmm

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 16, 2018
@k8s-ci-robot k8s-ci-robot merged commit bd240a4 into kubernetes-sigs:master Aug 16, 2018
@davidz627
Copy link
Contributor

Thanks @ehmm for jumping on this and submitting this PR! Feel free to reach out or open issues if you run into any other bugs or opportunities for improvement with the driver 👍

@ehmm
Copy link
Contributor Author

ehmm commented Aug 16, 2018

Sure thing @davidz627, thank you and @msau42 for the support, I will be playing around with the driver and if I see anything will definitely reach out. Also I added a link for a quick patch I'm using to support specifying a specific partition (#83), it is probably a bit rough and I'm not sure it's the direction you want to go (re the specification format) -- but for my projects it's currently good enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants