diff --git a/pkg/infra/assets.go b/pkg/infra/assets/assets.go similarity index 91% rename from pkg/infra/assets.go rename to pkg/infra/assets/assets.go index 176dd31459671abf94b28357dc93e6fb86e25dea..75fdc8d5ef9975d24a8048d8f78f8fce083e2e3d 100755 --- a/pkg/infra/assets.go +++ b/pkg/infra/assets/assets.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package infra +package assets // path : contents type Assets map[string][]byte @@ -30,3 +30,8 @@ func (a *Assets) Merge(b Assets) *Assets { type AssetsGenerator interface { GenerateAssets() Assets } + +type File struct { + Filename string + Data []byte +} diff --git a/pkg/infra/cluster.go b/pkg/infra/cluster.go new file mode 100644 index 0000000000000000000000000000000000000000..09b6274907be6a4c9b97536fab2d25c0dbf89eab --- /dev/null +++ b/pkg/infra/cluster.go @@ -0,0 +1,62 @@ +/* +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 infra + +import ( + "os" + "path/filepath" + + "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/assets" + "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform" + "github.com/hashicorp/terraform-exec/tfexec" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +type Cluster struct { + FileList []*assets.File +} + +func (c *Cluster) applyTerraform(tmpDir string, platform string, stage terraform.Stage, terraformBinary string, applyOpts ...tfexec.ApplyOption) (*assets.File, error) { + applyErr := terraform.TFApply(tmpDir, platform, stage, terraformBinary, applyOpts...) + + if data, err := os.ReadFile(filepath.Join(tmpDir, terraform.StateFilename)); err == nil { + c.FileList = append(c.FileList, &assets.File{ + Filename: stage.StateFilename(), + Data: data, + }) + } else 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.") + } + + outputs, err := terraform.Outputs(tmpDir, terraformBinary) + if err != nil { + return nil, errors.Wrapf(err, "could not get outputs from stage %q", stage.Name()) + } + + outputsFile := &assets.File{ + Filename: stage.OutputsFilename(), + Data: outputs, + } + + return outputsFile, nil +} diff --git a/pkg/infra/infradeployer.go b/pkg/infra/infradeployer.go index f2c42b459d856545a93188bee8b3fd7ecf401aea..dc281501a670283ff0bf41fe48ffe5ec7aeab22b 100755 --- a/pkg/infra/infradeployer.go +++ b/pkg/infra/infradeployer.go @@ -16,8 +16,6 @@ limitations under the License. package infra -import "github.com/hashicorp/terraform-exec/tfexec" - type InfraSpec struct { diskSize string memorySize string @@ -27,6 +25,4 @@ type InfraSpec struct { } type InfraDeployer interface { - Create(spec InfraSpec, config InitConfig, extraOpts ...tfexec.ApplyOption) error - Destroy(spec InfraSpec, config InitConfig, extraOpts ...tfexec.DestroyOption) error } diff --git a/pkg/infra/openstack.go b/pkg/infra/openstack.go index c3ad75a619d31b62731e3048c57fb82b250623bf..76d42327cb3bf3040b4f3389a1dfcbd3f02a4acd 100755 --- a/pkg/infra/openstack.go +++ b/pkg/infra/openstack.go @@ -16,32 +16,14 @@ limitations under the License. package infra -import ( - "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform" - "github.com/hashicorp/terraform-exec/tfexec" -) - type OpenstackDeployer struct { - workingDir string - platform string - target string - terraformBinary string } -func (t OpenstackDeployer) Create(applyOpts ...tfexec.ApplyOption) error { - applyErr := terraform.TFApply(t.workingDir, t.platform, t.target, t.terraformBinary, applyOpts) - if applyErr != nil { - return applyErr - } - +func (t OpenstackDeployer) Create() error { return nil } -func (t OpenstackDeployer) Destroy(destroyOpts ...tfexec.DestroyOption) error { - destroyErr := terraform.TFDestroy(t.workingDir, t.platform, t.target, t.terraformBinary, destroyOpts) - if destroyErr != nil { - return destroyErr - } +func (t OpenstackDeployer) Destroy() error { return nil } diff --git a/pkg/infra/terraform/state.go b/pkg/infra/terraform/state.go index e1601a996ad6728eb00f533a7fc2afa8374a4f9e..9509abc2d818ca9cf9c59d5959e96c7cc19cd009 100644 --- a/pkg/infra/terraform/state.go +++ b/pkg/infra/terraform/state.go @@ -18,21 +18,23 @@ package terraform import ( "context" + "encoding/json" "github.com/pkg/errors" ) -func Outputs(workingDir string, terraformBinary string) error { - // TODO 解析tfstate文件,读取特定内容 +const StateFilename = "terraform.tfstate" +// Reads the terraform state file. +func Outputs(workingDir string, terraformBinary string) ([]byte, error) { tf, err := newTFExec(workingDir, terraformBinary) if err != nil { - return err + return nil, err } tfoutput, err := tf.Output(context.Background()) if err != nil { - return errors.Wrap(err, "failed to read terraform state file") + return nil, errors.Wrap(err, "failed to read terraform state file") } outputs := make(map[string]interface{}, len(tfoutput)) @@ -40,7 +42,10 @@ func Outputs(workingDir string, terraformBinary string) error { outputs[key] = value.Value } - // TODO 解析outputs + data, err := json.Marshal(outputs) + if err != nil { + return nil, errors.Wrap(err, "could not marshal outputs") + } - return nil + return data, nil }