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
func DiscoverParseApply(ctx context.Context, client kubernetes.Interface, dynamicClient dynamic.Interface, manifest string) error { | |
groupResources, err := restmapper.GetAPIGroupResources(client.Discovery()) | |
if err != nil { | |
return err | |
} | |
var resources []*Resource | |
docs := ParseDocuments(manifest) | |
for _, doc := range docs { | |
resource, err := ParseSearchConstruct(groupResources, dynamicClient, doc) |
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
func restMapping(resources []*restmapper.APIGroupResources, gvk *schema.GroupVersionKind) (*meta.RESTMapping, error) { | |
mapping, err := restmapper.NewDiscoveryRESTMapper(resources).RESTMapping(schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}, gvk.Version) | |
if err != nil { | |
return nil, err | |
} | |
return mapping, nil | |
} |
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
func decode(doc string) (*unstructured.Unstructured, *schema.GroupVersionKind, error) { | |
unstruct := &unstructured.Unstructured{} | |
_, gvk, err := scheme.Codecs.UniversalDeserializer().Decode([]byte(doc), nil, unstruct) | |
if err != nil { | |
return nil, nil, err | |
} | |
return unstruct, gvk, nil | |
} |
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
func search(unstruct *unstructured.Unstructured, fields ...string) (interface{}, bool, error) { | |
field, found, err := unstructured.NestedFieldCopy(unstruct.Object, fields...) | |
if err != nil { | |
return "", found, err | |
} | |
return field, found, nil | |
} |
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
func ParseSearchConstruct(resources []*restmapper.APIGroupResources, dynamicClient dynamic.Interface, doc string) (*Resource, error) { | |
unstruct, gvk, err := decode(doc) | |
if err != nil { | |
return nil, err | |
} | |
ns, nsFound, err := search(unstruct, "metadata", "namespace") | |
if err != nil { | |
return nil, err | |
} |
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
var separator = regexp.MustCompile(`(?m)^---$`) | |
var commentStart = regexp.MustCompile(`(?m)^#.*$`) | |
// ParseDocuments parses a collection of YAML documents out of a single string. | |
func ParseDocuments(data string) []string { | |
docs := make([]string, 0) | |
if data == "" { | |
return docs | |
} |
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
// Apply takes parsed resources and applies them into a cluster. | |
func Apply(ctx context.Context, resources ...*Resource) error { | |
for _, resource := range resources { | |
_, createErr := resource.Action.Create(ctx, resource.Unstruct, metav1.CreateOptions{}) | |
if createErr != nil { | |
if apiErrs.IsAlreadyExists(createErr) { | |
updateErr := Update(ctx, resource) | |
if updateErr != nil { | |
return updateErr | |
} |
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
type Resource struct { | |
Action dynamic.ResourceInterface | |
Unstruct *unstructured.Unstructured | |
Mapping *meta.RESTMapping | |
Ns string | |
Name string | |
} |
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
// Apply takes parsed resources and applies them into a cluster. | |
func Apply(ctx context.Context, resources ...*Resource) error { | |
return nil | |
} |
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
{ | |
"__inputs": [], | |
"__requires": [ | |
{ | |
"type": "panel", | |
"id": "gauge", | |
"name": "Gauge", | |
"version": "" | |
}, | |
{ |
NewerOlder