55 "time"
66
77 . "github.com/onsi/ginkgo"
8- . "github.com/onsi/gomega"
98 . "sigs.k8s.io/multi-tenancy/incubator/hnc/pkg/testutils"
109)
1110
@@ -104,27 +103,30 @@ var _ = Describe("Quickstart", func() {
104103 })
105104
106105 It ("Should intergrate hierarchical network policy" , func (){
107- GinkgoT ().Log ("WARNING: IF THIS TEST FAILS, PLEASE CHECK THAT THE NETWORK POLICY IS ENABLED ON THE TEST CLUSTER" )
108-
109106 MustRun ("kubectl create ns" , nsOrg )
110107 MustRun ("kubectl hns create" , nsTeamA , "-n" , nsOrg )
111108 MustRun ("kubectl hns create" , nsTeamB , "-n" , nsOrg )
112109 MustRun ("kubectl hns create" , nsService1 , "-n" , nsTeamA )
113110 MustRun ("kubectl hns create" , nsService2 , "-n" , nsTeamA )
114111 // create a web service s2 in namespace service-2, and a client pod client-s1 in namespace service-1 that can access this web service
115112 MustRun ("kubectl run s2 -n" , nsService2 , "--image=nginx --restart=Never --expose --port 80" )
116- clientArgs := "-i --image=alpine --restart=Never --rm -- sh -c"
117- cmdln := "\" wget -qO- --timeout 2 http://s2.service-2\" "
118- // at least 20 seconds is needed here from experiments
119- RunShouldContain ("Welcome to nginx!" , 20 ,
120- "kubectl run client -n" , nsService1 , clientArgs , cmdln )
121- RunShouldContain ("Welcome to nginx!" , cleanupTimeout ,
122- "kubectl run client -n" , nsTeamA , clientArgs , cmdln )
123- RunShouldContain ("Welcome to nginx!" , cleanupTimeout ,
124- "kubectl run client -n" , nsTeamB , clientArgs , cmdln )
125-
126- // create a default network policy that blocks any ingress from other namespaces
127- policy := `# temp file created by quickstart_test.go
113+
114+ // Ensure that we can access the service from various other namespaces
115+ const (
116+ clientCmd = "kubectl run client -n"
117+ alpineArgs = "-i --image=alpine --restart=Never --rm -- sh -c"
118+
119+ // These need to be separate from alpineArgs because RunCommand only understands quoted args
120+ // if the double-quotes appears at the beginning and end of a single string.
121+ wgetArgs = "\" wget -qO- --timeout 2 http://s2.service-2\" "
122+ )
123+ // Up to 20 seconds is needed for the service to first come up from experiments
124+ RunShouldContain ("Welcome to nginx!" , 20 , clientCmd , nsService1 , alpineArgs , wgetArgs )
125+ RunShouldContain ("Welcome to nginx!" , defTimeout , clientCmd , nsTeamA , alpineArgs , wgetArgs )
126+ RunShouldContain ("Welcome to nginx!" , defTimeout , clientCmd , nsTeamB , alpineArgs , wgetArgs )
127+
128+ // create a default network policy in the root namespace that blocks any ingress from other namespaces
129+ policy := `# quickstart_test.go: netpol to block access across namespaces
128130kind: NetworkPolicy
129131apiVersion: networking.k8s.io/v1
130132metadata:
@@ -137,33 +139,35 @@ spec:
137139 - from:
138140 - podSelector: {}`
139141
140- filename := WriteTempFile (policy )
141- defer RemoveFile (filename )
142- MustRun ("kubectl apply -f" , filename )
143- // ensure this policy can be propagated to its descendants
142+ MustApplyYAML (policy )
143+ // Enable propagation for netpols and wait for it to get propagated at least to service-1
144144 MustRun ("kubectl hns config set-resource networkpolicies --group networking.k8s.io --mode Propagate --force" )
145- expected := "deny-from-other-namespaces"
146- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsOrg )
147- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsTeamA )
148- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsTeamB )
149- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsService1 )
150- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsService2 )
145+ RunShouldContain ("deny-from-other-namespaces" , defTimeout , "kubectl get netpol -n" , nsService1 )
151146
152147 // Now we’ll see that we can no longer access service-2 from the client in service-1. If we can,
153148 // that probably means that network policies aren't enabled on this cluster (e.g. Kind, GKE by
154149 // default) and we should skip the rest of this test.
155- netpolTestStdout := ""
156- Eventually (func () error {
157- stdout , err := RunCommand ("kubectl run client -n" , nsService1 , clientArgs , cmdln )
158- netpolTestStdout = stdout
159- return err
160- }).Should (Succeed ())
161- if ! strings .Contains (netpolTestStdout , "wget: download timed out" ) {
150+ //
151+ // The standard matching functions won't work here because we're looking for a particular error
152+ // string, but we don't want to fail if we've found it. So use the default timeout (2s) by
153+ // trying up to three times with a 1s gap in between.
154+ netpolWorks := false
155+ for i := 0 ; ! netpolWorks && i < 3 ; i ++ {
156+ // This command will return a non-nil error if it works correctly
157+ stdout , _ := RunCommand (clientCmd , nsService1 , alpineArgs , wgetArgs )
158+ if strings .Contains (stdout , "wget: download timed out" ) {
159+ netpolWorks = true
160+ }
161+ time .Sleep (1 * time .Second )
162+ }
163+ if ! netpolWorks {
162164 Skip ("Basic network policies don't appear to be working; skipping the netpol quickstart" )
163165 }
164166
165- // create a second network policy that will allow all namespaces within team-a to be able to communicate with each other
166- policy = `# temp file created by quickstart_test.go
167+ // create a second network policy that will allow all namespaces within team-a to be able to
168+ // communicate with each other, and wait for it to be propagated to the descendant we want to
169+ // test.
170+ policy = `# quickstart_test.go: netpol to allow communication within team-a subtree
167171kind: NetworkPolicy
168172apiVersion: networking.k8s.io/v1
169173metadata:
@@ -178,21 +182,12 @@ spec:
178182 matchExpressions:
179183 - key: 'team-a.tree.hnc.x-k8s.io/depth'
180184 operator: Exists`
181-
182- filename2 := WriteTempFile (policy )
183- defer RemoveFile (filename2 )
184- MustRun ("kubectl apply -f" , filename2 )
185-
186- expected = "allow-team-a"
187- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsTeamA )
188- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsService1 )
189- RunShouldContain (expected , defTimeout , "kubectl get netpol -n" , nsService2 )
185+ MustApplyYAML (policy )
186+ RunShouldContain ("allow-team-a" , defTimeout , "kubectl get netpol -n" , nsService1 )
190187
191188 // Now, we can access the service from other namespaces in team-a, but not outside of it:
192- RunShouldContain ("Welcome to nginx!" , cleanupTimeout ,
193- "kubectl run client -n" , nsService1 , clientArgs , cmdln )
194- RunErrorShouldContain ("wget: download timed out" , cleanupTimeout ,
195- "kubectl run client -n" , nsTeamB , clientArgs , cmdln )
189+ RunShouldContain ("Welcome to nginx!" , defTimeout , clientCmd , nsService1 , alpineArgs , wgetArgs )
190+ RunErrorShouldContain ("wget: download timed out" , defTimeout , clientCmd , nsTeamB , alpineArgs , wgetArgs )
196191 })
197192
198193 It ("Should create and delete subnamespaces" , func (){
0 commit comments