From adbaae218524bff6768fdaf82fea0498d8246f5a Mon Sep 17 00:00:00 2001 From: Amim Knabben Date: Thu, 22 Dec 2022 15:19:44 -0300 Subject: [PATCH] Overlay and multi-az for legacy Windows --- .../v1.5.1/ytt/overlay-windows.yaml | 76 ++++- providers/tests/go.mod | 10 +- providers/tests/go.sum | 15 + providers/tests/unit/avi_test.go | 34 --- providers/tests/unit/helpers_test.go | 56 ++++ providers/tests/unit/ip_family_test.go | 13 - providers/tests/unit/windows_test.go | 276 ++++++++++++------ 7 files changed, 338 insertions(+), 142 deletions(-) create mode 100644 providers/tests/unit/helpers_test.go diff --git a/providers/infrastructure-vsphere/v1.5.1/ytt/overlay-windows.yaml b/providers/infrastructure-vsphere/v1.5.1/ytt/overlay-windows.yaml index 832e6587d2..570de34274 100644 --- a/providers/infrastructure-vsphere/v1.5.1/ytt/overlay-windows.yaml +++ b/providers/infrastructure-vsphere/v1.5.1/ytt/overlay-windows.yaml @@ -285,7 +285,7 @@ spec: apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 kind: KubeadmConfigTemplate metadata: - name: #@ "{}-md-0-windows-containerd".format(data.values.CLUSTER_NAME) + name: #@ "{}-windows-containerd".format(data.values.CLUSTER_NAME) #@overlay/replace spec: template: @@ -430,11 +430,15 @@ spec: spec: bootstrap: configRef: - name: #@ "{}-md-0-windows-containerd".format(data.values.CLUSTER_NAME) + name: #@ "{}-windows-containerd".format(data.values.CLUSTER_NAME) clusterName: #@ data.values.CLUSTER_NAME infrastructureRef: name: #@ "{}-windows-containerd".format(data.values.CLUSTER_NAME) version: #@ data.values.KUBERNETES_VERSION + #@ if data.values.VSPHERE_AZ_0: + failureDomain: #@ data.values.VSPHERE_AZ_0 + #@ end + #@overlay/match by=overlay.subset({"kind": "Secret", "metadata":{"name": "${ CLUSTER_NAME }"}}) --- apiVersion: v1 @@ -566,3 +570,71 @@ data: type: kubernetes.io/service-account-token --- #@ end + +#@ if data.values.CLUSTER_PLAN == "prod" and data.values.IS_WINDOWS_WORKLOAD_CLUSTER: +#@overlay/match by=overlay.subset({"kind":"MachineDeployment", "metadata":{"name": "${ CLUSTER_NAME }-md-0-windows-containerd"}}) +#@overlay/append +--- +apiVersion: cluster.x-k8s.io/v1beta1 +kind: MachineDeployment +metadata: + labels: + cluster.x-k8s.io/cluster-name: #@ data.values.CLUSTER_NAME + name: #@ "{}-md-1-windows-containerd".format(data.values.CLUSTER_NAME) +spec: + clusterName: #@ data.values.CLUSTER_NAME + replicas: #@ data.values.WORKER_MACHINE_COUNT_1 + selector: + matchLabels: {} + template: + metadata: + labels: + node-pool: #@ "{}-worker-pool".format(data.values.CLUSTER_NAME) + spec: + bootstrap: + configRef: + apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 + kind: KubeadmConfigTemplate + name: #@ "{}-windows-containerd".format(data.values.CLUSTER_NAME) + clusterName: #@ data.values.CLUSTER_NAME + infrastructureRef: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: VSphereMachineTemplate + name: #@ "{}-windows-containerd".format(data.values.CLUSTER_NAME) + version: #@ data.values.KUBERNETES_VERSION + #@ if data.values.VSPHERE_AZ_1: + failureDomain: #@ data.values.VSPHERE_AZ_1 + #@ end +--- +apiVersion: cluster.x-k8s.io/v1beta1 +kind: MachineDeployment +metadata: + labels: + cluster.x-k8s.io/cluster-name: #@ data.values.CLUSTER_NAME + name: #@ "{}-md-2-windows-containerd".format(data.values.CLUSTER_NAME) +spec: + clusterName: #@ data.values.CLUSTER_NAME + replicas: #@ data.values.WORKER_MACHINE_COUNT_2 + selector: + matchLabels: {} + template: + metadata: + labels: + cluster.x-k8s.io/cluster-name: #@ data.values.CLUSTER_NAME + node-pool: #@ "{}-worker-pool".format(data.values.CLUSTER_NAME) + spec: + bootstrap: + configRef: + apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 + kind: KubeadmConfigTemplate + name: #@ "{}-windows-containerd".format(data.values.CLUSTER_NAME) + clusterName: #@ data.values.CLUSTER_NAME + infrastructureRef: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: VSphereMachineTemplate + name: #@ "{}-windows-containerd".format(data.values.CLUSTER_NAME) + version: #@ data.values.KUBERNETES_VERSION + #@ if data.values.VSPHERE_AZ_2: + failureDomain: #@ data.values.VSPHERE_AZ_2 + #@ end +#@ end diff --git a/providers/tests/go.mod b/providers/tests/go.mod index 3547dbe27c..1edb34100e 100644 --- a/providers/tests/go.mod +++ b/providers/tests/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/onsi/ginkgo v1.16.5 - github.com/onsi/gomega v1.19.0 + github.com/onsi/gomega v1.24.2 github.com/vmware-tanzu/tanzu-framework/test/pkg v0.0.0-00010101000000-000000000000 gopkg.in/yaml.v3 v3.0.1 sigs.k8s.io/cluster-api v1.2.7 @@ -22,17 +22,19 @@ require ( github.com/emicklei/go-restful/v3 v3.9.0 // indirect github.com/evanphx/json-patch v5.6.0+incompatible // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect - github.com/go-logr/logr v1.2.2 // indirect + github.com/go-logr/logr v1.2.3 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.19.5 // indirect github.com/go-openapi/swag v0.19.14 // indirect + github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/gobuffalo/flect v0.2.5 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/gnostic v0.5.7-v3refs // indirect - github.com/google/go-cmp v0.5.8 // indirect + github.com/google/go-cmp v0.5.9 // indirect github.com/google/gofuzz v1.2.0 // indirect + github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/google/uuid v1.3.0 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/josharian/intern v1.0.0 // indirect @@ -43,6 +45,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/nxadm/tail v1.4.8 // indirect + github.com/onsi/ginkgo/v2 v2.6.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.12.1 // indirect github.com/prometheus/client_model v0.2.0 // indirect @@ -56,6 +59,7 @@ require ( golang.org/x/term v0.3.0 // indirect golang.org/x/text v0.5.0 // indirect golang.org/x/time v0.3.0 // indirect + golang.org/x/tools v0.4.0 // indirect gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.28.0 // indirect diff --git a/providers/tests/go.sum b/providers/tests/go.sum index 1878c814fa..869eddf93c 100644 --- a/providers/tests/go.sum +++ b/providers/tests/go.sum @@ -159,6 +159,8 @@ github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTg github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.2.2 h1:ahHml/yUpnlb96Rp8HCvtYVPY8ZYpxq3g7UYchIYwbs= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/zapr v1.2.0 h1:n4JnPI1T3Qq1SFEi/F8rwLrZERp2bso19PJZDB9dayk= github.com/go-logr/zapr v1.2.0/go.mod h1:Qa4Bsj2Vb+FAVeAKsLD8RLQ+YRJB8YDmOAKxaBQf7Ro= github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= @@ -171,6 +173,7 @@ github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh github.com/go-openapi/swag v0.19.14 h1:gm3vOOXfiuw5i9p5N9xJvfjvuofpyvLA9Wr6QfK5Fng= github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/gobuffalo/flect v0.2.5 h1:H6vvsv2an0lalEaCDRThvtBfmg44W/QHXBCYUXf/6S4= github.com/gobuffalo/flect v0.2.5/go.mod h1:1ZyCLIbg0YD7sDkzvFdPoOydPtD8y9JQnrOROolUcM8= @@ -234,6 +237,8 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= @@ -252,6 +257,8 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= @@ -373,12 +380,18 @@ github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9k github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= github.com/onsi/ginkgo/v2 v2.1.4 h1:GNapqRSid3zijZ9H77KrgVG4/8KqiyRsxcSxe+7ApXY= +github.com/onsi/ginkgo/v2 v2.6.1 h1:1xQPCjcqYw/J5LchOcp4/2q/jzJFjiAOc25chhnDw+Q= +github.com/onsi/ginkgo/v2 v2.6.1/go.mod h1:yjiuMwPokqY1XauOgju45q3sJt6VzQ/Fict1LFVcsAo= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= +github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E= +github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM= +github.com/onsi/gomega v1.24.2 h1:J/tulyYK6JwBldPViHJReihxxZ+22FHs0piGjQAvoUE= +github.com/onsi/gomega v1.24.2/go.mod h1:gs3J10IS7Z7r7eXRoNJIrNqU4ToQukCJhFtKrWgHWnk= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= @@ -787,6 +800,8 @@ golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.10-0.20220218145154-897bd77cd717/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= +golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4= +golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/providers/tests/unit/avi_test.go b/providers/tests/unit/avi_test.go index b34712ee37..fe618c51f8 100644 --- a/providers/tests/unit/avi_test.go +++ b/providers/tests/unit/avi_test.go @@ -4,9 +4,7 @@ package unit import ( - "io" "path/filepath" - "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -15,38 +13,6 @@ import ( "github.com/vmware-tanzu/tanzu-framework/test/pkg/ytt" ) -type yttValues map[string]interface{} - -func (v yttValues) toReader() io.Reader { - return strings.NewReader(createDataValues(v)) -} - -func (v yttValues) Set(key string, value interface{}) { - v[key] = value -} - -func (v yttValues) Delete(key string) { - delete(v, key) -} - -func (v yttValues) DeepCopy() yttValues { - other := make(yttValues) - for key, value := range v { - other[key] = value - } - return other -} - -func assertNotFound(docs []string, err error) { - Expect(err).NotTo(HaveOccurred()) - Expect(docs).To(HaveLen(0)) -} - -func assertFoundOne(docs []string, err error) { - Expect(err).NotTo(HaveOccurred()) - Expect(docs).To(HaveLen(1)) -} - const ( AviUsername = "admin" AviPassword = "pass_1234" diff --git a/providers/tests/unit/helpers_test.go b/providers/tests/unit/helpers_test.go new file mode 100644 index 0000000000..80e1fe924d --- /dev/null +++ b/providers/tests/unit/helpers_test.go @@ -0,0 +1,56 @@ +// Copyright 2022 VMware, Inc. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package unit + +import ( + "io" + "strings" + + . "github.com/onsi/gomega" + "gopkg.in/yaml.v3" +) + +type yttValues map[string]interface{} + +func (v yttValues) toReader() io.Reader { + return strings.NewReader(createDataValues(v)) +} + +func (v yttValues) Set(key string, value interface{}) { + v[key] = value +} + +func (v yttValues) Delete(key string) { + delete(v, key) +} + +func (v yttValues) DeepCopy() yttValues { + other := make(yttValues) + for key, value := range v { + other[key] = value + } + return other +} + +func assertNotFound(docs []string, err error) { + Expect(err).NotTo(HaveOccurred()) + Expect(docs).To(HaveLen(0)) +} + +func assertFoundOne(docs []string, err error) { + Expect(err).NotTo(HaveOccurred()) + Expect(docs).To(HaveLen(1)) +} + +func createDataValues(values map[string]interface{}) string { + dataValues := "#@data/values\n---\n" + bytes, err := yaml.Marshal(values) + if err != nil { + return "" + } + valuesStr := string(bytes) + valuesStr = strings.ReplaceAll(valuesStr, "\"true\"", "true") + valuesStr = strings.ReplaceAll(valuesStr, "\"false\"", "false") + return dataValues + valuesStr +} diff --git a/providers/tests/unit/ip_family_test.go b/providers/tests/unit/ip_family_test.go index 1ae65195bb..3824a62bb0 100644 --- a/providers/tests/unit/ip_family_test.go +++ b/providers/tests/unit/ip_family_test.go @@ -10,7 +10,6 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" - "gopkg.in/yaml.v3" . "github.com/vmware-tanzu/tanzu-framework/test/pkg/matchers" "github.com/vmware-tanzu/tanzu-framework/test/pkg/ytt" @@ -875,15 +874,3 @@ var _ = Describe("TKG_IP_FAMILY Ytt Templating", func() { }) }) }) - -func createDataValues(values map[string]interface{}) string { - dataValues := "#@data/values\n---\n" - bytes, err := yaml.Marshal(values) - if err != nil { - return "" - } - valuesStr := string(bytes) - valuesStr = strings.ReplaceAll(valuesStr, "\"true\"", "true") - valuesStr = strings.ReplaceAll(valuesStr, "\"false\"", "false") - return dataValues + valuesStr -} diff --git a/providers/tests/unit/windows_test.go b/providers/tests/unit/windows_test.go index df6ca5ef7e..ad55ffd29f 100644 --- a/providers/tests/unit/windows_test.go +++ b/providers/tests/unit/windows_test.go @@ -6,119 +6,215 @@ package unit import ( "fmt" "path/filepath" - "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - utilyaml "sigs.k8s.io/cluster-api/util/yaml" - + "github.com/vmware-tanzu/tanzu-framework/test/pkg/matchers" "github.com/vmware-tanzu/tanzu-framework/test/pkg/ytt" ) var _ = Describe("Windows Ytt Templating", func() { var paths []string + var baseVal yttValues + BeforeEach(func() { paths = []string{ - // Map item (key 'infraProvider') on line stdin.yml:8: filepath.Join(yamlRoot, "config_default.yaml"), - filepath.Join("./fixtures/tkr-bom-v1.21.1.yaml"), - filepath.Join("./fixtures/tkg-bom-v1.4.0.yaml"), filepath.Join(yamlRoot, "infrastructure-vsphere", capvVersion, "ytt", "base-template.yaml"), filepath.Join(yamlRoot, "infrastructure-vsphere", capvVersion, "ytt", "overlay-windows.yaml"), - filepath.Join(yamlRoot, "ytt", "02_addons", "cni", "antrea", "antrea_addon_data.lib.yaml"), - filepath.Join(yamlRoot, "ytt", "02_addons", "cpi", "cpi_addon_data.lib.yaml"), - filepath.Join(yamlRoot, "ytt", "03_customizations", "02_avi", "ako-deployment.lib.yaml"), - //filepath.Join(YAML_ROOT, "provider-bundle", "providers", "ytt", "02_addons", "cpi", "cpi_addon_data.lib.yaml"), - filepath.Join(yamlRoot, "ytt", "03_customizations", "03_windows"), - filepath.Join(yamlRoot, "ytt"), // lib/helpers.star, lib/config_variable_association.star, lib/validate.star + filepath.Join("./fixtures/tkr-bom-v1.21.1.yaml"), + filepath.Join("./fixtures/tkg-bom-v1.4.0.yaml"), + filepath.Join(yamlRoot, "ytt"), } - }) - It("Has a windows overlay", func() { - values := createDataValues(map[string]interface{}{ - "VSPHERE_INSECURE": "true", - "PROVIDER_TYPE": "vsphere", + + baseVal = map[string]interface{}{ + "CLUSTER_NAME": "win-wl", + "CORE_DNS_IP": "10.64.0.10", "IS_WINDOWS_WORKLOAD_CLUSTER": "true", - "TKG_CLUSTER_ROLE": "management", - "VSPHERE_USERNAME": "user_blah", - "VSPHERE_PASSWORD": "pass_1234", - "VSPHERE_SERVER": "vmware-tanzu.com", + "KUBERNETES_RELEASE": "v1.21.2---vmware.1-tkg.1", + "PROVIDER_TYPE": "vsphere", + "TKG_CLUSTER_ROLE": "workload", + "TKG_DEFAULT_BOM": "tkg-bom-v1.4.0.yaml", + "WORKER_MACHINE_COUNT": 3, "VSPHERE_DATACENTER": "vmware-tanzu-dc.com", - "VSPHERE_RESOURCE_POOL": "myrp", "VSPHERE_FOLDER": "ds0", + "VSPHERE_INSECURE": "true", + "VSPHERE_PASSWORD": "pass_1234", + "VSPHERE_RESOURCE_POOL": "myrp", + "VSPHERE_SERVER": "vmware-tanzu.com", "VSPHERE_SSH_AUTHORIZED_KEY": "ssh-rsa AAAA...+M7Q== vmware-tanzu.local", - "TKG_DEFAULT_BOM": "tkg-bom-v1.4.0.yaml", - "KUBERNETES_RELEASE": "v1.21.2---vmware.1-tkg.1", - "CORE_DNS_IP": "10.64.0.10", - "CLUSTER_CIDR": "192.168.1.0/16", + "VSPHERE_USERNAME": "user_blah", + } + }) + + When("Using the default configuration", func() { + var value yttValues + BeforeEach(func() { + value = baseVal.DeepCopy() + }) + + It("has default Cluster vSphere objects", func() { + output, err := ytt.RenderYTTTemplate(ytt.CommandOptions{}, paths, value.toReader()) + Expect(err).NotTo(HaveOccurred()) + + tests := []struct { + kind string + counter int + }{ + { + kind: "Cluster", + counter: 1, + }, + { + kind: "VSphereCluster", + counter: 1, + }, + { + kind: "VSphereMachineTemplate", + counter: 2, + }, + } + for _, tt := range tests { + templates, err := matchers.FindDocsMatchingYAMLPath(output, map[string]string{"$.kind": tt.kind}) + Expect(err).NotTo(HaveOccurred()) + Expect(len(templates)).To(Equal(tt.counter)) + } }) + }) + + When("WORKER_MACHINE_COUNT is set", func() { + var value yttValues + var workloadCount = 6 - // useful debugging information that we don't actually need for day-to-day testing - rawClusterAPIYaml, err := ytt.RenderYTTTemplate(ytt.CommandOptions{}, paths, strings.NewReader(values)) - Expect(err).NotTo(HaveOccurred(), values) + BeforeEach(func() { + value = baseVal.DeepCopy() + }) - // Test 1: Making sure that we have a few basic ClusterAPI objects in the windows templates... + It("succeeds when using Windows nodes in the workload", func() { + value.Set("WORKER_MACHINE_COUNT", workloadCount) + value.Set("CLUSTER_PLAN", "prod") - // clusterAPIComponents is a list of all the 'kind' objects that we want to see. - // for windows, the most important thing to confirm is that we have 2 VsphereMachineTemplates, - // since there are obviously going to be linux as well as windows machine types. - clusterAPIComponents := map[string]int{ - "Cluster": 1, - "VSphereCluster": 1, - "VSphereMachineTemplate": 2, - } - seen, err := countCapiCompKinds(rawClusterAPIYaml) - Expect(err).NotTo(HaveOccurred()) - for k, v := range clusterAPIComponents { - val, ok := seen[k] - Expect(ok).To(BeTrue()) - Expect(val).To(Equal(v), fmt.Sprintf("Of type %s", k)) - } + output, err := ytt.RenderYTTTemplate(ytt.CommandOptions{}, paths, value.toReader()) + Expect(err).NotTo(HaveOccurred()) - // TODO add more validations for things like the antrea installation contents etc... + machineDeploymentTemplates, err := matchers.FindDocsMatchingYAMLPath(output, map[string]string{"$.kind": "MachineDeployment"}) + Expect(err).NotTo(HaveOccurred()) + Expect(len(machineDeploymentTemplates)).To(Equal(3)) + }) }) -}) -// countCapiCompKinds counts up the number of different api types in the final YAML output -func countCapiCompKinds(rawClusterAPIYaml string) (map[string]int, error) { - kinds := make(map[string]int) - - objs, err := utilyaml.ToUnstructured([]byte(rawClusterAPIYaml)) - if err != nil { - return nil, err - } - - for _, obj := range objs { - kinds[obj.GetKind()] = kinds[obj.GetKind()] + 1 - } - - return kinds, nil -} - -/** -Adopted from this hacky string... -/usr/local/bin/ytt ---ignore-unknown-comments ---data-value=infraProvider=vsphere ---data-value=TKG_CLUSTER_ROLE=management ---data-value=IS_WINDOWS_WORKLOAD_CLUSTER=true ---data-value=VSPHERE_USERNAME=a ---data-value=VSPHERE_PASSWORD=a ---data-value=VSPHERE_SERVER=a ---data-value=VSPHERE_DATACENTER=a ---data-value=VSPHERE_RESOURCE_POOL=a ---data-value=VSPHERE_FOLDER=a, ---data-value=VSPHERE_SSH_AUTHORIZED_KEY="a" --f tkr-bom-v1.21.1.yaml --f tkg-bom-v1.4.0.yaml --f config.yaml ---data-value=TKG_DEFAULT_BOM=tkg-bom-v1.4.0.yaml ---data-value=KUBERNETES_RELEASE=v1.21.2---vmware.1-tkg.2-20210924-539f8b15 --f ../../config_default.yaml --f ../../infrastructure-vsphere/v0.7.10/ytt/base-template.yaml --f ../../infrastructure-vsphere/v0.7.10/ytt/overlay-windows.yaml --f ../..//ytt/02_addons/cni/antrea/antrea_addon_data.lib.yaml --f ../../ytt/02_addons/cpi/cpi_addon_data.lib.yaml --f ../../provider-bundle/providers/ytt/02_addons/cpi/cpi_addon_data.lib.yaml --f ./ytt_libs_4_test/ -*/ + When("CLUSTER_PLAN is set", func() { + var value yttValues + BeforeEach(func() { + value = baseVal.DeepCopy() + }) + + It("succeeds when plan is prod and add cluster annotation", func() { + value.Set("CLUSTER_PLAN", "prod") + + output, err := ytt.RenderYTTTemplate(ytt.CommandOptions{}, paths, value.toReader()) + Expect(err).NotTo(HaveOccurred()) + + clusterTemplates, err := matchers.FindDocsMatchingYAMLPath(output, map[string]string{ + "$.kind": "Cluster", + "$.metadata.name": "win-wl", + }) + Expect(err).NotTo(HaveOccurred()) + Expect(len(clusterTemplates)).NotTo(Equal(0)) + + for _, clusterTemplate := range clusterTemplates { + Expect(clusterTemplate).To(matchers.HaveYAMLPathWithValue("$.metadata.annotations['tkg/plan']", "prod")) + } + }) + }) + + When("CNI is set", func() { + var value yttValues + BeforeEach(func() { + value = baseVal.DeepCopy() + }) + + It("succeeds rendering kubeadmConfigTemplate with CNI:antrea", func() { + kubeadmCommands := []string{ + "powershell C:/Temp/antrea.ps1 -ExecutionPolicy Bypass", + "powershell c:/k/prevent_windows_updates.ps1 -ExecutionPolicy Bypass", + "powershell C:/k/register_antrea_cleanup.ps1 -ExecutionPolicy Bypass", + } + kubeadmFiles := []string{ + "C:\\Temp\\antrea.ps1", + "c:\\k\\prevent_windows_updates.ps1", + "C:\\k\\antrea_cleanup.ps1", + "C:\\k\\register_antrea_cleanup.ps1", + } + value.Set("CNI", "antrea") + + output, err := ytt.RenderYTTTemplate(ytt.CommandOptions{}, paths, value.toReader()) + Expect(err).NotTo(HaveOccurred()) + + const kubeadmConfigName = "win-wl-windows-containerd" + kubeadmConfigTemplates, err := matchers.FindDocsMatchingYAMLPath(output, map[string]string{ + "$.kind": "KubeadmConfigTemplate", + "$.metadata.name": kubeadmConfigName, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(len(kubeadmConfigTemplates)).NotTo(Equal(0)) + + for _, kubeadmConfigTemplate := range kubeadmConfigTemplates { + // Match commands rendered in the template + kubeadmTemplatePath := "$.spec.template.spec.postKubeadmCommands" + for i, command := range kubeadmCommands { + Expect(kubeadmConfigTemplate).To(matchers.HaveYAMLPathWithValue( + fmt.Sprintf("%s[%d]", kubeadmTemplatePath, i), command, + )) + } + // Match files rendered in the template + filesTemplatePath := "$.spec.template.spec.files" + for i, file := range kubeadmFiles { + Expect(kubeadmConfigTemplate).To(matchers.HaveYAMLPathWithValue( + fmt.Sprintf("%s[%d]['path']", filesTemplatePath, i), file, + )) + } + } + }) + + It("succeeds with cluster objects with CNI:antrea", func() { + value.Set("CNI", "antrea") + + output, err := ytt.RenderYTTTemplate(ytt.CommandOptions{}, paths, value.toReader()) + Expect(err).NotTo(HaveOccurred()) + + tests := []struct { + name string + kind string + }{ + { + name: "tkg-antrea-rc-init", + kind: "ConfigMap", + }, + { + name: "win-wl-antrea-addon", + kind: "Secret", + }, + { + name: "win-wl-tkg-antrea-cls-init", + kind: "ClusterResourceSet", + }, + { + name: "win-wl-windows-containerd", + kind: "KubeadmConfigTemplate", + }, + } + + // Check the existence of Antrea installation objects + for _, tt := range tests { + templates, err := matchers.FindDocsMatchingYAMLPath(output, map[string]string{ + "$.kind": tt.kind, + "$.metadata.name": tt.name, + }) + Expect(err).NotTo(HaveOccurred()) + Expect(len(templates)).ToNot(Equal(0)) + } + }) + }) +})