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
13ec13af
Commit
13ec13af
authored
Nov 01, 2016
by
Adam Reese
Committed by
GitHub
Nov 01, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1487 from adamreese/ref/kube-builder-update
ref(kube): add info match helper functions
parents
eb3624a5
ab12c462
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
27 deletions
+24
-27
client.go
pkg/kube/client.go
+24
-27
No files found.
pkg/kube/client.go
View file @
13ec13af
...
...
@@ -180,11 +180,9 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
if
err
!=
nil
{
return
err
}
resourceName
:=
info
.
Name
resourceKind
:=
info
.
Mapping
.
GroupVersionKind
.
Kind
helper
:=
resource
.
NewHelper
(
info
.
Client
,
info
.
Mapping
)
if
_
,
err
:=
helper
.
Get
(
info
.
Namespace
,
resource
Name
,
info
.
Export
);
err
!=
nil
{
if
_
,
err
:=
helper
.
Get
(
info
.
Namespace
,
info
.
Name
,
info
.
Export
);
err
!=
nil
{
if
!
errors
.
IsNotFound
(
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)
}
kind
:=
info
.
Mapping
.
GroupVersionKind
.
Kind
log
.
Printf
(
"Created a new %s called %s
\n
"
,
kind
,
resource
Name
)
log
.
Printf
(
"Created a new %s called %s
\n
"
,
kind
,
info
.
Name
)
return
nil
}
currentObj
,
err
:=
getCurrentObject
(
resourceName
,
resourceKind
,
currentInfos
)
currentObj
,
err
:=
getCurrentObject
(
info
,
currentInfos
)
if
err
!=
nil
{
return
err
}
...
...
@@ -208,7 +206,7 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
if
alreadyExistErr
,
ok
:=
err
.
(
ErrAlreadyExists
);
ok
{
log
.
Printf
(
alreadyExistErr
.
errorMsg
)
}
else
{
log
.
Printf
(
"error updating the resource %s:
\n\t
%v"
,
resource
Name
,
err
)
log
.
Printf
(
"error updating the resource %s:
\n\t
%v"
,
info
.
Name
,
err
)
updateErrors
=
append
(
updateErrors
,
err
.
Error
())
}
}
...
...
@@ -220,7 +218,6 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader)
return
err
}
else
if
len
(
updateErrors
)
!=
0
{
return
fmt
.
Errorf
(
strings
.
Join
(
updateErrors
,
" && "
))
}
deleteUnwantedResources
(
currentInfos
,
targetInfos
)
return
nil
...
...
@@ -419,13 +416,7 @@ func (c *Client) ensureNamespace(namespace string) error {
func
deleteUnwantedResources
(
currentInfos
,
targetInfos
[]
*
resource
.
Info
)
{
for
_
,
cInfo
:=
range
currentInfos
{
found
:=
false
for
_
,
m
:=
range
targetInfos
{
if
m
.
Name
==
cInfo
.
Name
{
found
=
true
}
}
if
!
found
{
if
_
,
ok
:=
findMatchingInfo
(
cInfo
,
targetInfos
);
!
ok
{
log
.
Printf
(
"Deleting %s..."
,
cInfo
.
Name
)
if
err
:=
deleteResource
(
cInfo
);
err
!=
nil
{
log
.
Printf
(
"Failed to delete %s, err: %s"
,
cInfo
.
Name
,
err
)
...
...
@@ -434,20 +425,26 @@ func deleteUnwantedResources(currentInfos, targetInfos []*resource.Info) {
}
}
func
getCurrentObject
(
targetName
,
targetKind
string
,
infos
[]
*
resource
.
Info
)
(
runtime
.
Object
,
error
)
{
var
curr
*
resource
.
Info
for
_
,
currInfo
:=
range
infos
{
currKind
:=
currInfo
.
Mapping
.
GroupVersionKind
.
Kind
if
currInfo
.
Name
==
targetName
&&
currKind
==
targetKind
{
curr
=
currInfo
}
func
getCurrentObject
(
target
*
resource
.
Info
,
infos
[]
*
resource
.
Info
)
(
runtime
.
Object
,
error
)
{
if
found
,
ok
:=
findMatchingInfo
(
target
,
infos
);
ok
{
encoder
:=
api
.
Codecs
.
LegacyCodec
(
registered
.
EnabledVersions
()
...
)
defaultVersion
:=
unversioned
.
GroupVersion
{}
return
resource
.
AsVersionedObject
([]
*
resource
.
Info
{
found
},
false
,
defaultVersion
,
encoder
)
}
return
nil
,
fmt
.
Errorf
(
"No resource with the name %s found."
,
target
.
Name
)
}
if
curr
==
nil
{
return
nil
,
fmt
.
Errorf
(
"No resource with the name %s found."
,
targetName
)
}
// isMatchingInfo returns true if infos match on Name and Kind.
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
()
...
)
defaultVersion
:=
unversioned
.
GroupVersion
{}
return
resource
.
AsVersionedObject
([]
*
resource
.
Info
{
curr
},
false
,
defaultVersion
,
encoder
)
// findMatchingInfo returns the first object that matches target.
func
findMatchingInfo
(
target
*
resource
.
Info
,
infos
[]
*
resource
.
Info
)
(
*
resource
.
Info
,
bool
)
{
for
_
,
info
:=
range
infos
{
if
isMatchingInfo
(
target
,
info
)
{
return
info
,
true
}
}
return
nil
,
false
}
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