@@ -7,6 +7,7 @@ package cluster
77import (
88 "context"
99 "encoding/json"
10+ "fmt"
1011 "testing"
1112 "time"
1213
@@ -310,3 +311,197 @@ func TestInit(t *testing.T) {
310311 })
311312 }
312313}
314+
315+ func TestInitStateRegistryModeSwitch (t * testing.T ) {
316+ tests := []struct {
317+ name string
318+ current state.State
319+ opts InitStateOptions
320+ expected state.State
321+ }{
322+ {
323+ name : "nodeport to proxy resets injector port, port defaults to 5000, and enables mTLS" ,
324+ current : state.State {
325+ RegistryInfo : state.RegistryInfo {
326+ RegistryMode : state .RegistryModeNodePort ,
327+ MTLSStrategy : state .MTLSStrategyNone ,
328+ },
329+ InjectorInfo : state.InjectorInfo {Port : 31999 },
330+ },
331+ opts : InitStateOptions {
332+ RegistryInfo : state.RegistryInfo {RegistryMode : state .RegistryModeProxy },
333+ },
334+ expected : state.State {
335+ RegistryInfo : state.RegistryInfo {
336+ RegistryMode : state .RegistryModeProxy ,
337+ MTLSStrategy : state .MTLSStrategyZarfManaged ,
338+ NodePort : state .ZarfRegistryHostPort ,
339+ },
340+ InjectorInfo : state.InjectorInfo {Port : 0 },
341+ },
342+ },
343+ {
344+ name : "proxy to nodeport resets injector port and corrects out-of-range port" ,
345+ current : state.State {
346+ RegistryInfo : state.RegistryInfo {
347+ RegistryMode : state .RegistryModeProxy ,
348+ MTLSStrategy : state .MTLSStrategyZarfManaged ,
349+ },
350+ InjectorInfo : state.InjectorInfo {Port : 5000 },
351+ },
352+ opts : InitStateOptions {
353+ RegistryInfo : state.RegistryInfo {RegistryMode : state .RegistryModeNodePort },
354+ },
355+ expected : state.State {
356+ RegistryInfo : state.RegistryInfo {
357+ RegistryMode : state .RegistryModeNodePort ,
358+ MTLSStrategy : state .MTLSStrategyNone ,
359+ NodePort : state .ZarfInClusterContainerRegistryNodePort ,
360+ },
361+ InjectorInfo : state.InjectorInfo {Port : 0 },
362+ },
363+ },
364+ {
365+ name : "proxy to nodeport with explicit valid port uses provided port" ,
366+ current : state.State {
367+ RegistryInfo : state.RegistryInfo {
368+ RegistryMode : state .RegistryModeProxy ,
369+ MTLSStrategy : state .MTLSStrategyZarfManaged ,
370+ },
371+ InjectorInfo : state.InjectorInfo {Port : 5000 },
372+ },
373+ opts : InitStateOptions {
374+ RegistryInfo : state.RegistryInfo {
375+ RegistryMode : state .RegistryModeNodePort ,
376+ NodePort : 30500 ,
377+ },
378+ },
379+ expected : state.State {
380+ RegistryInfo : state.RegistryInfo {
381+ RegistryMode : state .RegistryModeNodePort ,
382+ MTLSStrategy : state .MTLSStrategyNone ,
383+ NodePort : 30500 ,
384+ },
385+ InjectorInfo : state.InjectorInfo {Port : 0 },
386+ },
387+ },
388+ {
389+ name : "nodeport to proxy with explicit port uses provided port" ,
390+ current : state.State {
391+ RegistryInfo : state.RegistryInfo {
392+ RegistryMode : state .RegistryModeNodePort ,
393+ MTLSStrategy : state .MTLSStrategyNone ,
394+ },
395+ InjectorInfo : state.InjectorInfo {Port : 31999 },
396+ },
397+ opts : InitStateOptions {
398+ RegistryInfo : state.RegistryInfo {
399+ RegistryMode : state .RegistryModeProxy ,
400+ NodePort : 8080 ,
401+ },
402+ },
403+ expected : state.State {
404+ RegistryInfo : state.RegistryInfo {
405+ RegistryMode : state .RegistryModeProxy ,
406+ MTLSStrategy : state .MTLSStrategyZarfManaged ,
407+ NodePort : 8080 ,
408+ },
409+ InjectorInfo : state.InjectorInfo {Port : 0 },
410+ },
411+ },
412+ {
413+ name : "nodeport to nodeport preserves existing port and injector port" ,
414+ current : state.State {
415+ RegistryInfo : state.RegistryInfo {
416+ RegistryMode : state .RegistryModeNodePort ,
417+ MTLSStrategy : state .MTLSStrategyNone ,
418+ NodePort : 30500 ,
419+ },
420+ InjectorInfo : state.InjectorInfo {Port : 31999 },
421+ },
422+ opts : InitStateOptions {
423+ RegistryInfo : state.RegistryInfo {RegistryMode : state .RegistryModeNodePort },
424+ },
425+ expected : state.State {
426+ RegistryInfo : state.RegistryInfo {
427+ RegistryMode : state .RegistryModeNodePort ,
428+ MTLSStrategy : state .MTLSStrategyNone ,
429+ NodePort : 30500 ,
430+ },
431+ InjectorInfo : state.InjectorInfo {Port : 31999 },
432+ },
433+ },
434+ {
435+ name : "proxy to proxy preserves injector port and refreshes mTLS" ,
436+ current : state.State {
437+ RegistryInfo : state.RegistryInfo {
438+ RegistryMode : state .RegistryModeProxy ,
439+ MTLSStrategy : state .MTLSStrategyZarfManaged ,
440+ },
441+ InjectorInfo : state.InjectorInfo {Port : 5000 },
442+ },
443+ opts : InitStateOptions {
444+ RegistryInfo : state.RegistryInfo {RegistryMode : state .RegistryModeProxy },
445+ },
446+ expected : state.State {
447+ RegistryInfo : state.RegistryInfo {
448+ RegistryMode : state .RegistryModeProxy ,
449+ MTLSStrategy : state .MTLSStrategyZarfManaged ,
450+ },
451+ InjectorInfo : state.InjectorInfo {Port : 5000 },
452+ },
453+ },
454+ }
455+ for _ , tt := range tests {
456+ t .Run (tt .name , func (t * testing.T ) {
457+ ctx := context .Background ()
458+ cs := fake .NewClientset ()
459+ c := & Cluster {
460+ Clientset : cs ,
461+ Watcher : healthchecks .NewImmediateWatcher (status .CurrentStatus ),
462+ }
463+
464+ // Seed the fake cluster with the minimum objects InitState expects:
465+ // a node, the zarf namespace, the state secret, and the IP family service.
466+ tt .current .Distro = DistroIsK3d
467+ tt .current .RegistryInfo .PushUsername = "push-user"
468+ tt .current .RegistryInfo .PullUsername = "pull-user"
469+ tt .current .RegistryInfo .Secret = "secret"
470+ if tt .current .RegistryInfo .NodePort == 0 {
471+ tt .current .RegistryInfo .NodePort = state .ZarfInClusterContainerRegistryNodePort
472+ }
473+ tt .current .RegistryInfo .Address = fmt .Sprintf ("127.0.0.1:%d" , tt .current .RegistryInfo .NodePort )
474+ currentData , err := json .Marshal (tt .current )
475+ require .NoError (t , err )
476+
477+ _ , err = cs .CoreV1 ().Nodes ().Create (ctx , & corev1.Node {
478+ ObjectMeta : metav1.ObjectMeta {Name : "node" },
479+ }, metav1.CreateOptions {})
480+ require .NoError (t , err )
481+ _ , err = cs .CoreV1 ().Namespaces ().Create (ctx , & corev1.Namespace {
482+ ObjectMeta : metav1.ObjectMeta {Name : state .ZarfNamespaceName },
483+ }, metav1.CreateOptions {})
484+ require .NoError (t , err )
485+ _ , err = cs .CoreV1 ().Secrets (state .ZarfNamespaceName ).Create (ctx , & corev1.Secret {
486+ ObjectMeta : metav1.ObjectMeta {Namespace : state .ZarfNamespaceName , Name : state .ZarfStateSecretName },
487+ Data : map [string ][]byte {state .ZarfStateDataKey : currentData },
488+ }, metav1.CreateOptions {})
489+ require .NoError (t , err )
490+ _ , err = cs .CoreV1 ().Services (state .ZarfNamespaceName ).Create (ctx , & corev1.Service {
491+ ObjectMeta : metav1.ObjectMeta {Name : "zarf-ip-family-test" , Namespace : state .ZarfNamespaceName },
492+ Spec : corev1.ServiceSpec {IPFamilies : []corev1.IPFamily {corev1 .IPv4Protocol }},
493+ }, metav1.CreateOptions {})
494+ require .NoError (t , err )
495+
496+ result , err := c .InitState (ctx , tt .opts )
497+ require .NoError (t , err )
498+
499+ require .Equal (t , tt .expected .RegistryInfo .RegistryMode , result .RegistryInfo .RegistryMode )
500+ require .Equal (t , tt .expected .InjectorInfo .Port , result .InjectorInfo .Port )
501+ require .Equal (t , tt .expected .RegistryInfo .MTLSStrategy , result .RegistryInfo .MTLSStrategy )
502+ if tt .expected .RegistryInfo .NodePort != 0 {
503+ require .Equal (t , tt .expected .RegistryInfo .NodePort , result .RegistryInfo .NodePort )
504+ }
505+ })
506+ }
507+ }
0 commit comments