Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
3b45b021
Commit
3b45b021
authored
Apr 16, 2018
by
leigh schrandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[kubeadm] Implement ReadStaticPodFromDisk
parent
d55d1b6f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
109 additions
and
0 deletions
+109
-0
marshal.go
cmd/kubeadm/app/util/marshal.go
+17
-0
BUILD
cmd/kubeadm/app/util/staticpod/BUILD
+1
-0
utils.go
cmd/kubeadm/app/util/staticpod/utils.go
+17
-0
utils_test.go
cmd/kubeadm/app/util/staticpod/utils_test.go
+74
-0
No files found.
cmd/kubeadm/app/util/marshal.go
View file @
3b45b021
...
@@ -41,3 +41,20 @@ func MarshalToYamlForCodecs(obj runtime.Object, gv schema.GroupVersion, codecs s
...
@@ -41,3 +41,20 @@ func MarshalToYamlForCodecs(obj runtime.Object, gv schema.GroupVersion, codecs s
encoder
:=
codecs
.
EncoderForVersion
(
info
.
Serializer
,
gv
)
encoder
:=
codecs
.
EncoderForVersion
(
info
.
Serializer
,
gv
)
return
runtime
.
Encode
(
encoder
,
obj
)
return
runtime
.
Encode
(
encoder
,
obj
)
}
}
// UnmarshalFromYaml unmarshals yaml into an object.
func
UnmarshalFromYaml
(
buffer
[]
byte
,
gv
schema
.
GroupVersion
)
(
runtime
.
Object
,
error
)
{
return
UnmarshalFromYamlForCodecs
(
buffer
,
gv
,
clientsetscheme
.
Codecs
)
}
// UnmarshalFromYamlForCodecs unmarshals yaml into an object using the specified codec
func
UnmarshalFromYamlForCodecs
(
buffer
[]
byte
,
gv
schema
.
GroupVersion
,
codecs
serializer
.
CodecFactory
)
(
runtime
.
Object
,
error
)
{
mediaType
:=
"application/yaml"
info
,
ok
:=
runtime
.
SerializerInfoForMediaType
(
codecs
.
SupportedMediaTypes
(),
mediaType
)
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"unsupported media type %q"
,
mediaType
)
}
decoder
:=
codecs
.
DecoderToVersion
(
info
.
Serializer
,
gv
)
return
runtime
.
Decode
(
decoder
,
buffer
)
}
cmd/kubeadm/app/util/staticpod/BUILD
View file @
3b45b021
...
@@ -14,6 +14,7 @@ go_test(
...
@@ -14,6 +14,7 @@ go_test(
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
"//cmd/kubeadm/app/constants:go_default_library",
"//cmd/kubeadm/app/constants:go_default_library",
"//cmd/kubeadm/app/features:go_default_library",
"//cmd/kubeadm/app/features:go_default_library",
"//cmd/kubeadm/test:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
...
...
cmd/kubeadm/app/util/staticpod/utils.go
View file @
3b45b021
...
@@ -198,6 +198,23 @@ func WriteStaticPodToDisk(componentName, manifestDir string, pod v1.Pod) error {
...
@@ -198,6 +198,23 @@ func WriteStaticPodToDisk(componentName, manifestDir string, pod v1.Pod) error {
return
nil
return
nil
}
}
// ReadStaticPodFromDisk reads a static pod file from disk
func
ReadStaticPodFromDisk
(
manifestPath
string
)
(
*
v1
.
Pod
,
error
)
{
buf
,
err
:=
ioutil
.
ReadFile
(
manifestPath
)
if
err
!=
nil
{
return
&
v1
.
Pod
{},
fmt
.
Errorf
(
"failed to read manifest for %q: %v"
,
manifestPath
,
err
)
}
obj
,
err
:=
util
.
UnmarshalFromYaml
(
buf
,
v1
.
SchemeGroupVersion
)
if
err
!=
nil
{
return
&
v1
.
Pod
{},
fmt
.
Errorf
(
"failed to unmarshal manifest for %q from YAML: %v"
,
manifestPath
,
err
)
}
pod
:=
obj
.
(
*
v1
.
Pod
)
return
pod
,
nil
}
// GetProbeAddress returns an IP address or 127.0.0.1 to use for liveness probes
// GetProbeAddress returns an IP address or 127.0.0.1 to use for liveness probes
// in static pod manifests.
// in static pod manifests.
func
GetProbeAddress
(
cfg
*
kubeadmapi
.
MasterConfiguration
,
componentName
string
)
string
{
func
GetProbeAddress
(
cfg
*
kubeadmapi
.
MasterConfiguration
,
componentName
string
)
string
{
...
...
cmd/kubeadm/app/util/staticpod/utils_test.go
View file @
3b45b021
...
@@ -17,6 +17,9 @@ limitations under the License.
...
@@ -17,6 +17,9 @@ limitations under the License.
package
staticpod
package
staticpod
import
(
import
(
"io/ioutil"
"os"
"path/filepath"
"reflect"
"reflect"
"sort"
"sort"
"testing"
"testing"
...
@@ -28,6 +31,7 @@ import (
...
@@ -28,6 +31,7 @@ import (
kubeadmapi
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmapi
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmconstants
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
kubeadmconstants
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
testutil
"k8s.io/kubernetes/cmd/kubeadm/test"
)
)
func
TestComponentResources
(
t
*
testing
.
T
)
{
func
TestComponentResources
(
t
*
testing
.
T
)
{
...
@@ -454,3 +458,73 @@ func TestGetExtraParameters(t *testing.T) {
...
@@ -454,3 +458,73 @@ func TestGetExtraParameters(t *testing.T) {
}
}
}
}
}
}
const
(
validPod
=
`
apiVersion: v1
kind: Pod
metadata:
labels:
component: etcd
tier: control-plane
name: etcd
namespace: kube-system
spec:
containers:
- image: gcr.io/google_containers/etcd-amd64:3.1.11
status: {}
`
invalidPod
=
`---{ broken yaml @@@`
)
func
TestReadStaticPodFromDisk
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
description
string
podYaml
string
expectErr
bool
writeManifest
bool
}{
{
description
:
"valid pod is marshaled"
,
podYaml
:
validPod
,
writeManifest
:
true
,
expectErr
:
false
,
},
{
description
:
"invalid pod fails to unmarshal"
,
podYaml
:
invalidPod
,
writeManifest
:
true
,
expectErr
:
true
,
},
{
description
:
"non-existent file returns error"
,
podYaml
:
``
,
writeManifest
:
false
,
expectErr
:
true
,
},
}
for
_
,
rt
:=
range
tests
{
tmpdir
:=
testutil
.
SetupTempDir
(
t
)
defer
os
.
RemoveAll
(
tmpdir
)
manifestPath
:=
filepath
.
Join
(
tmpdir
,
"pod.yaml"
)
if
rt
.
writeManifest
{
err
:=
ioutil
.
WriteFile
(
manifestPath
,
[]
byte
(
rt
.
podYaml
),
0644
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to write pod manifest
\n
%s
\n\t
fatal error: %v"
,
rt
.
description
,
err
)
}
}
_
,
actualErr
:=
ReadStaticPodFromDisk
(
manifestPath
)
if
(
actualErr
!=
nil
)
!=
rt
.
expectErr
{
t
.
Errorf
(
"ReadStaticPodFromDisk failed
\n
%s
\n\t
expected error: %t
\n\t
got: %t
\n\t
actual error: %v"
,
rt
.
description
,
rt
.
expectErr
,
(
actualErr
!=
nil
),
actualErr
,
)
}
}
}
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