|
| 1 | +package tests_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct" |
| 5 | + "sigs.k8s.io/kustomize/v3/k8sdeps/transformer" |
| 6 | + "sigs.k8s.io/kustomize/v3/pkg/fs" |
| 7 | + "sigs.k8s.io/kustomize/v3/pkg/loader" |
| 8 | + "sigs.k8s.io/kustomize/v3/pkg/plugins" |
| 9 | + "sigs.k8s.io/kustomize/v3/pkg/resmap" |
| 10 | + "sigs.k8s.io/kustomize/v3/pkg/resource" |
| 11 | + "sigs.k8s.io/kustomize/v3/pkg/target" |
| 12 | + "sigs.k8s.io/kustomize/v3/pkg/validators" |
| 13 | + "testing" |
| 14 | +) |
| 15 | + |
| 16 | +func writeProfilesOverlaysTest(th *KustTestHarness) { |
| 17 | + th.writeK("/manifests/profiles/overlays/test", ` |
| 18 | +apiVersion: kustomize.config.k8s.io/v1beta1 |
| 19 | +kind: Kustomization |
| 20 | +bases: |
| 21 | +- ../../base |
| 22 | +#generators: |
| 23 | +#- app_test.yaml |
| 24 | +`) |
| 25 | + th.writeF("/manifests/profiles/base/crd.yaml", ` |
| 26 | +apiVersion: apiextensions.k8s.io/v1beta1 |
| 27 | +kind: CustomResourceDefinition |
| 28 | +metadata: |
| 29 | + creationTimestamp: null |
| 30 | + labels: |
| 31 | + controller-tools.k8s.io: "1.0" |
| 32 | + name: profiles.kubeflow.org |
| 33 | +spec: |
| 34 | + group: kubeflow.org |
| 35 | + names: |
| 36 | + kind: Profile |
| 37 | + plural: profiles |
| 38 | + scope: Cluster |
| 39 | + validation: |
| 40 | + openAPIV3Schema: |
| 41 | + properties: |
| 42 | + apiVersion: |
| 43 | + description: 'APIVersion defines the versioned schema of this representation |
| 44 | + of an object. Servers should convert recognized schemas to the latest |
| 45 | + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources' |
| 46 | + type: string |
| 47 | + kind: |
| 48 | + description: 'Kind is a string value representing the REST resource this |
| 49 | + object represents. Servers may infer this from the endpoint the client |
| 50 | + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds' |
| 51 | + type: string |
| 52 | + metadata: |
| 53 | + type: object |
| 54 | + spec: |
| 55 | + properties: |
| 56 | + owner: |
| 57 | + description: The profile owner |
| 58 | + type: object |
| 59 | + type: object |
| 60 | + status: |
| 61 | + properties: |
| 62 | + message: |
| 63 | + type: string |
| 64 | + status: |
| 65 | + description: 'INSERT ADDITIONAL STATUS FIELD - define observed state |
| 66 | + of cluster Important: Run "make" to regenerate code after modifying |
| 67 | + this file' |
| 68 | + type: string |
| 69 | + type: object |
| 70 | + version: v1alpha1 |
| 71 | +status: |
| 72 | + acceptedNames: |
| 73 | + kind: "" |
| 74 | + plural: "" |
| 75 | + conditions: [] |
| 76 | + storedVersions: [] |
| 77 | +`) |
| 78 | + th.writeF("/manifests/profiles/base/service-account.yaml", ` |
| 79 | +--- |
| 80 | +apiVersion: v1 |
| 81 | +kind: ServiceAccount |
| 82 | +metadata: |
| 83 | + name: controller-service-account |
| 84 | +--- |
| 85 | +apiVersion: v1 |
| 86 | +kind: ServiceAccount |
| 87 | +metadata: |
| 88 | + name: default-service-account |
| 89 | +`) |
| 90 | + th.writeF("/manifests/profiles/base/cluster-role-binding.yaml", ` |
| 91 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 92 | +kind: ClusterRoleBinding |
| 93 | +metadata: |
| 94 | + name: cluster-role-binding |
| 95 | +roleRef: |
| 96 | + apiGroup: rbac.authorization.k8s.io |
| 97 | + kind: ClusterRole |
| 98 | + name: cluster-admin |
| 99 | +subjects: |
| 100 | +- kind: ServiceAccount |
| 101 | + name: controller-service-account |
| 102 | +`) |
| 103 | + th.writeF("/manifests/profiles/base/role.yaml", ` |
| 104 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 105 | +kind: Role |
| 106 | +metadata: |
| 107 | + name: default-role |
| 108 | +rules: |
| 109 | +- apiGroups: |
| 110 | + - kubeflow.org |
| 111 | + resources: |
| 112 | + - profiles |
| 113 | + verbs: |
| 114 | + - create |
| 115 | + - watch |
| 116 | + - list |
| 117 | +`) |
| 118 | + th.writeF("/manifests/profiles/base/role-binding.yaml", ` |
| 119 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 120 | +kind: RoleBinding |
| 121 | +metadata: |
| 122 | + name: default-role-binding |
| 123 | +roleRef: |
| 124 | + apiGroup: rbac.authorization.k8s.io |
| 125 | + kind: Role |
| 126 | + name: default-role |
| 127 | +subjects: |
| 128 | +- kind: ServiceAccount |
| 129 | + name: default-service-account |
| 130 | +`) |
| 131 | + th.writeF("/manifests/profiles/base/service.yaml", ` |
| 132 | +apiVersion: v1 |
| 133 | +kind: Service |
| 134 | +metadata: |
| 135 | + name: kfam |
| 136 | +spec: |
| 137 | + ports: |
| 138 | + - port: 8081 |
| 139 | +`) |
| 140 | + th.writeF("/manifests/profiles/base/deployment.yaml", ` |
| 141 | +apiVersion: apps/v1 |
| 142 | +kind: Deployment |
| 143 | +metadata: |
| 144 | + name: deployment |
| 145 | +spec: |
| 146 | + template: |
| 147 | + spec: |
| 148 | + containers: |
| 149 | + - command: |
| 150 | + - /manager |
| 151 | + args: |
| 152 | + - "-userid-header" |
| 153 | + - $(userid-header) |
| 154 | + - "-userid-prefix" |
| 155 | + - $(userid-prefix) |
| 156 | + image: gcr.io/kubeflow-images-public/profile-controller:v20190619-v0-219-gbd3daa8c-dirty-1ced0e |
| 157 | + imagePullPolicy: Always |
| 158 | + name: manager |
| 159 | + - command: |
| 160 | + - /opt/kubeflow/access-management |
| 161 | + args: |
| 162 | + - "-cluster-admin" |
| 163 | + - $(admin) |
| 164 | + - "-userid-header" |
| 165 | + - $(userid-header) |
| 166 | + - "-userid-prefix" |
| 167 | + - $(userid-prefix) |
| 168 | + image: gcr.io/kubeflow-images-public/kfam:v20190612-v0-170-ga06cdb79-dirty-a33ee4 |
| 169 | + imagePullPolicy: Always |
| 170 | + name: kfam |
| 171 | + serviceAccountName: controller-service-account |
| 172 | +`) |
| 173 | + th.writeF("/manifests/profiles/base/params.yaml", ` |
| 174 | +varReference: |
| 175 | +- path: spec/template/spec/containers/0/args/1 |
| 176 | + kind: Deployment |
| 177 | +- path: spec/template/spec/containers/0/args/3 |
| 178 | + kind: Deployment |
| 179 | +- path: spec/template/spec/containers/1/args/1 |
| 180 | + kind: Deployment |
| 181 | +- path: spec/template/spec/containers/1/args/3 |
| 182 | + kind: Deployment |
| 183 | +- path: spec/template/spec/containers/1/args/5 |
| 184 | + kind: Deployment |
| 185 | +`) |
| 186 | + th.writeF("/manifests/profiles/base/params.env", ` |
| 187 | +admin= |
| 188 | +userid-header= |
| 189 | +userid-prefix= |
| 190 | +`) |
| 191 | + th.writeK("/manifests/profiles/base", ` |
| 192 | +apiVersion: kustomize.config.k8s.io/v1beta1 |
| 193 | +kind: Kustomization |
| 194 | +resources: |
| 195 | +- crd.yaml |
| 196 | +- service-account.yaml |
| 197 | +- cluster-role-binding.yaml |
| 198 | +- role.yaml |
| 199 | +- role-binding.yaml |
| 200 | +- service.yaml |
| 201 | +- deployment.yaml |
| 202 | +namePrefix: profiles- |
| 203 | +namespace: kubeflow |
| 204 | +commonLabels: |
| 205 | + kustomize.component: profiles |
| 206 | +configMapGenerator: |
| 207 | +- name: profiles-parameters |
| 208 | + env: params.env |
| 209 | +images: |
| 210 | +- name: gcr.io/kubeflow-images-public/profile-controller |
| 211 | + newName: gcr.io/kubeflow-images-public/profile-controller |
| 212 | + newTag: v20190619-v0-219-gbd3daa8c-dirty-1ced0e |
| 213 | +- name: gcr.io/kubeflow-images-public/kfam |
| 214 | + newName: gcr.io/kubeflow-images-public/kfam |
| 215 | + newTag: v20190612-v0-170-ga06cdb79-dirty-a33ee4 |
| 216 | +vars: |
| 217 | +- name: admin |
| 218 | + objref: |
| 219 | + kind: ConfigMap |
| 220 | + name: profiles-parameters |
| 221 | + apiVersion: v1 |
| 222 | + fieldref: |
| 223 | + fieldpath: data.admin |
| 224 | +- name: userid-header |
| 225 | + objref: |
| 226 | + kind: ConfigMap |
| 227 | + name: profiles-parameters |
| 228 | + apiVersion: v1 |
| 229 | + fieldref: |
| 230 | + fieldpath: data.userid-header |
| 231 | +- name: userid-prefix |
| 232 | + objref: |
| 233 | + kind: ConfigMap |
| 234 | + name: profiles-parameters |
| 235 | + apiVersion: v1 |
| 236 | + fieldref: |
| 237 | + fieldpath: data.userid-prefix |
| 238 | +- name: namespace |
| 239 | + objref: |
| 240 | + kind: Service |
| 241 | + name: kfam |
| 242 | + apiVersion: v1 |
| 243 | + fieldref: |
| 244 | + fieldpath: metadata.namespace |
| 245 | +configurations: |
| 246 | +- params.yaml |
| 247 | +`) |
| 248 | +} |
| 249 | + |
| 250 | +func TestProfilesOverlaysTest(t *testing.T) { |
| 251 | + th := NewKustTestHarness(t, "/manifests/profiles/overlays/test") |
| 252 | + writeProfilesOverlaysTest(th) |
| 253 | + m, err := th.makeKustTarget().MakeCustomizedResMap() |
| 254 | + if err != nil { |
| 255 | + t.Fatalf("Err: %v", err) |
| 256 | + } |
| 257 | + expected, err := m.AsYaml() |
| 258 | + if err != nil { |
| 259 | + t.Fatalf("Err: %v", err) |
| 260 | + } |
| 261 | + targetPath := "../profiles/overlays/test" |
| 262 | + fsys := fs.MakeRealFS() |
| 263 | + lrc := loader.RestrictionRootOnly |
| 264 | + _loader, loaderErr := loader.NewLoader(lrc, validators.MakeFakeValidator(), targetPath, fsys) |
| 265 | + if loaderErr != nil { |
| 266 | + t.Fatalf("could not load kustomize loader: %v", loaderErr) |
| 267 | + } |
| 268 | + rf := resmap.NewFactory(resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()), transformer.NewFactoryImpl()) |
| 269 | + pc := plugins.DefaultPluginConfig() |
| 270 | + kt, err := target.NewKustTarget(_loader, rf, transformer.NewFactoryImpl(), plugins.NewLoader(pc, rf)) |
| 271 | + if err != nil { |
| 272 | + th.t.Fatalf("Unexpected construction error %v", err) |
| 273 | + } |
| 274 | + actual, err := kt.MakeCustomizedResMap() |
| 275 | + if err != nil { |
| 276 | + t.Fatalf("Err: %v", err) |
| 277 | + } |
| 278 | + th.assertActualEqualsExpected(actual, string(expected)) |
| 279 | +} |
0 commit comments