123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- /*
- Copyright 2017 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 proxy
- import (
- "strings"
- "testing"
- "time"
- apps "k8s.io/api/apps/v1"
- apierrors "k8s.io/apimachinery/pkg/api/errors"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- clientsetfake "k8s.io/client-go/kubernetes/fake"
- clientsetscheme "k8s.io/client-go/kubernetes/scheme"
- core "k8s.io/client-go/testing"
- kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
- "k8s.io/kubernetes/cmd/kubeadm/app/constants"
- kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
- configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config"
- api "k8s.io/kubernetes/pkg/apis/core"
- kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
- "k8s.io/utils/pointer"
- )
- func TestCreateServiceAccount(t *testing.T) {
- tests := []struct {
- name string
- createErr error
- expectErr bool
- }{
- {
- "error-free case",
- nil,
- false,
- },
- {
- "duplication errors should be ignored",
- apierrors.NewAlreadyExists(api.Resource(""), ""),
- false,
- },
- {
- "unexpected errors should be returned",
- apierrors.NewUnauthorized(""),
- true,
- },
- }
- for _, tc := range tests {
- t.Run(tc.name, func(t *testing.T) {
- client := clientsetfake.NewSimpleClientset()
- if tc.createErr != nil {
- client.PrependReactor("create", "serviceaccounts", func(action core.Action) (bool, runtime.Object, error) {
- return true, nil, tc.createErr
- })
- }
- err := CreateServiceAccount(client)
- if tc.expectErr {
- if err == nil {
- t.Errorf("CreateServiceAccounts(%s) wanted err, got nil", tc.name)
- }
- return
- } else if !tc.expectErr && err != nil {
- t.Errorf("CreateServiceAccounts(%s) returned unexpected err: %v", tc.name, err)
- }
- wantResourcesCreated := 1
- if len(client.Actions()) != wantResourcesCreated {
- t.Errorf("CreateServiceAccounts(%s) should have made %d actions, but made %d", tc.name, wantResourcesCreated, len(client.Actions()))
- }
- for _, action := range client.Actions() {
- if action.GetVerb() != "create" || action.GetResource().Resource != "serviceaccounts" {
- t.Errorf("CreateServiceAccounts(%s) called [%v %v], but wanted [create serviceaccounts]",
- tc.name, action.GetVerb(), action.GetResource().Resource)
- }
- }
- })
- }
- }
- func TestCompileManifests(t *testing.T) {
- var tests = []struct {
- name string
- manifest string
- data interface{}
- }{
- {
- name: "KubeProxyConfigMap19",
- manifest: KubeProxyConfigMap19,
- data: struct {
- ControlPlaneEndpoint, ProxyConfig, ProxyConfigMap, ProxyConfigMapKey string
- }{
- ControlPlaneEndpoint: "foo",
- ProxyConfig: " bindAddress: 0.0.0.0\n clusterCIDR: 192.168.1.1\n enableProfiling: false",
- ProxyConfigMap: "bar",
- ProxyConfigMapKey: "baz",
- },
- },
- {
- name: "KubeProxyDaemonSet19",
- manifest: KubeProxyDaemonSet19,
- data: struct{ Image, ProxyConfigMap, ProxyConfigMapKey string }{
- Image: "foo",
- ProxyConfigMap: "bar",
- ProxyConfigMapKey: "baz",
- },
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t *testing.T) {
- _, err := kubeadmutil.ParseTemplate(rt.manifest, rt.data)
- if err != nil {
- t.Errorf("unexpected ParseTemplate faiure: %+v", err)
- }
- })
- }
- }
- func TestEnsureProxyAddon(t *testing.T) {
- type SimulatedError int
- const (
- NoError SimulatedError = iota
- ServiceAccountError
- InvalidControlPlaneEndpoint
- IPv6SetBindAddress
- )
- var testCases = []struct {
- name string
- simError SimulatedError
- expErrString string
- expBindAddr string
- expClusterCIDR string
- }{
- {
- name: "Successful proxy addon",
- simError: NoError,
- expErrString: "",
- expBindAddr: "0.0.0.0",
- expClusterCIDR: "5.6.7.8/24",
- }, {
- name: "Simulated service account error",
- simError: ServiceAccountError,
- expErrString: "error when creating kube-proxy service account",
- expBindAddr: "0.0.0.0",
- expClusterCIDR: "5.6.7.8/24",
- }, {
- name: "IPv6 AdvertiseAddress address",
- simError: IPv6SetBindAddress,
- expErrString: "",
- expBindAddr: "::",
- expClusterCIDR: "2001:101::/96",
- },
- }
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- // Create a fake client and set up default test configuration
- client := clientsetfake.NewSimpleClientset()
- // TODO: Consider using a YAML file instead for this that makes it possible to specify YAML documents for the ComponentConfigs
- controlPlaneConfig := &kubeadmapiv1beta2.InitConfiguration{
- LocalAPIEndpoint: kubeadmapiv1beta2.APIEndpoint{
- AdvertiseAddress: "1.2.3.4",
- BindPort: 1234,
- },
- }
- controlPlaneClusterConfig := &kubeadmapiv1beta2.ClusterConfiguration{
- Networking: kubeadmapiv1beta2.Networking{
- PodSubnet: "5.6.7.8/24",
- },
- ImageRepository: "someRepo",
- KubernetesVersion: constants.MinimumControlPlaneVersion.String(),
- }
- // Simulate an error if necessary
- switch tc.simError {
- case ServiceAccountError:
- client.PrependReactor("create", "serviceaccounts", func(action core.Action) (bool, runtime.Object, error) {
- return true, nil, apierrors.NewUnauthorized("")
- })
- case InvalidControlPlaneEndpoint:
- controlPlaneConfig.LocalAPIEndpoint.AdvertiseAddress = "1.2.3"
- case IPv6SetBindAddress:
- controlPlaneConfig.LocalAPIEndpoint.AdvertiseAddress = "1:2::3:4"
- controlPlaneClusterConfig.Networking.PodSubnet = "2001:101::/96"
- }
- intControlPlane, err := configutil.DefaultedInitConfiguration(controlPlaneConfig, controlPlaneClusterConfig)
- if err != nil {
- t.Errorf("test failed to convert external to internal version")
- return
- }
- intControlPlane.ComponentConfigs.KubeProxy = &kubeproxyconfig.KubeProxyConfiguration{
- BindAddress: "",
- HealthzBindAddress: "0.0.0.0:10256",
- MetricsBindAddress: "127.0.0.1:10249",
- Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
- MaxPerCore: pointer.Int32Ptr(1),
- Min: pointer.Int32Ptr(1),
- TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
- TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
- },
- }
- // Run dynamic defaulting again as we changed the internal cfg
- if err := configutil.SetInitDynamicDefaults(intControlPlane); err != nil {
- t.Errorf("test failed to set dynamic defaults: %v", err)
- return
- }
- err = EnsureProxyAddon(&intControlPlane.ClusterConfiguration, &intControlPlane.LocalAPIEndpoint, client)
- // Compare actual to expected errors
- actErr := "No error"
- if err != nil {
- actErr = err.Error()
- }
- expErr := "No error"
- if tc.expErrString != "" {
- expErr = tc.expErrString
- }
- if !strings.Contains(actErr, expErr) {
- t.Errorf(
- "%s test failed, expected: %s, got: %s",
- tc.name,
- expErr,
- actErr)
- }
- if intControlPlane.ComponentConfigs.KubeProxy.BindAddress != tc.expBindAddr {
- t.Errorf("%s test failed, expected: %s, got: %s",
- tc.name,
- tc.expBindAddr,
- intControlPlane.ComponentConfigs.KubeProxy.BindAddress)
- }
- if intControlPlane.ComponentConfigs.KubeProxy.ClusterCIDR != tc.expClusterCIDR {
- t.Errorf("%s test failed, expected: %s, got: %s",
- tc.name,
- tc.expClusterCIDR,
- intControlPlane.ComponentConfigs.KubeProxy.ClusterCIDR)
- }
- })
- }
- }
- func TestDaemonSetsHaveSystemNodeCriticalPriorityClassName(t *testing.T) {
- testCases := []struct {
- name string
- manifest string
- data interface{}
- }{
- {
- name: "KubeProxyDaemonSet19",
- manifest: KubeProxyDaemonSet19,
- data: struct{ Image, ProxyConfigMap, ProxyConfigMapKey string }{
- Image: "foo",
- ProxyConfigMap: "foo",
- ProxyConfigMapKey: "foo",
- },
- },
- }
- for _, testCase := range testCases {
- t.Run(testCase.name, func(t *testing.T) {
- daemonSetBytes, _ := kubeadmutil.ParseTemplate(testCase.manifest, testCase.data)
- daemonSet := &apps.DaemonSet{}
- if err := runtime.DecodeInto(clientsetscheme.Codecs.UniversalDecoder(), daemonSetBytes, daemonSet); err != nil {
- t.Errorf("unexpected error: %v", err)
- }
- if daemonSet.Spec.Template.Spec.PriorityClassName != "system-node-critical" {
- t.Errorf("expected to see system-node-critical priority class name. Got %q instead", daemonSet.Spec.Template.Spec.PriorityClassName)
- }
- })
- }
- }
|