Skip to content

Commit

Permalink
Merge pull request #40 from p1ass/remove-kube-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
p1ass committed Feb 4, 2021
2 parents 34be8ed + 8143193 commit 2e213dc
Show file tree
Hide file tree
Showing 14 changed files with 14 additions and 1,344 deletions.
91 changes: 1 addition & 90 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

## Features
- Create GitHub releases with bumping Semantic Versioning tag
- Send pull requests which update docker image tag.
- Support `{{.Owner}}` and `{{.Repository}}` placeholder in environment variables and command-line options when setting configs.

## Installation

Expand All @@ -20,7 +18,7 @@
If you use Windows or Linux, replace `windows_amd64` or `linux_amd64` instead of `darwin_amd64`.

```bash
$ VERSION=0.2.0
$ VERSION=1.0.0
$ curl -O -L https://github.com/p1ass/mikku/releases/download/v${VERSION}/mikku_${VERSION}_darwin_amd64.tar.gz
$ tar -zxvf mikku_${VERSION}_darwin_amd64.tar.gz
$ chmod a+x mikku
Expand Down Expand Up @@ -66,27 +64,6 @@ $ mikku release sample-repository patch
Note that `mikku` doesn't build and push a docker image, so you have to do it using CI service such as CircleCI.


### Create a pull request updating docker image tag in Kubernetes manifest file

Update image tag in Kubernetes manifest file to the latest version.
```bash
$ export MIKKU_MANIFEST_REPOSITORY=sample-manifest-repository
$ export MIKKU_MANIFEST_FILEPATH=manifests/{{.Repository}}/deployment.yml
$ export MIKKU_DOCKER_IMAGE_NAME={{.Owner}}/{{.Repository}}

$ mikku pr sample-repository
```

```yaml
spec:
containers:
- name: sample-repository-container
image: p1ass/sample-repository:v1.0.0
↓ Replace
image: p1ass/sample-repository:v1.0.1
```


## Screenshots

### Release
Expand All @@ -98,14 +75,6 @@ $ mikku release sample-repository v0.1.1
![changelog](images/changelog.png)


### PullRequest

```bash
$ mikku pr memoito-server
```

![diff](images/diff.png)

## Commands

#### `mikku release <repository> <major | minor | patch | (version)>`
Expand All @@ -129,64 +98,6 @@ $ mikku release sample-repository minor # v1.0.1 → v1.1.0
$ mikku release sample-repository major # v1.1.0 → v2.0.0
```

#### `mikku pr [-m <manifest-repository>] [-p <path-to-manifest-file>] [-i <image-name>] <repository>`

Create a pull request updating Docker image tag written in Kubernetes manifest file.


##### Options

If you don't set environment variables, you must add options when executing commands.

- `--manifest, -m`
- Specify a repository existing Kubernetes manifest file.
- Optional.
- Default : `MIKKU_MANIFEST_REPOSITORY` environment variable.

- `--path, -p`
- File path where the target docker image is written.
- Optional.
- Default : `MIKKU_MANIFEST_FILEPATH` environment variable.
- You can use [text/template](https://golang.org/pkg/text/template/) in Go.
- Support variable : `{{.Owner}}`, `{{.Repository}}`
- Ex. `manifests/{{.Repository}}/deployment.yml`

- `--image, -i`
- Docker image name.
- Optional.
- Default : `MIKKU_DOCKER_IMAGE_NAME` environment variable.
- You can use [text/template](https://golang.org/pkg/text/template/) in Go.
- Support variable : `{{.Owner}}`, `{{.Repository}}`
- Ex. `asia.gcr.io/{{.Owner}}/{{.Repository}}`



##### Examples

```bash
$ export MIKKU_GITHUB_ACCESS_TOKEN=[YOUR_ACCESS_TOKEN]
$ export MIKKU_GITHUB_OWNER=p1ass

# You need to set environment variables. Otherwise, add options when executing commands
$ export MIKKU_MANIFEST_REPOSITORY=manifest-repository
$ export MIKKU_MANIFEST_FILEPATH=manifests/{{.Repository}}/deployment.yml
$ export MIKKU_DOCKER_IMAGE_NAME=asia.gcr.io/{{.Owner}}/{{.Repository}}

# The most simple case
# When the latest tag name is `v1.0.1`,
# replace p1ass/sample-repository:v1.0.0 existing in manifest-repository to p1ass/sample-repository:v1.0.1.
$ mikku pr sample-repository

# Override manifest repository
$ mikku pr --manifest other-manifest-repo sample-repository

# Override Kubernetes manifest file
$ mikku pr --path {{.Owner}}/{{.Repository}}/deployment.yml sample-repository

# Override docker image name
$ mikku pr --image docker.example.com/{{.Repository}} sample-repository
```

## For developers

### Build
Expand Down
54 changes: 1 addition & 53 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ var commandRelease = &cli.Command{
Action: doRelease,
}

var commandPullRequest = &cli.Command{
Name: "pr",
Usage: "Create a pull request updating Docker image tag written in Kubernetes manifest file",
UsageText: `
mikku pr [options...] <repository>
Create a pull request updating Docker image tag written in Kubernetes manifest file.
`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "manifest",
Aliases: []string{"m"},
Usage: "Repository existing Kubernetes manifest file. It overrides MIKKU_MANIFEST_REPOSITORY environment variable",
},
&cli.StringFlag{
Name: "path",
Aliases: []string{"p"},
Usage: "File path where the target docker image is written. It overrides MIKKU_MANIFEST_FILEPATH environment variable",
},
&cli.StringFlag{
Name: "image",
Aliases: []string{"i"},
Usage: "Docker image name. It overrides MIKKU_DOCKER_IMAGE_NAME environment variable",
},
},
Action: doPullRequest,
}

func doRelease(c *cli.Context) error {
if c.Args().Len() == 0 {
_ = cli.ShowCommandHelp(c, "release")
Expand All @@ -75,34 +47,11 @@ func doRelease(c *cli.Context) error {
return nil
}

func doPullRequest(c *cli.Context) error {
if c.Args().Len() == 0 {
_ = cli.ShowCommandHelp(c, "pr")
return nil
}

if c.Args().Len() != 1 {
return fmt.Errorf("One argument is required: repository")
}

repo := c.Args().Get(0)
manifestRepo := c.String("manifest")
pathToManifestFile := c.String("path")
image := c.String("image")

if err := PullRequest(repo, manifestRepo, pathToManifestFile, image); err != nil {
return fmt.Errorf("Failed to execute pr: %v", err)

}

return nil
}

// Run runs commands depending on the given argument
func Run(args []string) error {
app := &cli.App{
Name: "mikku",
Usage: "Bump Semantic Versioning tag, create GitHub release and update Kubernetes manifest file",
Usage: "Bump Semantic Versioning tag andcreate GitHub release",
Authors: []*cli.Author{
{
Name: "p1ass",
Expand All @@ -112,7 +61,6 @@ func Run(args []string) error {
Version: mikkuVersion,
Commands: []*cli.Command{
commandRelease,
commandPullRequest,
},
}

Expand Down
5 changes: 0 additions & 5 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ func TestRun(t *testing.T) {
args: []string{"", "release", "mikku"},
wantErr: true,
},
{
name: "pr: no arguments",
args: []string{"", "pr"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
83 changes: 2 additions & 81 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package mikku

import (
"bytes"
"errors"
"fmt"
"text/template"

"github.com/kelseyhightower/envconfig"
)

var (
errEmptyGitHubAccessToken = errors.New("should be set MIKKU_GITHUB_ACCESS_TOKEN")
errEmptyGitHubOwner = errors.New("should be set MIKKU_GITHUB_OWNER")
errEmptyManifestRepository = errors.New("should be set MIKKU_MANIFEST_REPOSITORY or --manifest option")
errEmptyManifestFilePath = errors.New("should be set MIKKU_MANIFEST_FILEPATH or --path option")
errEmptyDockerImageName = errors.New("should be set MIKKU_DOCKER_IMAGE_NAME or --image option")
errEmptyGitHubAccessToken = errors.New("should be set MIKKU_GITHUB_ACCESS_TOKEN")
errEmptyGitHubOwner = errors.New("should be set MIKKU_GITHUB_OWNER")
)

// Config represents config using all commands
Expand All @@ -35,84 +30,10 @@ func (cfg *Config) validate() error {
return nil
}

// PRConfig represents config using pr command
type PRConfig struct {
ManifestRepository string `envconfig:"MIKKU_MANIFEST_REPOSITORY"`
ManifestFilepath string `envconfig:"MIKKU_MANIFEST_FILEPATH"`
DockerImageName string `envconfig:"MIKKU_DOCKER_IMAGE_NAME"`
}

func (cfg *PRConfig) overrideConfig(manifestRepo, pathToManifestFile, imageName string) {
if manifestRepo != "" {
cfg.ManifestRepository = manifestRepo
}
if pathToManifestFile != "" {
cfg.ManifestFilepath = pathToManifestFile
}
if imageName != "" {
cfg.DockerImageName = imageName
}
}

func (cfg *PRConfig) validate() error {
if cfg.ManifestRepository == "" {
return errEmptyManifestRepository
}

if cfg.ManifestFilepath == "" {
return errEmptyManifestFilePath
}

if cfg.DockerImageName == "" {
return errEmptyDockerImageName
}
return nil
}

func (cfg *PRConfig) embedRepoInfo(owner, repo string) error {
info := map[string]interface{}{"Owner": owner, "Repository": repo}

embedManifestFilepath, err := parse(cfg.ManifestFilepath, info)
if err != nil {
return fmt.Errorf("parse manifest filepath: %w", err)
}
cfg.ManifestFilepath = embedManifestFilepath

embedDockerImageName, err := parse(cfg.DockerImageName, info)
if err != nil {
return fmt.Errorf("parse docker image name: %w", err)
}
cfg.DockerImageName = embedDockerImageName

return nil
}

func parse(text string, info map[string]interface{}) (string, error) {
tmpl, err := template.New("text").Parse(text)
if err != nil {
return "", fmt.Errorf("template parse error: %w", err)
}

buff := bytes.NewBuffer(make([]byte, 0, 20))
if err := tmpl.Execute(buff, info); err != nil {
return "", fmt.Errorf("template execute error: %w", err)
}

return buff.String(), nil
}

func readConfig() (*Config, error) {
cfg := &Config{}
if err := envconfig.Process("", cfg); err != nil {
return nil, fmt.Errorf("failed to read environment variables: %w", err)
}
return cfg, nil
}

func readPRConfig() (*PRConfig, error) {
cfg := &PRConfig{}
if err := envconfig.Process("", cfg); err != nil {
return nil, fmt.Errorf("failed to read environment variables: %w", err)
}
return cfg, nil
}

0 comments on commit 2e213dc

Please sign in to comment.