Skip to content

Commit

Permalink
feat: [CDS-76236]: Namespaced install of agent (#704)
Browse files Browse the repository at this point in the history
* feat: [CDS-76236]: add changelog

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

* feat: [CDS-76236]: docs namespaced agent

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

* feat: [CDS-76236]: docs namespaced agent

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

* feat: [CDS-76236]: namespaced agent

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>

---------

Signed-off-by: Mirko Teodorovic <mirko.teodorovic@harness.io>
  • Loading branch information
mteodor committed Oct 3, 2023
1 parent 87c52c3 commit 9994389
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/704.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Added is_namespaced for agent creating
```
1 change: 1 addition & 0 deletions docs/data-sources/platform_gitops_agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ Read-Only:

- `high_availability` (Boolean)
- `namespace` (String)
- `is_namespaced` (Boolean)
1 change: 1 addition & 0 deletions docs/resources/platform_gitops_agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Optional:

- `high_availability` (Boolean) Indicates if the deployment should be deployed using the deploy-ha.yaml
- `namespace` (String) The k8s namespace that this agent resides in.
- `is_namespaced` (Boolean) Indicates if the agent is namespaced. If true no cluster roles.

## Import

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func DataSourceGitopsAgent() *schema.Resource {
Type: schema.TypeBool,
Computed: true,
},
"is_namespaced": {
Description: "Indicates if the agent is namespaced.",
Type: schema.TypeBool,
Optional: true,
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func testAccDataSourceGitopsAgent(agentId string, name string, accountId string,
metadata {
namespace = "terraform-test"
high_availability = false
is_namespaced = true
}
operator = "ARGO"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func ResourceGitopsAgent() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"is_namespaced": {
Description: "Indicates if the agent is namespaced.",
Type: schema.TypeBool,
Optional: true,
},
}},
},
"agent_token": {
Expand Down Expand Up @@ -226,6 +231,9 @@ func buildCreateUpdateAgentRequest(d *schema.ResourceData) *nextgen.V1Agent {
if meta["namespace"] != nil {
v1MetaData.Namespace = meta["namespace"].(string)
}
if meta["is_namespaced"] != nil {
v1MetaData.IsNamespaced = meta["is_namespaced"].(bool)
}

v1Agent.Metadata = &v1MetaData
}
Expand Down Expand Up @@ -256,6 +264,7 @@ func readAgent(d *schema.ResourceData, agent *nextgen.V1Agent) {
metaDataMap := map[string]interface{}{}
metaDataMap["namespace"] = agent.Metadata.Namespace
metaDataMap["high_availability"] = agent.Metadata.HighAvailability
metaDataMap["is_namespaced"] = agent.Metadata.IsNamespaced
metadata = append(metadata, metaDataMap)
d.Set("metadata", metadata)
if agent.Credentials != nil && agent.Credentials.PrivateKey != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func TestAccResourceGitopsAgent(t *testing.T) {
CheckDestroy: testAccResourceGitopsAgentDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceGitopsAgentAccountLevel(id, accountId, agentName, namespace),
Config: testAccResourceGitopsAgentAccountLevel(id, accountId, agentName, namespace, "false"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", agentName),
resource.TestCheckResourceAttrSet(resourceName, "agent_token"),
),
},
{
Config: testAccResourceGitopsAgentAccountLevel(id, accountId, agentName, updatedNamespace),
Config: testAccResourceGitopsAgentAccountLevel(id, accountId, agentName, updatedNamespace, "false"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "metadata.0.namespace", updatedNamespace),
resource.TestCheckResourceAttrSet(resourceName, "agent_token"),
Expand Down Expand Up @@ -167,6 +167,48 @@ func TestAccResourceGitopsAgentFlamingo(t *testing.T) {
})
}

func TestAccResourceGitopsAgentNS(t *testing.T) {
// Account Level
id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5))
id = strings.ReplaceAll(id, "_", "")
accountId := os.Getenv("HARNESS_ACCOUNT_ID")
resourceName := "harness_platform_gitops_agent.test"
agentName := id
namespace := "terraform-test"
updatedNamespace := namespace + "-updated"
resource.UnitTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccResourceGitopsAgentDestroy(resourceName),
Steps: []resource.TestStep{
{
Config: testAccResourceGitopsAgentAccountLevel(id, accountId, agentName, namespace, "true"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", agentName),
resource.TestCheckResourceAttrSet(resourceName, "agent_token"),
resource.TestCheckResourceAttr(resourceName, "metadata.0.is_namespaced", "true"),
),
},
{
Config: testAccResourceGitopsAgentAccountLevel(id, accountId, agentName, updatedNamespace, "true"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "metadata.0.namespace", updatedNamespace),
resource.TestCheckResourceAttr(resourceName, "metadata.0.is_namespaced", "true"),
resource.TestCheckResourceAttrSet(resourceName, "agent_token"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"account_id", "agent_token"},
ImportStateIdFunc: acctest.ProjectResourceImportStateIdFunc(resourceName),
},
},
})

}

func testAccGetAgent(resourceName string, state *terraform.State) (*nextgen.V1Agent, error) {
r := acctest.TestAccGetResource(resourceName, state)
c, ctx := acctest.TestAccGetPlatformClientWithContext()
Expand Down Expand Up @@ -200,7 +242,7 @@ func testAccResourceGitopsAgentDestroy(resourceName string) resource.TestCheckFu

}

func testAccResourceGitopsAgentAccountLevel(agentId string, accountId string, agentName string, namespace string) string {
func testAccResourceGitopsAgentAccountLevel(agentId string, accountId string, agentName string, namespace string, isNamespaced string) string {
return fmt.Sprintf(`
resource "harness_platform_gitops_agent" "test" {
identifier = "%[1]s"
Expand All @@ -210,10 +252,11 @@ func testAccResourceGitopsAgentAccountLevel(agentId string, accountId string, ag
metadata {
namespace = "%[4]s"
high_availability = false
is_namespaced = %[5]s
}
operator = "ARGO"
}
`, agentId, accountId, agentName, namespace)
`, agentId, accountId, agentName, namespace, isNamespaced)
}
func testAccResourceGitopsAgentProjectLevel(agentId string, accountId string, agentName string, namespace string) string {
return fmt.Sprintf(`
Expand Down

0 comments on commit 9994389

Please sign in to comment.