Threeport SDK Tutorial¶
The following tutorial walks through building a Threeport extension for managing Wordpress deployments.
Note: This tutorial provides instructions to build a new Golang project from scratch. If you'd like to skip to the end, the finished project can be viewed on GitHub.
Prerequisites¶
Initialize Project¶
1 2 3 4 |
|
Create SDK Config¶
Create a new file at the root of the repo called sdk-config.yaml
with the
following contents. Replace the ImageRepo
value for one that you have access
to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Define API¶
Use the Threeport SDK to create scaffolding for new API objects. This will create a source code file for each API object group defined in the SDK config. In each source code file, an object that corresponds to a database table will be scaffolded.
1 |
|
For the SDK config shown above, it will create the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
This constitutes your Threeport extension's data model. Add fields to these objects for information that will need to be persisted that will configure the workloads that constitute your app.
Add the following fields so that the file looks as follows when you are finished.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Generate Source Code¶
1 |
|
This will generate all the source code boilerplate and scaffolding needed for the project.
Add Controller Business Logic¶
The next step is to add the business logic for managing wordpress instances to the scaffolding for the wordpress controller.
Updates to internal/wordpress/v0_wordpress_definition.go
¶
The first file we will modify managed WordpressDefinition
objects. The file
is located at internal/wordpress/v0_wordpress_definition.go
. If you open that
file, you'll see functions to create, update and delete those objects. These
functions run when the corresponding actions are executed through the Threeport
API.
Now, let's update that file to insert some business logic. In the following
code snippet, is a new constant and the contents for the v0WordpressDefinitionCreated
function. Update your code to reflect these changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
Now, let's update the v0WordpressDefinitionDeleted
function with the code shown
below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
Lastly, add these two functions at the end of that file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Ensure the following imports are included in this file.
1 2 3 4 5 6 7 8 9 10 11 |
|
Add internal/wordpress/wordpress_manifest.go
¶
The updates we made to the v0WordpressDefinitionCreated
function include a
reference to a function wordpressYaml
. Let's add that function in a separate
file. This function defines the Kubernetes manifests for the WordPress workload
and sets the variables needed for different install options.
Add a new file internal/wordpress/wordpress_manifest.go
with the following
contents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 |
|
Updates to internal/wordpress/v0_wordpress_instance.go
¶
Lastly, let's add the operations to mange WordpressInstance
objects. The
scaffolding for this is in internal/wordpress/v0_wordpress_instance.go
. If
you open that file, you'll see empty functions to create, update and delete
these objects. Again, when actions are executed through the Threeport API,
these functions will be called to reconcile the desired operations.
Update the v0WordpressInstanceCreated
function as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
Now, update the v0WordpressInstanceDeleted
function with the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
Ensure the following imports are included in this file.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
At this point the create and delete functionality for the WordPress objects has been implemented. For now, we will skip the update functionality for now. That can be added later.
Add Config Abstractions¶
Updates to pkg/config/v0/wordpress.go
¶
The file pkg/config/v0/wordpress.go
contains scaffolding for config
abstractions that allow a single user command to make multiple API calls on the
user's behalf. This is an important way to reduce toil on the user of the
system and provide useful user abstractions.
Let's add the source code to provide these config abstractions.
First, we need to update three types. These types determine the schema for config interfaces that will be used to manage WordPress objects.
Find the types below in that file and update them to match the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Next, let's update the methods on the WordpressValues
object to match the code
shown here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
Now, update the Create
and Delete
methods on the WordpressDefinitionValues
object as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
Finally, update the Create
and Delete
methods on the WordpressInstanceValues
object as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
Ensure the imports for this pkg/config/v0/wordpress.go
file match the
following.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
That's it! We've added 1 file and modified 3 others. We're ready to build and deploy the extension to Threeport.
Project Dependencies¶
Now, we need to get the project's go library dependencies and ensure they match the versions in the core Threeport repository. This is because we'll be building a shared object binary for the tptctl plugin and the dependency versions need to match.
The Threeport SDK has a command to do just this.
1 |
|
Running this command will update your go.mod
file so that your extension works
with the latest released version of Threeport. If you would like to sync it with
an earlier version of Threeport, or with a version of Threeport that is in
development on your local filesystem, check the usage for this command with
threeport-sdk sync -h
for instructions.
Build¶
The Threeport SDK provides mage targets with convenient
development utilities. To see all available mage targets, simple run mage
.
First, let's build the tptctl
plugin for the WordPress extension.
1 |
|
Note: If you're using a Mac, you may encounter security restrictions that prevent your workstation from running the plugin. If, when you run it, you see output similar to this:
Then run the following command.
1
[1] 40259 killed tptctl wordpress -h
Then repeat the two steps below to install and test the plugin.
1
codesign -f -s - bin/wordpress.so
Now, we can install the plugin. If you'd like to install the plugin to a
different location from the default ~/.threeport/plugins
, see the usage
information with tptctl help
for more info.
1 |
|
Check the plugin was successfully installed.
1 |
|
You should see the help output for the wordpress plugin similar to that shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Next, we need to build the binaries and container images for each containerized component that will be installed into a Threeport control plane. After the images are built, they will be pushed to a container registry to make them available for installation.
Note: In this and other following sections, you will have the option to test the WordPress extension in a "Local Dev Environment" on your workstation, or in a "Remote Environment" running in AWS.
The remote environment option will provide a more realistic use case and will allow you to test using AWS RDS as the persistent data store for WordPress, but requires an AWS account, will cost money and take a little longer. This tutorial will also require a Route53 hosted zone you can use for DNS. The local dev environment will be faster to spin up and down and cost nothing on your cloud bill. Choose the appropriate tab in each section for the method you choose.
If you're going to test this locally, first spin up a local container registry so we don't have to wait for images to pushed to - and pulled from - a remote registry. This mage target will create run a local docker container to serve as the container registry.
1 |
|
Now we can build and push the container images to the local registry.
1 |
|
If using a remote AWS environment, build for the release architecture (amd64).
This step will build the binaries and container images, then push them to your
registry (as defined in the sdk-config.yaml
file). This may take a few
minutes as three container images will be pushed to your remote container
registry. You will need to be logged in to your container registry from your
command line.
1 |
|
Threeport Control Plane¶
If you don't aleady have one, install a Threeport control plane for testing.
To install Threeport that will pull images from a local registry, use the following command.
1 2 3 4 5 |
|
For more info on installing Threeport locally, see the documentation to Install Threeport Locally.
To install Threeport in AWS, use the following command. Substitute your region of choice as needed.
1 2 3 4 |
|
For more info on installing Threeport remotely, see the documentation to Install Threeport on AWS.
Install WordPress Extension¶
To install the WordPress extension using the image pushed to the local dev registry, use the following command.
1 |
|
If installing remotely, run the install as shown here to pull images from
your default image repo (as declared in your sdk-config.yaml
file).
1 |
|
Use the WordpressExtension¶
For a local install of WordPress use the following config. The Environment
and ManagedDatabase
fields are shown with their default values and aren't
required. They are only included for the sake of explicitness for this
tutorial.
1 2 3 4 5 |
|
Install WordPress.
1 |
|
In order to use a Route53 hosted zone to manage DNS for your WordPress app,
you will need to register that with Threeport. Create a domain name
definition config like this. Replace the values for the Domain
and
AdminEmail
fields to work for your setup.
1 2 3 4 5 6 |
|
Now you can register this hosted zone with Threeport.
1 |
|
For a remote install, we can use AWS RDS for the database as well as Route53 for DNS.
1 2 3 4 5 6 7 8 9 10 |
|
Install WordPress.
1 |
|
Verification¶
After a few minutes, the wordpress instance should be running. You can view the app in your browser by following the following steps.
Note: You will need to have kubectl installed on your machine to perform these steps.
Get the Threeport-managed namespace that was created for the WordPress app.
1 |
|
Start a port forward to the WordPress service.
1 |
|
Now visit http://localhost:8080 in your browser.
When finished, hit Ctrl+C in the terminal where the port forward is running to cancel it.
To verify the app is up and running, just type blog.[your domain]
(using
the domain you configured in domain-name.yaml
) into a browser. By
default, Let's Encrypt is used as the TLS cert
provider. Also, by default, the staging environment is used. This means
the certificate will be valid but not publicly trusted. So your browser
will give you a warning. You can tell your browser to proceed anyway, and
you should get the default homepage served to you.
Clean Up¶
Follow the instructions to remove the WordPress app and Threeport for either the Local or Remote environment.
Remove the WordPress app by deleting it with tptctl.
1 |
|
Remove the local Threeport control plane.
1 |
|
Remove the local container registry.
1 |
|
Remove the WordPress app by deleting it with tptctl.
1 |
|
After deleting the WordPress app, wait a few minutes before proceeding. We need to give the system enough time to clean up your Route53 hosted zone before removing the support services in the next step. If you like, you can check your Route53 hosted zone in the AWS portal before proceeding. Otherwise, just wait 5 minutes to be safe.
Next, we need to Remove the support service workloads that were deployed. By default, Threeport will not remove a Kubernetes runtime with workloads deployed. We can view them with this command.
1 |
|
Delete the support service workloads.
1 2 |
|
Wait another 5 minutes before proceeding to ensure the AWS load balancer connected to your ingress layer is removed before deleting the control plane altogether in the next step.
Remove the local Threeport control plane.
1 |
|
Summary¶
In this tutorial we walked through each and every step to build a sample extension to the Threeport control plane. You can apply the same process to create any extension to Threeport that you like. Use cases are not limited to particular workload support. Other use cases can include:
- Support for alternative infrastructure providers.
- Support for managed services on infrastructure providers. This is not limited to cloud providers. It could include services like DataDog, Splunk, MongoDB, or literally any other software service that has an API.
- Support for alternative runtime environments besides Kubernetes, e.g. VMs or function-as-a-service offerings.