@@ -5,6 +5,7 @@ package kappcontroller
5
5
6
6
import (
7
7
"fmt"
8
+ "os"
8
9
"strings"
9
10
"testing"
10
11
@@ -71,6 +72,138 @@ spec:
71
72
kubectl .RunWithOpts ([]string {"get" , "configmap" , name + ".app" , "-n" , env .Namespace }, e2e.RunOpts {NoNamespace : true })
72
73
}
73
74
75
+ func Test_AppDefaultNamespace_WithTargetCluster (t * testing.T ) {
76
+ targetClusterKubeconfig := os .Getenv ("TEST_E2E_TARGET_CLUSTER_KUBECONFIG" )
77
+ if targetClusterKubeconfig == "" {
78
+ t .Skip ("Skipping test as target cluster kubeconfig is not set" )
79
+ }
80
+
81
+ kubeconfigFile , err := os .CreateTemp ("" , "e2e-kubeconfig-*" )
82
+ assert .NoError (t , err )
83
+ defer os .Remove (kubeconfigFile .Name ())
84
+
85
+ _ , err = kubeconfigFile .Write ([]byte (targetClusterKubeconfig ))
86
+ assert .NoError (t , err )
87
+
88
+ env := e2e .BuildEnv (t )
89
+ logger := e2e.Logger {}
90
+ kapp := e2e.Kapp {t , env .Namespace , logger }
91
+ kubectl := e2e.Kubectl {t , env .Namespace , logger }
92
+
93
+ name := "app-default-namespace-target-cluster"
94
+ defaultNamespace := "e2e-default-namespace"
95
+ clusterNamespace := "e2e-cluster-namespace"
96
+ namespaceApp := "namespace-app"
97
+ secretName := "e2e-kubeconfig-secret"
98
+
99
+ cleanUp := func () {
100
+ kapp .Run ([]string {"delete" , "-a" , name })
101
+
102
+ }
103
+ cleanUpTargetCluster := func () {
104
+ kapp .RunWithOpts ([]string {"delete" , "-a" , namespaceApp , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
105
+ }
106
+
107
+ cleanUp ()
108
+ cleanUpTargetCluster ()
109
+ defer cleanUp ()
110
+ defer cleanUpTargetCluster ()
111
+
112
+ namespaceYAML := fmt .Sprintf (`---
113
+ apiVersion: v1
114
+ kind: Namespace
115
+ metadata:
116
+ name: %s
117
+ ---
118
+ apiVersion: v1
119
+ kind: Namespace
120
+ metadata:
121
+ name: %s` , defaultNamespace , clusterNamespace )
122
+
123
+ secret := e2e.Secrets {secretName , env .Namespace , targetClusterKubeconfig }
124
+ appYAML := `---
125
+ apiVersion: kappctrl.k14s.io/v1alpha1
126
+ kind: App
127
+ metadata:
128
+ name: %s
129
+ namespace: %s
130
+ annotations:
131
+ kapp.k14s.io/change-group: "kappctrl-e2e.k14s.io/apps"
132
+ spec:
133
+ cluster:
134
+ namespace: %s
135
+ kubeconfigSecretRef:
136
+ name: %s
137
+ defaultNamespace: %s
138
+ fetch:
139
+ - inline:
140
+ paths:
141
+ file.yml: |
142
+ apiVersion: v1
143
+ kind: ConfigMap
144
+ metadata:
145
+ name: my-cm
146
+ data:
147
+ key: value
148
+ template:
149
+ - ytt: {}
150
+ deploy:
151
+ - kapp: {}`
152
+
153
+ // create test namespaces on target cluster
154
+ kapp .RunWithOpts ([]string {"deploy" , "-a" , namespaceApp , "-f" , "-" , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true , StdinReader : strings .NewReader (namespaceYAML )})
155
+
156
+ // Provide both _defaultNamespace_ and _cluster.namespace_
157
+ _ , err = kapp .RunWithOpts ([]string {"deploy" , "-a" , name , "-f" , "-" }, e2e.RunOpts {AllowError : true ,
158
+ StdinReader : strings .NewReader (fmt .Sprintf (appYAML , name , env .Namespace , clusterNamespace , secretName , defaultNamespace ) + secret .ForTargetCluster ())})
159
+ assert .NoError (t , err , "Expected app deploy to succeed, it did not" )
160
+
161
+ // Assert that app resources are in defaultNamespace
162
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , "my-cm" , "-n" , defaultNamespace , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
163
+
164
+ // Assert that kapp metaconfigmap is in cluster.namespace
165
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , name + ".app" , "-n" , clusterNamespace , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
166
+
167
+ cleanUp ()
168
+
169
+ // Provide only _cluster.namespace_
170
+ _ , err = kapp .RunWithOpts ([]string {"deploy" , "-a" , name , "-f" , "-" }, e2e.RunOpts {AllowError : true ,
171
+ StdinReader : strings .NewReader (fmt .Sprintf (appYAML , name , env .Namespace , clusterNamespace , secretName , "" ) + secret .ForTargetCluster ())})
172
+ assert .NoError (t , err , "Expected app deploy to succeed, it did not" )
173
+
174
+ // Assert that app resources are in cluster.namespace
175
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , "my-cm" , "-n" , clusterNamespace , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
176
+
177
+ // Assert that kapp metaconfigmap is in cluster.namespace
178
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , name + ".app" , "-n" , clusterNamespace , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
179
+
180
+ cleanUp ()
181
+
182
+ // Provide only _defaultNamespace_
183
+ _ , err = kapp .RunWithOpts ([]string {"deploy" , "-a" , name , "-f" , "-" }, e2e.RunOpts {AllowError : true ,
184
+ StdinReader : strings .NewReader (fmt .Sprintf (appYAML , name , env .Namespace , "" , secretName , defaultNamespace ) + secret .ForTargetCluster ())})
185
+ assert .NoError (t , err , "Expected app deploy to succeed, it did not" )
186
+
187
+ // Assert that app resources are in defaultNamespace
188
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , "my-cm" , "-n" , defaultNamespace , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
189
+
190
+ // Assert that kapp metaconfigmap is in kubeconfig preferred namespace (default)
191
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , name + ".app" , "-n" , "default" , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
192
+
193
+ cleanUp ()
194
+
195
+ // Do not provide _defaultNamespace_ and _cluster.namespace_
196
+ _ , err = kapp .RunWithOpts ([]string {"deploy" , "-a" , name , "-f" , "-" }, e2e.RunOpts {AllowError : true ,
197
+ StdinReader : strings .NewReader (fmt .Sprintf (appYAML , name , env .Namespace , "" , secretName , "" ) + secret .ForTargetCluster ())})
198
+ assert .NoError (t , err , "Expected app deploy to succeed, it did not" )
199
+
200
+ // Assert that app resources are in kubeconfig preferred namespace (default)
201
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , "my-cm" , "-n" , "default" , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
202
+
203
+ // Assert that kapp metaconfigmap is in kubeconfig preferred namespace (default)
204
+ kubectl .RunWithOpts ([]string {"get" , "configmap" , name + ".app" , "-n" , "default" , "--kubeconfig" , kubeconfigFile .Name ()}, e2e.RunOpts {NoNamespace : true })
205
+ }
206
+
74
207
func Test_PackageInstall_DefaultNamespace (t * testing.T ) {
75
208
env := e2e .BuildEnv (t )
76
209
logger := e2e.Logger {}
0 commit comments