diff --git a/api/krusty/fnplugin_test.go b/api/krusty/fnplugin_test.go index 40f63b642..5a521e7ca 100644 --- a/api/krusty/fnplugin_test.go +++ b/api/krusty/fnplugin_test.go @@ -504,313 +504,106 @@ func skipIfNoDocker(t *testing.T) { } func TestFnContainerGenerator(t *testing.T) { - t.Skip("wait for #3881") skipIfNoDocker(t) - - // Function plugins should not need the env setup done by MakeEnhancedHarness th := kusttest_test.MakeHarness(t) - - th.WriteK(".", ` + o := th.MakeOptionsPluginsEnabled() + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + th.WriteK(tmpDir.String(), ` resources: -- short_secret.yaml +- deployment.yaml generators: -- gener.yaml +- project-service-set.yaml `) // Create generator config - th.WriteF("gener.yaml", ` -apiVersion: examples.config.kubernetes.io/v1beta1 -kind: CockroachDB + th.WriteF(filepath.Join(tmpDir.String(), "project-service-set.yaml"), ` +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: ProjectServiceSet metadata: name: demo annotations: config.kubernetes.io/function: | container: - image: gcr.io/kustomize-functions/example-cockroachdb:v0.1.0 + image: gcr.io/kpt-fn/enable-gcp-services:v0.1.0 spec: - replicas: 3 + services: + - compute.googleapis.com + projectID: foo `) - // Create some additional resource just to make sure everything is added - th.WriteF("short_secret.yaml", ` -apiVersion: v1 -kind: Secret + // Create another resource just to make sure everything is added + th.WriteF(filepath.Join(tmpDir.String(), "deployment.yaml"), ` +apiVersion: apps/v1 +kind: Deployment metadata: - labels: - airshipit.org/ephemeral-user-data: "true" - name: node1-bmc-secret -type: Opaque -stringData: - userData: | - bootcmd: - - mkdir /mnt/vda + name: foo `) - m := th.Run(".", th.MakeOptionsPluginsEnabled()) - th.AssertActualEqualsExpected(m, ` -apiVersion: v1 -kind: Secret + m := th.Run(tmpDir.String(), o) + actual, err := m.AsYaml() + assert.NoError(t, err) + assert.Equal(t, `apiVersion: apps/v1 +kind: Deployment metadata: - labels: - airshipit.org/ephemeral-user-data: "true" - name: node1-bmc-secret -stringData: - userData: | - bootcmd: - - mkdir /mnt/vda -type: Opaque + name: foo --- -apiVersion: policy/v1beta1 -kind: PodDisruptionBudget -metadata: - labels: - app: cockroachdb - name: demo - name: demo-budget -spec: - minAvailable: 67% - selector: - matchLabels: - app: cockroachdb - name: demo ---- -apiVersion: v1 -kind: Service -metadata: - labels: - app: cockroachdb - name: demo - name: demo-public -spec: - ports: - - name: grpc - port: 26257 - targetPort: 26257 - - name: http - port: 8080 - targetPort: 8080 - selector: - app: cockroachdb - name: demo ---- -apiVersion: v1 +apiVersion: serviceusage.cnrm.cloud.google.com/v1beta1 kind: Service metadata: annotations: - prometheus.io/path: _status/vars - prometheus.io/port: "8080" - prometheus.io/scrape: "true" - service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" - labels: - app: cockroachdb - name: demo - name: demo + blueprints.cloud.google.com/ownerReference: blueprints.cloud.google.com/ProjectServiceSet/demo + config.kubernetes.io/function: | + container: + image: gcr.io/kpt-fn/enable-gcp-services:v0.1.0 + name: demo-compute spec: - clusterIP: None - ports: - - name: grpc - port: 26257 - targetPort: 26257 - - name: http - port: 8080 - targetPort: 8080 - selector: - app: cockroachdb - name: demo ---- -apiVersion: apps/v1 -kind: StatefulSet -metadata: - labels: - app: cockroachdb - name: demo - name: demo -spec: - replicas: 3 - selector: - matchLabels: - app: cockroachdb - name: demo - serviceName: demo - template: - metadata: - labels: - app: cockroachdb - name: demo - spec: - affinity: - podAntiAffinity: - preferredDuringSchedulingIgnoredDuringExecution: - - podAffinityTerm: - labelSelector: - matchExpressions: - - key: app - operator: In - values: - - cockroachdb - topologyKey: kubernetes.io/hostname - weight: 100 - containers: - - command: - - /bin/bash - - -ecx - - | - # The use of qualified `+"`hostname -f`"+` is crucial: - # Other nodes aren't able to look up the unqualified hostname. - CRARGS=("start" "--logtostderr" "--insecure" "--host" "$(hostname -f)" "--http-host" "0.0.0.0") - # We only want to initialize a new cluster (by omitting the join flag) - # if we're sure that we're the first node (i.e. index 0) and that - # there aren't any other nodes running as part of the cluster that - # this is supposed to be a part of (which indicates that a cluster - # already exists and we should make sure not to create a new one). - # It's fine to run without --join on a restart if there aren't any - # other nodes. - if [ ! "$(hostname)" == "cockroachdb-0" ] || [ -e "/cockroach/cockroach-data/cluster_exists_marker" ] - then - # We don't join cockroachdb in order to avoid a node attempting - # to join itself, which currently doesn't work - # (https://github.com/cockroachdb/cockroach/issues/9625). - CRARGS+=("--join" "cockroachdb-public") - fi - exec /cockroach/cockroach ${CRARGS[*]} - image: cockroachdb/cockroach:v1.1.0 - imagePullPolicy: IfNotPresent - name: demo - ports: - - containerPort: 26257 - name: grpc - - containerPort: 8080 - name: http - volumeMounts: - - mountPath: /cockroach/cockroach-data - name: datadir - initContainers: - - args: - - -on-start=/on-start.sh - - -service=cockroachdb - env: - - name: POD_NAMESPACE - valueFrom: - fieldRef: - fieldPath: metadata.namespace - image: cockroachdb/cockroach-k8s-init:0.1 - imagePullPolicy: IfNotPresent - name: bootstrap - volumeMounts: - - mountPath: /cockroach/cockroach-data - name: datadir - terminationGracePeriodSeconds: 60 - volumes: - - name: datadir - persistentVolumeClaim: - claimName: datadir - volumeClaimTemplates: - - metadata: - name: datadir - spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 1Gi -`) + projectRef: + external: foo + resourceID: compute.googleapis.com +`, string(actual)) } func TestFnContainerTransformer(t *testing.T) { - t.Skip("wait for #3881") skipIfNoDocker(t) - - // Function plugins should not need the env setup done by MakeEnhancedHarness th := kusttest_test.MakeHarness(t) - - th.WriteK(".", ` + o := th.MakeOptionsPluginsEnabled() + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + th.WriteK(tmpDir.String(), ` resources: -- data.yaml +- deployment.yaml transformers: -- transf1.yaml -- transf2.yaml +- e2econtainerconfig.yaml `) - - th.WriteF("data.yaml", ` + th.WriteF(filepath.Join(tmpDir.String(), "deployment.yaml"), ` apiVersion: apps/v1 kind: Deployment metadata: - name: nginx - labels: - app: nginx - annotations: - tshirt-size: small # this injects the resource reservations -spec: - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - name: nginx - image: nginx + name: foo `) - // This transformer should add resource reservations based on annotation in data.yaml - // See https://github.com/kubernetes-sigs/kustomize/tree/master/functions/examples/injection-tshirt-sizes - th.WriteF("transf1.yaml", ` -apiVersion: examples.config.kubernetes.io/v1beta1 -kind: Validator + th.WriteF(filepath.Join(tmpDir.String(), "e2econtainerconfig.yaml"), ` +apiVersion: example.com/v1alpha1 +kind: Input metadata: - name: valid - annotations: - config.kubernetes.io/function: |- - container: - image: gcr.io/kustomize-functions/example-tshirt:v0.2.0 -`) - // This transformer will check resources without and won't do any changes - // See https://github.com/kubernetes-sigs/kustomize/tree/master/functions/examples/validator-kubeval - th.WriteF("transf2.yaml", ` -apiVersion: examples.config.kubernetes.io/v1beta1 -kind: Kubeval -metadata: - name: validate + name: foo annotations: config.kubernetes.io/function: | container: - image: gcr.io/kustomize-functions/example-validator-kubeval:v0.1.0 -spec: - strict: true - ignoreMissingSchemas: true - - # TODO: Update this to use network/volumes features. - # Relevant issues: - # - https://github.com/kubernetes-sigs/kustomize/issues/1901 - # - https://github.com/kubernetes-sigs/kustomize/issues/1902 - kubernetesVersion: "1.16.0" - schemaLocation: "file:///schemas" + image: "gcr.io/kustomize-functions/e2econtainerconfig" `) - m := th.Run(".", th.MakeOptionsPluginsEnabled()) - th.AssertActualEqualsExpected(m, ` -apiVersion: apps/v1 + build := exec.Command("docker", "build", ".", "-t", "gcr.io/kustomize-functions/e2econtainerconfig") + build.Dir = "../../cmd/config/internal/commands/e2e/e2econtainerconfig" + assert.NoError(t, build.Run()) + m := th.Run(tmpDir.String(), o) + actual, err := m.AsYaml() + assert.NoError(t, err) + assert.Equal(t, `apiVersion: apps/v1 kind: Deployment metadata: annotations: - tshirt-size: small - labels: - app: nginx - name: nginx -spec: - selector: - matchLabels: - app: nginx - template: - metadata: - labels: - app: nginx - spec: - containers: - - image: nginx - name: nginx - resources: - requests: - cpu: 200m - memory: 50M -`) + a-bool-value: "false" + a-int-value: "0" + a-string-value: "" + name: foo +`, string(actual)) } func TestFnContainerTransformerWithConfig(t *testing.T) {