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

Allow Kubernetes Agent to be registered as a worker #655

Merged
merged 41 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e44242f
add k8s worker to terraform
rain-on Jun 17, 2024
4bfd2d8
Merge remote-tracking branch 'origin/main' into tmm/k8gent_worker
rain-on Jun 25, 2024
74eb654
create schema_worker
rain-on Jun 26, 2024
cc23624
adding examples I think
rain-on Jun 26, 2024
e3b972f
might be working
rain-on Jun 26, 2024
cea6f0f
don't lose file
rain-on Jun 26, 2024
e60707a
personal review of production code
rain-on Jun 26, 2024
72fd4c7
move files around for testing
rain-on Jun 27, 2024
46c29f2
updated integration test
rain-on Jun 27, 2024
c183325
trying to get workerpool_ids working again
rain-on Jun 27, 2024
cb50746
force worker to on in int tests
rain-on Jun 27, 2024
949a1b6
no idea
rain-on Jul 1, 2024
b04c7ea
pre merge
rain-on Jul 16, 2024
577db6a
Merge remote-tracking branch 'origin/main' into tmm/k8gent_worker
rain-on Jul 16, 2024
6432d3b
use newer test framework
rain-on Jul 19, 2024
ae82ee0
new framework for test
rain-on Jul 19, 2024
3955a95
add some help output
rain-on Jul 19, 2024
9215277
change comms style
rain-on Jul 19, 2024
3153a42
fix enabled/disabled
rain-on Jul 21, 2024
ea9b891
fix naming
rain-on Jul 22, 2024
142eeeb
flip it all around again
rain-on Jul 22, 2024
1631a56
ensure upgrade locked is false by default
rain-on Jul 22, 2024
fcd96a8
revert workflow file
rain-on Jul 22, 2024
c73f5e4
Merge remote-tracking branch 'origin/main' into tmm/k8gent_worker
rain-on Jul 29, 2024
133eb2b
Merge remote-tracking branch 'origin/main' into tmm/k8gent_worker
rain-on Aug 5, 2024
7ef0bb6
not sure what was done in this commit - maybe drop?
rain-on Aug 16, 2024
8bdcb0b
Merge remote-tracking branch 'origin/main' into tmm/k8gent_worker
rain-on Aug 16, 2024
1d7e1cd
hack container creation into int test
rain-on Aug 16, 2024
b316ba8
Merge branch 'main' into tmm/k8gent_worker
kevjt Aug 19, 2024
8c7d0c0
update test to conform with the new way of integration testing
kevjt Aug 20, 2024
140a672
regenerate docs
kevjt Aug 20, 2024
fbae2c2
edit
kevjt Aug 20, 2024
a4ffcd1
remove random char
kevjt Aug 20, 2024
653b443
fix
kevjt Aug 20, 2024
63b0412
use space aware go client functions
kevjt Aug 21, 2024
8db84aa
Merge branch 'main' into tmm/k8gent_worker
kevjt Aug 21, 2024
5cecf88
regenerate docs to pass the doc validation github action
kevjt Aug 21, 2024
0dc91ee
try add newline
kevjt Aug 21, 2024
7e4a4d7
isolate test container
kevjt Aug 21, 2024
318d6b9
data source result must not be optional
kevjt Aug 23, 2024
a7cfbf9
put newline back so validate docs is happy
kevjt Aug 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/data-sources/kubernetes_agent_workers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "octopusdeploy_kubernetes_agent_workers Data Source - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
Provides information about existing kubernetes agent workers.
---

# octopusdeploy_kubernetes_agent_workers (Data Source)

Provides information about existing kubernetes agent workers.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `health_statuses` (List of String) A filter to search by a list of health statuses of resources. Valid health statuses are `HasWarnings`, `Healthy`, `Unavailable`, `Unhealthy`, or `Unknown`.
- `ids` (List of String) A filter to search by a list of IDs.
- `is_disabled` (Boolean) A filter to search by the disabled status of a resource.
- `name` (String) A filter to search by name.
- `partial_name` (String) A filter to search by the partial match of a name.
- `roles` (List of String) A filter to search by a list of role IDs.
- `shell_names` (List of String) A list of shell names to match in the query and/or search
- `skip` (Number) A filter to specify the number of items to skip in the response.
- `space_id` (String) The space ID associated with this resource.
- `take` (Number) A filter to specify the number of items to take (or return) in the response.
- `thumbprint` (String) The thumbprint of the deployment target to match in the query and/or search

### Read-Only

- `id` (String) An auto-generated identifier that includes the timestamp when this data source was last modified.
- `kubernetes_agent_workers` (List of Object) A list of kubernetes agent workers that match the filter(s). (see [below for nested schema](#nestedatt--kubernetes_agent_workers))

<a id="nestedatt--kubernetes_agent_workers"></a>
### Nested Schema for `kubernetes_agent_workers`

Read-Only:

- `agent_helm_release_name` (String)
- `agent_kubernetes_namespace` (String)
- `agent_tentacle_version` (String)
- `agent_upgrade_status` (String)
- `agent_version` (String)
- `communication_mode` (String)
- `id` (String)
- `is_disabled` (Boolean)
- `machine_policy_id` (String)
- `name` (String)
- `space_id` (String)
- `thumbprint` (String)
- `upgrade_locked` (Boolean)
- `uri` (String)
- `worker_pool_ids` (List of String)


67 changes: 67 additions & 0 deletions docs/resources/kubernetes_agent_worker.md
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we have docs in other places that give examples on how to use this terraform provider in conjunction with the helm provider to actually create the kubernetes agent/worker?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no - not yet - we'll make docs along the lines of those used for the deployment-target docs

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "octopusdeploy_kubernetes_agent_worker Resource - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
This resource manages Kubernetes agent workers in Octopus Deploy.
---

# octopusdeploy_kubernetes_agent_worker (Resource)

This resource manages Kubernetes agent workers in Octopus Deploy.

## Example Usage

```terraform
resource "octopusdeploy_kubernetes_agent_worker" "minimal" {
name = "agent-minimal"
worker_pools = ["worker-pools-1"]
thumbprint = "96203ED84246201C26A2F4360D7CBC36AC1D232D"
uri = "poll://kcxzcv2fpsxkn6tk9u6d/"
}

resource "octopusdeploy_kubernetes_agent_worker" "optionals" {
name = "agent-optionals"
worker_pools = ["worker-pools-1", "worker-pools-3"]
thumbprint = "96203ED84246201C26A2F4360D7CBC36AC1D232D"
uri = "poll://kcxzcv2fpsxkn6tk9u6d/"
machine_policy_id = "machinepolicies-1"
upgrade_locked = true
is_disabled = true
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of this resource.
- `thumbprint` (String) The thumbprint of the Kubernetes agent's certificate used by server to verify the identity of the agent. This is the same thumbprint that was used when installing the agent.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Most other targets have thumbprint as optional. This is probably required for the agent vs other targets but just want to raise it to double check this is correct.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is required here because of the way the Kubernetes agent is installed as a polling Tentacle. Normally the agent sends its own thumbprint to Octopus server during installation where it registers itself as a target/worker. However, in the case of Terraform the resource in Octopus server is being created first so the exact thumbprint must be known ahead of time.

- `uri` (String) The URI of the Kubernetes agent's used by the server to queue messages. This is the same subscription uri that was used when installing the agent.
- `worker_pool_ids` (List of String) A list of worker pool Ids specifying the pools in which this worker belongs

### Optional

- `communication_mode` (String) The communication mode used by the Kubernetes agent to communicate with Octopus Server. Currently, the only supported value is 'Polling'.
- `id` (String) The unique ID for this resource.
- `is_disabled` (Boolean) Whether the Kubernetes agent is disabled. If the agent is disabled, it will not be included in any deployments.
- `machine_policy_id` (String) Optional ID of the machine policy that the Kubernetes agent will use. If not provided the default machine policy will be used.
- `space_id` (String) The space ID associated with this resource.
- `upgrade_locked` (Boolean) If enabled the Kubernetes agent will not automatically upgrade and will stay on the currently installed version, even if the associated machine policy is configured to automatically upgrade.

### Read-Only

- `agent_helm_release_name` (String) Name of the Helm release that the agent belongs to.
- `agent_kubernetes_namespace` (String) Name of the Kubernetes namespace where the agent is installed.
- `agent_tentacle_version` (String) Current Tentacle version of the agent
- `agent_upgrade_status` (String) Current upgrade availability status of the agent. One of 'NoUpgrades', 'UpgradeAvailable', 'UpgradeSuggested', 'UpgradeRequired'
- `agent_version` (String) Current Helm chart version of the agent.

## Import

Import is supported using the following syntax:

```shell
terraform import [options] octopusdeploy_kubernetes_agent_worker.<name> <machine-id>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import [options] octopusdeploy_kubernetes_agent_worker.<name> <machine-id>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "octopusdeploy_kubernetes_agent_worker" "minimal" {
name = "agent-minimal"
worker_pools = ["worker-pools-1"]
thumbprint = "96203ED84246201C26A2F4360D7CBC36AC1D232D"
uri = "poll://kcxzcv2fpsxkn6tk9u6d/"
}

resource "octopusdeploy_kubernetes_agent_worker" "optionals" {
name = "agent-optionals"
worker_pools = ["worker-pools-1", "worker-pools-3"]
thumbprint = "96203ED84246201C26A2F4360D7CBC36AC1D232D"
uri = "poll://kcxzcv2fpsxkn6tk9u6d/"
machine_policy_id = "machinepolicies-1"
upgrade_locked = true
is_disabled = true
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/OctopusDeploy/terraform-provider-octopusdeploy
go 1.21

require (
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.49.2
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/Microsoft/hcsshim v0.12.4 h1:Ev7YUMHAHoWNm+aDSPzc5W9s6E2jyL1szpVDJeZ/
github.com/Microsoft/hcsshim v0.12.4/go.mod h1:Iyl1WVpZzr+UkzjekHZbV8o5Z9ZkxNGx6CtY2Qg/JVQ=
github.com/OctopusDeploy/go-octodiff v1.0.0 h1:U+ORg6azniwwYo+O44giOw6TiD5USk8S4VDhOQ0Ven0=
github.com/OctopusDeploy/go-octodiff v1.0.0/go.mod h1:Mze0+EkOWTgTmi8++fyUc6r0aLZT7qD9gX+31t8MmIU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.49.2 h1:ENd1MyQbYIDiW1ZyXRUcZr4OQ0d8j47I5a6DOT9Ez4o=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.49.2/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0 h1:rQiLEbqt/D3lPQw3pq9sXAW1C0WhVLrfN/h0cqUzaFY=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4 h1:QfbVf0bOIRMp/WHAWsuVDB7KHoWnRsGbvDuOf2ua7k4=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4/go.mod h1:Oq9KbiRNDBB5jFmrwnrgLX0urIqR/1ptY18TzkqXm7M=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 h1:bkyFVUP+ROOARdgCiJzNQo2V2kiB97LyUpzH9P6Hrlg=
Expand Down
51 changes: 51 additions & 0 deletions octopusdeploy/data_source_kubernetes_agent_workers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package octopusdeploy

import (
"context"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"time"
)

func dataSourceKubernetesAgentWorkers() *schema.Resource {
return &schema.Resource{
Description: "Provides information about existing kubernetes agent workers.",
ReadContext: dataSourceKubernetesAgentWorkersRead,
Schema: getKubernetesAgentWorkerDataSchema(),
}
}

func dataSourceKubernetesAgentWorkersRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
query := machines.WorkersQuery{
CommunicationStyles: []string{"KubernetesTentacle"},
HealthStatuses: expandArray(d.Get("health_statuses").([]interface{})),
IDs: expandArray(d.Get("ids").([]interface{})),
IsDisabled: d.Get("is_disabled").(bool),
Name: d.Get("name").(string),
PartialName: d.Get("partial_name").(string),
ShellNames: expandArray(d.Get("shell_names").([]interface{})),
Skip: d.Get("skip").(int),
Take: d.Get("take").(int),
Thumbprint: d.Get("thumbprint").(string),
}

client := m.(*client.Client)
existingWorkers, err := client.Workers.Get(query)
if err != nil {
return diag.FromErr(err)
}

flattenedKubernetesAgents := []interface{}{}
for _, worker := range existingWorkers.Items {
flattenedKubernetesAgents = append(flattenedKubernetesAgents, flattenKubernetesAgentWorker(worker))
}

err = d.Set("kubernetes_agent_workers", flattenedKubernetesAgents)
if err != nil {
return diag.FromErr(err)
}
d.SetId("KubernetesAgentWorkers " + time.Now().UTC().String())
return nil
}
2 changes: 2 additions & 0 deletions octopusdeploy/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func Provider() *schema.Provider {
"octopusdeploy_channels": dataSourceChannels(),
"octopusdeploy_deployment_targets": dataSourceDeploymentTargets(),
"octopusdeploy_kubernetes_agent_deployment_targets": dataSourceKubernetesAgentDeploymentTargets(),
"octopusdeploy_kubernetes_agent_workers": dataSourceKubernetesAgentWorkers(),
"octopusdeploy_kubernetes_cluster_deployment_targets": dataSourceKubernetesClusterDeploymentTargets(),
"octopusdeploy_listening_tentacle_deployment_targets": dataSourceListeningTentacleDeploymentTargets(),
"octopusdeploy_machine": dataSourceMachine(),
Expand Down Expand Up @@ -47,6 +48,7 @@ func Provider() *schema.Provider {
"octopusdeploy_dynamic_worker_pool": resourceDynamicWorkerPool(),
"octopusdeploy_gcp_account": resourceGoogleCloudPlatformAccount(),
"octopusdeploy_kubernetes_agent_deployment_target": resourceKubernetesAgentDeploymentTarget(),
"octopusdeploy_kubernetes_agent_worker": resourceKubernetesAgentWorker(),
"octopusdeploy_kubernetes_cluster_deployment_target": resourceKubernetesClusterDeploymentTarget(),
"octopusdeploy_listening_tentacle_deployment_target": resourceListeningTentacleDeploymentTarget(),
"octopusdeploy_machine_policy": resourceMachinePolicy(),
Expand Down
79 changes: 79 additions & 0 deletions octopusdeploy/resource_kubernetes_agent_worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package octopusdeploy

import (
"context"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/workers"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceKubernetesAgentWorker() *schema.Resource {
return &schema.Resource{
CreateContext: resourceKubernetesAgentWorkerCreate,
DeleteContext: resourceKubernetesAgentWorkerDelete,
Description: "This resource manages Kubernetes agent workers in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceKubernetesAgentWorkerRead,
Schema: getKubernetesAgentWorkerSchema(),
UpdateContext: resourceKubernetesAgentWorkerUpdate,
}
}

func resourceKubernetesAgentWorkerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
worker := expandKubernetesAgentWorker(d)
client := m.(*client.Client)
createdWorker, err := workers.Add(client, worker)
if err != nil {
return diag.FromErr(err)
}

d.SetId(createdWorker.GetID())
return nil
}

func resourceKubernetesAgentWorkerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*client.Client)
Worker, err := workers.GetByID(client, d.Get("space_id").(string), d.Id())
if err != nil {
return errors.ProcessApiError(ctx, d, err, "kubernetes tentacle worker")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any reason why .ProcessApiError is only used in the read operation and not the others? I can see the extra thing it does on top of just returning an error is it deletes the resource from the terraform state if the api returns 404. I'm curious why this logic should exist for read and not update for example.

}

flattenedKubernetesAgentWorker := flattenKubernetesAgentWorker(Worker)
for key, value := range flattenedKubernetesAgentWorker {
if key != "id" {
err := d.Set(key, value)
if err != nil {
return diag.FromErr(err)
}
}
}

return nil
}

func resourceKubernetesAgentWorkerDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*client.Client)
if err := workers.DeleteByID(client, d.Get("space_id").(string), d.Id()); err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
}

func resourceKubernetesAgentWorkerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
worker := expandKubernetesAgentWorker(d)
client := m.(*client.Client)

worker.ID = d.Id()

updatedWorker, err := workers.Update(client, worker)
if err != nil {
return diag.FromErr(err)
}

d.SetId(updatedWorker.GetID())

return nil
}
Loading
Loading