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

ref(kube): add newBuilder function to dry up builder calls
pull/1486/head
Adam Reese 8 years ago committed by GitHub
commit 534f0df907

@ -93,6 +93,15 @@ func (c *Client) Create(namespace string, reader io.Reader) error {
return perform(c, namespace, reader, createResource) return perform(c, namespace, reader, createResource)
} }
func (c *Client) newBuilder(namespace string, reader io.Reader) *resource.Builder {
return c.NewBuilder(c.IncludeThirdPartyAPIs).
ContinueOnError().
NamespaceParam(namespace).
DefaultNamespace().
Stream(reader, "").
Flatten()
}
// Get gets kubernetes resources as pretty printed string // Get gets kubernetes resources as pretty printed string
// //
// Namespace will set the namespace // Namespace will set the namespace
@ -153,32 +162,20 @@ func (c *Client) Get(namespace string, reader io.Reader) (string, error) {
// //
// Namespace will set the namespaces // Namespace will set the namespaces
func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) error { func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) error {
current := c.NewBuilder(c.IncludeThirdPartyAPIs). currentInfos, err := c.newBuilder(namespace, currentReader).Do().Infos()
ContinueOnError().
NamespaceParam(namespace).
DefaultNamespace().
Stream(currentReader, "").
Flatten().
Do()
target := c.NewBuilder(c.IncludeThirdPartyAPIs).
ContinueOnError().
NamespaceParam(namespace).
DefaultNamespace().
Stream(targetReader, "").
Flatten().
Do()
currentInfos, err := current.Infos()
if err != nil { if err != nil {
return err return fmt.Errorf("failed decoding reader into objects: %s", err)
}
target := c.newBuilder(namespace, targetReader).Do()
if target.Err() != nil {
return fmt.Errorf("failed decoding reader into objects: %s", target.Err())
} }
targetInfos := []*resource.Info{} targetInfos := []*resource.Info{}
updateErrors := []string{} updateErrors := []string{}
err = target.Visit(func(info *resource.Info, err error) error { err = target.Visit(func(info *resource.Info, err error) error {
targetInfos = append(targetInfos, info) targetInfos = append(targetInfos, info)
if err != nil { if err != nil {
return err return err
@ -194,7 +191,7 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
// Since the resource does not exist, create it. // Since the resource does not exist, create it.
if err := createResource(info); err != nil { if err := createResource(info); err != nil {
return err return fmt.Errorf("failed to create resource: %s", err)
} }
kind := info.Mapping.GroupVersionKind.Kind kind := info.Mapping.GroupVersionKind.Kind
@ -219,17 +216,14 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
return nil return nil
}) })
deleteUnwantedResources(currentInfos, targetInfos)
if err != nil { if err != nil {
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)
return nil return nil
} }
// Delete deletes kubernetes resources from an io.reader // Delete deletes kubernetes resources from an io.reader
@ -261,7 +255,6 @@ func skipIfNotFound(err error) error {
log.Printf("%v", err) log.Printf("%v", err)
return nil return nil
} }
return err return err
} }
@ -284,36 +277,17 @@ func (c *Client) WatchUntilReady(namespace string, reader io.Reader) error {
} }
func perform(c *Client, namespace string, reader io.Reader, fn ResourceActorFunc) error { func perform(c *Client, namespace string, reader io.Reader, fn ResourceActorFunc) error {
r := c.NewBuilder(c.IncludeThirdPartyAPIs). infos, err := c.newBuilder(namespace, reader).Do().Infos()
ContinueOnError(). switch {
NamespaceParam(namespace). case err != nil:
DefaultNamespace(). return err
Stream(reader, ""). case len(infos) == 0:
Flatten(). return fmt.Errorf("no objects visited")
Do()
if r.Err() != nil {
return r.Err()
} }
for _, info := range infos {
count := 0 if err := fn(info); err != nil {
err := r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err return err
} }
err = fn(info)
if err == nil {
count++
}
return err
})
if err != nil {
return err
}
if count == 0 {
return fmt.Errorf("no objects visited")
} }
return nil return nil
} }

Loading…
Cancel
Save