3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TerraformでOpenShiftクラスターのnamespaceを作成してみる

Last updated at Posted at 2021-07-08

はじめに

事前準備

  • Openshitfクラスターを作成済みで、端末からoc login可能な状態であること
  • 端末上でTerraformコマンドが実行可能であること

検証メモ

Terraform Providerの調査

RegistryでのProvider検索

Providerの利用方法の確認

  • 見つけたProviderの使い方を確認します
    • [USE PROVIDER]ボタンをクリックします
      image.png
    • tfファイルのProviderセクションへの記述が表示されます
      image.png

GitHubの情報確認

動かしてみる

main.tfファイルに必要な情報の確認

  1. Openshift ClusterへのアクセスTokenとURLの確認
    • 今回はRed Hat OpenShift on IBM Cloud上に作成したClusterを利用します
    • OpenShift Webコンソールを開き、以下の[Copy Login Command]メニューをクリックします
      image.png
    • [Display Token]リンクをクリックします
      image.png
    • 表示された項目の[Your API token is]の値と、[Login with this token]のコマンドに記載されている「--server」の値(URL)を確認します
      image.png

main.tfファイルの作成

  1. 空のディレクトリを用意し、作成したディレクトリ内にtfファイルを新規作成します
  • [Providerの利用方法の確認](### Providerの利用方法の確認)で確認した情報を基に記載します
terraform {
 required_providers {
    openshift = {
      source = "llomgui/openshift"
      version = "1.1.0"
    }
  }
}

provider "openshift" {
  load_config_file = "false"
  host      = "https://XXXX.us-south.containers.cloud.ibm.com:31989"
  token = "sha256~EXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXc"
}

resource "openshift_project" "create" {
  metadata {
    annotations = {
      "openshift.io/description" = "hanako-description"
      "openshift.io/display-name" = "hanako--display-name"
    }
    labels = {
      name      = "hanako-terraform-ns"
    }
    name = "hanako-terraform-ns"
  }
}

terraform initの実行

  1. tfファイルを作成したディレクトリ上でterraform initコマンドを実行します
PS C:\mywork\roks\myoc> terraform init

Initializing the backend...

Initializing provider plugins...
- Finding llomgui/openshift versions matching "1.1.0"...
- Installing llomgui/openshift v1.1.0...
- Installed llomgui/openshift v1.1.0 (self-signed, key ID 660E3614297F7EEC)

Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
PS C:\mywork\roks\myoc>

terraform planの実行

  1. tfファイルを作成したディレクトリ上でterraform planコマンドを実行します
PS C:\mywork\roks\myoc> terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # openshift_project.create will be created
  + resource "openshift_project" "create" {
      + id = (known after apply)

      + metadata {
          + annotations      = {
              + "openshift.io/description"  = "hanako-description"
              + "openshift.io/display-name" = "hanako--display-name"
            }
          + generation       = (known after apply)
          + labels           = {
              + "name" = "hanako-terraform-ns"
            }
          + name             = "hanako-terraform-ns"
          + resource_version = (known after apply)
          + self_link        = (known after apply)
          + uid              = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
PS C:\mywork\roks\myoc>

terraform applyの実行

  1. tfファイルを作成したディレクトリ上でterraform applyコマンドを実行します
PS C:\mywork\roks\myoc> terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # openshift_project.create will be created
  + resource "openshift_project" "create" {
      + id = (known after apply)

      + metadata {
          + annotations      = {
              + "openshift.io/description"  = "hanako-description"
              + "openshift.io/display-name" = "hanako--display-name"
            }
          + generation       = (known after apply)
          + labels           = {
              + "name" = "hanako-terraform-ns"
            }
          + name             = "hanako-terraform-ns"
          + resource_version = (known after apply)
          + self_link        = (known after apply)
          + uid              = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value:
  • [Enter a value]に対して「yes」を入力しEnterを実行します
  Enter a value: yes

openshift_project.create: Creating...
openshift_project.create: Creation complete after 1s [id=hanako-terraform-ns]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
PS C:\mywork\roks\myoc>

Web Console上での確認

  1. Projectとして作成されていることを確認します
    image.png

  2. labelsdescriptionにも、指定した内容が記載されていることを確認します
    image.png

感想など

以上。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?