From 35a6e3a32aab696b33353dd288f73622a0990977 Mon Sep 17 00:00:00 2001 From: Enxebre Date: Thu, 25 Oct 2018 20:38:11 +0200 Subject: [PATCH] drop deprecated config. Use installer config --- ...hine-api-operator_01_images.configmap.yaml | 2 +- .../clusterapi-manager-controllers.yaml | 24 +-- pkg/operator/config.go | 163 +++++++++++++++++ pkg/operator/config_test.go | 170 ++++++++++++++++++ pkg/operator/fixtures/images.json | 5 + pkg/operator/operator.go | 74 +++----- pkg/operator/operator_test.go | 38 ---- pkg/operator/sync.go | 17 +- pkg/render/config.go | 59 ------ pkg/render/render.go | 64 ------- pkg/render/render_test.go | 121 ------------- tests/e2e/main.go | 19 +- tests/e2e/manifests/install-config.yaml | 32 ++++ tests/e2e/manifests/mao-config.yaml | 4 - 14 files changed, 405 insertions(+), 387 deletions(-) create mode 100644 pkg/operator/config.go create mode 100644 pkg/operator/config_test.go create mode 100644 pkg/operator/fixtures/images.json delete mode 100644 pkg/operator/operator_test.go delete mode 100644 pkg/render/config.go delete mode 100644 pkg/render/render.go delete mode 100644 pkg/render/render_test.go create mode 100644 tests/e2e/manifests/install-config.yaml delete mode 100644 tests/e2e/manifests/mao-config.yaml diff --git a/install/0000_50_machine-api-operator_01_images.configmap.yaml b/install/0000_50_machine-api-operator_01_images.configmap.yaml index bd6fe16ebf..d385c083d6 100644 --- a/install/0000_50_machine-api-operator_01_images.configmap.yaml +++ b/install/0000_50_machine-api-operator_01_images.configmap.yaml @@ -4,4 +4,4 @@ metadata: name: machine-api-operator-images namespace: openshift-cluster-api data: - images.json: '{"clusterAPIControllerAWS": "docker.io/openshift/origin-aws-machine-controllers:v4.0.0", "clusterAPIControllerManagerAWS": "docker.io/openshift/origin-aws-machine-controllers:v4.0.0", "clusterAPIControllerOpenStack": "docker.io/openshift/origin-openstack-machine-controllers:v4.0.0", "clusterAPIControllerManagerOpenStack": "docker.io/openshift/origin-openstack-machine-controllers:v4.0.0", "clusterAPIControllerManagerLibvirt": "docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0", "clusterAPIControllerLibvirt": "docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0"}' + images.json: '{"clusterAPIControllerAWS": "docker.io/openshift/origin-aws-machine-controllers:v4.0.0", "clusterAPIControllerOpenStack": "docker.io/openshift/origin-openstack-machine-controllers:v4.0.0", "clusterAPIControllerLibvirt": "docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0"}' diff --git a/owned-manifests/clusterapi-manager-controllers.yaml b/owned-manifests/clusterapi-manager-controllers.yaml index 53d25f77bb..48da8e429e 100644 --- a/owned-manifests/clusterapi-manager-controllers.yaml +++ b/owned-manifests/clusterapi-manager-controllers.yaml @@ -37,15 +37,7 @@ spec: operator: Exists containers: - name: controller-manager - {{- if eq .Provider "aws" }} - image: {{ .Images.ClusterAPIControllerManagerAWS }} - {{end}} - {{- if eq .Provider "openstack" }} - image: {{ .Images.ClusterAPIControllerManagerOpenStack }} - {{end}} - {{- if eq .Provider "libvirt" }} - image: {{ .Images.ClusterAPIControllerManagerLibvirt }} - {{- end}} + image: {{ .Controller }} command: - "./manager" resources: @@ -55,18 +47,8 @@ spec: limits: cpu: 100m memory: 30Mi - {{- if eq .Provider "aws" }} - - name: aws-machine-controller - image: {{ .Images.ClusterAPIControllerAWS }} - {{end}} - {{- if eq .Provider "openstack" }} - - name: openstack-machine-controller - image: {{ .Images.ClusterAPIControllerOpenStack }} - {{end}} - {{- if eq .Provider "libvirt" }} - - name: libvirt-machine-controller - image: {{ .Images.ClusterAPIControllerLibvirt }} - {{- end}} + - name: machine-controller + image: {{ .Controller }} env: - name: NODE_NAME valueFrom: diff --git a/pkg/operator/config.go b/pkg/operator/config.go new file mode 100644 index 0000000000..ed32e80bb0 --- /dev/null +++ b/pkg/operator/config.go @@ -0,0 +1,163 @@ +package operator + +import ( + "encoding/json" + "fmt" + "io/ioutil" + + "github.com/ghodss/yaml" + + "bytes" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "reflect" + "text/template" +) + +const ( + // ClusterConfigNamespace is the namespace containing the cluster config + ClusterConfigNamespace = "kube-system" + // ClusterConfigName is the name of the cluster config configmap + ClusterConfigName = "cluster-config-v1" + // InstallConfigKey is the key in the cluster config configmap containing yaml installConfig data + InstallConfigKey = "install-config" + // AWSPlatformType is used to install on AWS + AWSProvider = Provider("aws") + // LibvirtPlatformType is used to install of libvirt + LibvirtProvider = Provider("libvirt") + // OpenStackPlatformType is used to install on OpenStack + OpenStackProvider = Provider("openstack") +) + +type Provider string + +// OperatorConfig contains configuration for MAO +type OperatorConfig struct { + TargetNamespace string `json:"targetNamespace"` + Controller string `json:"images"` +} + +// Images allows build systems to inject images for MAO components +type Images struct { + ClusterAPIControllerAWS string `json:"clusterAPIControllerAWS"` + ClusterAPIControllerOpenStack string `json:"clusterAPIControllerOpenStack"` + ClusterAPIControllerLibvirt string `json:"clusterAPIControllerLibvirt"` +} + +// InstallConfig contains the mao relevant config coming from the install config, i.e provider +type InstallConfig struct { + InstallPlatform `json:"platform"` +} + +// InstallPlatform is the configuration for the specific platform upon which to perform +// the installation. Only one of the platform configuration should be set +type InstallPlatform struct { + // AWS is the configuration used when running on AWS + AWS interface{} `json:"aws,omitempty"` + + // Libvirt is the configuration used when running on libvirt + Libvirt interface{} `json:"libvirt,omitempty"` + + // OpenStack is the configuration used when running on OpenStack + OpenStack interface{} `json:"openstack,omitempty"` +} + +func getInstallConfig(client kubernetes.Interface) (*InstallConfig, error) { + cm, err := client.CoreV1().ConfigMaps(ClusterConfigNamespace).Get(ClusterConfigName, metav1.GetOptions{}) + if err != nil { + return nil, fmt.Errorf("failed getting clusterconfig %s/%s: %v", ClusterConfigNamespace, ClusterConfigName, err) + } + + return getInstallConfigFromClusterConfig(cm) +} + +// getInstallConfigFromClusterConfig builds an install config from the cluster config. +func getInstallConfigFromClusterConfig(clusterConfig *corev1.ConfigMap) (*InstallConfig, error) { + icYaml, ok := clusterConfig.Data[InstallConfigKey] + if !ok { + return nil, fmt.Errorf("missing %q in configmap", InstallConfigKey) + } + var ic InstallConfig + if err := yaml.Unmarshal([]byte(icYaml), &ic); err != nil { + return nil, fmt.Errorf("invalid InstallConfig: %v yaml: %s", err, icYaml) + } + return &ic, nil +} + +func getProviderFromInstallConfig(installConfig *InstallConfig) (Provider, error) { + v := reflect.ValueOf(installConfig.InstallPlatform) + var nonNilFields int + + for i := 0; i < v.NumField(); i++ { + if v.Field(i).Interface() != nil { + nonNilFields = nonNilFields + 1 + } + if nonNilFields > 1 { + return "", fmt.Errorf("more than one platform provider given") + } + } + + if installConfig.AWS != nil { + return AWSProvider, nil + } + if installConfig.Libvirt != nil { + return LibvirtProvider, nil + } + if installConfig.OpenStack != nil { + return OpenStackProvider, nil + } + return "", fmt.Errorf("no platform provider found on install config") +} + +func getImagesFromJSONFile(filePath string) (*Images, error) { + data, err := ioutil.ReadFile(filePath) + if err != nil { + return nil, err + } + + var i Images + if err := json.Unmarshal(data, &i); err != nil { + return nil, err + } + return &i, nil +} + +func getProviderControllerFromImages(provider Provider, images Images) (string, error) { + switch provider { + case AWSProvider: + return images.ClusterAPIControllerAWS, nil + case LibvirtProvider: + return images.ClusterAPIControllerLibvirt, nil + case OpenStackProvider: + return images.ClusterAPIControllerOpenStack, nil + } + return "", fmt.Errorf("not known platform provider given %s", provider) +} + +// PopulateTemplate receives a template file path and renders its content populated with the config +func PopulateTemplate(config *OperatorConfig, path string) ([]byte, error) { + + data, err := ioutil.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("failed reading file, %v", err) + } + + buf := &bytes.Buffer{} + tmpl, err := template.New("").Option("missingkey=error").Parse(string(data)) + if err != nil { + return nil, err + } + + tmplData := struct { + OperatorConfig + }{ + OperatorConfig: *config, + } + + if err := tmpl.Execute(buf, tmplData); err != nil { + return nil, err + } + + return buf.Bytes(), nil +} diff --git a/pkg/operator/config_test.go b/pkg/operator/config_test.go new file mode 100644 index 0000000000..02aeec87e3 --- /dev/null +++ b/pkg/operator/config_test.go @@ -0,0 +1,170 @@ +package operator + +import ( + "testing" + + "k8s.io/api/core/v1" +) + +var ( + imagesJSONFile = "fixtures/images.json" + expectedAWSImage = "docker.io/openshift/origin-aws-machine-controllers:v4.0.0" + expectedLibvirtImage = "docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0" + expectedOpenstackImage = "docker.io/openshift/origin-openstack-machine-controllers:v4.0.0" +) + +func TestInstallConfigFromClusterConfig(t *testing.T) { + data := make(map[string]string) + data[InstallConfigKey] = ` +admin: + email: test + password: test + sshKey: | + test +baseDomain: a-domain.com +clusterID: a7265676-7dc3-4ff3-8759-f2d6e3934e76 +machines: +- name: master + platform: {} + replicas: 3 +- name: worker + platform: {} + replicas: 3 +metadata: + creationTimestamp: null + name: test +networking: + podCIDR: 10.2.0.0/16 + serviceCIDR: 10.3.0.0/16 + type: flannel +platform: + aws: + region: us-east-1 + vpcCIDRBlock: 10.0.0.0/16 + vpcID: "" +pullSecret: “" +` + cfg := v1.ConfigMap{ + Data: data, + } + + res, err := getInstallConfigFromClusterConfig(&cfg) + if err != nil { + t.Errorf("failed to get install config: %v", err) + } + if res.InstallPlatform.AWS != nil && res.InstallPlatform.Libvirt == nil && res.InstallPlatform.OpenStack == nil { + t.Logf("got install config successfully: %+v", res) + } else { + t.Errorf("failed to getInstallConfigFromClusterConfig. Expected aws to be not nil, got: %+v", res) + } +} + +func TestGetProviderFromInstallConfig(t *testing.T) { + var notNil = "not nil" + tests := []struct { + ic *InstallConfig + expected Provider + }{{ + ic: &InstallConfig{ + InstallPlatform{ + AWS: notNil, + Libvirt: nil, + OpenStack: nil, + }, + }, + expected: AWSProvider, + }, + { + ic: &InstallConfig{ + InstallPlatform{ + AWS: nil, + Libvirt: notNil, + OpenStack: nil, + }, + }, + expected: LibvirtProvider, + }, + { + ic: &InstallConfig{ + InstallPlatform{ + AWS: nil, + Libvirt: nil, + OpenStack: notNil, + }, + }, + expected: OpenStackProvider, + }} + + for _, test := range tests { + res, err := getProviderFromInstallConfig(test.ic) + if err != nil { + t.Errorf("failed getProviderFromInstallConfig: %v", err) + } + if test.expected != res { + t.Errorf("failed getProviderFromInstallConfig. Expected: %q, got: %q", test.expected, res) + } + } + + // More than one installPlatform should error + ic := &InstallConfig{ + InstallPlatform{ + AWS: nil, + Libvirt: notNil, + OpenStack: notNil, + }, + } + res, err := getProviderFromInstallConfig(ic) + if err == nil { + t.Errorf("failed getProviderFromInstallConfig. Expected error, got: %v", res) + } +} + +func TestGetImagesFromJSONFile(t *testing.T) { + img, err := getImagesFromJSONFile(imagesJSONFile) + if err != nil { + t.Errorf("failed getImagesFromJSONFile") + } + if img.ClusterAPIControllerAWS != expectedAWSImage { + t.Errorf("failed getImagesFromJSONFile. Expected: %s, got: %s", expectedAWSImage, img.ClusterAPIControllerAWS) + } + if img.ClusterAPIControllerLibvirt != expectedLibvirtImage { + t.Errorf("failed getImagesFromJSONFile. Expected: %s, got: %s", expectedLibvirtImage, img.ClusterAPIControllerLibvirt) + } + if img.ClusterAPIControllerOpenStack != expectedOpenstackImage { + t.Errorf("failed getImagesFromJSONFile. Expected: %s, got: %s", expectedOpenstackImage, img.ClusterAPIControllerOpenStack) + } +} + +func TestGetProviderControllerFromImages(t *testing.T) { + tests := []struct { + provider Provider + expectedImage string + }{{ + provider: AWSProvider, + expectedImage: expectedAWSImage, + }, + { + provider: LibvirtProvider, + expectedImage: expectedLibvirtImage, + }, + { + provider: OpenStackProvider, + expectedImage: expectedOpenstackImage, + }} + + imagesJSONFile := "fixtures/images.json" + img, err := getImagesFromJSONFile(imagesJSONFile) + if err != nil { + t.Errorf("failed getImagesFromJSONFile, %v", err) + } + + for _, test := range tests { + res, err := getProviderControllerFromImages(test.provider, *img) + if err != nil { + t.Errorf("failed getProviderControllerFromImages: %v", err) + } + if test.expectedImage != res { + t.Errorf("failed getProviderControllerFromImages. Expected: %q, got: %q", test.expectedImage, res) + } + } +} diff --git a/pkg/operator/fixtures/images.json b/pkg/operator/fixtures/images.json new file mode 100644 index 0000000000..8a75c9f3ab --- /dev/null +++ b/pkg/operator/fixtures/images.json @@ -0,0 +1,5 @@ +{ + "clusterAPIControllerAWS": "docker.io/openshift/origin-aws-machine-controllers:v4.0.0", + "clusterAPIControllerOpenStack": "docker.io/openshift/origin-openstack-machine-controllers:v4.0.0", + "clusterAPIControllerLibvirt": "docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0" +} diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index f015735869..2e20ca42a4 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -1,12 +1,9 @@ package operator import ( - "encoding/json" "fmt" - "io/ioutil" "time" - "github.com/ghodss/yaml" "github.com/golang/glog" cvoclientset "github.com/openshift/cluster-version-operator/pkg/generated/clientset/versioned" @@ -14,7 +11,6 @@ import ( apiextclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" apiextinformersv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1beta1" apiextlistersv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1beta1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/wait" appsinformersv1 "k8s.io/client-go/informers/apps/v1" @@ -27,8 +23,6 @@ import ( "k8s.io/client-go/tools/record" "k8s.io/client-go/util/workqueue" - "github.com/openshift/machine-api-operator/pkg/render" - apiregistrationclientset "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset" "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset" "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/scheme" @@ -42,9 +36,6 @@ const ( // // 5ms, 10ms, 20ms, 40ms, 80ms, 160ms, 320ms, 640ms, 1.3s, 2.6s, 5.1s, 10.2s, 20.4s, 41s, 82s maxRetries = 15 - providerAWS = "aws" - providerOpenStack = "openstack" - providerLibvirt = "libvirt" ownedManifestsDir = "owned-manifests" ) @@ -204,26 +195,6 @@ func (optr *Operator) handleErr(err error, key interface{}) { optr.queue.Forget(key) } -func (optr *Operator) updateImageDetails(config *render.OperatorConfig) error { - filesData := map[string][]byte{} - files := []string{ - optr.imagesFile, - } - for _, file := range files { - data, err := ioutil.ReadFile(file) - if err != nil { - return err - } - filesData[file] = data - } - var imgs render.Images - if err := json.Unmarshal(filesData[optr.imagesFile], &imgs); err != nil { - return err - } - (*config).Images = &imgs - return nil -} - func (optr *Operator) sync(key string) error { startTime := time.Now() glog.V(4).Infof("Started syncing operator %q (%v)", key, startTime) @@ -232,14 +203,9 @@ func (optr *Operator) sync(key string) error { }() glog.Infof("Getting operator config using kubeclient") - operatorConfig, err := optr.getOperatorConfig() - if err != nil { - glog.Fatalf("Error decoding operator config: %v", err) - return err - } - err = optr.updateImageDetails(operatorConfig) + operatorConfig, err := optr.maoConfigFromInstallConfig() if err != nil { - glog.Fatalf("Error getting image details: %v", err) + glog.Fatalf("failed getting operator config: %v", err) return err } @@ -258,29 +224,29 @@ func (optr *Operator) sync(key string) error { return optr.syncAll(*operatorConfig) } -func (optr *Operator) getOperatorConfig() (*render.OperatorConfig, error) { - clusterConfigNamespace := "kube-system" - clusterConfigName := "cluster-config-v1" - clusterConfig, err := optr.kubeClient.CoreV1().ConfigMaps(clusterConfigNamespace).Get(clusterConfigName, metav1.GetOptions{}) +func (optr *Operator) maoConfigFromInstallConfig() (*OperatorConfig, error) { + installConfig, err := getInstallConfig(optr.kubeClient) if err != nil { - return nil, fmt.Errorf("could not find cluster-config-v1 namespace: %v", err) + return nil, err } - return optr.mcFromClusterConfig(clusterConfig) -} -func (optr *Operator) mcFromClusterConfig(cm *v1.ConfigMap) (*render.OperatorConfig, error) { - mcKey := "mao-config" - var operatorConfig render.OperatorConfig - mcData, ok := cm.Data[mcKey] - if !ok { - return nil, fmt.Errorf("%s doesn't exist", mcKey) + provider, err := getProviderFromInstallConfig(installConfig) + if err != nil { + return nil, err } - if err := yaml.Unmarshal([]byte(mcData), &operatorConfig); err != nil { - return nil, fmt.Errorf("failed unmarshalling config file: %v", err) + images, err := getImagesFromJSONFile(optr.imagesFile) + if err != nil { + return nil, err } - if operatorConfig.TargetNamespace == "" { - operatorConfig.TargetNamespace = optr.namespace + + controllerImage, err := getProviderControllerFromImages(provider, *images) + if err != nil { + return nil, err } - return &operatorConfig, nil + + return &OperatorConfig{ + optr.namespace, + controllerImage, + }, nil } diff --git a/pkg/operator/operator_test.go b/pkg/operator/operator_test.go deleted file mode 100644 index d117aee69c..0000000000 --- a/pkg/operator/operator_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package operator - -import ( - "testing" - - "k8s.io/api/core/v1" -) - -func TestMinIncomingConfig(t *testing.T) { - data := make(map[string]string) - data["mao-config"] = ` -apiVersion: v1 -kind: machineAPIOperatorConfig -provider: aws -targetNamespace: openshift-cluster-api -` - cfg := v1.ConfigMap{ - Data: data, - } - - optr := &Operator{} - res, err := optr.mcFromClusterConfig(&cfg) - if err != nil { - t.Errorf("failed to get config: %v", err) - } - if res.APIVersion != "v1" { - t.Errorf("failed to get config: %v", err) - } - if res.Kind != "machineAPIOperatorConfig" { - t.Errorf("failed to get config: %v", err) - } - if res.Provider != "aws" { - t.Errorf("failed to get config: %v", err) - } - if res.TargetNamespace != "openshift-cluster-api" { - t.Errorf("failed to get config: %v", err) - } -} diff --git a/pkg/operator/sync.go b/pkg/operator/sync.go index f5e019db20..35566d7c90 100644 --- a/pkg/operator/sync.go +++ b/pkg/operator/sync.go @@ -14,12 +14,11 @@ import ( "github.com/openshift/machine-api-operator/lib/resourceapply" "github.com/openshift/machine-api-operator/lib/resourceread" - "github.com/openshift/machine-api-operator/pkg/render" ) -type syncFunc func(config render.OperatorConfig) error +type syncFunc func(config OperatorConfig) error -func (optr *Operator) syncAll(rconfig render.OperatorConfig) error { +func (optr *Operator) syncAll(config OperatorConfig) error { glog.Infof("Syncing operatorstatus") if err := optr.syncStatus(v1.OperatorStatusCondition{ @@ -36,7 +35,7 @@ func (optr *Operator) syncAll(rconfig render.OperatorConfig) error { }) } -func (optr *Operator) syncCustomResourceDefinitions(config render.OperatorConfig) error { +func (optr *Operator) syncCustomResourceDefinitions(config OperatorConfig) error { crds := []string{ "/machine.crd.yaml", "/machineset.crd.yaml", @@ -45,7 +44,7 @@ func (optr *Operator) syncCustomResourceDefinitions(config render.OperatorConfig } for _, crd := range crds { - crdBytes, err := render.PopulateTemplate(&config, ownedManifestsDir+crd) + crdBytes, err := PopulateTemplate(&config, ownedManifestsDir+crd) if err != nil { return fmt.Errorf("error getting asset %s: %v", crd, err) } @@ -63,8 +62,8 @@ func (optr *Operator) syncCustomResourceDefinitions(config render.OperatorConfig return nil } -func (optr *Operator) syncClusterAPIController(config render.OperatorConfig) error { - crBytes, err := render.PopulateTemplate(&config, fmt.Sprintf("%s/clusterapi-manager-cluster-role.yaml", ownedManifestsDir)) +func (optr *Operator) syncClusterAPIController(config OperatorConfig) error { + crBytes, err := PopulateTemplate(&config, fmt.Sprintf("%s/clusterapi-manager-cluster-role.yaml", ownedManifestsDir)) if err != nil { return err } @@ -73,7 +72,7 @@ func (optr *Operator) syncClusterAPIController(config render.OperatorConfig) err if err != nil { return err } - crbBytes, err := render.PopulateTemplate(&config, fmt.Sprintf("%s/clusterapi-manager-cluster-role-binding.yaml", ownedManifestsDir)) + crbBytes, err := PopulateTemplate(&config, fmt.Sprintf("%s/clusterapi-manager-cluster-role-binding.yaml", ownedManifestsDir)) if err != nil { return err } @@ -82,7 +81,7 @@ func (optr *Operator) syncClusterAPIController(config render.OperatorConfig) err if err != nil { return err } - controllerBytes, err := render.PopulateTemplate(&config, fmt.Sprintf("%s/clusterapi-manager-controllers.yaml", ownedManifestsDir)) + controllerBytes, err := PopulateTemplate(&config, fmt.Sprintf("%s/clusterapi-manager-controllers.yaml", ownedManifestsDir)) if err != nil { return err } diff --git a/pkg/render/config.go b/pkg/render/config.go deleted file mode 100644 index ce3664ca08..0000000000 --- a/pkg/render/config.go +++ /dev/null @@ -1,59 +0,0 @@ -package render - -import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - -const ( - // Kind is the TypeMeta.Kind for the OperatorConfig. - Kind = "MachineAPIOperatorConfig" - // APIVersion is the TypeMeta.APIVersion for the OperatorConfig. - APIVersion = "v1" -) - -// OperatorConfig contains configuration for MAO -type OperatorConfig struct { - metav1.TypeMeta `json:",inline"` - TargetNamespace string `json:"targetNamespace"` - APIServiceCA string `json:"apiServiceCA"` - Provider string `json:"provider"` - AWS *AWSConfig `json:"aws"` - OpenStack *OpenStackConfig `json:"openstack,omitempty"` - Libvirt *LibvirtConfig `json:"libvirt"` - Images *Images `json:"images"` -} - -// LibvirtConfig contains specific config for Libvirt -type LibvirtConfig struct { - URI string `json:"uri"` - NetworkName string `json:"networkName"` - IPRange string `json:"ipRange"` - ClusterName string `json:"clusterName"` - Replicas string `json:"replicas"` -} - -// AWSConfig contains specific config for AWS -type AWSConfig struct { - ClusterName string `json:"clusterName"` - ClusterID string `json:"clusterID"` - Region string `json:"region"` - AvailabilityZone string `json:"availabilityZone"` - Image string `json:"image"` - ReleaseChannel string `json:"releaseChannel"` - ContainerLinuxVersion string `json:"containerLinuxVersion"` - Replicas string `json:"replicas"` - WithCreds bool `json:"withCreds"` -} - -type OpenStackConfig struct { -} - -// Images allows build systems to inject images for MAO components. -type Images struct { - ClusterAPIControllerAWS string `json:"clusterAPIControllerAWS"` - ClusterAPIControllerOpenStack string `json:"clusterAPIControllerOpenStack"` - ClusterAPIControllerLibvirt string `json:"clusterAPIControllerLibvirt"` - ClusterAPIControllerManagerAWS string `json:"clusterAPIControllerManagerAWS"` - ClusterAPIControllerManagerOpenStack string `json:"clusterAPIControllerManagerOpenStack"` - ClusterAPIControllerManagerLibvirt string `json:"clusterAPIControllerManagerLibvirt"` - ClusterAPIServer string `json:"clusterAPIServer"` - Etcd string `json:"Etcd"` -} diff --git a/pkg/render/render.go b/pkg/render/render.go deleted file mode 100644 index 19aecdacee..0000000000 --- a/pkg/render/render.go +++ /dev/null @@ -1,64 +0,0 @@ -package render - -import ( - "bytes" - "encoding/base64" - "fmt" - "io/ioutil" - "text/template" - - "github.com/golang/glog" -) - -const ( - providerAWS = "aws" - providerOpenStack = "openstack" - providerLibvirt = "libvirt" -) - -// Manifests takes the config object that contains the templated value, -// and uses that to render the templated manifest. -// 'config' must be non-nil, 'data' is the rawdata of a manifest file. -func Manifests(config *OperatorConfig, data []byte) ([]byte, error) { - if config == nil { - return nil, fmt.Errorf("no config is given") - } - - buf := new(bytes.Buffer) - - tmpl, err := template.New("").Option("missingkey=error").Parse(string(data)) - if err != nil { - return nil, err - } - - encodedCA := base64.StdEncoding.EncodeToString([]byte(config.APIServiceCA)) - - tmplData := struct { - OperatorConfig - EncodedAPIServiceCA string - }{ - OperatorConfig: *config, - EncodedAPIServiceCA: encodedCA, - } - - if err := tmpl.Execute(buf, tmplData); err != nil { - return nil, err - } - - return buf.Bytes(), nil -} - -// PopulateTemplate takes the config object, and uses that to render the templated manifest -func PopulateTemplate(config *OperatorConfig, path string) ([]byte, error) { - - data, err := ioutil.ReadFile(path) - if err != nil { - glog.Fatalf("Error reading %#v", err) - } - - populatedData, err := Manifests(config, data) - if err != nil { - glog.Fatalf("Unable to render manifests %q: %v", data, err) - } - return populatedData, nil -} diff --git a/pkg/render/render_test.go b/pkg/render/render_test.go deleted file mode 100644 index 36a11af900..0000000000 --- a/pkg/render/render_test.go +++ /dev/null @@ -1,121 +0,0 @@ -package render - -import ( - "io/ioutil" - "path/filepath" - "strings" - "testing" -) - -func testRenderManifest(t *testing.T, filename string, config *OperatorConfig, expectedConfig string) { - t.Helper() - - manifest, err := filepath.Abs(filename) - if err != nil { - t.Fatalf("Failed to obtain absolute path of manifest %q: %v", filename, err) - } - - data, err := ioutil.ReadFile(manifest) - if err != nil { - t.Fatalf("Failed to ingest manifest %q: %v", manifest, err) - } - - actual, err := Manifests(config, data) - if err != nil { - t.Fatalf("Failed to render manifest template: %v", err) - } - - a := strings.TrimSpace(expectedConfig) - b := strings.TrimSpace(string(actual)) - - if a != b { - t.Errorf("Expected:\n%v\nGot:\n%v", a, b) - } -} - -func TestClusterapiControllerManifest(t *testing.T) { - config := OperatorConfig{ - TargetNamespace: "go-test", - Provider: "libvirt", - Libvirt: &LibvirtConfig{ - URI: "qemu+tcp://host_private_ip/system", - NetworkName: "testNet", - IPRange: "192.168.124.0/24", - Replicas: "2", - ClusterName: "test", - }, - Images: &Images{ - ClusterAPIControllerManagerLibvirt: "docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0", - ClusterAPIControllerLibvirt: "docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0", - }, - } - - testRenderManifest(t, "../../owned-manifests/clusterapi-manager-controllers.yaml", &config, ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: clusterapi-manager-controllers - namespace: go-test - labels: - api: clusterapi - k8s-app: controller - tectonic-operators.coreos.com/managed-by: machine-api-operator -spec: - securityContext: - runAsNonRoot: true - runAsUser: 65534 - selector: - matchLabels: - api: clusterapi - k8s-app: controller - replicas: 1 - template: - metadata: - labels: - api: clusterapi - k8s-app: controller - spec: - nodeSelector: - node-role.kubernetes.io/master: "" - tolerations: - - effect: NoSchedule - key: node-role.kubernetes.io/master - - key: CriticalAddonsOnly - operator: Exists - - effect: NoExecute - key: node.alpha.kubernetes.io/notReady - operator: Exists - - effect: NoExecute - key: node.alpha.kubernetes.io/unreachable - operator: Exists - containers: - - name: controller-manager - image: docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0 - command: - - "./manager" - resources: - requests: - cpu: 100m - memory: 20Mi - limits: - cpu: 100m - memory: 30Mi - - name: libvirt-machine-controller - image: docker.io/openshift/origin-libvirt-machine-controllers:v4.0.0 - env: - - name: NODE_NAME - valueFrom: - fieldRef: - fieldPath: spec.nodeName - command: - - /machine-controller-manager - args: - - --log-level=debug - resources: - requests: - cpu: 100m - memory: 20Mi - limits: - cpu: 100m - memory: 30Mi`) -} diff --git a/tests/e2e/main.go b/tests/e2e/main.go index f79cbfc1b9..9b1e5f97c5 100644 --- a/tests/e2e/main.go +++ b/tests/e2e/main.go @@ -10,7 +10,6 @@ import ( "time" "github.com/golang/glog" - "github.com/openshift/machine-api-operator/pkg/render" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -254,21 +253,9 @@ var rootCmd = &cobra.Command{ } } - // generate mao config - maoConfigTemplateData, err := ioutil.ReadFile(filepath.Join(assetsPath, "manifests/mao-config.yaml")) + installConfigData, err := ioutil.ReadFile(filepath.Join(assetsPath, "manifests/install-config.yaml")) if err != nil { - glog.Fatalf("Error reading %#v", err) - } - configValues := &render.OperatorConfig{ - AWS: &render.AWSConfig{ - ClusterID: clusterID, - ClusterName: clusterID, - Region: region, - }, - } - maoConfigPopulatedData, err := render.Manifests(configValues, maoConfigTemplateData) - if err != nil { - glog.Fatalf("Unable to render manifests %q: %v", maoConfigTemplateData, err) + glog.Fatalf("Unable to render manifests %q: %v", installConfigData, err) } else { mapConfigMap := &apiv1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ @@ -276,7 +263,7 @@ var rootCmd = &cobra.Command{ Namespace: "kube-system", }, Data: map[string]string{ - "mao-config": string(maoConfigPopulatedData), + "install-config": string(installConfigData), }, } if err := createConfigMap(testConfig, mapConfigMap); err != nil { diff --git a/tests/e2e/manifests/install-config.yaml b/tests/e2e/manifests/install-config.yaml new file mode 100644 index 0000000000..0803865903 --- /dev/null +++ b/tests/e2e/manifests/install-config.yaml @@ -0,0 +1,32 @@ +admin: + email: test + password: test + sshKey: | + ssh-rsa +baseDomain: test +clusterID: a7265676-7dc3-4ff3-8759-f2d6e3934e76 +machines: +- name: master + platform: {} + replicas: 3 +- name: worker + platform: {} + replicas: 3 + name: meh +networking: + podCIDR: 10.2.0.0/16 + serviceCIDR: 10.3.0.0/16 + type: flannel +platform: + aws: + region: us-east-1 + vpcCIDRBlock: 10.0.0.0/16 + vpcID: "" +pullSecret: '' +network-config: | +apiVersion: v1 +calicoConfig: + mtu: "1450" +kind: TectonicNetworkOperatorConfig +networkProfile: flannel +podCIDR: 10.2.0.0/16 diff --git a/tests/e2e/manifests/mao-config.yaml b/tests/e2e/manifests/mao-config.yaml deleted file mode 100644 index b4aa5cef62..0000000000 --- a/tests/e2e/manifests/mao-config.yaml +++ /dev/null @@ -1,4 +0,0 @@ -apiVersion: v1 -kind: machineAPIOperatorConfig -provider: aws -targetNamespace: openshift-cluster-api