Skip to content

Commit

Permalink
improvements from comments
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crenshaw <[email protected]>
  • Loading branch information
crenshaw-dev committed Jun 28, 2024
1 parent 849d3b8 commit ed672ad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pkg/cache/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ func (c *clusterCache) watchEvents(ctx context.Context, api kube.APIResourceInfo
err = runSynced(&c.lock, func() error {
openAPISchema, gvkParser, err := c.kubectl.LoadOpenAPISchema(c.config)
if err != nil {
return err
return fmt.Errorf("failed to load open api schema while handling CRD change: %w", err)
}
if gvkParser != nil {
c.gvkParser = gvkParser
Expand Down Expand Up @@ -817,7 +817,7 @@ func (c *clusterCache) sync() error {

openAPISchema, gvkParser, err := c.kubectl.LoadOpenAPISchema(config)
if err != nil {
return err
return fmt.Errorf("failed to load open api schema while syncing cluster cache: %w", err)
}

if gvkParser != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/kube/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (k *KubectlCmd) LoadOpenAPISchema(config *rest.Config) (openapi.Resources,
}
gvkParser, err := k.newGVKParser(oapiGetter)
if err != nil {
return oapiResources, nil, err
return oapiResources, nil, fmt.Errorf("error getting gvk parser: %s", err)
}
return oapiResources, gvkParser, nil
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/utils/kube/uniqueprotomodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func newUniqueModels(models proto.Models) (proto.Models, []schema.GroupVersionKi
panic(fmt.Sprintf("ListModels returns a model that can't be looked-up for: %v", modelName))
}
gvkList := parseGroupVersionKind(model)
gvk, wouldError := wouldTriggerDuplicateError(model, gvks)
if !wouldError {
gvk, wasProcessed := modelGvkWasAlreadyProcessed(model, gvks)
if !wasProcessed {
um.models[modelName] = model

// Add GVKs to the map, so we can detect a duplicate GVK later.
Expand All @@ -114,9 +114,15 @@ func newUniqueModels(models proto.Models) (proto.Models, []schema.GroupVersionKi
return um, taintedGVKs
}

func wouldTriggerDuplicateError(model proto.Schema, gvks map[schema.GroupVersionKind]string) (schema.GroupVersionKind, bool) {
// modelGvkWasAlreadyProcessed inspects a model to determine if it would trigger a duplicate GVK error. The gvks map
// holds the GVKs of all the models that have already been processed. If the model would trigger a duplicate GVK error,
// the function returns the GVK that would trigger the error and true. Otherwise, it returns an empty GVK and false.
func modelGvkWasAlreadyProcessed(model proto.Schema, gvks map[schema.GroupVersionKind]string) (schema.GroupVersionKind, bool) {
gvkList := parseGroupVersionKind(model)
// Not every model has a GVK extension specified. For those models, this loop will be skipped.
for _, gvk := range gvkList {
// The kind length check is copied from managedfields.NewGVKParser. It's unclear what edge case it's handling,
// but the behavior of this function should match NewGVKParser.
if len(gvk.Kind) > 0 {
_, ok := gvks[gvk]
if ok {
Expand All @@ -134,7 +140,7 @@ func wouldTriggerDuplicateError(model proto.Schema, gvks map[schema.GroupVersion
// Copied verbatim from: https://github.com/kubernetes/apimachinery/blob/eb26334eeb0f769be8f0c5665ff34713cfdec83e/pkg/util/managedfields/gvkparser.go#L29-L32
const groupVersionKindExtensionKey = "x-kubernetes-group-version-kind"

// Get and parse GroupVersionKind from the extension. Returns empty if it doesn't have one.
// parseGroupVersionKind gets and parses GroupVersionKind from the extension. Returns empty if it doesn't have one.
// Copied verbatim from: https://github.com/kubernetes/apimachinery/blob/eb26334eeb0f769be8f0c5665ff34713cfdec83e/pkg/util/managedfields/gvkparser.go#L82-L128
func parseGroupVersionKind(s proto.Schema) []schema.GroupVersionKind {
extensions := s.GetExtensions()
Expand Down

0 comments on commit ed672ad

Please sign in to comment.