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

fix: Allow serving CRs and CRDs and other improvements #125

Merged
merged 7 commits into from
Oct 12, 2023

Conversation

banjoh
Copy link
Member

@banjoh banjoh commented Oct 4, 2023

Here are the list of changes made to achieve being able to read custom resources. I also added some improvements in the process

  • Use unstructured objects whenever possible especially when concrete types are unknown.
  • Add middleware to dump requests and responses. These dumps are logged whenever you use --debug CLI option. They are handy when developing.
  • Add getVersion API to allow running kubectl cluster-info. Some tools like k9s use this to get server info
  • Always set selfsubjectaccessreviews to status.allowed=true so as to authorise all requests. k9s for example needs this
  • For all listing API calls, respond with empty lists not 404s. This makes kubectl behave as expected whenever there are no resources found
[evans] $ kubectl get pods -n not-there
No resources found in not-there namespace.
  • Better handling of Table responses by respecting groups Accept header values

Fixes: #48
Partially addresses #75

@banjoh banjoh changed the title Fix serving CRs and CRDs Allow serving CRs and CRDs Oct 4, 2023
@banjoh banjoh marked this pull request as draft October 6, 2023 11:05
* Use `unstructured` objects whenever possible especially when concrete types are unknown
* Add middleware to dump requests and responses
* Add getVersion API to allow running `kubectl cluster-info`. Some tools like `k9s` use this to get server info
* Always allow selfsubjectaccessreviews to authorize all requests. `k9s` for example needs this
* For all listing API calls, respond with empty lists not 404s
* Better handling of Table responses by respecting groups Accept header value
@banjoh banjoh changed the title Allow serving CRs and CRDs fix: Allow serving CRs and CRDs and other improvements Oct 12, 2023
@banjoh banjoh marked this pull request as ready for review October 12, 2023 14:27
@banjoh
Copy link
Member Author

banjoh commented Oct 12, 2023

As a side effect of these changes, I got k9s working. I have not tested it much but some basics work already.

Screenshot 2023-10-12 at 15 32 10

@banjoh banjoh mentioned this pull request Oct 12, 2023
}

func (w *requestResponseDumper) Flush() {
w.ResponseWriter.(http.Flusher).Flush()
Copy link
Member

@divolgin divolgin Oct 12, 2023

Choose a reason for hiding this comment

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

Nit for error checking

if flussher, ok := w.ResponseWriter.(http.Flusher); ok {
    flusher.Flush()
}

return w.ResponseWriter.(http.Hijacker).Hijack()
}

func logObject(prefix string, o any) {
Copy link
Member

Choose a reason for hiding this comment

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

Why use any instead of interface{}? This breaks codebase consistency.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've found any a clearer type to use but I'll revert in favour of consistency

type handler struct {
clusterData sbctl.ClusterData
}
type (
Copy link
Member

Choose a reason for hiding this comment

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

consistency

type <name> struct

@@ -161,12 +173,36 @@ func (h handler) getAPI(w http.ResponseWriter, r *http.Request) {
JSON(w, http.StatusOK, apiVersions)
}

func (h handler) getVersion(w http.ResponseWriter, r *http.Request) {
log.Println("called getVersion")
data, err := os.ReadFile(h.clusterData.ClusterInfoFile)
Copy link
Member

Choose a reason for hiding this comment

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

This function is tricky. While server version is returned from support bundle, this tool may not be implementing that particular version. But if this helps in some cases, it's ok. I don't really have any good suggestions.

})

result = &obj
break
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 do nothing and continue instead.

Kind: resource,
})

result = &obj
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 happen after the loop if result is not set to anything.

})

result = &obj
break
Copy link
Member

Choose a reason for hiding this comment

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

Do nothing and continue. Set result to empty object if no data was found in the entire loop.

pkg/api/utils.go Outdated
// parseAcceptHeader parses the a header and returns a map of key value pairs
// An kubectl Accept header for example looks like this:
// application/json;as=Table;g=meta.k8s.io;v=v1beta1, application/json
func parseAcceptHeader(headers []string) map[string]string {
Copy link
Member

Choose a reason for hiding this comment

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

This function only works with the inputs in the test case. It will break if there are white spaces in the header for example. Is there an existing package that can be used instead? Http parameter parsing is not a new thing. It's gotta be implemented somewhere already.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll search around for any existing implementation

// Try to decode object into an unstructured object
var v unstructured.Unstructured
err = json.Unmarshal(originalData, &v)
if err == nil {
Copy link
Member

Choose a reason for hiding this comment

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

value of err changes a few times in this block. how about logging it every time

if err != nil {
   // log and continue
}

@banjoh banjoh merged commit 571efef into replicatedhq:main Oct 12, 2023
4 checks passed
@banjoh banjoh deleted the em/fix-crs-and-crds branch October 12, 2023 19:39
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.

Add support for CRDs and CRs
2 participants