Skip to content

Instantly share code, notes, and snippets.

@displague
Created March 4, 2020 15:49
Show Gist options
  • Save displague/5fa749e0044b78552366f0ca60a205bf to your computer and use it in GitHub Desktop.
Save displague/5fa749e0044b78552366f0ca60a205bf to your computer and use it in GitHub Desktop.
// crdsFromStack fetches the CRDs of the Stack using the shared parent labels
// TODO(displague) change this to use GET on each, the CRDs may not be ready
func (h *stackHandler) crdsFromStack(ctx context.Context) ([]apiextensions.CustomResourceDefinition, error) {
results := []apiextensions.CustomResourceDefinition{}
// TODO(displague) list CRDs by chained field selector, use apigroup+kind,
// not labels.
// Fetch CRDs because h.ext.Spec.CRDs doesn't have plural names
//labelSelector := client.MatchingLabels(map[string]string{
// stacks.LabelKubernetesManagedBy: "stack-manager",
//})
// TODO(displague) fetching one crd at a time is not idea. The is no "or"
// operator for selectors. Consider fetching all CRDs with "managed-by"
// labels, trading API calls for API size.
for _, crdWant := range h.ext.Spec.CRDs {
// TODO(displague) - can't use this: "non-exact field matches are not
// supported by the cache"
/*
fieldSelector := client.MatchingFields{
"spec.names.kind": crdWant.Kind,
"spec.group": crdWant.GroupVersionKind().Group,
// "spec.group" is not a known field selector: only "metadata.name", "metadata.namespace"
}
*/
//setA := fields.Set{"spec.names.kind": crdWant.Kind}
setB := fields.Set{"spec.group": crdWant.GroupVersionKind().Group}
//fieldSelector := client.MatchingFields(setA)
//fieldSelector2 := client.MatchingFields(setB)
sel := fields.AndSelectors(fields.SelectorFromSet(setB))
// "non-exact field matches are not
// supported by the cache"
fieldSelector := client.MatchingFieldsSelector{Selector: sel}
crds := &apiextensions.CustomResourceDefinitionList{}
/*
crdsRaw := &unstructured.UnstructuredList{}
gvk := crds.GroupVersionKind()
gvk.Kind = "CustomResourceDefinition"
gvk.Group = apiextensions.SchemeGroupVersion.Group
gvk.Version = apiextensions.SchemeGroupVersion.Version
crdsRaw.SetGroupVersionKind(gvk)
*/
if err := h.kube.List(ctx, crds, fieldSelector); err != nil {
return nil, errors.Wrap(err, "CRDs could not be listed")
}
// expected count is 1 if we could filter by fields
if len(crds.Items) == 0 {
return nil, fmt.Errorf("CRDs not found filtering by spec.group %s", setB["spec.group"])
}
// crd := &apiextensions.CustomResourceDefinition{}
/*
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(crdsRaw.Items[0].UnstructuredContent(), &crd); err != nil {
return nil, errors.Wrap(err, "CRD could not be converted from unstructured")
}
*/
found := false
for i := range crds.Items {
if crds.Items[i].Spec.Names.Kind != crdWant.Kind {
continue
}
version := crdWant.GroupVersionKind().Version
if !crdVersionExists(&crds.Items[i], crdWant.GroupVersionKind().Version) {
return nil, fmt.Errorf("CRD not found, expecting %s with version %s", crdWant, version)
}
found = true
results = append(results, crds.Items[i])
}
if !found {
return nil, fmt.Errorf("CRD not found, expecting %s", crdWant)
}
}
if h.crdListsDiffer(results) {
return nil, errors.New("CRDs found do not match expected CRDs")
}
return results, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment