Commit afb24d00 authored by Adam Reese's avatar Adam Reese

ref(kube): add newBuilder function to dry up builder calls

parent 91005b8f
...@@ -93,6 +93,15 @@ func (c *Client) Create(namespace string, reader io.Reader) error { ...@@ -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) { ...@@ -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) ...@@ -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) ...@@ -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 { ...@@ -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 { ...@@ -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
} }
......
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