chore(kube): make update logic more generic

pull/1099/head
Michelle Noorali 9 years ago
parent 197e466b9a
commit c7bec34454

@ -129,13 +129,13 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) {
return buf.String(), err return buf.String(), err
} }
// Update reads in the current configuration and a modified configuration from io.reader // Update reads in the current configuration and a target configuration from io.reader
// and creates resources that don't already exists, updates resources that have been modified // and creates resources that don't already exists, updates resources that have been modified
// and deletes resources from the current configuration that are not present in the // in the target configuration and deletes resources from the current configuration that are
// modified configuration // not present in the target configuration
// //
// Namespace will set the namespaces // Namespace will set the namespaces
func (c *Client) Update(namespace string, currentReader, modifiedReader io.Reader) error { func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) error {
current := c.NewBuilder(includeThirdPartyAPIs). current := c.NewBuilder(includeThirdPartyAPIs).
ContinueOnError(). ContinueOnError().
NamespaceParam(namespace). NamespaceParam(namespace).
@ -144,11 +144,11 @@ func (c *Client) Update(namespace string, currentReader, modifiedReader io.Reade
Flatten(). Flatten().
Do() Do()
modified := c.NewBuilder(includeThirdPartyAPIs). target := c.NewBuilder(includeThirdPartyAPIs).
ContinueOnError(). ContinueOnError().
NamespaceParam(namespace). NamespaceParam(namespace).
DefaultNamespace(). DefaultNamespace().
Stream(modifiedReader, ""). Stream(targetReader, "").
Flatten(). Flatten().
Do() Do()
@ -157,11 +157,11 @@ func (c *Client) Update(namespace string, currentReader, modifiedReader io.Reade
return err return err
} }
modifiedInfos := []*resource.Info{} targetInfos := []*resource.Info{}
updateErrors := []string{} updateErrors := []string{}
err = modified.Visit(func(info *resource.Info, err error) error { err = target.Visit(func(info *resource.Info, err error) error {
modifiedInfos = append(modifiedInfos, info) targetInfos = append(targetInfos, info)
if err != nil { if err != nil {
return err return err
} }
@ -196,7 +196,7 @@ func (c *Client) Update(namespace string, currentReader, modifiedReader io.Reade
return nil return nil
}) })
deleteUnwantedResources(currentInfos, modifiedInfos) deleteUnwantedResources(currentInfos, targetInfos)
if err != nil { if err != nil {
return err return err
@ -292,7 +292,7 @@ func deleteResource(info *resource.Info) error {
return resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name) return resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name)
} }
func updateResource(modified *resource.Info, currentObj runtime.Object) error { func updateResource(target *resource.Info, currentObj runtime.Object) error {
encoder := api.Codecs.LegacyCodec(registered.EnabledVersions()...) encoder := api.Codecs.LegacyCodec(registered.EnabledVersions()...)
originalSerialization, err := runtime.Encode(encoder, currentObj) originalSerialization, err := runtime.Encode(encoder, currentObj)
@ -300,7 +300,7 @@ func updateResource(modified *resource.Info, currentObj runtime.Object) error {
return err return err
} }
editedSerialization, err := runtime.Encode(encoder, modified.Object) editedSerialization, err := runtime.Encode(encoder, target.Object)
if err != nil { if err != nil {
return err return err
} }
@ -316,7 +316,7 @@ func updateResource(modified *resource.Info, currentObj runtime.Object) error {
} }
if reflect.DeepEqual(originalJS, editedJS) { if reflect.DeepEqual(originalJS, editedJS) {
return fmt.Errorf("Looks like there are no changes for %s", modified.Name) return fmt.Errorf("Looks like there are no changes for %s", target.Name)
} }
patch, err := strategicpatch.CreateStrategicMergePatch(originalJS, editedJS, currentObj) patch, err := strategicpatch.CreateStrategicMergePatch(originalJS, editedJS, currentObj)
@ -325,8 +325,8 @@ func updateResource(modified *resource.Info, currentObj runtime.Object) error {
} }
// send patch to server // send patch to server
helper := resource.NewHelper(modified.Client, modified.Mapping) helper := resource.NewHelper(target.Client, target.Mapping)
if _, err = helper.Patch(modified.Namespace, modified.Name, api.StrategicMergePatchType, patch); err != nil { if _, err = helper.Patch(target.Namespace, target.Name, api.StrategicMergePatchType, patch); err != nil {
return err return err
} }
@ -411,10 +411,10 @@ func (c *Client) ensureNamespace(namespace string) error {
return nil return nil
} }
func deleteUnwantedResources(currentInfos, modifiedInfos []*resource.Info) { func deleteUnwantedResources(currentInfos, targetInfos []*resource.Info) {
for _, cInfo := range currentInfos { for _, cInfo := range currentInfos {
found := false found := false
for _, m := range modifiedInfos { for _, m := range targetInfos {
if m.Name == cInfo.Name { if m.Name == cInfo.Name {
found = true found = true
} }

Loading…
Cancel
Save