|
| 1 | +// Copyright 2018 Red Hat, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package kubernetes |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/coreos/mantle/kola/cluster" |
| 23 | + "github.com/coreos/mantle/kola/register" |
| 24 | + "github.com/coreos/mantle/platform/conf" |
| 25 | + "github.com/coreos/mantle/util" |
| 26 | + |
| 27 | + "github.com/vincent-petithory/dataurl" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + kubeletConfigYAML = dataurl.Escape([]byte(`kind: KubeletConfiguration |
| 32 | +apiVersion: kubelet.config.k8s.io/v1beta1 |
| 33 | +clusterDomain: kube.local |
| 34 | +clusterDNS: [10.254.0.10] |
| 35 | +cgroupDriver: systemd |
| 36 | +staticPodPath: /etc/kubernetes/manifests/ |
| 37 | +authorization: |
| 38 | + mode: AlwaysAllow |
| 39 | +authentication: |
| 40 | + anonymous: |
| 41 | + enabled: true |
| 42 | + webhook: |
| 43 | + enabled: false`)) |
| 44 | + |
| 45 | + staticPodYAML = dataurl.Escape([]byte(`apiVersion: v1 |
| 46 | +kind: Pod |
| 47 | +metadata: |
| 48 | + name: static-web |
| 49 | + labels: |
| 50 | + role: myrole |
| 51 | +spec: |
| 52 | + containers: |
| 53 | + - name: web |
| 54 | + image: nginx |
| 55 | + ports: |
| 56 | + - name: web |
| 57 | + containerPort: 80 |
| 58 | + hostPort: 80`)) |
| 59 | + |
| 60 | + kubeletUnit = `[Unit] |
| 61 | +Description=Kubernetes Kubelet |
| 62 | +Requires=docker.service |
| 63 | +After=docker.service |
| 64 | +
|
| 65 | +[Service] |
| 66 | +ExecStart=/usr/bin/hyperkube kubelet --config /etc/kubernetes/kubeletconfig |
| 67 | +
|
| 68 | +Restart=always |
| 69 | +RestartSec=10 |
| 70 | +
|
| 71 | +[Install] |
| 72 | +WantedBy=multi-user.target` |
| 73 | + |
| 74 | + staticPodIgnitionConfig = conf.Ignition(fmt.Sprintf(`{ |
| 75 | + "ignition": { |
| 76 | + "config": {}, |
| 77 | + "security": { |
| 78 | + "tls": {} |
| 79 | + }, |
| 80 | + "timeouts": {}, |
| 81 | + "version": "2.2.0" |
| 82 | + }, |
| 83 | + "networkd": {}, |
| 84 | + "passwd": {}, |
| 85 | + "storage": { |
| 86 | + "files": [ |
| 87 | + { |
| 88 | + "contents": { |
| 89 | + "source": "data:,%s", |
| 90 | + "verification": {} |
| 91 | + }, |
| 92 | + "filesystem": "root", |
| 93 | + "mode": 420, |
| 94 | + "path": "/etc/kubernetes/kubeletconfig" |
| 95 | + }, |
| 96 | + { |
| 97 | + "contents": { |
| 98 | + "source": "data:,%s", |
| 99 | + "verification": {} |
| 100 | + }, |
| 101 | + "filesystem": "root", |
| 102 | + "mode": 420, |
| 103 | + "path": "/etc/kubernetes/manifests/static-pod.yaml" |
| 104 | + } |
| 105 | + ] |
| 106 | + }, |
| 107 | + "systemd": { |
| 108 | + "units": [ |
| 109 | + { |
| 110 | + "contents": %q, |
| 111 | + "enabled": true, |
| 112 | + "name": "kubelet.service" |
| 113 | + } |
| 114 | + ] |
| 115 | + } |
| 116 | +}`, kubeletConfigYAML, staticPodYAML, kubeletUnit)) |
| 117 | +) |
| 118 | + |
| 119 | +func init() { |
| 120 | + // Test: verify kubelet can schedule a static pod |
| 121 | + register.Register(®ister.Test{ |
| 122 | + Name: "kubernetes.kubelet.static-pod", |
| 123 | + Run: kubeletStaticPodTest, |
| 124 | + ClusterSize: 1, |
| 125 | + UserData: staticPodIgnitionConfig, |
| 126 | + ExcludePlatforms: []string{"qemu"}, |
| 127 | + Distros: []string{"rhcos"}, |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +func kubeletStaticPodTest(c cluster.TestCluster) { |
| 132 | + m := c.Machines()[0] |
| 133 | + |
| 134 | + podIsRunning := func() error { |
| 135 | + b, err := c.SSH(m, `curl -f http://localhost 2>/dev/null`) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + if !bytes.Contains(b, []byte("nginx")) { |
| 140 | + return fmt.Errorf("nginx pod is not running %s", b) |
| 141 | + } |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + if err := util.Retry(10, 10*time.Second, podIsRunning); err != nil { |
| 146 | + c.Fatal(err) |
| 147 | + } |
| 148 | +} |
0 commit comments