This repository was archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: check for resources #9
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e37c5f
chore: rename RBACRequirements to ResourceRequirements, add Version f…
johnstcn a656d5d
chore: refactor ResourceRequirements for ease of lookup
johnstcn 73c9394
internal/checks/kube: add resource unit test
johnstcn abbc290
internal/checks/kube: fix type in resources test
johnstcn dc3d8fd
feat: check required api resources
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
feat: check required api resources
- Loading branch information
commit dc3d8fdfb08ad01bb739feb28a905f757dfc3b5d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,60 @@ package kube | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"golang.org/x/xerrors" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
"github.com/cdr/coder-doctor/internal/api" | ||
) | ||
|
||
func (k *KubernetesChecker) CheckResources(ctx context.Context) []*api.CheckResult { | ||
func (k *KubernetesChecker) CheckResources(_ context.Context) []*api.CheckResult { | ||
const checkName = "kubernetes-resources" | ||
results := make([]*api.CheckResult, 0) | ||
dc := k.client.Discovery() | ||
lists, err := dc.ServerPreferredResources() | ||
if err != nil { | ||
results = append(results, api.ErrorResult(checkName, "unable to fetch api resources from server", err)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be SKIP rather than FAIL? after all, it's an indeterminate result - we don't know that the API resources are missing, and we don't know that they're available, either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not being able to check for API resources implies a lack of permissions or some other issue, which I'd argue is a FAIL. Edit: nah you're right it's a skip. I've to add more resources, so will do this in another PR. |
||
return results | ||
} | ||
|
||
resourcesAvailable := make(map[ResourceRequirement]bool) | ||
for _, list := range lists { | ||
if len(list.APIResources) == 0 { | ||
continue | ||
} | ||
|
||
gv, err := schema.ParseGroupVersion(list.GroupVersion) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
for _, resource := range list.APIResources { | ||
if len(resource.Verbs) == 0 { | ||
continue | ||
} | ||
|
||
r := ResourceRequirement{ | ||
Group: gv.Group, | ||
Version: gv.String(), | ||
Resource: resource.Name, | ||
} | ||
resourcesAvailable[r] = true | ||
} | ||
} | ||
|
||
versionReqs := findClosestVersionRequirements(k.coderVersion) | ||
for versionReq := range versionReqs { | ||
if !resourcesAvailable[*versionReq] { | ||
msg := fmt.Sprintf("missing required resource:%q group:%q version:%q", versionReq.Resource, versionReq.Group, versionReq.Version) | ||
errResult := api.ErrorResult(checkName, msg, xerrors.New(msg)) | ||
results = append(results, errResult) | ||
continue | ||
} | ||
msg := fmt.Sprintf("found required resource:%q group:%q version:%q", versionReq.Resource, versionReq.Group, versionReq.Version) | ||
results = append(results, api.PassResult(checkName, msg)) | ||
} | ||
|
||
return results | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the Discovery APIs unfortunately don't accept a context, which means they can't be cancelled -- it's why the version checks re-implement the Discovery stuff
I filed a bug but 🤷♂️ I don't think anything can be done about it, really kubernetes/client-go#1001
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's unfortunate :( At least here, the worst case is the user gets impatient and hits
^C
.