From c388ec4bed41ae0e0ca90b1ae38f9ab0ec0ac5b6 Mon Sep 17 00:00:00 2001 From: jianli-97 Date: Tue, 20 Jun 2023 10:45:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0infra-provider=E5=8A=9F?= =?UTF-8?q?=E8=83=BD,=E4=B8=BB=E8=A6=81=E5=AE=9E=E7=8E=B0terraform?= =?UTF-8?q?=E7=9A=84init,apply=E5=92=8Cdestroy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 9 +++- go.sum | 35 ++++++++++++++ pkg/cert/etcd.go | 20 ++++++-- pkg/infra/assets.go | 18 ++++++- pkg/infra/factory.go | 23 +++++++-- pkg/infra/infradeployer.go | 35 ++++++++++---- pkg/infra/initconfig.go | 19 +++++++- pkg/infra/openstack.go | 45 ++++++++++++++++- pkg/infra/terraform/logger.go | 27 +++++++++++ pkg/infra/terraform/state.go | 46 ++++++++++++++++++ pkg/infra/terraform/terraform.go | 83 ++++++++++++++++++++++++++++++++ 11 files changed, 338 insertions(+), 22 deletions(-) mode change 100755 => 100644 go.mod mode change 100755 => 100644 go.sum create mode 100644 pkg/infra/terraform/logger.go create mode 100644 pkg/infra/terraform/state.go create mode 100644 pkg/infra/terraform/terraform.go diff --git a/go.mod b/go.mod old mode 100755 new mode 100644 index 0fe9ed1..ca5e20f --- a/go.mod +++ b/go.mod @@ -1,14 +1,21 @@ module gitee.com/openeuler/nestos-kubernetes-deployer -go 1.18 +go 1.19 require ( + github.com/hashicorp/terraform-exec v0.18.1 github.com/lithammer/dedent v1.1.0 github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.7.0 ) require ( + github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect + github.com/hashicorp/go-version v1.6.0 // indirect + github.com/hashicorp/terraform-json v0.16.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/zclconf/go-cty v1.13.1 // indirect + golang.org/x/crypto v0.7.0 // indirect + golang.org/x/text v0.8.0 // indirect ) diff --git a/go.sum b/go.sum old mode 100755 new mode 100644 index 1516f81..ec697df --- a/go.sum +++ b/go.sum @@ -1,14 +1,49 @@ +github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk= +github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ= +github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= +github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw= +github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= +github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= +github.com/go-git/go-billy/v5 v5.3.1 h1:CPiOUAzKtMRvolEKw+bG1PLRpT7D3LIs3/3ey4Aiu34= +github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/hc-install v0.5.0 h1:D9bl4KayIYKEeJ4vUDe9L5huqxZXczKaykSRcmQ0xY0= +github.com/hashicorp/terraform-exec v0.18.1 h1:LAbfDvNQU1l0NOQlTuudjczVhHj061fNX5H8XZxHlH4= +github.com/hashicorp/terraform-exec v0.18.1/go.mod h1:58wg4IeuAJ6LVsLUeD2DWZZoc/bYi6dzhLHzxM41980= +github.com/hashicorp/terraform-json v0.16.0 h1:UKkeWRWb23do5LNAFlh/K3N0ymn1qTOO8c+85Albo3s= +github.com/hashicorp/terraform-json v0.16.0/go.mod h1:v0Ufk9jJnk6tcIZvScHvetlKfiNTC+WS21mnXIlc0B0= +github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck= github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY= github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= +github.com/zclconf/go-cty v1.13.1 h1:0a6bRwuiSHtAmqCqNOE+c2oHgepv0ctoxU4FUe43kwc= +github.com/zclconf/go-cty v1.13.1/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0= +golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/cert/etcd.go b/pkg/cert/etcd.go index 93cc24f..57a64f5 100755 --- a/pkg/cert/etcd.go +++ b/pkg/cert/etcd.go @@ -1,11 +1,25 @@ +/* +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 cert import ( "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra" ) - - type EtcdCaGenerator struct { } @@ -14,4 +28,4 @@ func (e EtcdCaGenerator) GenerateAssets() infra.Assets { } func init() { -} \ No newline at end of file +} diff --git a/pkg/infra/assets.go b/pkg/infra/assets.go index 8ebcc7e..176dd31 100755 --- a/pkg/infra/assets.go +++ b/pkg/infra/assets.go @@ -1,3 +1,19 @@ +/* +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 // path : contents @@ -13,4 +29,4 @@ func (a *Assets) Merge(b Assets) *Assets { type AssetsGenerator interface { GenerateAssets() Assets -} \ No newline at end of file +} diff --git a/pkg/infra/factory.go b/pkg/infra/factory.go index eb0c340..3db63de 100755 --- a/pkg/infra/factory.go +++ b/pkg/infra/factory.go @@ -1,18 +1,33 @@ +/* +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 "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/bootconfig" func GetInfraDeployer(driverType string) InfraDeployer { - if driverType == "openstack"{ + if driverType == "openstack" { return OpenstackDeployer{} } return nil } - func GetBootConfigAssembler(osType string) BootConfigAssembler { - if osType == "NestOS"{ + if osType == "NestOS" { return bootconfig.IgnitionAssembler{} } return nil -} \ No newline at end of file +} diff --git a/pkg/infra/infradeployer.go b/pkg/infra/infradeployer.go index d666ce6..f2c42b4 100755 --- a/pkg/infra/infradeployer.go +++ b/pkg/infra/infradeployer.go @@ -1,17 +1,32 @@ +/* +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 -type InfraSpec struct{ - diskSize string - memorySize string - image string - clusterCIDR string +import "github.com/hashicorp/terraform-exec/tfexec" + +type InfraSpec struct { + diskSize string + memorySize string + image string + clusterCIDR string initFileContent string } - type InfraDeployer interface { - Create(spec InfraSpec, config InitConfig) error + Create(spec InfraSpec, config InitConfig, extraOpts ...tfexec.ApplyOption) error + Destroy(spec InfraSpec, config InitConfig, extraOpts ...tfexec.DestroyOption) error } - - - diff --git a/pkg/infra/initconfig.go b/pkg/infra/initconfig.go index 97c4bde..288c3b3 100755 --- a/pkg/infra/initconfig.go +++ b/pkg/infra/initconfig.go @@ -1,10 +1,25 @@ +/* +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 -type InitConfig struct{ +type InitConfig struct { OsType string } - type BootConfigAssembler interface { Assemble(assets Assets) InitConfig } diff --git a/pkg/infra/openstack.go b/pkg/infra/openstack.go index 393a880..be3bc69 100755 --- a/pkg/infra/openstack.go +++ b/pkg/infra/openstack.go @@ -1,10 +1,53 @@ +/* +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 ( + "gitee.com/openeuler/nestos-kubernetes-deployer/pkg/infra/terraform" + "github.com/hashicorp/terraform-exec/tfexec" +) type OpenstackDeployer struct { + tfFilePath string + terraformBinary string +} + +func (t OpenstackDeployer) Create(extraOpts ...tfexec.ApplyOption) error { + if err := terraform.Init(t.tfFilePath, t.terraformBinary); err != nil { + return err + } + + applyErr := terraform.Apply(t.tfFilePath, t.terraformBinary, extraOpts...) + if applyErr != nil { + return applyErr + } + + return nil } +func (t OpenstackDeployer) Destroy(extraOpts ...tfexec.DestroyOption) error { + if err := terraform.Init(t.tfFilePath, t.terraformBinary); err != nil { + return err + } + + destroyErr := terraform.Destroy(t.tfFilePath, t.terraformBinary, extraOpts) + if destroyErr != nil { + return destroyErr + } -func (t OpenstackDeployer) Create(spec InfraSpec, config InitConfig) error{ return nil } diff --git a/pkg/infra/terraform/logger.go b/pkg/infra/terraform/logger.go new file mode 100644 index 0000000..bfb609d --- /dev/null +++ b/pkg/infra/terraform/logger.go @@ -0,0 +1,27 @@ +/* +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 + +type printfer struct { +} + +func newPrintfer() *printfer { + return &printfer{} +} + +func (p *printfer) Printf(format string, ifs ...interface{}) { +} diff --git a/pkg/infra/terraform/state.go b/pkg/infra/terraform/state.go new file mode 100644 index 0000000..fd7481e --- /dev/null +++ b/pkg/infra/terraform/state.go @@ -0,0 +1,46 @@ +/* +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" + + "github.com/pkg/errors" +) + +func Outputs(tfFilePath string, terraformBinary string) error { + // TODO 解析tfstate文件,读取特定内容 + + tf, err := newTFExec(tfFilePath, terraformBinary) + if err != nil { + return err + } + + tfoutput, err := tf.Output(context.Background()) + if err != nil { + return errors.Wrap(err, "failed to read terraform state file") + } + + outputs := make(map[string]interface{}, len(tfoutput)) + for key, value := range tfoutput { + outputs[key] = value.Value + } + + // TODO 解析outputs + + return nil +} diff --git a/pkg/infra/terraform/terraform.go b/pkg/infra/terraform/terraform.go new file mode 100644 index 0000000..7c387a7 --- /dev/null +++ b/pkg/infra/terraform/terraform.go @@ -0,0 +1,83 @@ +/* +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" +) + +func newTFExec(tfFilePath string, terraformBinary string) (*tfexec.Terraform, error) { + tf, err := tfexec.NewTerraform(tfFilePath, terraformBinary) + if err != nil { + return nil, err + } + + // TODO 日志输出 + // tf.SetStdout() + // tf.SetStderr() + // tf.SetLogger(newPrintfer()) + + return tf, nil +} + +func Init(tfFilePath string, terraformBinary string) error { + tf, err := newTFExec(tfFilePath, terraformBinary) + if err != nil { + return errors.Wrap(err, "failed to create a new tfexec.") + } + + // 如果想导入本地已有插件,需手动构建相应目录 + // 如openstack插件所需目录为plugins/registry.terraform.io/terraform-provider-openstack/openstack/1.51.1/linux_arm64/terraform-provider-openstack_v1.51.1 + return errors.Wrap( + tf.Init(context.Background(), tfexec.PluginDir(filepath.Join(terraformBinary, "plugins"))), + "failed doing terraform init.", + ) +} + +func Apply(tfFilePath string, terraformBinary string, extraOpts ...tfexec.ApplyOption) error { + tf, err := newTFExec(tfFilePath, terraformBinary) + if err != nil { + return errors.Wrap(err, "failed to create a new tfexec.") + } + + // TODO 日志输出 + // tf.SetStdout() + // tf.SetStderr() + tf.SetLogger(newPrintfer()) + + err = tf.Apply(context.Background(), extraOpts...) + return errors.Wrap(err, "failed to apply Terraform.") +} + +func Destroy(tfFilePath string, terraformBinary string, extraOpts ...tfexec.DestroyOption) error { + tf, err := newTFExec(tfFilePath, terraformBinary) + if err != nil { + return errors.Wrap(err, "failed to create a new tfexec.") + } + + // TODO 日志输出 + // tf.SetStdout() + // tf.SetStderr() + tf.SetLogger(newPrintfer()) + + err = tf.Destroy(context.Background(), extraOpts...) + return errors.Wrap(err, "failed to apply Terraform.") +} -- Gitee