Skip to content

Commit

Permalink
Merge pull request #598 from anvial/JUJU-6811-implement-kubernetes-cl…
Browse files Browse the repository at this point in the history
…oud-resource-create-function

#598

## Description
- Added CRUD methods in `kubernetesCloudsClient` to handle the creation of Kubernetes clouds.
- Integrated the new method into the Terraform provider's CRUD functions in `kubernetesCloudResource`.
- Updated the schema and model to include necessary attributes for Kubernetes cloud create/read/update/delete.

## Type of change

- Change existing resource

## Environment

- Juju controller version: 

- Terraform version: 

## QA steps

```bash
# get k8s cloud config yaml
microk8s.config view > test-k8s-config.yaml
```

```tf
terraform {
 required_providers {
 juju = {
 version = "0.15.0"
 source = "juju/juju"
 }
 }
}

resource "juju_kubernetes_cloud" "test-k8s-cloud" {
 name = "test-k8s-cloud"
 kubernetesconfig = file("<path-to-test-k8s-config.yaml>")
}

```

```
juju clouds --controller <controller_name>
# output should demonstrate freshly added cloud to the controller
```
  • Loading branch information
jujubot authored Oct 9, 2024
2 parents 1a0a37b + fd816d3 commit 21c1238
Show file tree
Hide file tree
Showing 15 changed files with 880 additions and 90 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/test_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ jobs:
channel: ${{ matrix.action-operator.cloud-channel }}
juju-channel: ${{ matrix.action-operator.juju }}
lxd-channel: ${{ matrix.action-operator.lxd-channel }}
- name: In case of LXD setup also microk8s
if: ${{ matrix.action-operator.cloud == 'lxd' }}
run: |
sudo snap install microk8s --channel=1.28-strict/stable
sudo usermod -a -G snap_microk8s $USER
sudo chown -R $USER ~/.kube
sudo microk8s.enable dns storage
sudo microk8s.enable dns local-storage
sudo -g snap_microk8s -E microk8s status --wait-ready --timeout=600
- name: Create additional networks when testing with LXD
if: ${{ matrix.action-operator.cloud == 'lxd' }}
run: |
Expand All @@ -88,6 +97,11 @@ jobs:
echo "EOF" >> $GITHUB_ENV
echo "TEST_MANAGEMENT_BR=10.150.40.0/24" >> $GITHUB_ENV
echo "TEST_PUBLIC_BR=10.170.80.0/24" >> $GITHUB_ENV
- name: "Set additional environment for LXD"
if: ${{ matrix.action-operator.cloud == 'lxd' }}
# language=bash
run: |
sudo microk8s.config > /home/$USER/microk8s-config.yaml
- run: go mod download
- env:
TF_ACC: "1"
Expand Down
11 changes: 11 additions & 0 deletions .github/workflows/test_integration_jaas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ jobs:
jimm-version: v3.1.10
juju-channel: 3/stable
ghcr-pat: ${{ secrets.GITHUB_TOKEN }}
- name: Setup microk8s for juju_kubernetes_cloud test
run: |
sudo snap install microk8s --channel=1.28-strict/stable
sudo usermod -a -G snap_microk8s $USER
sudo chown -R $USER ~/.kube
sudo microk8s.enable dns storage
sudo microk8s.enable dns local-storage
sudo -g snap_microk8s -E microk8s status --wait-ready --timeout=600
echo "MICROK8S_CONFIG<<EOF" >> $GITHUB_ENV
sudo microk8s.config view >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Create additional networks when testing with LXD
run: |
sudo lxc network create management-br ipv4.address=10.150.40.1/24 ipv4.nat=true ipv6.address=none ipv6.nat=false
Expand Down
46 changes: 46 additions & 0 deletions docs/resources/kubernetes_cloud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "juju_kubernetes_cloud Resource - terraform-provider-juju"
subcategory: ""
description: |-
A resource that represent a Juju Cloud for existing controller.
---

# juju_kubernetes_cloud (Resource)

A resource that represent a Juju Cloud for existing controller.

## Example Usage

```terraform
resource "juju_kubernetes_cloud" "my-k8s-cloud" {
name = "my-k8s-cloud"
kubernetes_config = file("<path-to-my-kubennetes-cloud-config>.yaml")
}
resource "juju_model" "my-model" {
name = "my-model"
credential = juju_kubernetes_cloud.my-k8s-cloud.credential
cloud {
name = juju_kubernetes_cloud.my-k8s-cloud.name
}
}
```

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

### Required

- `name` (String) The name of the cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform.

### Optional

- `kubernetes_config` (String, Sensitive) The kubernetes config file path for the cloud. Cloud credentials will be added to the Juju controller for you.
- `parent_cloud_name` (String) The parent cloud name in case adding k8s cluster from existed cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform.
- `parent_cloud_region` (String) The parent cloud region name in case adding k8s cluster from existed cloud. Changing this value will cause the cloud to be destroyed and recreated by terraform.

### Read-Only

- `credential` (String) The name of the credential created for this cloud.
- `id` (String) The ID of this resource.
12 changes: 12 additions & 0 deletions examples/resources/juju_kubernetes_cloud/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "juju_kubernetes_cloud" "my-k8s-cloud" {
name = "my-k8s-cloud"
kubernetes_config = file("<path-to-my-kubennetes-cloud-config>.yaml")
}

resource "juju_model" "my-model" {
name = "my-model"
credential = juju_kubernetes_cloud.my-k8s-cloud.credential
cloud {
name = juju_kubernetes_cloud.my-k8s-cloud.name
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
gopkg.in/httprequest.v1 v1.2.1
gopkg.in/macaroon.v2 v2.1.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/client-go v0.29.0
)

require (
Expand Down Expand Up @@ -224,7 +225,6 @@ require (
k8s.io/api v0.29.0 // indirect
k8s.io/apiextensions-apiserver v0.29.0 // indirect
k8s.io/apimachinery v0.29.0 // indirect
k8s.io/client-go v0.29.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
Expand Down
11 changes: 11 additions & 0 deletions internal/juju/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
apiresources "github.com/juju/juju/api/client/resources"
apisecrets "github.com/juju/juju/api/client/secrets"
apicommoncharm "github.com/juju/juju/api/common/charm"
jujucloud "github.com/juju/juju/cloud"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/model"
"github.com/juju/juju/core/resources"
Expand Down Expand Up @@ -94,3 +95,13 @@ type JaasAPIClient interface {
RenameGroup(req *jaasparams.RenameGroupRequest) error
RemoveGroup(req *jaasparams.RemoveGroupRequest) error
}

// KubernetesCloudAPIClient defines the set of methods that the Kubernetes cloud API provides.
type KubernetesCloudAPIClient interface {
AddCloud(cloud jujucloud.Cloud, force bool) error
Cloud(tag names.CloudTag) (jujucloud.Cloud, error)
UpdateCloud(cloud jujucloud.Cloud) error
RemoveCloud(cloud string) error
AddCredential(cloud string, credential jujucloud.Credential) error
UserCredentials(user names.UserTag, cloud names.CloudTag) ([]names.CloudCredentialTag, error)
}
Loading

0 comments on commit 21c1238

Please sign in to comment.