Giter Site home page Giter Site logo

seal-io / tap Goto Github PK

View Code? Open in Web Editor NEW
14.0 5.0 0.0 6.4 MB

Patch Terraform Resource As Your Mind.

License: Mozilla Public License 2.0

Makefile 0.03% Go 98.19% Shell 0.24% HCL 0.03% Python 0.04% Batchfile 0.01% Ragel 1.33% Ruby 0.06% Perl 0.06%
override patch terraform opentofu

tap's Introduction

Terraform Advanced Patcher (TAP)

Terraform Advanced Patcher, aka. TAP, is a tool to patch Terraform file.

This tool is maintained by Seal.

Background

In some cases, consuming native Terraform Override can complete some additional expansion, like overriding a nested block or changing a predefined attribute, but the capabilities are limited. For example, it needs accurate block header to patch, and it's impossible to conditionally make changes to nested blocks or attributes.

TAP is designed to satisfy the above features.

Implementation

As we all know, the HCL used by Terraform supports JSON syntax. Therefore, TAP can be implemented using JSON patching.

The patching mode of Terraform Override looks like JSON Merge Patch, RFC 7386, but TAP is working as JSON Patch, RFC 6902.

For a comparison of JSON patch and JSON merge patch, see JSON Patch and JSON Merge Patch.

Note

Although TAP and Terraform Override path in different ways, they have one thing in same, that is, top-level blocks cannot be deleted. The core reason is that top-level blocks can configure Meta-Argument or participate in the configuration of Meta-Argument. Directly deleting a top-level block will cause many problems.

Usage

TAP is not a complete JSON patch for Terraform JSON Configuration SyntaxThe original JSON path needs its operation to have exactly one "path" member, which values with a JSON Pointer, but TAP implements limited operations: add, remove, and replace, and also introduces a new operation: set.

# tap.hcl

tap {
  path_syntax = "json_pointer"
}

resource "kubernetes_deployment" {
  type_alias = ["kubernetes_deployment_v1"]

  add {
    path  = "/metadata/0/labels/new-label"
    value = "new-label-value"
  }

  remove {
    path = "/metadata/0/labels/old-label"
  }

  replace {
    path  = "/spec/0/template/0/spec/0/replicas"
    value = "2"
  }

  set {
    path = "/spec/0/selector/0"
    value {
      match_labels = local.selectors
    }
  }
}

TAP recognizes the path syntax according to the path_syntax attribute in the tap block, in which the default value is json_pointer. We are going to support more path syntax in the future.

TAP, at present, only supports patching resource and data blocks, and filters out the target blocks by type_alias or name_match attributes.

# tap.hcl

tap {
  path_syntax = "json_pointer"
}

resource "kubernetes_deployment" {
  type_alias = ["kubernetes_deployment_v1"]
  name_match = ["nginx"]

  # ... operations
}

data "kubernetes_config_map" {
  type_alias = ["kubernetes_config_map_v1"]
  name_match = ["nginx"]

  # ... operations
}

TAP also allows ignoring error if patching fails, and it can be configured by the continue_on_error attribute in the tap block.

# tap.hcl

tap {
  continue_on_error = true               # global
  path_syntax       = "json_pointer"
}

resource "kubernetes_deployment" {
  continue_on_error = false              # local
  type_alias        = ["kubernetes_deployment_v1"]

  # ... operations
}

TAP is a wrapper to Terraform or OpenTofu, you can simplify alias TAP as tf, and use it as a drop-in replacement for Terraform or OpenTofu.

TAP is not a fork of Terraform or OpenTofu, so you still need to install the CLI of Terraform or OpenTofu at first.

$ go install github.com/seal-io/tap/cmd/tap@latest
$ mv "${GOPATH}"/bin/tap "${GOPATH}"/bin/tf
$ tf --version

Put the tap.hcl file in the same directory as the main.tf file, and then execute tf plan or tf apply to see the effect.

Example YouTube Overview

Requirements

License

Mozilla Public License v2.0

tap's People

Contributors

thxcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.