@@ -29,10 +29,13 @@ import (
2929 "github.com/openshift/api/annotations"
3030 kubecontrolplanev1 "github.com/openshift/api/kubecontrolplane/v1"
3131 operatorv1 "github.com/openshift/api/operator/v1"
32+ "github.com/openshift/cluster-kube-apiserver-operator/bindata"
3233 "github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/operatorclient"
3334 "github.com/openshift/library-go/pkg/operator/events"
3435 "github.com/openshift/library-go/pkg/operator/resource/resourcemerge"
36+ "github.com/openshift/library-go/pkg/operator/resource/resourceread"
3537 "github.com/stretchr/testify/require"
38+ clientgotesting "k8s.io/client-go/testing"
3639)
3740
3841var codec = scheme .Codecs .LegacyCodec (scheme .Scheme .PrioritizedVersionsAllGroups ()... )
@@ -1217,3 +1220,95 @@ func generateTemporaryCertificate() (certPEM []byte, err error) {
12171220
12181221 return certPEM , nil
12191222}
1223+
1224+ // TestEnsureKubeAPIServerExtensionAuthenticationCA tests the behavior of ensureKubeAPIServerExtensionAuthenticationCA
1225+ func TestEnsureKubeAPIServerExtensionAuthenticationCA (t * testing.T ) {
1226+ ctx := context .Background ()
1227+ recorder := events .NewInMemoryRecorder ("test" , clock.RealClock {})
1228+
1229+ t .Run ("configmap not found (Get error)" , func (t * testing.T ) {
1230+ // Create a fake client with no configmap in kube-system
1231+ client := fake .NewSimpleClientset ()
1232+ err := ensureKubeAPIServerExtensionAuthenticationCA (ctx , client .CoreV1 (), recorder )
1233+ if err != nil {
1234+ t .Fatalf ("expected nil error when configmap is missing, got: %v" , err )
1235+ }
1236+ })
1237+
1238+ t .Run ("configmap exists but missing annotations, update succeeds" , func (t * testing.T ) {
1239+ // Create a configmap without annotations
1240+ cm := & corev1.ConfigMap {
1241+ ObjectMeta : metav1.ObjectMeta {
1242+ Name : "extension-apiserver-authentication" ,
1243+ Namespace : "kube-system" ,
1244+ },
1245+ }
1246+ client := fake .NewSimpleClientset (cm )
1247+ err := ensureKubeAPIServerExtensionAuthenticationCA (ctx , client .CoreV1 (), recorder )
1248+ if err != nil {
1249+ t .Fatalf ("expected nil error after update, got: %v" , err )
1250+ }
1251+ updatedCM , err := client .CoreV1 ().ConfigMaps ("kube-system" ).Get (ctx , "extension-apiserver-authentication" , metav1.GetOptions {})
1252+ if err != nil {
1253+ t .Fatalf ("failed to get updated configmap: %v" , err )
1254+ }
1255+ if updatedCM .Annotations == nil || updatedCM .Annotations [annotations .OpenShiftComponent ] != "kube-apiserver" {
1256+ t .Fatalf ("expected annotation not set, got: %v" , updatedCM .Annotations )
1257+ }
1258+ })
1259+
1260+ t .Run ("configmap exists with correct annotations, no update needed" , func (t * testing.T ) {
1261+ required := resourceread .ReadConfigMapV1OrDie (bindata .MustAsset ("assets/kube-apiserver/extension-apiserver-authentication-cm.yaml" ))
1262+
1263+ // Create a configmap with the expected annotation already present
1264+ cm := & corev1.ConfigMap {
1265+ ObjectMeta : metav1.ObjectMeta {
1266+ Name : "extension-apiserver-authentication" ,
1267+ Namespace : "kube-system" ,
1268+ Annotations : required .Annotations ,
1269+ },
1270+ }
1271+ client := fake .NewSimpleClientset (cm )
1272+ err := ensureKubeAPIServerExtensionAuthenticationCA (ctx , client .CoreV1 (), recorder )
1273+ if err != nil {
1274+ t .Fatalf ("expected nil error when annotations are already correct, got: %v" , err )
1275+ }
1276+
1277+ // Check that client only did one action)
1278+ if len (client .Actions ()) != 1 {
1279+ t .Fatalf ("expected one action, got: %v" , client .Actions ())
1280+ }
1281+ action := client .Actions ()[0 ]
1282+ if action .GetVerb () != "get" {
1283+ t .Fatalf ("expected get action, got: %v" , action )
1284+ }
1285+ getAction := action .(clientgotesting.GetAction )
1286+ if getAction .GetName () != "extension-apiserver-authentication" {
1287+ t .Fatalf ("expected get action for configmap 'extension-apiserver-authentication', got: %v" , getAction )
1288+ }
1289+ if getAction .GetNamespace () != "kube-system" {
1290+ t .Fatalf ("expected get action for namespace 'kube-system', got: %v" , getAction )
1291+ }
1292+ })
1293+
1294+ t .Run ("update failure propagates error" , func (t * testing.T ) {
1295+ // Create a configmap without annotations
1296+ cm := & corev1.ConfigMap {
1297+ ObjectMeta : metav1.ObjectMeta {
1298+ Name : "extension-apiserver-authentication" ,
1299+ Namespace : "kube-system" ,
1300+ },
1301+ }
1302+ client := fake .NewSimpleClientset (cm )
1303+
1304+ // Inject reactor to simulate update failure
1305+ client .Fake .PrependReactor ("update" , "configmaps" , func (action clientgotesting.Action ) (bool , runtime.Object , error ) {
1306+ return true , nil , fmt .Errorf ("simulated update failure" )
1307+ })
1308+
1309+ err := ensureKubeAPIServerExtensionAuthenticationCA (ctx , client .CoreV1 (), recorder )
1310+ if err == nil || ! strings .Contains (err .Error (), "simulated update failure" ) {
1311+ t .Fatalf ("expected update failure error, got: %v" , err )
1312+ }
1313+ })
1314+ }
0 commit comments