k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/controlplane/controller/defaultservicecidr/default_servicecidr_controller_test.go (about) 1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package defaultservicecidr 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/google/go-cmp/cmp" 24 networkingapiv1alpha1 "k8s.io/api/networking/v1alpha1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/client-go/informers" 27 "k8s.io/client-go/kubernetes/fake" 28 k8stesting "k8s.io/client-go/testing" 29 "k8s.io/client-go/tools/record" 30 "k8s.io/utils/ptr" 31 ) 32 33 const ( 34 defaultIPv4CIDR = "10.16.0.0/16" 35 defaultIPv6CIDR = "2001:db8::/64" 36 ) 37 38 func newController(t *testing.T, objects []*networkingapiv1alpha1.ServiceCIDR) (*fake.Clientset, *Controller) { 39 client := fake.NewSimpleClientset() 40 41 informerFactory := informers.NewSharedInformerFactory(client, 0) 42 serviceCIDRInformer := informerFactory.Networking().V1alpha1().ServiceCIDRs() 43 44 store := serviceCIDRInformer.Informer().GetStore() 45 for _, obj := range objects { 46 err := store.Add(obj) 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 } 52 c := &Controller{ 53 client: client, 54 interval: time.Second, 55 cidrs: []string{defaultIPv4CIDR, defaultIPv6CIDR}, 56 eventRecorder: record.NewFakeRecorder(100), 57 serviceCIDRLister: serviceCIDRInformer.Lister(), 58 serviceCIDRsSynced: func() bool { return true }, 59 } 60 61 return client, c 62 } 63 64 func TestControllerSync(t *testing.T) { 65 testCases := []struct { 66 name string 67 cidrs []*networkingapiv1alpha1.ServiceCIDR 68 actions [][]string // verb and resource 69 }{ 70 { 71 name: "no existing service CIDRs", 72 actions: [][]string{{"create", "servicecidrs"}, {"patch", "servicecidrs"}}, 73 }, 74 { 75 name: "existing default service CIDR update Ready condition", 76 cidrs: []*networkingapiv1alpha1.ServiceCIDR{ 77 { 78 ObjectMeta: metav1.ObjectMeta{ 79 Name: DefaultServiceCIDRName, 80 }, 81 Spec: networkingapiv1alpha1.ServiceCIDRSpec{ 82 CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR}, 83 }, 84 }, 85 }, 86 actions: [][]string{{"patch", "servicecidrs"}}, 87 }, 88 { 89 name: "existing default service CIDR not matching cidrs", 90 cidrs: []*networkingapiv1alpha1.ServiceCIDR{ 91 { 92 ObjectMeta: metav1.ObjectMeta{ 93 Name: DefaultServiceCIDRName, 94 }, 95 Spec: networkingapiv1alpha1.ServiceCIDRSpec{ 96 CIDRs: []string{"fd00::/112"}, 97 }, 98 }, 99 }, 100 }, 101 { 102 name: "existing default service CIDR not ready", 103 cidrs: []*networkingapiv1alpha1.ServiceCIDR{ 104 { 105 ObjectMeta: metav1.ObjectMeta{ 106 Name: DefaultServiceCIDRName, 107 }, 108 Spec: networkingapiv1alpha1.ServiceCIDRSpec{ 109 CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR}, 110 }, 111 Status: networkingapiv1alpha1.ServiceCIDRStatus{ 112 Conditions: []metav1.Condition{ 113 { 114 Type: string(networkingapiv1alpha1.ServiceCIDRConditionReady), 115 Status: metav1.ConditionFalse, 116 }, 117 }, 118 }, 119 }, 120 }, 121 }, 122 { 123 name: "existing default service CIDR being deleted", 124 cidrs: []*networkingapiv1alpha1.ServiceCIDR{ 125 { 126 ObjectMeta: metav1.ObjectMeta{ 127 Name: DefaultServiceCIDRName, 128 DeletionTimestamp: ptr.To(metav1.Now()), 129 }, 130 Spec: networkingapiv1alpha1.ServiceCIDRSpec{ 131 CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR}, 132 }, 133 }, 134 }, 135 }, 136 { 137 name: "existing service CIDRs but not default", 138 cidrs: []*networkingapiv1alpha1.ServiceCIDR{ 139 { 140 ObjectMeta: metav1.ObjectMeta{ 141 Name: "non-default-cidr", 142 }, 143 Spec: networkingapiv1alpha1.ServiceCIDRSpec{ 144 CIDRs: []string{defaultIPv4CIDR, defaultIPv6CIDR}, 145 }, 146 }, 147 }, 148 actions: [][]string{{"create", "servicecidrs"}, {"patch", "servicecidrs"}}, 149 }, 150 } 151 152 for _, tc := range testCases { 153 t.Run(tc.name, func(t *testing.T) { 154 client, controller := newController(t, tc.cidrs) 155 controller.sync() 156 expectAction(t, client.Actions(), tc.actions) 157 }) 158 } 159 } 160 161 func expectAction(t *testing.T, actions []k8stesting.Action, expected [][]string) { 162 t.Helper() 163 if len(actions) != len(expected) { 164 t.Fatalf("Expected at least %d actions, got %d \ndiff: %v", len(expected), len(actions), cmp.Diff(expected, actions)) 165 } 166 167 for i, action := range actions { 168 verb := expected[i][0] 169 if action.GetVerb() != verb { 170 t.Errorf("Expected action %d verb to be %s, got %s", i, verb, action.GetVerb()) 171 } 172 resource := expected[i][1] 173 if action.GetResource().Resource != resource { 174 t.Errorf("Expected action %d resource to be %s, got %s", i, resource, action.GetResource().Resource) 175 } 176 } 177 }