|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + tfv1 "github.com/rancher/terraform-controller/pkg/apis/terraformcontroller.cattle.io/v1" |
| 8 | + tf "github.com/rancher/terraform-controller/pkg/generated/clientset/versioned/typed/terraformcontroller.cattle.io/v1" |
| 9 | + "github.com/rancher/wrangler/pkg/crd" |
| 10 | + "github.com/rancher/wrangler/pkg/signals" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + v12 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/util/wait" |
| 16 | + "k8s.io/client-go/kubernetes" |
| 17 | + "k8s.io/client-go/rest" |
| 18 | + "k8s.io/client-go/tools/clientcmd" |
| 19 | +) |
| 20 | + |
| 21 | +type E2E struct { |
| 22 | + ctx context.Context |
| 23 | + cs *kubernetes.Clientset |
| 24 | + cfg *rest.Config |
| 25 | + kubeconfig string |
| 26 | + namespace string |
| 27 | + module_url string |
| 28 | + crds []crd.CRD |
| 29 | +} |
| 30 | + |
| 31 | +func NewE2E(namespace, kubeconfig, module string, crdsNames []string) *E2E { |
| 32 | + cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig) |
| 33 | + if err != nil { |
| 34 | + logrus.Fatalf("Error building kubeconfig: %s", err.Error()) |
| 35 | + } |
| 36 | + |
| 37 | + cs, err := kubernetes.NewForConfig(cfg) |
| 38 | + if err != nil { |
| 39 | + logrus.Fatalf("Error building kubeconfig: %s", err.Error()) |
| 40 | + } |
| 41 | + |
| 42 | + var crds = make([]crd.CRD, len(crdsNames)) |
| 43 | + for k, v := range crdsNames { |
| 44 | + crds[k] = crd.FromGV(tfv1.SchemeGroupVersion, v) |
| 45 | + } |
| 46 | + |
| 47 | + return &E2E{ |
| 48 | + ctx: signals.SetupSignalHandler(context.Background()), |
| 49 | + cs: cs, |
| 50 | + cfg: cfg, |
| 51 | + kubeconfig: kubeconfig, |
| 52 | + namespace: namespace, |
| 53 | + module_url: module, |
| 54 | + crds: crds, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (e *E2E) createState() error { |
| 59 | + cs, err := tf.NewForConfig(e.cfg) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + _, err = cs.States(e.namespace).Create(e.getState()) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func (e *E2E) createModule() error { |
| 73 | + cs, err := tf.NewForConfig(e.cfg) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + _, err = cs.Modules(e.namespace).Create(e.getModule()) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + err = wait.Poll(time.Second, 15*time.Second, func() (bool, error) { |
| 84 | + module, err := cs.Modules(e.namespace).Get(e.generateModuleName(), v12.GetOptions{}) |
| 85 | + if err == nil && module.Status.ContentHash != "" { |
| 86 | + return true, nil |
| 87 | + } |
| 88 | + |
| 89 | + if errors.IsNotFound(err) { |
| 90 | + return false, nil |
| 91 | + } |
| 92 | + |
| 93 | + logrus.Printf("Waiting for Module to be processed by terraform-controller: %+v\n", err) |
| 94 | + return false, err |
| 95 | + }) |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func (e *E2E) createVariables() error { |
| 105 | + _, err := e.cs.CoreV1().Secrets(e.namespace).Create(e.getSecret()) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + _, err = e.cs.CoreV1().Secrets(e.namespace).Create(e.getSecretEnv()) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + _, err = e.cs.CoreV1().ConfigMaps(e.namespace).Create(e.getConfigMap()) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + _, err = e.cs.CoreV1().ConfigMaps(e.namespace).Create(e.getConfigMapEnv()) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (e *E2E) getState() *tfv1.State { |
| 129 | + return &tfv1.State{ |
| 130 | + ObjectMeta: v12.ObjectMeta{ |
| 131 | + Name: e.generateStateName(), |
| 132 | + Namespace: e.namespace, |
| 133 | + }, |
| 134 | + Spec: tfv1.StateSpec{ |
| 135 | + ModuleName: e.generateModuleName(), |
| 136 | + AutoConfirm: true, |
| 137 | + DestroyOnDelete: true, |
| 138 | + Image: "rancher/terraform-controller-executor:v0.0.10-alpha1", |
| 139 | + Variables: tfv1.Variables{ |
| 140 | + ConfigNames: []string{e.generateConfigMapName()}, |
| 141 | + EnvConfigName: []string{e.generateConfigMapEnvName()}, |
| 142 | + SecretNames: []string{e.generateSecretName()}, |
| 143 | + EnvSecretNames: []string{e.generateSecretEnvName()}, |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func (e *E2E) generateStateName() string { |
| 150 | + return e.namespace + "-state" |
| 151 | +} |
| 152 | + |
| 153 | +func (e *E2E) generateModuleName() string { |
| 154 | + return e.namespace + "-module" |
| 155 | +} |
| 156 | + |
| 157 | +func (e *E2E) generateSecretName() string { |
| 158 | + return e.namespace + "-secret" |
| 159 | +} |
| 160 | + |
| 161 | +func (e *E2E) generateSecretEnvName() string { |
| 162 | + return e.namespace + "-secret-env" |
| 163 | +} |
| 164 | + |
| 165 | +func (e *E2E) getSecret() *v1.Secret { |
| 166 | + return &v1.Secret{ |
| 167 | + ObjectMeta: v12.ObjectMeta{ |
| 168 | + Name: e.generateSecretName(), |
| 169 | + Namespace: e.namespace, |
| 170 | + }, |
| 171 | + Type: "opaque", |
| 172 | + StringData: map[string]string{ |
| 173 | + "test_secret": e.namespace, |
| 174 | + }, |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func (e *E2E) getSecretEnv() *v1.Secret { |
| 179 | + return &v1.Secret{ |
| 180 | + ObjectMeta: v12.ObjectMeta{ |
| 181 | + Name: e.generateSecretEnvName(), |
| 182 | + Namespace: e.namespace, |
| 183 | + }, |
| 184 | + Type: "opaque", |
| 185 | + StringData: map[string]string{ |
| 186 | + "test_secret_env": e.namespace, |
| 187 | + }, |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +func (e *E2E) generateConfigMapName() string { |
| 192 | + return e.namespace + "-config-map" |
| 193 | +} |
| 194 | + |
| 195 | +func (e *E2E) generateConfigMapEnvName() string { |
| 196 | + return e.namespace + "-config-map-env" |
| 197 | +} |
| 198 | + |
| 199 | +func (e *E2E) getConfigMap() *v1.ConfigMap { |
| 200 | + return &v1.ConfigMap{ |
| 201 | + ObjectMeta: v12.ObjectMeta{ |
| 202 | + Name: e.generateConfigMapName(), |
| 203 | + Namespace: e.namespace, |
| 204 | + }, |
| 205 | + Data: map[string]string{ |
| 206 | + "test_config_map": e.namespace, |
| 207 | + }, |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func (e *E2E) getConfigMapEnv() *v1.ConfigMap { |
| 212 | + return &v1.ConfigMap{ |
| 213 | + ObjectMeta: v12.ObjectMeta{ |
| 214 | + Name: e.generateConfigMapEnvName(), |
| 215 | + Namespace: e.namespace, |
| 216 | + }, |
| 217 | + Data: map[string]string{ |
| 218 | + "test_config_map_env": e.namespace, |
| 219 | + }, |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +func (e *E2E) getModule() *tfv1.Module { |
| 224 | + return &tfv1.Module{ |
| 225 | + ObjectMeta: v12.ObjectMeta{ |
| 226 | + Name: e.generateModuleName(), |
| 227 | + Namespace: e.namespace, |
| 228 | + }, |
| 229 | + Spec: tfv1.ModuleSpec{ |
| 230 | + ModuleContent: tfv1.ModuleContent{ |
| 231 | + Git: tfv1.GitLocation{ |
| 232 | + URL: e.module_url, |
| 233 | + }, |
| 234 | + }, |
| 235 | + }, |
| 236 | + } |
| 237 | +} |
0 commit comments