Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom Terraform backends #291

Merged
merged 46 commits into from
May 25, 2022

Conversation

loheagn
Copy link
Contributor

@loheagn loheagn commented Apr 12, 2022

This pr is supposed to implement the proposal which is the concluation of the issuse #288 .

Currently, the changes only add some fields to the spec.backend to enable the end-users to custom the Terraform backend configuration.

Signed-off-by: loheagn [email protected]

@codecov
Copy link

codecov bot commented Apr 12, 2022

Codecov Report

Merging #291 (796aa9b) into master (16bd07e) will not change coverage.
The diff coverage is n/a.

❗ Current head 796aa9b differs from pull request most recent head aaad840. Consider uploading reports for the commit aaad840 to get more accurate results

@@          Coverage Diff           @@
##           master    #291   +/-   ##
======================================
  Coverage    0.00%   0.00%           
======================================
  Files           1       1           
  Lines          72      72           
======================================
  Misses         72      72           
Flag Coverage Δ
e2e 0.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 16bd07e...aaad840. Read the comment docs.

InClusterConfig bool `json:"inClusterConfig,omitempty"`

// HCL allows users to use raw hcl code to specify their Terraform backend configuration
HCL string `json:"hcl,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change HCL to something else, like raw?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Maybe you can use inline.

// Type specifies the Terraform backend type, for example, "kubernetes", "s3", etc
Type string `json:"type,omitempty"`
// Config is the detail configurations of the Terraform backend,
// and it represents the key-value pairs inside the terraform backend block in the hcl files
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users don't know how to set such fields. As Terraform backends are limited, we can support them one by one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that the users can write the spec.backend like this?

spec:
  backend: 
    s3:
      bucket: "mybucket"
      key: "path/to/my/key"
      region: "my-region"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the one hand, we need to use have a clear guide on how to set the backend just per the API, on the other hand, the spec.backend should be as simple as possible. Let's set aside some time on the design to check whether it's possible.

Maybe we can refer a Backend object, like we referring to a Provider object in Configuration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can refer a Backend object, like we referring to a Provider object in Configuration.

I don't think it's necessary to maintain an additional Backend object, if only to provide custom backend configuration in the Configuration object.

But if we can add backup and restore configuration and backup life cycle management to Backend object, I think it will make sense.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1.

}

// BackendSpecInnerMain defines all options supported by the Terraform backend configuration.
type BackendSpecInnerMain struct {
Copy link
Collaborator

@zzxwill zzxwill Apr 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the differences between creating a new Backend CRD and expanding spec.Backend of Configuration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new Backend CRD will make the Configuration more simple and we can also add more fields like options describing backup and resotre to the new CRD.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As one Configuration will match one Backend, so you can also describe backup in Configuration. The CRD yamls will get longer, but for end-users, it won't as they will only choose one backend.
Besides, maintaining another Object will need to take care of its deletion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I will move the spec into the Configuratoin.

@loheagn loheagn marked this pull request as draft April 24, 2022 08:52
@loheagn loheagn changed the title WIP: Avoid overriding the specified backend configuration in the user's input Avoid overriding the specified backend configuration in the user's input Apr 24, 2022
@loheagn loheagn force-pushed the backend_specification branch from 564db97 to 94f42f4 Compare April 24, 2022 10:03

// Artifactory is needed for the Terraform `artifactory` backend type.
Artifactory ArtifactoryBackendConf `json:"artifactory,omitempty"`
Artifactory *ArtifactoryBackendConf `json:"artifactory,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these backend are belong to Remote?

Copy link
Contributor Author

@loheagn loheagn Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, these backends don't belong to Remote. I think users can also use these backends if they use inline HCL configuration.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these backend are belong to Remote?

The meaning of "Remote" is the spec.Remote?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right.

@zzxwill zzxwill changed the title Avoid overriding the specified backend configuration in the user's input Support custom Terraform backends Apr 26, 2022
@zzxwill
Copy link
Collaborator

zzxwill commented Apr 26, 2022

I would suggest you support Kubernetes first, then S3, and other backends in other PRs. Or you might get desperate with tons of unit-test lines:)

@loheagn
Copy link
Contributor Author

loheagn commented Apr 26, 2022

I would suggest you support Kubernetes first, then S3, and other backends in other PRs. Or you might get desperate with tons of unit-test lines:)

Do we need to write unit-test for each backend type as different types of backend will be uniformly converted to backend TF string using reflection in the rendering stage? I think we just need to write unit-tests for some special types, like which has secretRef fields.

@zzxwill
Copy link
Collaborator

zzxwill commented May 5, 2022

Okay.

@loheagn loheagn force-pushed the backend_specification branch 2 times, most recently from c759a59 to 76795dc Compare May 14, 2022 13:30
@loheagn loheagn marked this pull request as ready for review May 14, 2022 14:45
@loheagn loheagn force-pushed the backend_specification branch from 5afacce to 2eac213 Compare May 17, 2022 04:27
Inline string `json:"inline,omitempty"`

// BackendType indicates which backend type to use. This field is needed for custom backend configuration.
BackendType string `json:"backend_type,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use enum to specify all its accepted values, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

// ConsulBackendConf defines all options supported by the Terraform `consul` backend type.
// You can refer to https://www.terraform.io/language/settings/backends/consul for the usage of each option.
type ConsulBackendConf struct {
Path string `json:"path" hcl:"path"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What hcl is used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hcl tag is used when encode the struct to hcl or decode the hcl into go struct.


}

func handleInlineBackendHCL(code string) (string, string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to handle inlined backend HCL? How about transparently storing it in backend.tf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used to check for illegal properties in the inline hcl code.

func (d ConfData) Get(key string) interface{} {
x, ok := d[key]
if !ok || x == nil {
klog.Errorf("fetch %s from the custom backend conf error", key)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please print the backend information, or it will do any help for debugging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

@loheagn loheagn force-pushed the backend_specification branch from 568f271 to 336e8a4 Compare May 21, 2022 06:03
}

// CurrentNSSecretSelector is used to specify the key in a secret in the current namespace.
type CurrentNSSecretSelector struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the current namespace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same namespace as the configuration.


switch {

case backend != nil && len(backend.Inline) > 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if Inline is not empty and BackType is not empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we will use the inline and ignore the backendType.

loheagn added 23 commits May 25, 2022 09:11
Signed-off-by: loheagn <[email protected]>
Signed-off-by: loheagn <[email protected]>
Signed-off-by: loheagn <[email protected]>
Signed-off-by: loheagn <[email protected]>
Signed-off-by: loheagn <[email protected]>
Signed-off-by: loheagn <[email protected]>
Signed-off-by: loheagn <[email protected]>
Signed-off-by: loheagn <[email protected]>
@loheagn loheagn force-pushed the backend_specification branch from aaad840 to 97feed1 Compare May 25, 2022 01:11
@codecov
Copy link

codecov bot commented May 25, 2022

Codecov Report

❗ No coverage uploaded for pull request base (master@8601f66). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master     #291   +/-   ##
=========================================
  Coverage          ?   78.18%           
=========================================
  Files             ?       23           
  Lines             ?     1595           
  Branches          ?        0           
=========================================
  Hits              ?     1247           
  Misses            ?      268           
  Partials          ?       80           
Flag Coverage Δ
e2e 0.00% <0.00%> (?)
unit 81.87% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8601f66...6ab7aa0. Read the comment docs.

@lgtm-com
Copy link

lgtm-com bot commented May 25, 2022

This pull request introduces 3 alerts when merging 6ab7aa0 into 8601f66 - view on LGTM.com

new alerts:

  • 2 for Missing error check
  • 1 for Useless assignment to local variable

@zzxwill zzxwill merged commit 971d169 into kubevela:master May 25, 2022
@loheagn loheagn deleted the backend_specification branch May 25, 2022 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants