Toi aussi apprends à développer un opérateur Kubernetes en Java avec Quarkus

A presentation at Snowcamp 2023 in January 2023 in Grenoble, France by Horacio Gonzalez

Slide 1

Slide 1

Let’s dive into Terraform provider creation Aurélie Vache - Horacio Gonzalez 2023-01-26

Slide 2

Slide 2

Merci à nos sponsors e toil E « » con o l «F » 2023

Slide 3

Slide 3

Aurélie Vache @aurelievache DevRel at Conferences organizer Tech visual articles & books Sketchnoter … & ❤ Retrogaming https://www.youtube.com/c/AurelieVache https://dev.to/aurelievache/ Les Productions de MOA

Slide 4

Slide 4

Horacio Gonzalez @LostInBrittany Spaniard lost in Brittany, developer, dreamer and all-around geek

Slide 5

Slide 5

OVHcloud: A global leader Web Cloud & Telcom 30 Data Centers in 12 locations 1 Million+ Servers produced since 1999 Private Cloud 34 Points of Presence on a 20 TBPS Bandwidth Network 1.5 Million Customers across 132 countries Public Cloud 2200 Employees worldwide 3.8 Million Websites hosting Storage 115K Private Cloud VMS running 1.5 Billion Euros Invested since 2016 300K Public Cloud instances running P.U.E. 1.09 Energy efficiency indicator 380K Physical Servers running in our data centers 20+ Years in Business Disrupting since 1999 Network & Security

Slide 6

Slide 6

Warning Gophers, gophers everywhere!

Slide 7

Slide 7

Terraform De facto standard for IaC

Slide 8

Slide 8

Infrastructure as Code (IaC)

Slide 9

Slide 9

Terraform becoming the de facto standard

Slide 10

Slide 10

HashiCorp Terraform

Slide 11

Slide 11

Modular architecture: providers

Slide 12

Slide 12

Configuration packages: modules

Slide 13

Slide 13

Terraform registry

Slide 14

Slide 14

Writing Terraform providers Defining new Terraform resources

Slide 15

Slide 15

Provider SDK https://developer.hashicorp.com/terraform/plugin/sdkv2

Slide 16

Slide 16

Installing Terraform providers $ terraform init Initializing provider plugins… - Finding terraform.local/local/myprovider versions matching “0.0.1”… - Installing terraform.local/local/myprovider v0.0.1…

Slide 17

Slide 17

Installing providers from registry $ vi provider.tf terraform { required_providers { thenamespace = { source = “thenamespace/myprovider” } } } If your provider is on the official registry at https://registry.terraform.io/providers/thenamespace/myprovider

Slide 18

Slide 18

Installing providers locally $ go build -o terraform-provider-myprovider $ mkdir -p ~/.terraform.d/plugins/terraform.local/local/myprovider/0.0.1/darwin_amd64 $ mv terraform-provider-myprovider ~/.terraform.d/plugins/terraform.local/local/myprovider/0.0.1/darwin_amd64

Slide 19

Slide 19

Installing providers locally $ vi provider.tf terraform { required_providers { thenamespace = { source = “terraform.local/local/myprovider” version = “0.0.1” } } }

Slide 20

Slide 20

Do I need a Terraform provider? If you have an API, you should have a Terraform provider

Slide 21

Slide 21

Letʼs create a provider! Step by step

Slide 22

Slide 22

What do we want? ● In a simple and easy Terraform provider ● Handle cute Gophers ● In Go, because providers are made in Go 😁

Slide 23

Slide 23

Everything begins with an API https://github.com/scraly/gophers-api

Slide 24

Slide 24

For the demos we will use Gitpod Automated, ephemeral developer environments in the web

Slide 25

Slide 25

Everything begins with an API $ task swagger.serve task: [swagger.serve] swagger serve -F swagger ./pkg/swagger/swagger.yml —no-open 2022/10/31 20:16:51 serving docs at http://localhost:38457/docs

Slide 26

Slide 26

Everything begins with an API

Slide 27

Slide 27

Everything begins with an API $ task run task: [run] GOFLAGS=-mod=mod go run internal/main.go 2022/10/30 20:22:05 Serving gophers API at http://[::]:8080 $ curl localhost:8080/gophers [{“name”:”5th-element”,”displayname”:”5th Element.png”,”url”:”https://raw.githubusercontent.com/scraly/gophers/main/5th-ele ment.png”}]

Slide 28

Slide 28

Gophers deserve to be seen https://github.com/LostInBrittany/gophers-api-watcher

Slide 29

Slide 29

Everything begins with an API $ curl -X POST localhost:8080/gopher -H “Content-Type: application/json” -d \ ‘{“name”:”yoda-gopher”,”displayname”:”Yodada Gopher”,”url”:”https://raw.githubusercontent.com/scraly/gophers/main/yoda-gopher. png”}’ $ curl -X DELETE localhost:8080/gopher?name=5th-element $ curl -X PUT localhost:8080/gopher \ -H “Content-Type: application/json” -d \ ‘{“name”:”yoda-gopher”,”displayname”:”Yoda Gopher”,”url”:”https://raw.githubusercontent.com/scraly/gophers/main/yoda-gopher. png”}’

Slide 30

Slide 30

Let’s create our provider! 1. Create the skeleton of our provider thanks to scaffolding https://github.com/hashicorp/terraform-provider-scaffolding

Slide 31

Slide 31

Let’s create our provider! https://github.com/scraly/terraform-provider-gophers

Slide 32

Slide 32

Demo time!

Slide 33

Slide 33

Provider > Makefile

Slide 34

Slide 34

Demo time!

Slide 35

Slide 35

Some concepts to introduce…

Slide 36

Slide 36

Customizing provider definition

Slide 37

Slide 37

Test it! $ vi provider.tf terraform { required_providers { gophers = { source = “terraform.local/local/gophers” version = “0.0.1” } } } provider “gophers” { endpoint = “http://myawesomeurl.com” }

Slide 38

Slide 38

Adding datasource: gophers

Slide 39

Slide 39

Adding the schema “Translating” the Swagger into a Go schema

Slide 40

Slide 40

Test it! $ vi gophers_data.tf # List of available gophers data “gophers” “my_gophers” { } output “return_gophers” { value = length(data.gophers.my_gophers.gophers) >= 1 }

Slide 41

Slide 41

Adding datasource: gopher

Slide 42

Slide 42

Test it! $ vi gopher_data.tf # Display information about a Gopher data “gophers_gopher” “moultipass” { name = “5th-element” }

Slide 43

Slide 43

Adding resource: gopher

Slide 44

Slide 44

Test it! $ vi gopher_resource.tf resource “gophers_gopher” “x-files” { name = “x-files” displayname = “X Files” url } = “https://raw.githubusercontent.com/scraly/gophers/main/x-files.png”

Slide 45

Slide 45

Testing the provider locally $ go build -o terraform-provider-gophers $ mkdir -p ~/.terraform.d/plugins/terraform.local/local/gophers/0.0.1/darwin_arm64 $ mv terraform-provider-gophers ~/.terraform.d/plugins/terraform.local/local/gophers/0.0.1/darwin_arm64 $ make install

Slide 46

Slide 46

Testing the provider locally $ rm .terraform.lock.hcl && terraform init $ terraform apply

Slide 47

Slide 47

Testing the provider locally $ terraform destroy

Slide 48

Slide 48

Slide 49

Slide 49

OVHcloud Terraform Provider To easily manage OVHcloud products

Slide 50

Slide 50

OVHcloud Terraform Provider https://registry.terraform.io/providers/ovh/ovh/latest/docs

Slide 51

Slide 51

OVHcloud Terraform Provider https://github.com/ovh/terraform-provider-ovh

Slide 52

Slide 52

Best practices But we have learnt with our providers

Slide 53

Slide 53

Doc is not optional $ tfplugindocs generate Generate the doc of your provider. Based on the schema the provider exposes. https://github.com/hashicorp/terraform-plugin-docs

Slide 54

Slide 54

Write useful examples in your doc Examples in your documentation should be: ● Useful ● Up-to-date ● Working Users will copy paste your examples! 😉

Slide 55

Slide 55

And… test your doc! Use the doc preview tool https://registry.terraform.io/tools/doc-preview

Slide 56

Slide 56

Acceptance tests $ make testacc $ make testacc TESTARGS=”-run TestAccDataSourceGopher”

Slide 57

Slide 57

Have the simplest JSON structures

Slide 58

Slide 58

Provider is a reflection of your API client Think about API first design

Slide 59

Slide 59

Use the logs for debugging $ TF_LOG=INFO terraform plan

Slide 60

Slide 60

Set timeouts / retry Timeouts: &schema.ResourceTimeout{ Create: schema. DefaultTimeout (20 * time.Minute), Update: schema. DefaultTimeout (20 * time.Minute), Delete: schema. DefaultTimeout (20 * time.Minute), }, Timeout/retry par resource

Slide 61

Slide 61

Read the code See how other open source providers are written

Slide 62

Slide 62

The “3 P” rule Practice, practice, practice

Slide 63

Slide 63

One more thing… Or two or three

Slide 64

Slide 64

A handy cheat sheet https://github.com/scraly/terraform-cheat-sheet/

Slide 65

Slide 65

Thank you! https://bit.ly/tf-provider-snowcamp

Slide 66

Slide 66

We ❤ feedbacks https://bit.ly/vote-tf-provider-snow