123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- /*
- 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 master
- import (
- "reflect"
- "testing"
- corev1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/util/diff"
- "k8s.io/client-go/kubernetes/fake"
- clienttesting "k8s.io/client-go/testing"
- )
- func TestWriteClientCAs(t *testing.T) {
- tests := []struct {
- name string
- hook ClientCARegistrationHook
- preexistingObjs []runtime.Object
- expectedConfigMaps map[string]*corev1.ConfigMap
- expectUpdate bool
- }{
- {
- name: "basic",
- hook: ClientCARegistrationHook{
- ClientCA: []byte("foo"),
- RequestHeaderUsernameHeaders: []string{"alfa", "bravo", "charlie"},
- RequestHeaderGroupHeaders: []string{"delta"},
- RequestHeaderExtraHeaderPrefixes: []string{"echo", "foxtrot"},
- RequestHeaderCA: []byte("bar"),
- RequestHeaderAllowedNames: []string{"first", "second"},
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{
- "extension-apiserver-authentication": {
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "client-ca-file": "foo",
- "requestheader-username-headers": `["alfa","bravo","charlie"]`,
- "requestheader-group-headers": `["delta"]`,
- "requestheader-extra-headers-prefix": `["echo","foxtrot"]`,
- "requestheader-client-ca-file": "bar",
- "requestheader-allowed-names": `["first","second"]`,
- },
- },
- },
- },
- {
- name: "skip extension-apiserver-authentication",
- hook: ClientCARegistrationHook{
- RequestHeaderCA: []byte("bar"),
- RequestHeaderAllowedNames: []string{"first", "second"},
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{
- "extension-apiserver-authentication": {
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "requestheader-username-headers": `null`,
- "requestheader-group-headers": `null`,
- "requestheader-extra-headers-prefix": `null`,
- "requestheader-client-ca-file": "bar",
- "requestheader-allowed-names": `["first","second"]`,
- },
- },
- },
- },
- {
- name: "skip extension-apiserver-authentication",
- hook: ClientCARegistrationHook{
- ClientCA: []byte("foo"),
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{
- "extension-apiserver-authentication": {
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "client-ca-file": "foo",
- },
- },
- },
- },
- {
- name: "empty allowed names",
- hook: ClientCARegistrationHook{
- RequestHeaderCA: []byte("bar"),
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{
- "extension-apiserver-authentication": {
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "requestheader-username-headers": `null`,
- "requestheader-group-headers": `null`,
- "requestheader-extra-headers-prefix": `null`,
- "requestheader-client-ca-file": "bar",
- "requestheader-allowed-names": `null`,
- },
- },
- },
- },
- {
- name: "overwrite extension-apiserver-authentication",
- hook: ClientCARegistrationHook{
- ClientCA: []byte("foo"),
- },
- preexistingObjs: []runtime.Object{
- &corev1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "client-ca-file": "other",
- },
- },
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{
- "extension-apiserver-authentication": {
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "client-ca-file": "foo",
- },
- },
- },
- expectUpdate: true,
- },
- {
- name: "overwrite extension-apiserver-authentication requestheader",
- hook: ClientCARegistrationHook{
- RequestHeaderUsernameHeaders: []string{},
- RequestHeaderGroupHeaders: []string{},
- RequestHeaderExtraHeaderPrefixes: []string{},
- RequestHeaderCA: []byte("bar"),
- RequestHeaderAllowedNames: []string{},
- },
- preexistingObjs: []runtime.Object{
- &corev1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "requestheader-username-headers": `null`,
- "requestheader-group-headers": `null`,
- "requestheader-extra-headers-prefix": `null`,
- "requestheader-client-ca-file": "something",
- "requestheader-allowed-names": `null`,
- },
- },
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{
- "extension-apiserver-authentication": {
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "requestheader-username-headers": `[]`,
- "requestheader-group-headers": `[]`,
- "requestheader-extra-headers-prefix": `[]`,
- "requestheader-client-ca-file": "bar",
- "requestheader-allowed-names": `[]`,
- },
- },
- },
- expectUpdate: true,
- },
- {
- name: "namespace exists",
- hook: ClientCARegistrationHook{
- ClientCA: []byte("foo"),
- },
- preexistingObjs: []runtime.Object{
- &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: metav1.NamespaceSystem}},
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{
- "extension-apiserver-authentication": {
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "client-ca-file": "foo",
- },
- },
- },
- },
- {
- name: "skip on no change",
- hook: ClientCARegistrationHook{
- RequestHeaderUsernameHeaders: []string{},
- RequestHeaderGroupHeaders: []string{},
- RequestHeaderExtraHeaderPrefixes: []string{},
- RequestHeaderCA: []byte("bar"),
- RequestHeaderAllowedNames: []string{},
- },
- preexistingObjs: []runtime.Object{
- &corev1.ConfigMap{
- ObjectMeta: metav1.ObjectMeta{Namespace: metav1.NamespaceSystem, Name: "extension-apiserver-authentication"},
- Data: map[string]string{
- "requestheader-username-headers": `[]`,
- "requestheader-group-headers": `[]`,
- "requestheader-extra-headers-prefix": `[]`,
- "requestheader-client-ca-file": "bar",
- "requestheader-allowed-names": `[]`,
- },
- },
- },
- expectedConfigMaps: map[string]*corev1.ConfigMap{},
- expectUpdate: false,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- client := fake.NewSimpleClientset(test.preexistingObjs...)
- test.hook.tryToWriteClientCAs(client.CoreV1())
- actualConfigMaps, updated := getFinalConfigMaps(client)
- if !reflect.DeepEqual(test.expectedConfigMaps, actualConfigMaps) {
- t.Fatalf("%s: %v", test.name, diff.ObjectReflectDiff(test.expectedConfigMaps, actualConfigMaps))
- }
- if test.expectUpdate != updated {
- t.Fatalf("%s: expected %v, got %v", test.name, test.expectUpdate, updated)
- }
- })
- }
- }
- func getFinalConfigMaps(client *fake.Clientset) (map[string]*corev1.ConfigMap, bool) {
- ret := map[string]*corev1.ConfigMap{}
- updated := false
- for _, action := range client.Actions() {
- if action.Matches("create", "configmaps") {
- obj := action.(clienttesting.CreateAction).GetObject().(*corev1.ConfigMap)
- ret[obj.Name] = obj
- }
- if action.Matches("update", "configmaps") {
- updated = true
- obj := action.(clienttesting.UpdateAction).GetObject().(*corev1.ConfigMap)
- ret[obj.Name] = obj
- }
- }
- return ret, updated
- }
|