Commit 13ec13af authored by Adam Reese's avatar Adam Reese Committed by GitHub

Merge pull request #1487 from adamreese/ref/kube-builder-update

ref(kube): add info match helper functions
parents eb3624a5 ab12c462
...@@ -180,11 +180,9 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) ...@@ -180,11 +180,9 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
if err != nil { if err != nil {
return err return err
} }
resourceName := info.Name
resourceKind := info.Mapping.GroupVersionKind.Kind
helper := resource.NewHelper(info.Client, info.Mapping) helper := resource.NewHelper(info.Client, info.Mapping)
if _, err := helper.Get(info.Namespace, resourceName, info.Export); err != nil { if _, err := helper.Get(info.Namespace, info.Name, info.Export); err != nil {
if !errors.IsNotFound(err) { if !errors.IsNotFound(err) {
return fmt.Errorf("Could not get information about the resource: err: %s", err) return fmt.Errorf("Could not get information about the resource: err: %s", err)
} }
...@@ -195,11 +193,11 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) ...@@ -195,11 +193,11 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
} }
kind := info.Mapping.GroupVersionKind.Kind kind := info.Mapping.GroupVersionKind.Kind
log.Printf("Created a new %s called %s\n", kind, resourceName) log.Printf("Created a new %s called %s\n", kind, info.Name)
return nil return nil
} }
currentObj, err := getCurrentObject(resourceName, resourceKind, currentInfos) currentObj, err := getCurrentObject(info, currentInfos)
if err != nil { if err != nil {
return err return err
} }
...@@ -208,7 +206,7 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) ...@@ -208,7 +206,7 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
if alreadyExistErr, ok := err.(ErrAlreadyExists); ok { if alreadyExistErr, ok := err.(ErrAlreadyExists); ok {
log.Printf(alreadyExistErr.errorMsg) log.Printf(alreadyExistErr.errorMsg)
} else { } else {
log.Printf("error updating the resource %s:\n\t %v", resourceName, err) log.Printf("error updating the resource %s:\n\t %v", info.Name, err)
updateErrors = append(updateErrors, err.Error()) updateErrors = append(updateErrors, err.Error())
} }
} }
...@@ -220,7 +218,6 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) ...@@ -220,7 +218,6 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
return err return err
} else if len(updateErrors) != 0 { } else if len(updateErrors) != 0 {
return fmt.Errorf(strings.Join(updateErrors, " && ")) return fmt.Errorf(strings.Join(updateErrors, " && "))
} }
deleteUnwantedResources(currentInfos, targetInfos) deleteUnwantedResources(currentInfos, targetInfos)
return nil return nil
...@@ -419,13 +416,7 @@ func (c *Client) ensureNamespace(namespace string) error { ...@@ -419,13 +416,7 @@ func (c *Client) ensureNamespace(namespace string) error {
func deleteUnwantedResources(currentInfos, targetInfos []*resource.Info) { func deleteUnwantedResources(currentInfos, targetInfos []*resource.Info) {
for _, cInfo := range currentInfos { for _, cInfo := range currentInfos {
found := false if _, ok := findMatchingInfo(cInfo, targetInfos); !ok {
for _, m := range targetInfos {
if m.Name == cInfo.Name {
found = true
}
}
if !found {
log.Printf("Deleting %s...", cInfo.Name) log.Printf("Deleting %s...", cInfo.Name)
if err := deleteResource(cInfo); err != nil { if err := deleteResource(cInfo); err != nil {
log.Printf("Failed to delete %s, err: %s", cInfo.Name, err) log.Printf("Failed to delete %s, err: %s", cInfo.Name, err)
...@@ -434,20 +425,26 @@ func deleteUnwantedResources(currentInfos, targetInfos []*resource.Info) { ...@@ -434,20 +425,26 @@ func deleteUnwantedResources(currentInfos, targetInfos []*resource.Info) {
} }
} }
func getCurrentObject(targetName, targetKind string, infos []*resource.Info) (runtime.Object, error) { func getCurrentObject(target *resource.Info, infos []*resource.Info) (runtime.Object, error) {
var curr *resource.Info if found, ok := findMatchingInfo(target, infos); ok {
for _, currInfo := range infos { encoder := api.Codecs.LegacyCodec(registered.EnabledVersions()...)
currKind := currInfo.Mapping.GroupVersionKind.Kind defaultVersion := unversioned.GroupVersion{}
if currInfo.Name == targetName && currKind == targetKind { return resource.AsVersionedObject([]*resource.Info{found}, false, defaultVersion, encoder)
curr = currInfo
}
} }
return nil, fmt.Errorf("No resource with the name %s found.", target.Name)
}
if curr == nil { // isMatchingInfo returns true if infos match on Name and Kind.
return nil, fmt.Errorf("No resource with the name %s found.", targetName) func isMatchingInfo(a, b *resource.Info) bool {
} return a.Name == b.Name && a.Mapping.GroupVersionKind.Kind == b.Mapping.GroupVersionKind.Kind
}
encoder := api.Codecs.LegacyCodec(registered.EnabledVersions()...) // findMatchingInfo returns the first object that matches target.
defaultVersion := unversioned.GroupVersion{} func findMatchingInfo(target *resource.Info, infos []*resource.Info) (*resource.Info, bool) {
return resource.AsVersionedObject([]*resource.Info{curr}, false, defaultVersion, encoder) for _, info := range infos {
if isMatchingInfo(target, info) {
return info, true
}
}
return nil, false
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment