Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
H
helm3
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
helm3
Commits
afb24d00
Commit
afb24d00
authored
Oct 31, 2016
by
Adam Reese
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ref(kube): add newBuilder function to dry up builder calls
parent
91005b8f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
51 deletions
+25
-51
client.go
pkg/kube/client.go
+25
-51
No files found.
pkg/kube/client.go
View file @
afb24d00
...
@@ -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
()
.
Stream
(
reader
,
""
)
.
Flatten
()
.
Do
()
if
r
.
Err
()
!=
nil
{
return
r
.
Err
()
}
count
:=
0
err
:=
r
.
Visit
(
func
(
info
*
resource
.
Info
,
err
error
)
error
{
if
err
!=
nil
{
return
err
return
err
case
len
(
infos
)
==
0
:
return
fmt
.
Errorf
(
"no objects visited"
)
}
}
err
=
fn
(
info
)
for
_
,
info
:=
range
infos
{
if
err
:=
fn
(
info
);
err
!=
nil
{
if
err
==
nil
{
count
++
}
return
err
})
if
err
!=
nil
{
return
err
return
err
}
}
if
count
==
0
{
return
fmt
.
Errorf
(
"no objects visited"
)
}
}
return
nil
return
nil
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment