|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package v1alpha2 |
| 5 | + |
| 6 | +import ( |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | +) |
| 9 | + |
| 10 | +// The Management Policy defines how the controller will manage tokens in the specified Agent Pool. |
| 11 | +// - `merge` — the controller will manage its tokens alongside any existing tokens in the pool, without modifying or deleting tokens it does not own. |
| 12 | +// - `owner` — the controller assumes full ownership of all agent tokens in the pool, managing and potentially modifying or deleting all tokens, including those not created by it. |
| 13 | +type AgentTokenManagementPolicy string |
| 14 | + |
| 15 | +const ( |
| 16 | + AgentTokenManagementPolicyMerge AgentTokenManagementPolicy = "merge" |
| 17 | + AgentTokenManagementPolicyOwner AgentTokenManagementPolicy = "owner" |
| 18 | +) |
| 19 | + |
| 20 | +// The Deletion Policy defines how managed tokens and Kubernetes Secrets should be handled when the custom resource is deleted. |
| 21 | +// - `retain`: When the custom resource is deleted, the operator will remove only the resource itself. |
| 22 | +// The managed HCP Terraform Agent tokens will remain active on the HCP Terraform side, and the corresponding Kubernetes Secret will not be modified. |
| 23 | +// - `destroy`: The operator will attempt to delete the managed HCP Terraform Agent tokens and remove the corresponding Kubernetes Secret. |
| 24 | +type AgentTokenDeletionPolicy string |
| 25 | + |
| 26 | +const ( |
| 27 | + AgentTokenDeletionPolicyRetain AgentTokenDeletionPolicy = "retain" |
| 28 | + AgentTokenDeletionPolicyDestroy AgentTokenDeletionPolicy = "destroy" |
| 29 | +) |
| 30 | + |
| 31 | +// AgentTokenSpec defines the desired state of AgentToken. |
| 32 | +type AgentTokenSpec struct { |
| 33 | + // Organization name where the Workspace will be created. |
| 34 | + // More information: |
| 35 | + // - https://developer.hashicorp.com/terraform/cloud-docs/users-teams-organizations/organizations |
| 36 | + // |
| 37 | + //+kubebuilder:validation:MinLength:=1 |
| 38 | + Organization string `json:"organization"` |
| 39 | + // API Token to be used for API calls. |
| 40 | + Token Token `json:"token"` |
| 41 | + // The Deletion Policy defines how managed tokens and Kubernetes Secrets should be handled when the custom resource is deleted. |
| 42 | + // - `retain`: When the custom resource is deleted, the operator will remove only the resource itself. |
| 43 | + // The managed HCP Terraform Agent tokens will remain active on the HCP Terraform side, and the corresponding Kubernetes Secret will not be modified. |
| 44 | + // - `destroy`: The operator will attempt to delete the managed HCP Terraform Agent tokens and remove the corresponding Kubernetes Secret. |
| 45 | + // Default: `retain`. |
| 46 | + // |
| 47 | + //+kubebuilder:validation:Enum:=retain;destroy |
| 48 | + //+kubebuilder:default=retain |
| 49 | + //+optional |
| 50 | + DeletionPolicy AgentTokenDeletionPolicy `json:"deletionPolicy,omitempty"` |
| 51 | + // The Agent Pool name or ID where the tokens will be managed. |
| 52 | + AgentPool AgentPoolRef `json:"agentPool"` |
| 53 | + // The Management Policy defines how the controller will manage tokens in the specified Agent Pool. |
| 54 | + // - `merge` — the controller will manage its tokens alongside any existing tokens in the pool, without modifying or deleting tokens it does not own. |
| 55 | + // - `owner` — the controller assumes full ownership of all agent tokens in the pool, managing and potentially modifying or deleting all tokens, including those not created by it. |
| 56 | + // Default: `merge`. |
| 57 | + // |
| 58 | + //+kubebuilder:validation:Enum:=merge;owner |
| 59 | + //+kubebuilder:default=merge |
| 60 | + //+optional |
| 61 | + ManagementPolicy AgentTokenManagementPolicy `json:"managementPolicy,omitempty"` |
| 62 | + // List of the HCP Terraform Agent tokens to manage. |
| 63 | + // |
| 64 | + //+kubebuilder:validation:MinItems:=1 |
| 65 | + AgentTokens []AgentAPIToken `json:"agentTokens"` |
| 66 | + // secretName specifies the name of the Kubernetes Secret |
| 67 | + // where the HCP Terraform Agent tokens are stored. |
| 68 | + // |
| 69 | + //+kubebuilder:validation:MinLength:=1 |
| 70 | + SecretName string `json:"secretName"` |
| 71 | +} |
| 72 | + |
| 73 | +// AgentTokenStatus defines the observed state of AgentToken. |
| 74 | +type AgentTokenStatus struct { |
| 75 | + // Real world state generation. |
| 76 | + ObservedGeneration int64 `json:"observedGeneration"` |
| 77 | + // Agent Pool where tokens are managed by the controller. |
| 78 | + AgentPool *AgentPoolRef `json:"agentPool,omitempty"` |
| 79 | + // List of the agent tokens managed by the controller. |
| 80 | + // |
| 81 | + //+optional |
| 82 | + AgentTokens []*AgentAPIToken `json:"agentTokens,omitempty"` |
| 83 | +} |
| 84 | + |
| 85 | +// +kubebuilder:object:root=true |
| 86 | +// +kubebuilder:subresource:status |
| 87 | +//+kubebuilder:printcolumn:name="Agent Pool Name",type=string,JSONPath=`.status.agentPool.name` |
| 88 | +//+kubebuilder:printcolumn:name="Agent Pool ID",type=string,JSONPath=`.status.agentPool.id` |
| 89 | +//+kubebuilder:metadata:labels="app.terraform.io/crd-schema-version=v25.9.0" |
| 90 | + |
| 91 | +// AgentToken manages HCP Terraform Agent Tokens. |
| 92 | +// More information: |
| 93 | +// - https://developer.hashicorp.com/terraform/cloud-docs/users-teams-organizations/api-tokens#agent-api-tokens |
| 94 | +type AgentToken struct { |
| 95 | + metav1.TypeMeta `json:",inline"` |
| 96 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 97 | + |
| 98 | + Spec AgentTokenSpec `json:"spec"` |
| 99 | + Status AgentTokenStatus `json:"status,omitempty"` |
| 100 | +} |
| 101 | + |
| 102 | +// +kubebuilder:object:root=true |
| 103 | + |
| 104 | +// AgentTokenList contains a list of AgentToken. |
| 105 | +type AgentTokenList struct { |
| 106 | + metav1.TypeMeta `json:",inline"` |
| 107 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 108 | + Items []AgentToken `json:"items"` |
| 109 | +} |
| 110 | + |
| 111 | +func init() { |
| 112 | + SchemeBuilder.Register(&AgentToken{}, &AgentTokenList{}) |
| 113 | +} |
0 commit comments