|
| 1 | +/* |
| 2 | +Copyright 2022 The KubeVela Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package backend |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/hashicorp/hcl/v2" |
| 26 | + "github.com/hashicorp/hcl/v2/gohcl" |
| 27 | + "github.com/hashicorp/hcl/v2/hclparse" |
| 28 | + "github.com/hashicorp/hcl/v2/hclwrite" |
| 29 | + "github.com/oam-dev/terraform-controller/api/v1beta2" |
| 30 | + "github.com/pkg/errors" |
| 31 | + "github.com/zclconf/go-cty/cty" |
| 32 | + "github.com/zclconf/go-cty/cty/gocty" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 34 | +) |
| 35 | + |
| 36 | +var backendInitFuncMap = map[string]*backendInitFunc{ |
| 37 | + "kubernetes": { |
| 38 | + initFuncFromHCL: newK8SBackendFromInline, |
| 39 | + initFuncFromConf: newK8SBackendFromExplicit, |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +// Backend is an abstraction of what all backend types can do |
| 44 | +type Backend interface { |
| 45 | + // HCL can get the hcl code string |
| 46 | + HCL() string |
| 47 | + |
| 48 | + // GetTFStateJSON is used to get the Terraform state json from backend |
| 49 | + GetTFStateJSON(ctx context.Context) ([]byte, error) |
| 50 | + |
| 51 | + // CleanUp is used to clean up the backend when delete the configuration object |
| 52 | + // For example, if the configuration use kubernetes backend, CleanUp will delete the backend secret |
| 53 | + CleanUp(ctx context.Context) error |
| 54 | +} |
| 55 | + |
| 56 | +type backendInitFunc struct { |
| 57 | + initFuncFromHCL func(*ParsedBackendConfig, client.Client) (Backend, error) |
| 58 | + initFuncFromConf func(interface{}, client.Client) (Backend, error) |
| 59 | +} |
| 60 | + |
| 61 | +// ParsedBackendConfig is a struct parsed from the backend hcl block |
| 62 | +type ParsedBackendConfig struct { |
| 63 | + // Name is the label of the backend hcl block |
| 64 | + // It means which backend type the configuration will use |
| 65 | + Name string `hcl:"name,label"` |
| 66 | + // Attrs are the key-value pairs in the backend hcl block |
| 67 | + Attrs hcl.Body `hcl:",remain"` |
| 68 | +} |
| 69 | + |
| 70 | +func (conf ParsedBackendConfig) getAttrValue(key string) (*cty.Value, error) { |
| 71 | + attrs, diags := conf.Attrs.JustAttributes() |
| 72 | + if diags.HasErrors() { |
| 73 | + return nil, diags |
| 74 | + } |
| 75 | + attr := attrs[key] |
| 76 | + if attr == nil { |
| 77 | + return nil, fmt.Errorf("cannot find attr %s", key) |
| 78 | + } |
| 79 | + v, diags := attr.Expr.Value(nil) |
| 80 | + if diags.HasErrors() { |
| 81 | + return nil, diags |
| 82 | + } |
| 83 | + return &v, nil |
| 84 | +} |
| 85 | + |
| 86 | +func (conf ParsedBackendConfig) getAttrString(key string) (string, error) { |
| 87 | + v, err := conf.getAttrValue(key) |
| 88 | + if err != nil { |
| 89 | + return "", err |
| 90 | + } |
| 91 | + result := "" |
| 92 | + err = gocty.FromCtyValue(*v, &result) |
| 93 | + return result, err |
| 94 | +} |
| 95 | + |
| 96 | +// ParseConfigurationBackend parses backend Conf from the v1beta2.Configuration |
| 97 | +func ParseConfigurationBackend(configuration *v1beta2.Configuration, k8sClient client.Client) (Backend, error) { |
| 98 | + backend := configuration.Spec.Backend |
| 99 | + |
| 100 | + switch { |
| 101 | + |
| 102 | + case backend == nil || (backend.Inline == "" && backend.BackendType == ""): |
| 103 | + // use the default k8s backend |
| 104 | + if configuration.Spec.Backend != nil { |
| 105 | + if configuration.Spec.Backend.SecretSuffix == "" { |
| 106 | + configuration.Spec.Backend.SecretSuffix = configuration.Name |
| 107 | + } |
| 108 | + configuration.Spec.Backend.InClusterConfig = true |
| 109 | + } else { |
| 110 | + configuration.Spec.Backend = &v1beta2.Backend{ |
| 111 | + SecretSuffix: configuration.Name, |
| 112 | + InClusterConfig: true, |
| 113 | + } |
| 114 | + } |
| 115 | + return newDefaultK8SBackend(configuration.Spec.Backend.SecretSuffix, k8sClient), nil |
| 116 | + |
| 117 | + case backend.Inline != "" && backend.BackendType != "": |
| 118 | + return nil, errors.New("it's not allowed to set `spec.backend.inline` and `spec.backend.backendType` at the same time") |
| 119 | + |
| 120 | + case backend.Inline != "": |
| 121 | + // In this case, use the inline custom backend |
| 122 | + return handleInlineBackendHCL(backend.Inline, k8sClient) |
| 123 | + |
| 124 | + case backend.BackendType != "": |
| 125 | + // In this case, use the explicit custom backend |
| 126 | + |
| 127 | + // first, check if is valid custom backend |
| 128 | + backendType := backend.BackendType |
| 129 | + // fetch backendConfValue using reflection |
| 130 | + backendStructValue := reflect.ValueOf(backend) |
| 131 | + if backendStructValue.Kind() == reflect.Ptr { |
| 132 | + backendStructValue = backendStructValue.Elem() |
| 133 | + } |
| 134 | + backendField := backendStructValue.FieldByNameFunc(func(name string) bool { |
| 135 | + return strings.EqualFold(name, backendType) |
| 136 | + }) |
| 137 | + if backendField.IsNil() { |
| 138 | + return nil, fmt.Errorf("there is no configuration for backendType %s", backend.BackendType) |
| 139 | + } |
| 140 | + backendConfValue := backendField.Interface() |
| 141 | + |
| 142 | + // second, handle the backendConf |
| 143 | + return handleExplicitBackend(backendConfValue, backendType, k8sClient) |
| 144 | + } |
| 145 | + |
| 146 | + return nil, nil |
| 147 | +} |
| 148 | + |
| 149 | +func handleInlineBackendHCL(code string, k8sClient client.Client) (Backend, error) { |
| 150 | + |
| 151 | + type BackendConfigWrap struct { |
| 152 | + Backend ParsedBackendConfig `hcl:"backend,block"` |
| 153 | + } |
| 154 | + |
| 155 | + type TerraformConfig struct { |
| 156 | + Remain interface{} `hcl:",remain"` |
| 157 | + Terraform struct { |
| 158 | + Remain interface{} `hcl:",remain"` |
| 159 | + Backend ParsedBackendConfig `hcl:"backend,block"` |
| 160 | + } `hcl:"terraform,block"` |
| 161 | + } |
| 162 | + |
| 163 | + hclFile, diags := hclparse.NewParser().ParseHCL([]byte(code), "backend") |
| 164 | + if diags.HasErrors() { |
| 165 | + return nil, fmt.Errorf("there are syntax errors in the inline backend hcl code: %w", diags) |
| 166 | + } |
| 167 | + |
| 168 | + // try to parse hclFile to Config or BackendConfig |
| 169 | + config := &TerraformConfig{} |
| 170 | + // nolint:staticcheck |
| 171 | + backendConfig := &ParsedBackendConfig{} |
| 172 | + diags = gohcl.DecodeBody(hclFile.Body, nil, config) |
| 173 | + if diags.HasErrors() || config.Terraform.Backend.Name == "" { |
| 174 | + backendConfigWrap := &BackendConfigWrap{} |
| 175 | + diags = gohcl.DecodeBody(hclFile.Body, nil, backendConfigWrap) |
| 176 | + if diags.HasErrors() || backendConfigWrap.Backend.Name == "" { |
| 177 | + return nil, fmt.Errorf("the inline backend hcl code is not valid Terraform backend configuration: %w", diags) |
| 178 | + } |
| 179 | + backendConfig = &backendConfigWrap.Backend |
| 180 | + } else { |
| 181 | + backendConfig = &config.Terraform.Backend |
| 182 | + } |
| 183 | + |
| 184 | + initFunc := backendInitFuncMap[backendConfig.Name] |
| 185 | + if initFunc == nil || initFunc.initFuncFromHCL == nil { |
| 186 | + return nil, fmt.Errorf("backend type (%s) is not supported", backendConfig.Name) |
| 187 | + } |
| 188 | + return initFunc.initFuncFromHCL(backendConfig, k8sClient) |
| 189 | +} |
| 190 | + |
| 191 | +func handleExplicitBackend(backendConf interface{}, backendType string, k8sClient client.Client) (Backend, error) { |
| 192 | + hclFile := hclwrite.NewEmptyFile() |
| 193 | + gohcl.EncodeIntoBody(backendConf, hclFile.Body()) |
| 194 | + |
| 195 | + initFunc := backendInitFuncMap[backendType] |
| 196 | + if initFunc == nil || initFunc.initFuncFromConf == nil { |
| 197 | + return nil, fmt.Errorf("backend type (%s) is not supported", backendType) |
| 198 | + } |
| 199 | + return initFunc.initFuncFromConf(backendConf, k8sClient) |
| 200 | +} |
0 commit comments