From 8fd2928be7f3acfd59d0c94024f6da65dd84b554 Mon Sep 17 00:00:00 2001 From: jianli-97 Date: Mon, 31 Jul 2023 15:23:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86=E6=9C=AA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E5=87=BD=E6=95=B0=E6=88=96=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/infra/assets/cluster/cluster.go | 112 +++++++++--------- pkg/infra/assets/cluster/tfvars.go | 6 +- pkg/infra/terraform/init.go | 41 ------- pkg/infra/terraform/providers/providers.go | 35 ------ pkg/infra/terraform/stage.go | 26 ---- .../terraform/stages/openstack/stages.go | 19 --- pkg/infra/terraform/stages/platform/stages.go | 19 --- pkg/infra/terraform/stages/split.go | 43 ------- pkg/infra/terraform/state.go | 4 +- pkg/infra/terraform/terraform.go | 42 +++++-- 10 files changed, 91 insertions(+), 256 deletions(-) delete mode 100644 pkg/infra/terraform/init.go delete mode 100644 pkg/infra/terraform/providers/providers.go delete mode 100644 pkg/infra/terraform/stage.go delete mode 100644 pkg/infra/terraform/stages/openstack/stages.go delete mode 100644 pkg/infra/terraform/stages/platform/stages.go delete mode 100644 pkg/infra/terraform/stages/split.go diff --git a/pkg/infra/assets/cluster/cluster.go b/pkg/infra/assets/cluster/cluster.go index 5b38e6ae..012ada54 100644 --- a/pkg/infra/assets/cluster/cluster.go +++ b/pkg/infra/assets/cluster/cluster.go @@ -27,78 +27,80 @@ import ( "github.com/sirupsen/logrus" ) -/* - installDir:自定义工作目录,在该路径下自动创建terraform文件夹 - dir:该路径存放tf配置文件 - terraformDir:该路径包含bin、plugins目录,存放terraform执行文件以及所需plugins -*/ - type InfraProvider interface { Create() Destroy() } type Cluster struct { - InstallDir string - Platform string - Name string + // 由用户自定义,会在该路径下创建工作目录/nkd + Dir string + // 平台类别 + Platform string + // 节点类别 + Node string + Provider InfraProvider } // TODO: 配置文件准备阶段 func (c *Cluster) Create() error { - terraformDir := filepath.Join(c.InstallDir, "terraform") - dir := filepath.Join(terraformDir, c.Platform, c.Name) - - terraformVariables := &TerraformVariables{} - tfvarsFiles := make([]*assets.File, 0, len(terraformVariables.Files())+len(c.Platform)+len(c.Name)) - tfvarsFiles = append(tfvarsFiles, terraformVariables.Files()...) + // 工作目录,包含terraform执行文件以及所需plugins + workDir := filepath.Join(c.Dir, "nkd") + // tf配置文件所在目录 + tfDir := filepath.Join(workDir, c.Platform, c.Node) + + terraformVariables := &TerraformVariables{ + File: &assets.File{ + Filename: "newFile", + Data: []byte{}, + }, + } + tfvarsFile := terraformVariables.File - logrus.Infof("start to create %s in %s", c.Name, c.Platform) + logrus.Infof("start to create %s in %s", c.Node, c.Platform) - outputs, err := c.applyStage(dir, terraformDir, tfvarsFiles) + outputs, err := executeApplyTerraform(tfDir, workDir, tfvarsFile) if err != nil { - return errors.Wrapf(err, "failed to create %s in %s", c.Name, c.Platform) + return errors.Wrapf(err, "failed to create %s in %s", c.Node, c.Platform) } logrus.Info(string(outputs.Data)) - logrus.Infof("succeed in creating %s in %s", c.Name, c.Platform) + logrus.Infof("succeed in creating %s in %s", c.Node, c.Platform) return nil } -func (c *Cluster) applyStage(dir string, terraformDir string, tfvarsFiles []*assets.File) (*assets.File, error) { +func executeApplyTerraform(tfDir string, terraformDir string, tfvarsFile *assets.File) (*assets.File, error) { var applyOpts []tfexec.ApplyOption - for _, file := range tfvarsFiles { - if err := os.WriteFile(filepath.Join(dir, file.Filename), file.Data, 0o600); err != nil { - return nil, err - } - applyOpts = append(applyOpts, tfexec.VarFile(filepath.Join(dir, file.Filename))) + + if err := os.WriteFile(filepath.Join(tfDir, tfvarsFile.Filename), tfvarsFile.Data, 0o600); err != nil { + return nil, err } + applyOpts = append(applyOpts, tfexec.VarFile(filepath.Join(tfDir, tfvarsFile.Filename))) - return c.applyTerraform(dir, terraformDir, applyOpts...) + return applyTerraform(tfDir, terraformDir, applyOpts...) } -func (c *Cluster) applyTerraform(dir string, terraformDir string, applyOpts ...tfexec.ApplyOption) (*assets.File, error) { - applyErr := terraform.TFApply(dir, terraformDir, applyOpts...) +func applyTerraform(tfDir string, terraformDir string, applyOpts ...tfexec.ApplyOption) (*assets.File, error) { + applyErr := terraform.TFApply(tfDir, terraformDir, applyOpts...) - _, err := os.Stat(filepath.Join(dir, "terraform.tfstate")) + _, err := os.Stat(filepath.Join(tfDir, "terraform.tfstate")) if os.IsNotExist(err) { - logrus.Errorf("Failed to read tfstate: %v", err) return nil, errors.Wrap(err, "failed to read tfstate") } if applyErr != nil { - return nil, errors.WithMessage(applyErr, "failed to apply Terraform") + return nil, errors.Wrap(applyErr, "failed to apply Terraform") } - outputs, err := terraform.Outputs(dir, terraformDir) + outputs, err := terraform.Outputs(tfDir, terraformDir) if err != nil { - return nil, errors.Wrap(err, "could not get outputs file") + return nil, errors.Wrap(err, "failed to get outputs file") } outputsFile := &assets.File{ - Filename: "outputs", + Filename: "tfOutputs", Data: outputs, } @@ -106,44 +108,46 @@ func (c *Cluster) applyTerraform(dir string, terraformDir string, applyOpts ...t } func (c *Cluster) Destroy() error { - terraformDir := filepath.Join(c.InstallDir, "terraform") - dir := filepath.Join(terraformDir, c.Platform, c.Name) + workDir := filepath.Join(c.Dir, "nkd") + tfDir := filepath.Join(workDir, c.Platform, c.Node) - logrus.Infof("start to destroy %s in %s", c.Name, c.Platform) + logrus.Infof("start to destroy %s in %s", c.Node, c.Platform) // Question: Destroy的tfvarsFiles的获取 - terraformVariables := &TerraformVariables{} - tfvarsFiles := make([]*assets.File, 0, len(terraformVariables.Files())+len(c.Platform)+len(c.Name)) - tfvarsFiles = append(tfvarsFiles, terraformVariables.Files()...) + terraformVariables := &TerraformVariables{ + File: &assets.File{ + Filename: "newFile", + Data: []byte{}, + }, + } + tfvarsFile := terraformVariables.File - err := c.destroyStage(dir, terraformDir, tfvarsFiles) + err := executeDestroyTerraform(tfDir, workDir, tfvarsFile) if err != nil { - return errors.Wrapf(err, "failed to destroy %s in %s", c.Name, c.Platform) + return errors.Wrapf(err, "failed to destroy %s in %s", c.Node, c.Platform) } - os.Remove(dir) + os.RemoveAll(tfDir) - logrus.Infof("succeed in destroying %s in %s", c.Name, c.Platform) + logrus.Infof("succeed in destroying %s in %s", c.Node, c.Platform) return nil } -func (c *Cluster) destroyStage(dir string, terraformDir string, tfvarsFiles []*assets.File) error { +func executeDestroyTerraform(tfDir string, terraformDir string, tfvarsFile *assets.File) error { var destroyOpts []tfexec.DestroyOption - for _, file := range tfvarsFiles { - if err := os.WriteFile(filepath.Join(dir, file.Filename), file.Data, 0o600); err != nil { - return err - } - destroyOpts = append(destroyOpts, tfexec.VarFile(filepath.Join(dir, file.Filename))) + if err := os.WriteFile(filepath.Join(tfDir, tfvarsFile.Filename), tfvarsFile.Data, 0o600); err != nil { + return err } + destroyOpts = append(destroyOpts, tfexec.VarFile(filepath.Join(tfDir, tfvarsFile.Filename))) - return destroyTerraform(dir, terraformDir, destroyOpts...) + return destroyTerraform(tfDir, terraformDir, destroyOpts...) } -func destroyTerraform(dir string, terraformDir string, destroyOpts ...tfexec.DestroyOption) error { - destroyErr := terraform.TFDestroy(dir, terraformDir, destroyOpts...) +func destroyTerraform(tfDir string, terraformDir string, destroyOpts ...tfexec.DestroyOption) error { + destroyErr := terraform.TFDestroy(tfDir, terraformDir, destroyOpts...) if destroyErr != nil { - return errors.WithMessage(destroyErr, "failed to destroy Terraform") + return errors.Wrap(destroyErr, "failed to destroy Terraform") } return nil diff --git a/pkg/infra/assets/cluster/tfvars.go b/pkg/infra/assets/cluster/tfvars.go index a2f1b179..ab832563 100644 --- a/pkg/infra/assets/cluster/tfvars.go +++ b/pkg/infra/assets/cluster/tfvars.go @@ -28,11 +28,7 @@ import ( ) type TerraformVariables struct { - FileList []*assets.File -} - -func (t *TerraformVariables) Files() []*assets.File { - return t.FileList + File *assets.File } func Generate() error { diff --git a/pkg/infra/terraform/init.go b/pkg/infra/terraform/init.go deleted file mode 100644 index cbd3ad30..00000000 --- a/pkg/infra/terraform/init.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright 2023 KylinSoft Co., Ltd. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package terraform - -import ( - "context" - "path/filepath" - - "github.com/hashicorp/terraform-exec/tfexec" - "github.com/pkg/errors" -) - -// terraform init -func TFInit(dir string, terraformDir string) error { - tf, err := newTFExec(dir, terraformDir) - if err != nil { - return errors.Wrap(err, "failed to create a new tfexec") - } - - // 使用本地terraform插件 - err = tf.Init(context.Background(), tfexec.PluginDir(filepath.Join(terraformDir, "plugins"))) - if err != nil { - return errors.Wrap(err, "failed to init terraform") - } - - return nil -} diff --git a/pkg/infra/terraform/providers/providers.go b/pkg/infra/terraform/providers/providers.go deleted file mode 100644 index 986ee8aa..00000000 --- a/pkg/infra/terraform/providers/providers.go +++ /dev/null @@ -1,35 +0,0 @@ -/* -Copyright 2023 KylinSoft Co., Ltd. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package providers - -import "fmt" - -var ( - OpenStack = provider("openstack") -) - -type Provider struct { - Name string - Source string -} - -func provider(name string) Provider { - return Provider{ - Name: name, - Source: fmt.Sprintf("nkd/local/%s", name), - } -} diff --git a/pkg/infra/terraform/stage.go b/pkg/infra/terraform/stage.go deleted file mode 100644 index dabe48f2..00000000 --- a/pkg/infra/terraform/stage.go +++ /dev/null @@ -1,26 +0,0 @@ -/* -Copyright 2023 KylinSoft Co., Ltd. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package terraform - -import "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform/providers" - -type Stage interface { - Name() string - - // the list of providers that are used for the stage - Providers() []providers.Provider -} diff --git a/pkg/infra/terraform/stages/openstack/stages.go b/pkg/infra/terraform/stages/openstack/stages.go deleted file mode 100644 index 1b1f18a2..00000000 --- a/pkg/infra/terraform/stages/openstack/stages.go +++ /dev/null @@ -1,19 +0,0 @@ -package openstack - -import ( - "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform" - "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform/providers" - "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform/stages" -) - -var PlatformStages = []terraform.Stage{} - -func AddPlatformStage(name string) { - newStage := stages.NewStage( - "openstack", - name, - []providers.Provider{providers.OpenStack}, - ) - - PlatformStages = append(PlatformStages, newStage) -} diff --git a/pkg/infra/terraform/stages/platform/stages.go b/pkg/infra/terraform/stages/platform/stages.go deleted file mode 100644 index 7a99cad0..00000000 --- a/pkg/infra/terraform/stages/platform/stages.go +++ /dev/null @@ -1,19 +0,0 @@ -package platform - -import ( - "errors" - "fmt" - - "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform" - "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform/stages/openstack" -) - -func StagesForPlatform(platform string, stage string) ([]terraform.Stage, error) { - switch platform { - case "openstack": - openstack.AddPlatformStage(stage) - return openstack.PlatformStages, nil - default: - return nil, errors.New(fmt.Sprintf("unsupported platform %q", platform)) - } -} diff --git a/pkg/infra/terraform/stages/split.go b/pkg/infra/terraform/stages/split.go deleted file mode 100644 index caf0de7e..00000000 --- a/pkg/infra/terraform/stages/split.go +++ /dev/null @@ -1,43 +0,0 @@ -package stages - -import ( - "fmt" - - "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform/providers" -) - -type StageOption func(*SplitStage) - -func NewStage(platform, name string, providers []providers.Provider, opts ...StageOption) SplitStage { - s := SplitStage{ - platform: platform, - name: name, - providers: providers, - } - for _, opt := range opts { - opt(&s) - } - return s -} - -type SplitStage struct { - platform string - name string - providers []providers.Provider -} - -func (s SplitStage) Name() string { - return s.name -} - -func (s SplitStage) Providers() []providers.Provider { - return s.providers -} - -func (s SplitStage) StateFilename() string { - return fmt.Sprintf("terraform.%s.tfstate", s.name) -} - -func (s SplitStage) OutputsFilename() string { - return fmt.Sprintf("%s.tfvars.json", s.name) -} diff --git a/pkg/infra/terraform/state.go b/pkg/infra/terraform/state.go index 9509abc2..cf56d9a5 100644 --- a/pkg/infra/terraform/state.go +++ b/pkg/infra/terraform/state.go @@ -23,9 +23,7 @@ import ( "github.com/pkg/errors" ) -const StateFilename = "terraform.tfstate" - -// Reads the terraform state file. +// Read the terraform state file func Outputs(workingDir string, terraformBinary string) ([]byte, error) { tf, err := newTFExec(workingDir, terraformBinary) if err != nil { diff --git a/pkg/infra/terraform/terraform.go b/pkg/infra/terraform/terraform.go index 6bb6a9cf..b6fb8983 100644 --- a/pkg/infra/terraform/terraform.go +++ b/pkg/infra/terraform/terraform.go @@ -20,7 +20,6 @@ import ( "context" "os" "path/filepath" - "runtime" "github.com/hashicorp/terraform-exec/tfexec" "github.com/openshift/installer/pkg/lineprinter" @@ -28,9 +27,14 @@ import ( "github.com/sirupsen/logrus" ) -func newTFExec(dir string, terraformDir string) (*tfexec.Terraform, error) { - tfPath := filepath.Join(terraformDir, "bin", runtime.GOOS+"_"+runtime.GOARCH, "terraform") - tf, err := tfexec.NewTerraform(dir, tfPath) +/* + tfDir: tf配置文件所在目录 + terraformDir: terraform执行文件所在目录 +*/ + +func newTFExec(tfDir string, terraformDir string) (*tfexec.Terraform, error) { + tfPath := filepath.Join(terraformDir, "terraform") + tf, err := tfexec.NewTerraform(tfDir, tfPath) if err != nil { return nil, err } @@ -59,13 +63,29 @@ func newTFExec(dir string, terraformDir string) (*tfexec.Terraform, error) { return tf, nil } +// terraform init +func TFInit(tfDir string, terraformDir string) (err error) { + tf, err := newTFExec(tfDir, terraformDir) + if err != nil { + return errors.Wrap(err, "failed to create a new tfexec") + } + + // 使用本地terraform插件 + err = tf.Init(context.Background(), tfexec.PluginDir(filepath.Join(terraformDir, "plugins"))) + if err != nil { + return errors.Wrap(err, "failed to init terraform") + } + + return nil +} + // terraform apply -func TFApply(dir string, terraformDir string, applyOpts ...tfexec.ApplyOption) error { - if err := TFInit(dir, terraformDir); err != nil { - return err +func TFApply(tfDir string, terraformDir string, applyOpts ...tfexec.ApplyOption) error { + if err := TFInit(tfDir, terraformDir); err != nil { + return errors.Wrap(err, "failed to init terraform") } - tf, err := newTFExec(dir, terraformDir) + tf, err := newTFExec(tfDir, terraformDir) if err != nil { return errors.Wrap(err, "failed to create a new tfexec") } @@ -79,12 +99,12 @@ func TFApply(dir string, terraformDir string, applyOpts ...tfexec.ApplyOption) e } // terraform destroy -func TFDestroy(dir string, terraformDir string, destroyOpts ...tfexec.DestroyOption) error { - if err := TFInit(dir, terraformDir); err != nil { +func TFDestroy(tfDir string, terraformDir string, destroyOpts ...tfexec.DestroyOption) error { + if err := TFInit(tfDir, terraformDir); err != nil { return err } - tf, err := newTFExec(dir, terraformDir) + tf, err := newTFExec(tfDir, terraformDir) if err != nil { return errors.Wrap(err, "failed to destroy a new tfexec") } -- Gitee