123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361 |
- /*
- Copyright 2016 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package kubelet
- import (
- "errors"
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "sort"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "k8s.io/api/core/v1"
- apierrors "k8s.io/apimachinery/pkg/api/errors"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/labels"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/types"
- utilfeature "k8s.io/apiserver/pkg/util/feature"
- core "k8s.io/client-go/testing"
- "k8s.io/client-go/tools/record"
- featuregatetesting "k8s.io/component-base/featuregate/testing"
- // TODO: remove this import if
- // api.Registry.GroupOrDie(v1.GroupName).GroupVersions[0].String() is changed
- // to "v1"?
- _ "k8s.io/kubernetes/pkg/apis/core/install"
- "k8s.io/kubernetes/pkg/features"
- kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
- containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
- "k8s.io/kubernetes/pkg/kubelet/server/portforward"
- "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
- "k8s.io/kubernetes/pkg/util/mount"
- "k8s.io/kubernetes/pkg/volume/util/subpath"
- )
- func TestDisabledSubpath(t *testing.T) {
- fm := &mount.FakeMounter{}
- fsp := &subpath.FakeSubpath{}
- pod := v1.Pod{
- Spec: v1.PodSpec{
- HostNetwork: true,
- },
- }
- podVolumes := kubecontainer.VolumeMap{
- "disk": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/mnt/disk"}},
- }
- cases := map[string]struct {
- container v1.Container
- expectError bool
- }{
- "subpath not specified": {
- v1.Container{
- VolumeMounts: []v1.VolumeMount{
- {
- MountPath: "/mnt/path3",
- Name: "disk",
- ReadOnly: true,
- },
- },
- },
- false,
- },
- "subpath specified": {
- v1.Container{
- VolumeMounts: []v1.VolumeMount{
- {
- MountPath: "/mnt/path3",
- SubPath: "/must/not/be/absolute",
- Name: "disk",
- ReadOnly: true,
- },
- },
- },
- true,
- },
- }
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.VolumeSubpath, false)()
- for name, test := range cases {
- _, _, err := makeMounts(&pod, "/pod", &test.container, "fakepodname", "", "", podVolumes, fm, fsp, nil)
- if err != nil && !test.expectError {
- t.Errorf("test %v failed: %v", name, err)
- }
- if err == nil && test.expectError {
- t.Errorf("test %v failed: expected error", name)
- }
- }
- }
- func TestNodeHostsFileContent(t *testing.T) {
- testCases := []struct {
- hostsFileName string
- hostAliases []v1.HostAlias
- rawHostsFileContent string
- expectedHostsFileContent string
- }{
- {
- "hosts_test_file1",
- []v1.HostAlias{},
- `# hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 123.45.67.89 some.domain
- `,
- `# Kubernetes-managed hosts file (host network).
- # hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 123.45.67.89 some.domain
- `,
- },
- {
- "hosts_test_file2",
- []v1.HostAlias{},
- `# another hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 12.34.56.78 another.domain
- `,
- `# Kubernetes-managed hosts file (host network).
- # another hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 12.34.56.78 another.domain
- `,
- },
- {
- "hosts_test_file1_with_host_aliases",
- []v1.HostAlias{
- {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
- },
- `# hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 123.45.67.89 some.domain
- `,
- `# Kubernetes-managed hosts file (host network).
- # hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 123.45.67.89 some.domain
- # Entries added by HostAliases.
- 123.45.67.89 foo bar baz
- `,
- },
- {
- "hosts_test_file2_with_host_aliases",
- []v1.HostAlias{
- {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
- {IP: "456.78.90.123", Hostnames: []string{"park", "doo", "boo"}},
- },
- `# another hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 12.34.56.78 another.domain
- `,
- `# Kubernetes-managed hosts file (host network).
- # another hosts file for testing.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 12.34.56.78 another.domain
- # Entries added by HostAliases.
- 123.45.67.89 foo bar baz
- 456.78.90.123 park doo boo
- `,
- },
- }
- for _, testCase := range testCases {
- tmpdir, err := writeHostsFile(testCase.hostsFileName, testCase.rawHostsFileContent)
- require.NoError(t, err, "could not create a temp hosts file")
- defer os.RemoveAll(tmpdir)
- actualContent, fileReadErr := nodeHostsFileContent(filepath.Join(tmpdir, testCase.hostsFileName), testCase.hostAliases)
- require.NoError(t, fileReadErr, "could not create read hosts file")
- assert.Equal(t, testCase.expectedHostsFileContent, string(actualContent), "hosts file content not expected")
- }
- }
- // writeHostsFile will write a hosts file into a temporary dir, and return that dir.
- // Caller is responsible for deleting the dir and its contents.
- func writeHostsFile(filename string, cfg string) (string, error) {
- tmpdir, err := ioutil.TempDir("", "kubelet=kubelet_pods_test.go=")
- if err != nil {
- return "", err
- }
- return tmpdir, ioutil.WriteFile(filepath.Join(tmpdir, filename), []byte(cfg), 0644)
- }
- func TestManagedHostsFileContent(t *testing.T) {
- testCases := []struct {
- hostIP string
- hostName string
- hostDomainName string
- hostAliases []v1.HostAlias
- expectedContent string
- }{
- {
- "123.45.67.89",
- "podFoo",
- "",
- []v1.HostAlias{},
- `# Kubernetes-managed hosts file.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 123.45.67.89 podFoo
- `,
- },
- {
- "203.0.113.1",
- "podFoo",
- "domainFoo",
- []v1.HostAlias{},
- `# Kubernetes-managed hosts file.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 203.0.113.1 podFoo.domainFoo podFoo
- `,
- },
- {
- "203.0.113.1",
- "podFoo",
- "domainFoo",
- []v1.HostAlias{
- {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
- },
- `# Kubernetes-managed hosts file.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 203.0.113.1 podFoo.domainFoo podFoo
- # Entries added by HostAliases.
- 123.45.67.89 foo bar baz
- `,
- },
- {
- "203.0.113.1",
- "podFoo",
- "domainFoo",
- []v1.HostAlias{
- {IP: "123.45.67.89", Hostnames: []string{"foo", "bar", "baz"}},
- {IP: "456.78.90.123", Hostnames: []string{"park", "doo", "boo"}},
- },
- `# Kubernetes-managed hosts file.
- 127.0.0.1 localhost
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- fe00::0 ip6-mcastprefix
- fe00::1 ip6-allnodes
- fe00::2 ip6-allrouters
- 203.0.113.1 podFoo.domainFoo podFoo
- # Entries added by HostAliases.
- 123.45.67.89 foo bar baz
- 456.78.90.123 park doo boo
- `,
- },
- }
- for _, testCase := range testCases {
- actualContent := managedHostsFileContent(testCase.hostIP, testCase.hostName, testCase.hostDomainName, testCase.hostAliases)
- assert.Equal(t, testCase.expectedContent, string(actualContent), "hosts file content not expected")
- }
- }
- func TestRunInContainerNoSuchPod(t *testing.T) {
- testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
- defer testKubelet.Cleanup()
- kubelet := testKubelet.kubelet
- fakeRuntime := testKubelet.fakeRuntime
- fakeRuntime.PodList = []*containertest.FakePod{}
- podName := "podFoo"
- podNamespace := "nsFoo"
- containerName := "containerFoo"
- output, err := kubelet.RunInContainer(
- kubecontainer.GetPodFullName(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: podName, Namespace: podNamespace}}),
- "",
- containerName,
- []string{"ls"})
- assert.Error(t, err)
- assert.Nil(t, output, "output should be nil")
- }
- func TestRunInContainer(t *testing.T) {
- for _, testError := range []error{nil, errors.New("bar")} {
- testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
- defer testKubelet.Cleanup()
- kubelet := testKubelet.kubelet
- fakeRuntime := testKubelet.fakeRuntime
- fakeCommandRunner := containertest.FakeContainerCommandRunner{
- Err: testError,
- Stdout: "foo",
- }
- kubelet.runner = &fakeCommandRunner
- containerID := kubecontainer.ContainerID{Type: "test", ID: "abc1234"}
- fakeRuntime.PodList = []*containertest.FakePod{
- {Pod: &kubecontainer.Pod{
- ID: "12345678",
- Name: "podFoo",
- Namespace: "nsFoo",
- Containers: []*kubecontainer.Container{
- {Name: "containerFoo",
- ID: containerID,
- },
- },
- }},
- }
- cmd := []string{"ls"}
- actualOutput, err := kubelet.RunInContainer("podFoo_nsFoo", "", "containerFoo", cmd)
- assert.Equal(t, containerID, fakeCommandRunner.ContainerID, "(testError=%v) ID", testError)
- assert.Equal(t, cmd, fakeCommandRunner.Cmd, "(testError=%v) command", testError)
- // this isn't 100% foolproof as a bug in a real ContainerCommandRunner where it fails to copy to stdout/stderr wouldn't be caught by this test
- assert.Equal(t, "foo", string(actualOutput), "(testError=%v) output", testError)
- assert.Equal(t, err, testError, "(testError=%v) err", testError)
- }
- }
- type testServiceLister struct {
- services []*v1.Service
- }
- func (ls testServiceLister) List(labels.Selector) ([]*v1.Service, error) {
- return ls.services, nil
- }
- type envs []kubecontainer.EnvVar
- func (e envs) Len() int {
- return len(e)
- }
- func (e envs) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
- func (e envs) Less(i, j int) bool { return e[i].Name < e[j].Name }
- func buildService(name, namespace, clusterIP, protocol string, port int) *v1.Service {
- return &v1.Service{
- ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
- Spec: v1.ServiceSpec{
- Ports: []v1.ServicePort{{
- Protocol: v1.Protocol(protocol),
- Port: int32(port),
- }},
- ClusterIP: clusterIP,
- },
- }
- }
- func TestMakeEnvironmentVariables(t *testing.T) {
- trueVal := true
- services := []*v1.Service{
- buildService("kubernetes", metav1.NamespaceDefault, "1.2.3.1", "TCP", 8081),
- buildService("test", "test1", "1.2.3.3", "TCP", 8083),
- buildService("kubernetes", "test2", "1.2.3.4", "TCP", 8084),
- buildService("test", "test2", "1.2.3.5", "TCP", 8085),
- buildService("test", "test2", "None", "TCP", 8085),
- buildService("test", "test2", "", "TCP", 8085),
- buildService("kubernetes", "kubernetes", "1.2.3.6", "TCP", 8086),
- buildService("not-special", "kubernetes", "1.2.3.8", "TCP", 8088),
- buildService("not-special", "kubernetes", "None", "TCP", 8088),
- buildService("not-special", "kubernetes", "", "TCP", 8088),
- }
- trueValue := true
- falseValue := false
- testCases := []struct {
- name string // the name of the test case
- ns string // the namespace to generate environment for
- enableServiceLinks *bool // enabling service links
- container *v1.Container // the container to use
- masterServiceNs string // the namespace to read master service info from
- nilLister bool // whether the lister should be nil
- configMap *v1.ConfigMap // an optional ConfigMap to pull from
- secret *v1.Secret // an optional Secret to pull from
- expectedEnvs []kubecontainer.EnvVar // a set of expected environment vars
- expectedError bool // does the test fail
- expectedEvent string // does the test emit an event
- }{
- {
- name: "api server = Y, kubelet = Y",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {Name: "FOO", Value: "BAR"},
- {Name: "TEST_SERVICE_HOST", Value: "1.2.3.3"},
- {Name: "TEST_SERVICE_PORT", Value: "8083"},
- {Name: "TEST_PORT", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP_PROTO", Value: "tcp"},
- {Name: "TEST_PORT_8083_TCP_PORT", Value: "8083"},
- {Name: "TEST_PORT_8083_TCP_ADDR", Value: "1.2.3.3"},
- },
- },
- masterServiceNs: metav1.NamespaceDefault,
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "FOO", Value: "BAR"},
- {Name: "TEST_SERVICE_HOST", Value: "1.2.3.3"},
- {Name: "TEST_SERVICE_PORT", Value: "8083"},
- {Name: "TEST_PORT", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP_PROTO", Value: "tcp"},
- {Name: "TEST_PORT_8083_TCP_PORT", Value: "8083"},
- {Name: "TEST_PORT_8083_TCP_ADDR", Value: "1.2.3.3"},
- {Name: "KUBERNETES_SERVICE_PORT", Value: "8081"},
- {Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.1"},
- {Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.1:8081"},
- {Name: "KUBERNETES_PORT_8081_TCP", Value: "tcp://1.2.3.1:8081"},
- {Name: "KUBERNETES_PORT_8081_TCP_PROTO", Value: "tcp"},
- {Name: "KUBERNETES_PORT_8081_TCP_PORT", Value: "8081"},
- {Name: "KUBERNETES_PORT_8081_TCP_ADDR", Value: "1.2.3.1"},
- },
- },
- {
- name: "api server = Y, kubelet = N",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {Name: "FOO", Value: "BAR"},
- {Name: "TEST_SERVICE_HOST", Value: "1.2.3.3"},
- {Name: "TEST_SERVICE_PORT", Value: "8083"},
- {Name: "TEST_PORT", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP_PROTO", Value: "tcp"},
- {Name: "TEST_PORT_8083_TCP_PORT", Value: "8083"},
- {Name: "TEST_PORT_8083_TCP_ADDR", Value: "1.2.3.3"},
- },
- },
- masterServiceNs: metav1.NamespaceDefault,
- nilLister: true,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "FOO", Value: "BAR"},
- {Name: "TEST_SERVICE_HOST", Value: "1.2.3.3"},
- {Name: "TEST_SERVICE_PORT", Value: "8083"},
- {Name: "TEST_PORT", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP_PROTO", Value: "tcp"},
- {Name: "TEST_PORT_8083_TCP_PORT", Value: "8083"},
- {Name: "TEST_PORT_8083_TCP_ADDR", Value: "1.2.3.3"},
- },
- },
- {
- name: "api server = N; kubelet = Y",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {Name: "FOO", Value: "BAZ"},
- },
- },
- masterServiceNs: metav1.NamespaceDefault,
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "FOO", Value: "BAZ"},
- {Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.1"},
- {Name: "KUBERNETES_SERVICE_PORT", Value: "8081"},
- {Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.1:8081"},
- {Name: "KUBERNETES_PORT_8081_TCP", Value: "tcp://1.2.3.1:8081"},
- {Name: "KUBERNETES_PORT_8081_TCP_PROTO", Value: "tcp"},
- {Name: "KUBERNETES_PORT_8081_TCP_PORT", Value: "8081"},
- {Name: "KUBERNETES_PORT_8081_TCP_ADDR", Value: "1.2.3.1"},
- },
- },
- {
- name: "api server = N; kubelet = Y; service env vars",
- ns: "test1",
- enableServiceLinks: &trueValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {Name: "FOO", Value: "BAZ"},
- },
- },
- masterServiceNs: metav1.NamespaceDefault,
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "FOO", Value: "BAZ"},
- {Name: "TEST_SERVICE_HOST", Value: "1.2.3.3"},
- {Name: "TEST_SERVICE_PORT", Value: "8083"},
- {Name: "TEST_PORT", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP", Value: "tcp://1.2.3.3:8083"},
- {Name: "TEST_PORT_8083_TCP_PROTO", Value: "tcp"},
- {Name: "TEST_PORT_8083_TCP_PORT", Value: "8083"},
- {Name: "TEST_PORT_8083_TCP_ADDR", Value: "1.2.3.3"},
- {Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.1"},
- {Name: "KUBERNETES_SERVICE_PORT", Value: "8081"},
- {Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.1:8081"},
- {Name: "KUBERNETES_PORT_8081_TCP", Value: "tcp://1.2.3.1:8081"},
- {Name: "KUBERNETES_PORT_8081_TCP_PROTO", Value: "tcp"},
- {Name: "KUBERNETES_PORT_8081_TCP_PORT", Value: "8081"},
- {Name: "KUBERNETES_PORT_8081_TCP_ADDR", Value: "1.2.3.1"},
- },
- },
- {
- name: "master service in pod ns",
- ns: "test2",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {Name: "FOO", Value: "ZAP"},
- },
- },
- masterServiceNs: "kubernetes",
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "FOO", Value: "ZAP"},
- {Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.6"},
- {Name: "KUBERNETES_SERVICE_PORT", Value: "8086"},
- {Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.6:8086"},
- {Name: "KUBERNETES_PORT_8086_TCP", Value: "tcp://1.2.3.6:8086"},
- {Name: "KUBERNETES_PORT_8086_TCP_PROTO", Value: "tcp"},
- {Name: "KUBERNETES_PORT_8086_TCP_PORT", Value: "8086"},
- {Name: "KUBERNETES_PORT_8086_TCP_ADDR", Value: "1.2.3.6"},
- },
- },
- {
- name: "master service in pod ns, service env vars",
- ns: "test2",
- enableServiceLinks: &trueValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {Name: "FOO", Value: "ZAP"},
- },
- },
- masterServiceNs: "kubernetes",
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "FOO", Value: "ZAP"},
- {Name: "TEST_SERVICE_HOST", Value: "1.2.3.5"},
- {Name: "TEST_SERVICE_PORT", Value: "8085"},
- {Name: "TEST_PORT", Value: "tcp://1.2.3.5:8085"},
- {Name: "TEST_PORT_8085_TCP", Value: "tcp://1.2.3.5:8085"},
- {Name: "TEST_PORT_8085_TCP_PROTO", Value: "tcp"},
- {Name: "TEST_PORT_8085_TCP_PORT", Value: "8085"},
- {Name: "TEST_PORT_8085_TCP_ADDR", Value: "1.2.3.5"},
- {Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.4"},
- {Name: "KUBERNETES_SERVICE_PORT", Value: "8084"},
- {Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.4:8084"},
- {Name: "KUBERNETES_PORT_8084_TCP", Value: "tcp://1.2.3.4:8084"},
- {Name: "KUBERNETES_PORT_8084_TCP_PROTO", Value: "tcp"},
- {Name: "KUBERNETES_PORT_8084_TCP_PORT", Value: "8084"},
- {Name: "KUBERNETES_PORT_8084_TCP_ADDR", Value: "1.2.3.4"},
- },
- },
- {
- name: "pod in master service ns",
- ns: "kubernetes",
- enableServiceLinks: &falseValue,
- container: &v1.Container{},
- masterServiceNs: "kubernetes",
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.6"},
- {Name: "KUBERNETES_SERVICE_PORT", Value: "8086"},
- {Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.6:8086"},
- {Name: "KUBERNETES_PORT_8086_TCP", Value: "tcp://1.2.3.6:8086"},
- {Name: "KUBERNETES_PORT_8086_TCP_PROTO", Value: "tcp"},
- {Name: "KUBERNETES_PORT_8086_TCP_PORT", Value: "8086"},
- {Name: "KUBERNETES_PORT_8086_TCP_ADDR", Value: "1.2.3.6"},
- },
- },
- {
- name: "pod in master service ns, service env vars",
- ns: "kubernetes",
- enableServiceLinks: &trueValue,
- container: &v1.Container{},
- masterServiceNs: "kubernetes",
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "NOT_SPECIAL_SERVICE_HOST", Value: "1.2.3.8"},
- {Name: "NOT_SPECIAL_SERVICE_PORT", Value: "8088"},
- {Name: "NOT_SPECIAL_PORT", Value: "tcp://1.2.3.8:8088"},
- {Name: "NOT_SPECIAL_PORT_8088_TCP", Value: "tcp://1.2.3.8:8088"},
- {Name: "NOT_SPECIAL_PORT_8088_TCP_PROTO", Value: "tcp"},
- {Name: "NOT_SPECIAL_PORT_8088_TCP_PORT", Value: "8088"},
- {Name: "NOT_SPECIAL_PORT_8088_TCP_ADDR", Value: "1.2.3.8"},
- {Name: "KUBERNETES_SERVICE_HOST", Value: "1.2.3.6"},
- {Name: "KUBERNETES_SERVICE_PORT", Value: "8086"},
- {Name: "KUBERNETES_PORT", Value: "tcp://1.2.3.6:8086"},
- {Name: "KUBERNETES_PORT_8086_TCP", Value: "tcp://1.2.3.6:8086"},
- {Name: "KUBERNETES_PORT_8086_TCP_PROTO", Value: "tcp"},
- {Name: "KUBERNETES_PORT_8086_TCP_PORT", Value: "8086"},
- {Name: "KUBERNETES_PORT_8086_TCP_ADDR", Value: "1.2.3.6"},
- },
- },
- {
- name: "downward api pod",
- ns: "downward-api",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {
- Name: "POD_NAME",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "metadata.name",
- },
- },
- },
- {
- Name: "POD_NAMESPACE",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "metadata.namespace",
- },
- },
- },
- {
- Name: "POD_NODE_NAME",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "spec.nodeName",
- },
- },
- },
- {
- Name: "POD_SERVICE_ACCOUNT_NAME",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "spec.serviceAccountName",
- },
- },
- },
- {
- Name: "POD_IP",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "status.podIP",
- },
- },
- },
- {
- Name: "HOST_IP",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "status.hostIP",
- },
- },
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: true,
- expectedEnvs: []kubecontainer.EnvVar{
- {Name: "POD_NAME", Value: "dapi-test-pod-name"},
- {Name: "POD_NAMESPACE", Value: "downward-api"},
- {Name: "POD_NODE_NAME", Value: "node-name"},
- {Name: "POD_SERVICE_ACCOUNT_NAME", Value: "special"},
- {Name: "POD_IP", Value: "1.2.3.4"},
- {Name: "HOST_IP", Value: testKubeletHostIP},
- },
- },
- {
- name: "env expansion",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "POD_NAME",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1", //legacyscheme.Registry.GroupOrDie(v1.GroupName).GroupVersion.String(),
- FieldPath: "metadata.name",
- },
- },
- },
- {
- Name: "OUT_OF_ORDER_TEST",
- Value: "$(OUT_OF_ORDER_TARGET)",
- },
- {
- Name: "OUT_OF_ORDER_TARGET",
- Value: "FOO",
- },
- {
- Name: "EMPTY_VAR",
- },
- {
- Name: "EMPTY_TEST",
- Value: "foo-$(EMPTY_VAR)",
- },
- {
- Name: "POD_NAME_TEST2",
- Value: "test2-$(POD_NAME)",
- },
- {
- Name: "POD_NAME_TEST3",
- Value: "$(POD_NAME_TEST2)-3",
- },
- {
- Name: "LITERAL_TEST",
- Value: "literal-$(TEST_LITERAL)",
- },
- {
- Name: "TEST_UNDEFINED",
- Value: "$(UNDEFINED_VAR)",
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "POD_NAME",
- Value: "dapi-test-pod-name",
- },
- {
- Name: "POD_NAME_TEST2",
- Value: "test2-dapi-test-pod-name",
- },
- {
- Name: "POD_NAME_TEST3",
- Value: "test2-dapi-test-pod-name-3",
- },
- {
- Name: "LITERAL_TEST",
- Value: "literal-test-test-test",
- },
- {
- Name: "OUT_OF_ORDER_TEST",
- Value: "$(OUT_OF_ORDER_TARGET)",
- },
- {
- Name: "OUT_OF_ORDER_TARGET",
- Value: "FOO",
- },
- {
- Name: "TEST_UNDEFINED",
- Value: "$(UNDEFINED_VAR)",
- },
- {
- Name: "EMPTY_VAR",
- },
- {
- Name: "EMPTY_TEST",
- Value: "foo-",
- },
- },
- },
- {
- name: "env expansion, service env vars",
- ns: "test1",
- enableServiceLinks: &trueValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "POD_NAME",
- ValueFrom: &v1.EnvVarSource{
- FieldRef: &v1.ObjectFieldSelector{
- APIVersion: "v1",
- FieldPath: "metadata.name",
- },
- },
- },
- {
- Name: "OUT_OF_ORDER_TEST",
- Value: "$(OUT_OF_ORDER_TARGET)",
- },
- {
- Name: "OUT_OF_ORDER_TARGET",
- Value: "FOO",
- },
- {
- Name: "EMPTY_VAR",
- },
- {
- Name: "EMPTY_TEST",
- Value: "foo-$(EMPTY_VAR)",
- },
- {
- Name: "POD_NAME_TEST2",
- Value: "test2-$(POD_NAME)",
- },
- {
- Name: "POD_NAME_TEST3",
- Value: "$(POD_NAME_TEST2)-3",
- },
- {
- Name: "LITERAL_TEST",
- Value: "literal-$(TEST_LITERAL)",
- },
- {
- Name: "SERVICE_VAR_TEST",
- Value: "$(TEST_SERVICE_HOST):$(TEST_SERVICE_PORT)",
- },
- {
- Name: "TEST_UNDEFINED",
- Value: "$(UNDEFINED_VAR)",
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: false,
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "POD_NAME",
- Value: "dapi-test-pod-name",
- },
- {
- Name: "POD_NAME_TEST2",
- Value: "test2-dapi-test-pod-name",
- },
- {
- Name: "POD_NAME_TEST3",
- Value: "test2-dapi-test-pod-name-3",
- },
- {
- Name: "LITERAL_TEST",
- Value: "literal-test-test-test",
- },
- {
- Name: "TEST_SERVICE_HOST",
- Value: "1.2.3.3",
- },
- {
- Name: "TEST_SERVICE_PORT",
- Value: "8083",
- },
- {
- Name: "TEST_PORT",
- Value: "tcp://1.2.3.3:8083",
- },
- {
- Name: "TEST_PORT_8083_TCP",
- Value: "tcp://1.2.3.3:8083",
- },
- {
- Name: "TEST_PORT_8083_TCP_PROTO",
- Value: "tcp",
- },
- {
- Name: "TEST_PORT_8083_TCP_PORT",
- Value: "8083",
- },
- {
- Name: "TEST_PORT_8083_TCP_ADDR",
- Value: "1.2.3.3",
- },
- {
- Name: "SERVICE_VAR_TEST",
- Value: "1.2.3.3:8083",
- },
- {
- Name: "OUT_OF_ORDER_TEST",
- Value: "$(OUT_OF_ORDER_TARGET)",
- },
- {
- Name: "OUT_OF_ORDER_TARGET",
- Value: "FOO",
- },
- {
- Name: "TEST_UNDEFINED",
- Value: "$(UNDEFINED_VAR)",
- },
- {
- Name: "EMPTY_VAR",
- },
- {
- Name: "EMPTY_TEST",
- Value: "foo-",
- },
- },
- },
- {
- name: "configmapkeyref_missing_optional",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {
- Name: "POD_NAME",
- ValueFrom: &v1.EnvVarSource{
- ConfigMapKeyRef: &v1.ConfigMapKeySelector{
- LocalObjectReference: v1.LocalObjectReference{Name: "missing-config-map"},
- Key: "key",
- Optional: &trueVal,
- },
- },
- },
- },
- },
- masterServiceNs: "nothing",
- expectedEnvs: nil,
- },
- {
- name: "configmapkeyref_missing_key_optional",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {
- Name: "POD_NAME",
- ValueFrom: &v1.EnvVarSource{
- ConfigMapKeyRef: &v1.ConfigMapKeySelector{
- LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"},
- Key: "key",
- Optional: &trueVal,
- },
- },
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: true,
- configMap: &v1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-configmap",
- },
- Data: map[string]string{
- "a": "b",
- },
- },
- expectedEnvs: nil,
- },
- {
- name: "secretkeyref_missing_optional",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {
- Name: "POD_NAME",
- ValueFrom: &v1.EnvVarSource{
- SecretKeyRef: &v1.SecretKeySelector{
- LocalObjectReference: v1.LocalObjectReference{Name: "missing-secret"},
- Key: "key",
- Optional: &trueVal,
- },
- },
- },
- },
- },
- masterServiceNs: "nothing",
- expectedEnvs: nil,
- },
- {
- name: "secretkeyref_missing_key_optional",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- Env: []v1.EnvVar{
- {
- Name: "POD_NAME",
- ValueFrom: &v1.EnvVarSource{
- SecretKeyRef: &v1.SecretKeySelector{
- LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"},
- Key: "key",
- Optional: &trueVal,
- },
- },
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: true,
- secret: &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-secret",
- },
- Data: map[string][]byte{
- "a": []byte("b"),
- },
- },
- expectedEnvs: nil,
- },
- {
- name: "configmap",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {
- ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}},
- },
- {
- Prefix: "p_",
- ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}},
- },
- },
- Env: []v1.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "$(REPLACE_ME)",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: false,
- configMap: &v1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-configmap",
- },
- Data: map[string]string{
- "REPLACE_ME": "FROM_CONFIG_MAP",
- "DUPE_TEST": "CONFIG_MAP",
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "REPLACE_ME",
- Value: "FROM_CONFIG_MAP",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "FROM_CONFIG_MAP",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- {
- Name: "p_REPLACE_ME",
- Value: "FROM_CONFIG_MAP",
- },
- {
- Name: "p_DUPE_TEST",
- Value: "CONFIG_MAP",
- },
- },
- },
- {
- name: "configmap, service env vars",
- ns: "test1",
- enableServiceLinks: &trueValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {
- ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}},
- },
- {
- Prefix: "p_",
- ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}},
- },
- },
- Env: []v1.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "$(REPLACE_ME)",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: false,
- configMap: &v1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-configmap",
- },
- Data: map[string]string{
- "REPLACE_ME": "FROM_CONFIG_MAP",
- "DUPE_TEST": "CONFIG_MAP",
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "TEST_SERVICE_HOST",
- Value: "1.2.3.3",
- },
- {
- Name: "TEST_SERVICE_PORT",
- Value: "8083",
- },
- {
- Name: "TEST_PORT",
- Value: "tcp://1.2.3.3:8083",
- },
- {
- Name: "TEST_PORT_8083_TCP",
- Value: "tcp://1.2.3.3:8083",
- },
- {
- Name: "TEST_PORT_8083_TCP_PROTO",
- Value: "tcp",
- },
- {
- Name: "TEST_PORT_8083_TCP_PORT",
- Value: "8083",
- },
- {
- Name: "TEST_PORT_8083_TCP_ADDR",
- Value: "1.2.3.3",
- },
- {
- Name: "REPLACE_ME",
- Value: "FROM_CONFIG_MAP",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "FROM_CONFIG_MAP",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- {
- Name: "p_REPLACE_ME",
- Value: "FROM_CONFIG_MAP",
- },
- {
- Name: "p_DUPE_TEST",
- Value: "CONFIG_MAP",
- },
- },
- },
- {
- name: "configmap_missing",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}}},
- },
- },
- masterServiceNs: "nothing",
- expectedError: true,
- },
- {
- name: "configmap_missing_optional",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {ConfigMapRef: &v1.ConfigMapEnvSource{
- Optional: &trueVal,
- LocalObjectReference: v1.LocalObjectReference{Name: "missing-config-map"}}},
- },
- },
- masterServiceNs: "nothing",
- expectedEnvs: nil,
- },
- {
- name: "configmap_invalid_keys",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}}},
- },
- },
- masterServiceNs: "nothing",
- configMap: &v1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-configmap",
- },
- Data: map[string]string{
- "1234": "abc",
- "1z": "abc",
- "key": "value",
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "key",
- Value: "value",
- },
- },
- expectedEvent: "Warning InvalidEnvironmentVariableNames Keys [1234, 1z] from the EnvFrom configMap test/test-config-map were skipped since they are considered invalid environment variable names.",
- },
- {
- name: "configmap_invalid_keys_valid",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {
- Prefix: "p_",
- ConfigMapRef: &v1.ConfigMapEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-config-map"}},
- },
- },
- },
- masterServiceNs: "",
- configMap: &v1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-configmap",
- },
- Data: map[string]string{
- "1234": "abc",
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "p_1234",
- Value: "abc",
- },
- },
- },
- {
- name: "secret",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {
- SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
- },
- {
- Prefix: "p_",
- SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
- },
- },
- Env: []v1.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "$(REPLACE_ME)",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: false,
- secret: &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-secret",
- },
- Data: map[string][]byte{
- "REPLACE_ME": []byte("FROM_SECRET"),
- "DUPE_TEST": []byte("SECRET"),
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "REPLACE_ME",
- Value: "FROM_SECRET",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "FROM_SECRET",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- {
- Name: "p_REPLACE_ME",
- Value: "FROM_SECRET",
- },
- {
- Name: "p_DUPE_TEST",
- Value: "SECRET",
- },
- },
- },
- {
- name: "secret, service env vars",
- ns: "test1",
- enableServiceLinks: &trueValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {
- SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
- },
- {
- Prefix: "p_",
- SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
- },
- },
- Env: []v1.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "$(REPLACE_ME)",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- },
- },
- masterServiceNs: "nothing",
- nilLister: false,
- secret: &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-secret",
- },
- Data: map[string][]byte{
- "REPLACE_ME": []byte("FROM_SECRET"),
- "DUPE_TEST": []byte("SECRET"),
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "TEST_LITERAL",
- Value: "test-test-test",
- },
- {
- Name: "TEST_SERVICE_HOST",
- Value: "1.2.3.3",
- },
- {
- Name: "TEST_SERVICE_PORT",
- Value: "8083",
- },
- {
- Name: "TEST_PORT",
- Value: "tcp://1.2.3.3:8083",
- },
- {
- Name: "TEST_PORT_8083_TCP",
- Value: "tcp://1.2.3.3:8083",
- },
- {
- Name: "TEST_PORT_8083_TCP_PROTO",
- Value: "tcp",
- },
- {
- Name: "TEST_PORT_8083_TCP_PORT",
- Value: "8083",
- },
- {
- Name: "TEST_PORT_8083_TCP_ADDR",
- Value: "1.2.3.3",
- },
- {
- Name: "REPLACE_ME",
- Value: "FROM_SECRET",
- },
- {
- Name: "EXPANSION_TEST",
- Value: "FROM_SECRET",
- },
- {
- Name: "DUPE_TEST",
- Value: "ENV_VAR",
- },
- {
- Name: "p_REPLACE_ME",
- Value: "FROM_SECRET",
- },
- {
- Name: "p_DUPE_TEST",
- Value: "SECRET",
- },
- },
- },
- {
- name: "secret_missing",
- ns: "test1",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}}},
- },
- },
- masterServiceNs: "nothing",
- expectedError: true,
- },
- {
- name: "secret_missing_optional",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {SecretRef: &v1.SecretEnvSource{
- LocalObjectReference: v1.LocalObjectReference{Name: "missing-secret"},
- Optional: &trueVal}},
- },
- },
- masterServiceNs: "nothing",
- expectedEnvs: nil,
- },
- {
- name: "secret_invalid_keys",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}}},
- },
- },
- masterServiceNs: "nothing",
- secret: &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-secret",
- },
- Data: map[string][]byte{
- "1234": []byte("abc"),
- "1z": []byte("abc"),
- "key.1": []byte("value"),
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "key.1",
- Value: "value",
- },
- },
- expectedEvent: "Warning InvalidEnvironmentVariableNames Keys [1234, 1z] from the EnvFrom secret test/test-secret were skipped since they are considered invalid environment variable names.",
- },
- {
- name: "secret_invalid_keys_valid",
- ns: "test",
- enableServiceLinks: &falseValue,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {
- Prefix: "p_",
- SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
- },
- },
- },
- masterServiceNs: "",
- secret: &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-secret",
- },
- Data: map[string][]byte{
- "1234.name": []byte("abc"),
- },
- },
- expectedEnvs: []kubecontainer.EnvVar{
- {
- Name: "p_1234.name",
- Value: "abc",
- },
- },
- },
- {
- name: "nil_enableServiceLinks",
- ns: "test",
- enableServiceLinks: nil,
- container: &v1.Container{
- EnvFrom: []v1.EnvFromSource{
- {
- Prefix: "p_",
- SecretRef: &v1.SecretEnvSource{LocalObjectReference: v1.LocalObjectReference{Name: "test-secret"}},
- },
- },
- },
- masterServiceNs: "",
- secret: &v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: "test1",
- Name: "test-secret",
- },
- Data: map[string][]byte{
- "1234.name": []byte("abc"),
- },
- },
- expectedError: true,
- },
- }
- for _, tc := range testCases {
- fakeRecorder := record.NewFakeRecorder(1)
- testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
- testKubelet.kubelet.recorder = fakeRecorder
- defer testKubelet.Cleanup()
- kl := testKubelet.kubelet
- kl.masterServiceNamespace = tc.masterServiceNs
- if tc.nilLister {
- kl.serviceLister = nil
- } else {
- kl.serviceLister = testServiceLister{services}
- }
- testKubelet.fakeKubeClient.AddReactor("get", "configmaps", func(action core.Action) (bool, runtime.Object, error) {
- var err error
- if tc.configMap == nil {
- err = apierrors.NewNotFound(action.GetResource().GroupResource(), "configmap-name")
- }
- return true, tc.configMap, err
- })
- testKubelet.fakeKubeClient.AddReactor("get", "secrets", func(action core.Action) (bool, runtime.Object, error) {
- var err error
- if tc.secret == nil {
- err = apierrors.NewNotFound(action.GetResource().GroupResource(), "secret-name")
- }
- return true, tc.secret, err
- })
- testKubelet.fakeKubeClient.AddReactor("get", "secrets", func(action core.Action) (bool, runtime.Object, error) {
- var err error
- if tc.secret == nil {
- err = errors.New("no secret defined")
- }
- return true, tc.secret, err
- })
- testPod := &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Namespace: tc.ns,
- Name: "dapi-test-pod-name",
- },
- Spec: v1.PodSpec{
- ServiceAccountName: "special",
- NodeName: "node-name",
- EnableServiceLinks: tc.enableServiceLinks,
- },
- }
- podIP := "1.2.3.4"
- result, err := kl.makeEnvironmentVariables(testPod, tc.container, podIP)
- select {
- case e := <-fakeRecorder.Events:
- assert.Equal(t, tc.expectedEvent, e)
- default:
- assert.Equal(t, "", tc.expectedEvent)
- }
- if tc.expectedError {
- assert.Error(t, err, tc.name)
- } else {
- assert.NoError(t, err, "[%s]", tc.name)
- sort.Sort(envs(result))
- sort.Sort(envs(tc.expectedEnvs))
- assert.Equal(t, tc.expectedEnvs, result, "[%s] env entries", tc.name)
- }
- }
- }
- func waitingState(cName string) v1.ContainerStatus {
- return v1.ContainerStatus{
- Name: cName,
- State: v1.ContainerState{
- Waiting: &v1.ContainerStateWaiting{},
- },
- }
- }
- func waitingStateWithLastTermination(cName string) v1.ContainerStatus {
- return v1.ContainerStatus{
- Name: cName,
- State: v1.ContainerState{
- Waiting: &v1.ContainerStateWaiting{},
- },
- LastTerminationState: v1.ContainerState{
- Terminated: &v1.ContainerStateTerminated{
- ExitCode: 0,
- },
- },
- }
- }
- func runningState(cName string) v1.ContainerStatus {
- return v1.ContainerStatus{
- Name: cName,
- State: v1.ContainerState{
- Running: &v1.ContainerStateRunning{},
- },
- }
- }
- func stoppedState(cName string) v1.ContainerStatus {
- return v1.ContainerStatus{
- Name: cName,
- State: v1.ContainerState{
- Terminated: &v1.ContainerStateTerminated{},
- },
- }
- }
- func succeededState(cName string) v1.ContainerStatus {
- return v1.ContainerStatus{
- Name: cName,
- State: v1.ContainerState{
- Terminated: &v1.ContainerStateTerminated{
- ExitCode: 0,
- },
- },
- }
- }
- func failedState(cName string) v1.ContainerStatus {
- return v1.ContainerStatus{
- Name: cName,
- State: v1.ContainerState{
- Terminated: &v1.ContainerStateTerminated{
- ExitCode: -1,
- },
- },
- }
- }
- func TestPodPhaseWithRestartAlways(t *testing.T) {
- desiredState := v1.PodSpec{
- NodeName: "machine",
- Containers: []v1.Container{
- {Name: "containerA"},
- {Name: "containerB"},
- },
- RestartPolicy: v1.RestartPolicyAlways,
- }
- tests := []struct {
- pod *v1.Pod
- status v1.PodPhase
- test string
- }{
- {&v1.Pod{Spec: desiredState, Status: v1.PodStatus{}}, v1.PodPending, "waiting"},
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- runningState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "all running",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- stoppedState("containerA"),
- stoppedState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "all stopped with restart always",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- stoppedState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "mixed state #1 with restart always",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- },
- },
- },
- v1.PodPending,
- "mixed state #2 with restart always",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- waitingState("containerB"),
- },
- },
- },
- v1.PodPending,
- "mixed state #3 with restart always",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- waitingStateWithLastTermination("containerB"),
- },
- },
- },
- v1.PodRunning,
- "backoff crashloop container with restart always",
- },
- }
- for _, test := range tests {
- status := getPhase(&test.pod.Spec, test.pod.Status.ContainerStatuses)
- assert.Equal(t, test.status, status, "[test %s]", test.test)
- }
- }
- func TestPodPhaseWithRestartNever(t *testing.T) {
- desiredState := v1.PodSpec{
- NodeName: "machine",
- Containers: []v1.Container{
- {Name: "containerA"},
- {Name: "containerB"},
- },
- RestartPolicy: v1.RestartPolicyNever,
- }
- tests := []struct {
- pod *v1.Pod
- status v1.PodPhase
- test string
- }{
- {&v1.Pod{Spec: desiredState, Status: v1.PodStatus{}}, v1.PodPending, "waiting"},
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- runningState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "all running with restart never",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- succeededState("containerA"),
- succeededState("containerB"),
- },
- },
- },
- v1.PodSucceeded,
- "all succeeded with restart never",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- failedState("containerA"),
- failedState("containerB"),
- },
- },
- },
- v1.PodFailed,
- "all failed with restart never",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- succeededState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "mixed state #1 with restart never",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- },
- },
- },
- v1.PodPending,
- "mixed state #2 with restart never",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- waitingState("containerB"),
- },
- },
- },
- v1.PodPending,
- "mixed state #3 with restart never",
- },
- }
- for _, test := range tests {
- status := getPhase(&test.pod.Spec, test.pod.Status.ContainerStatuses)
- assert.Equal(t, test.status, status, "[test %s]", test.test)
- }
- }
- func TestPodPhaseWithRestartOnFailure(t *testing.T) {
- desiredState := v1.PodSpec{
- NodeName: "machine",
- Containers: []v1.Container{
- {Name: "containerA"},
- {Name: "containerB"},
- },
- RestartPolicy: v1.RestartPolicyOnFailure,
- }
- tests := []struct {
- pod *v1.Pod
- status v1.PodPhase
- test string
- }{
- {&v1.Pod{Spec: desiredState, Status: v1.PodStatus{}}, v1.PodPending, "waiting"},
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- runningState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "all running with restart onfailure",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- succeededState("containerA"),
- succeededState("containerB"),
- },
- },
- },
- v1.PodSucceeded,
- "all succeeded with restart onfailure",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- failedState("containerA"),
- failedState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "all failed with restart never",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- succeededState("containerB"),
- },
- },
- },
- v1.PodRunning,
- "mixed state #1 with restart onfailure",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- },
- },
- },
- v1.PodPending,
- "mixed state #2 with restart onfailure",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- waitingState("containerB"),
- },
- },
- },
- v1.PodPending,
- "mixed state #3 with restart onfailure",
- },
- {
- &v1.Pod{
- Spec: desiredState,
- Status: v1.PodStatus{
- ContainerStatuses: []v1.ContainerStatus{
- runningState("containerA"),
- waitingStateWithLastTermination("containerB"),
- },
- },
- },
- v1.PodRunning,
- "backoff crashloop container with restart onfailure",
- },
- }
- for _, test := range tests {
- status := getPhase(&test.pod.Spec, test.pod.Status.ContainerStatuses)
- assert.Equal(t, test.status, status, "[test %s]", test.test)
- }
- }
- func TestGetExec(t *testing.T) {
- const (
- podName = "podFoo"
- podNamespace = "nsFoo"
- podUID types.UID = "12345678"
- containerID = "containerFoo"
- tty = true
- )
- var (
- podFullName = kubecontainer.GetPodFullName(podWithUIDNameNs(podUID, podName, podNamespace))
- command = []string{"ls"}
- )
- testcases := []struct {
- description string
- podFullName string
- container string
- expectError bool
- }{{
- description: "success case",
- podFullName: podFullName,
- container: containerID,
- }, {
- description: "no such pod",
- podFullName: "bar" + podFullName,
- container: containerID,
- expectError: true,
- }, {
- description: "no such container",
- podFullName: podFullName,
- container: "containerBar",
- expectError: true,
- }}
- for _, tc := range testcases {
- testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
- defer testKubelet.Cleanup()
- kubelet := testKubelet.kubelet
- testKubelet.fakeRuntime.PodList = []*containertest.FakePod{
- {Pod: &kubecontainer.Pod{
- ID: podUID,
- Name: podName,
- Namespace: podNamespace,
- Containers: []*kubecontainer.Container{
- {Name: containerID,
- ID: kubecontainer.ContainerID{Type: "test", ID: containerID},
- },
- },
- }},
- }
- description := "streaming - " + tc.description
- fakeRuntime := &containertest.FakeStreamingRuntime{FakeRuntime: testKubelet.fakeRuntime}
- kubelet.containerRuntime = fakeRuntime
- kubelet.streamingRuntime = fakeRuntime
- redirect, err := kubelet.GetExec(tc.podFullName, podUID, tc.container, command, remotecommand.Options{})
- if tc.expectError {
- assert.Error(t, err, description)
- } else {
- assert.NoError(t, err, description)
- assert.Equal(t, containertest.FakeHost, redirect.Host, description+": redirect")
- }
- }
- }
- func TestGetPortForward(t *testing.T) {
- const (
- podName = "podFoo"
- podNamespace = "nsFoo"
- podUID types.UID = "12345678"
- port int32 = 5000
- )
- testcases := []struct {
- description string
- podName string
- expectError bool
- }{{
- description: "success case",
- podName: podName,
- }, {
- description: "no such pod",
- podName: "bar",
- expectError: true,
- }}
- for _, tc := range testcases {
- testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
- defer testKubelet.Cleanup()
- kubelet := testKubelet.kubelet
- testKubelet.fakeRuntime.PodList = []*containertest.FakePod{
- {Pod: &kubecontainer.Pod{
- ID: podUID,
- Name: podName,
- Namespace: podNamespace,
- Containers: []*kubecontainer.Container{
- {Name: "foo",
- ID: kubecontainer.ContainerID{Type: "test", ID: "foo"},
- },
- },
- }},
- }
- description := "streaming - " + tc.description
- fakeRuntime := &containertest.FakeStreamingRuntime{FakeRuntime: testKubelet.fakeRuntime}
- kubelet.containerRuntime = fakeRuntime
- kubelet.streamingRuntime = fakeRuntime
- redirect, err := kubelet.GetPortForward(tc.podName, podNamespace, podUID, portforward.V4Options{})
- if tc.expectError {
- assert.Error(t, err, description)
- } else {
- assert.NoError(t, err, description)
- assert.Equal(t, containertest.FakeHost, redirect.Host, description+": redirect")
- }
- }
- }
- func TestHasHostMountPVC(t *testing.T) {
- tests := map[string]struct {
- pvError error
- pvcError error
- expected bool
- podHasPVC bool
- pvcIsHostPath bool
- }{
- "no pvc": {podHasPVC: false, expected: false},
- "error fetching pvc": {
- podHasPVC: true,
- pvcError: fmt.Errorf("foo"),
- expected: false,
- },
- "error fetching pv": {
- podHasPVC: true,
- pvError: fmt.Errorf("foo"),
- expected: false,
- },
- "host path pvc": {
- podHasPVC: true,
- pvcIsHostPath: true,
- expected: true,
- },
- "non host path pvc": {
- podHasPVC: true,
- pvcIsHostPath: false,
- expected: false,
- },
- }
- for k, v := range tests {
- testKubelet := newTestKubelet(t, false)
- defer testKubelet.Cleanup()
- pod := &v1.Pod{
- Spec: v1.PodSpec{},
- }
- volumeToReturn := &v1.PersistentVolume{
- Spec: v1.PersistentVolumeSpec{},
- }
- if v.podHasPVC {
- pod.Spec.Volumes = []v1.Volume{
- {
- VolumeSource: v1.VolumeSource{
- PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{},
- },
- },
- }
- if v.pvcIsHostPath {
- volumeToReturn.Spec.PersistentVolumeSource = v1.PersistentVolumeSource{
- HostPath: &v1.HostPathVolumeSource{},
- }
- }
- }
- testKubelet.fakeKubeClient.AddReactor("get", "persistentvolumeclaims", func(action core.Action) (bool, runtime.Object, error) {
- return true, &v1.PersistentVolumeClaim{
- Spec: v1.PersistentVolumeClaimSpec{
- VolumeName: "foo",
- },
- }, v.pvcError
- })
- testKubelet.fakeKubeClient.AddReactor("get", "persistentvolumes", func(action core.Action) (bool, runtime.Object, error) {
- return true, volumeToReturn, v.pvError
- })
- actual := testKubelet.kubelet.hasHostMountPVC(pod)
- if actual != v.expected {
- t.Errorf("%s expected %t but got %t", k, v.expected, actual)
- }
- }
- }
- func TestHasNonNamespacedCapability(t *testing.T) {
- createPodWithCap := func(caps []v1.Capability) *v1.Pod {
- pod := &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{{}},
- },
- }
- if len(caps) > 0 {
- pod.Spec.Containers[0].SecurityContext = &v1.SecurityContext{
- Capabilities: &v1.Capabilities{
- Add: caps,
- },
- }
- }
- return pod
- }
- nilCaps := createPodWithCap([]v1.Capability{v1.Capability("foo")})
- nilCaps.Spec.Containers[0].SecurityContext = nil
- tests := map[string]struct {
- pod *v1.Pod
- expected bool
- }{
- "nil security contxt": {createPodWithCap(nil), false},
- "nil caps": {nilCaps, false},
- "namespaced cap": {createPodWithCap([]v1.Capability{v1.Capability("foo")}), false},
- "non-namespaced cap MKNOD": {createPodWithCap([]v1.Capability{v1.Capability("MKNOD")}), true},
- "non-namespaced cap SYS_TIME": {createPodWithCap([]v1.Capability{v1.Capability("SYS_TIME")}), true},
- "non-namespaced cap SYS_MODULE": {createPodWithCap([]v1.Capability{v1.Capability("SYS_MODULE")}), true},
- }
- for k, v := range tests {
- actual := hasNonNamespacedCapability(v.pod)
- if actual != v.expected {
- t.Errorf("%s failed, expected %t but got %t", k, v.expected, actual)
- }
- }
- }
- func TestHasHostVolume(t *testing.T) {
- pod := &v1.Pod{
- Spec: v1.PodSpec{
- Volumes: []v1.Volume{
- {
- VolumeSource: v1.VolumeSource{
- HostPath: &v1.HostPathVolumeSource{},
- },
- },
- },
- },
- }
- result := hasHostVolume(pod)
- if !result {
- t.Errorf("expected host volume to enable host user namespace")
- }
- pod.Spec.Volumes[0].VolumeSource.HostPath = nil
- result = hasHostVolume(pod)
- if result {
- t.Errorf("expected nil host volume to not enable host user namespace")
- }
- }
- func TestHasHostNamespace(t *testing.T) {
- tests := map[string]struct {
- ps v1.PodSpec
- expected bool
- }{
- "nil psc": {
- ps: v1.PodSpec{},
- expected: false},
- "host pid true": {
- ps: v1.PodSpec{
- HostPID: true,
- SecurityContext: &v1.PodSecurityContext{},
- },
- expected: true,
- },
- "host ipc true": {
- ps: v1.PodSpec{
- HostIPC: true,
- SecurityContext: &v1.PodSecurityContext{},
- },
- expected: true,
- },
- "host net true": {
- ps: v1.PodSpec{
- HostNetwork: true,
- SecurityContext: &v1.PodSecurityContext{},
- },
- expected: true,
- },
- "no host ns": {
- ps: v1.PodSpec{
- SecurityContext: &v1.PodSecurityContext{},
- },
- expected: false,
- },
- }
- for k, v := range tests {
- pod := &v1.Pod{
- Spec: v.ps,
- }
- actual := hasHostNamespace(pod)
- if actual != v.expected {
- t.Errorf("%s failed, expected %t but got %t", k, v.expected, actual)
- }
- }
- }
- func TestTruncatePodHostname(t *testing.T) {
- for c, test := range map[string]struct {
- input string
- output string
- }{
- "valid hostname": {
- input: "test.pod.hostname",
- output: "test.pod.hostname",
- },
- "too long hostname": {
- input: "1234567.1234567.1234567.1234567.1234567.1234567.1234567.1234567.1234567.", // 8*9=72 chars
- output: "1234567.1234567.1234567.1234567.1234567.1234567.1234567.1234567", //8*8-1=63 chars
- },
- "hostname end with .": {
- input: "1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456.1234567.", // 8*9-1=71 chars
- output: "1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456", //8*8-2=62 chars
- },
- "hostname end with -": {
- input: "1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456-1234567.", // 8*9-1=71 chars
- output: "1234567.1234567.1234567.1234567.1234567.1234567.1234567.123456", //8*8-2=62 chars
- },
- } {
- t.Logf("TestCase: %q", c)
- output, err := truncatePodHostnameIfNeeded("test-pod", test.input)
- assert.NoError(t, err)
- assert.Equal(t, test.output, output)
- }
- }
|