Skip to content
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

feat(crd): add reference to Grafana secret in Cryostat CR #432

Merged
merged 6 commits into from
Jul 27, 2022

Conversation

maxcao13
Copy link
Member

Fixes #391

Thoughts?

Now, to get the Grafana secrets, users can run

$ kubectl get cryostat -o jsonpath='{$.items[0].status.grafanaSecret.data.GF_SECURITY_ADMIN_USER}' | base64 -d
\\ admin
$ kubectl get cryostat -o jsonpath='{$.items[0].status.grafanaSecret.data.GF_SECURITY_ADMIN_PASSWORD}' | base64 -d
'\\ someRandomGeneratedPassword
  1. I'm not sure the best way to test the password since it's generated randomly so I just tested if it was a string and non-empty. There must be better ways right?
  2. Also, would it be better to also have the secrets be revealed in ASCII form when doing
    kubectl get cryostat ?

Note: Thanks to @tthvo a lot for helping me understand a lot of the new concepts and helping me with this PR!

@maxcao13 maxcao13 added the feat New feature or request label Jul 22, 2022
@maxcao13 maxcao13 requested a review from ebaron July 22, 2022 23:12
@tthvo
Copy link
Member

tthvo commented Jul 25, 2022

I'm not sure the best way to test the password since it's generated randomly so I just tested if it was a string and non-empty. There must be better ways right?

Wondering if we could get the current grafana-basic secret to check since that is what the status is set to be?

@ebaron
Copy link
Member

ebaron commented Jul 25, 2022

Hi @maxcao13, thanks for working on this! Sorry I wasn't clear in the issue, but I intended to just store the secret name in the Status and not the entire secret. Secrets have some security advantages over other Kubernetes objects [1], so storying the contents of the secret in our custom resource would lose those advantages. Your changes would look somewhat similar though.

[1] https://kubernetes.io/docs/concepts/configuration/secret/#information-security-for-secrets

I'm not sure the best way to test the password since it's generated randomly so I just tested if it was a string and non-empty. There must be better ways right?

You won't need to worry about this with just the name being stored in the CR now.

Copy link
Member

@ebaron ebaron left a comment

Choose a reason for hiding this comment

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

Hey @maxcao13, nice work! I just have a few minor suggestions below.

@@ -118,7 +118,7 @@ type CryostatStatus struct {
ApplicationURL string `json:"applicationUrl"`
// Grafana secret
// +operator-sdk:csv:customresourcedefinitions:type=status
GrafanaSecret *corev1.Secret `json:"grafanaSecret"`
GrafanaSecret string `json:"grafanaSecret"`
Copy link
Member

Choose a reason for hiding this comment

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

Could you add the // +optional marker for this field, and also append omitempty to the JSON tag? Optional tells the Kubernetes API server that this field is not required, such as before the secret is created. omitempty is similar, but functions on the JSON level. If the field is missing, it won't be transmitted in the JSON body.

There's one more marker I'd like to add here. If you look at other fields, you'll see an xDescriptors marker. This controls some generated UI in the OpenShift Console. We can tell the Console that this field refers to the name of a Kubernetes Secret. When it gets rendered in the OpenShift Console, it should create a link to the named Secret that users can click on. The example here conveniently shows how this works for Secrets, but also works for other kinds of Kubernetes objects: https://github.com/openshift/console/blob/master/frontend/packages/operator-lifecycle-manager/src/components/descriptors/reference/reference.md#3-k8sResourcePrefix

@@ -116,6 +116,9 @@ type CryostatStatus struct {
// Address of the deployed Cryostat web application
// +operator-sdk:csv:customresourcedefinitions:type=status,xDescriptors={"urn:alm:descriptor:org.w3:link"}
ApplicationURL string `json:"applicationUrl"`
// Grafana secret
Copy link
Member

Choose a reason for hiding this comment

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

This comment turns into the field's description, which is shown to the user. We might want to elaborate a bit on what this contains. Maybe something like: "Secret containing the generated Grafana credentials"

@@ -316,6 +316,12 @@ func (r *CryostatReconciler) Reconcile(ctx context.Context, request ctrl.Request
}
}

instance.Status.GrafanaSecret = instance.Name + "-grafana-basic"
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps for this, we could use the value of grafanaSecret.Name? It'll be less error-prone if we decide to change the secret's name in the future.

@@ -316,6 +316,12 @@ func (r *CryostatReconciler) Reconcile(ctx context.Context, request ctrl.Request
}
}

instance.Status.GrafanaSecret = instance.Name + "-grafana-basic"
err = r.Client.Status().Update(context.Background(), instance)
Copy link
Member

Choose a reason for hiding this comment

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

This should use ctx instead of context.Background(). We used context.Background() a lot before the Reconcile method provided a context to use, but I've been trying to replace this whenever making other changes to the affected code.

Copy link
Member

@ebaron ebaron left a comment

Choose a reason for hiding this comment

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

The code changes look good, but I'm having trouble pushing to Quay.io at the moment so I can't test it. Hopefully I'll have better luck tomorrow

Copy link
Member

@ebaron ebaron left a comment

Choose a reason for hiding this comment

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

Hi Max, got Quay working. Your PR works as expected. Thanks for taking this on!

If you're curious, this is how the Grafana Secret looks in the OpenShift Console:

  1. The Cryostat CR page has a clickable link to the Grafana Secret.
    Screenshot 2022-07-27 at 14-49-46 Red Hat OpenShift Container Platform

  2. Clicking on the Secret takes you to the Secret in the console, and you can scroll down to reveal the Grafana credentials.
    Screenshot 2022-07-27 at 14-50-17 cryostat-sample-grafana-basic · Details · Red Hat OpenShift Container Platform
    Screenshot 2022-07-27 at 14-55-49 cryostat-sample-grafana-basic · Details · Red Hat OpenShift Container Platform

@ebaron ebaron merged commit 4e6f0c7 into cryostatio:main Jul 27, 2022
@maxcao13 maxcao13 deleted the grafana-secret branch July 27, 2022 19:04
@maxcao13
Copy link
Member Author

Thanks for helping Elliott!

For future reference, how are you able to install my build onto the OperatorHub as an "Installed Operator"? I have been testing using crc and have only been able to make deploy so I can only see the operator as part of the Deployments and Pods.

@andrewazores
Copy link
Member

andrewazores commented Jul 27, 2022

For future reference, how are you able to install my build onto the OperatorHub as an "Installed Operator"? I have been testing using crc and have only been able to make deploy so I can only see the operator as part of the Deployments and Pods.

make bundle, make bundle-build, and make deploy_bundle, essentially.

@ebaron
Copy link
Member

ebaron commented Jul 27, 2022

Right, you'll need to make sure that the bundle points to your custom operator image. Something like this:

export OPERATOR_IMG=quay.io/me/cryostat-operator:latest
export BUNDLE_IMG=quay.io/me/cryostat-operator-bundle:latest
make oci-build
podman push $OPERATOR_IMG
make bundle 
make bundle-build
podman push $BUNDLE_IMG
make deploy_bundle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Grafana Secret to CRD Status
4 participants